Coverage Report

Created: 2026-10-28 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/odp-util.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
#include <sys/types.h>
19
#include <netinet/in.h>
20
#include <arpa/inet.h>
21
#include "odp-util.h"
22
#include <errno.h>
23
#include <inttypes.h>
24
#include <math.h>
25
#include <netinet/icmp6.h>
26
#include <netinet/ip6.h>
27
#include <stdlib.h>
28
#include <string.h>
29
30
#include "byte-order.h"
31
#include "coverage.h"
32
#include "dpif.h"
33
#include "openvswitch/dynamic-string.h"
34
#include "flow.h"
35
#include "netlink.h"
36
#include "openvswitch/ofpbuf.h"
37
#include "packets.h"
38
#include "simap.h"
39
#include "timeval.h"
40
#include "tun-metadata.h"
41
#include "unaligned.h"
42
#include "util.h"
43
#include "uuid.h"
44
#include "openvswitch/vlog.h"
45
#include "openvswitch/match.h"
46
#include "odp-netlink-macros.h"
47
#include "csum.h"
48
49
VLOG_DEFINE_THIS_MODULE(odp_util);
50
51
/* The interface between userspace and kernel uses an "OVS_*" prefix.
52
 * Since this is fairly non-specific for the OVS userspace components,
53
 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
54
 * interactions with the datapath.
55
 */
56
57
/* The set of characters that may separate one action or one key attribute
58
 * from another. */
59
static const char *delimiters = ", \t\r\n";
60
static const char *delimiters_end = ", \t\r\n)";
61
62
0
#define MAX_ODP_NESTED 32
63
64
struct parse_odp_context {
65
    const struct simap *port_names;
66
    int depth; /* Current nested depth of odp string. */
67
};
68
69
static int parse_odp_key_mask_attr(struct parse_odp_context *, const char *,
70
                                   struct ofpbuf *, struct ofpbuf *);
71
72
static int parse_odp_key_mask_attr__(struct parse_odp_context *, const char *,
73
                                   struct ofpbuf *, struct ofpbuf *);
74
75
static void format_odp_key_attr(const struct nlattr *a,
76
                                const struct nlattr *ma,
77
                                const struct hmap *portno_names, struct ds *ds,
78
                                bool verbose);
79
80
struct geneve_scan {
81
    struct geneve_opt d[63];
82
    int len;
83
};
84
85
static int scan_geneve(const char *s, struct geneve_scan *key,
86
                       struct geneve_scan *mask);
87
static void format_geneve_opts(const struct geneve_opt *opt,
88
                               const struct geneve_opt *mask, int opts_len,
89
                               struct ds *, bool verbose);
90
91
static struct nlattr *generate_all_wildcard_mask(const struct attr_len_tbl tbl[],
92
                                                 int max, struct ofpbuf *,
93
                                                 const struct nlattr *key);
94
static void format_u128(struct ds *d, const ovs_32aligned_u128 *key,
95
                        const ovs_32aligned_u128 *mask, bool verbose);
96
static int scan_u128(const char *s, ovs_u128 *value, ovs_u128 *mask);
97
98
static int parse_odp_action(struct parse_odp_context *context, const char *s,
99
                            struct ofpbuf *actions);
100
101
static int parse_odp_action__(struct parse_odp_context *context, const char *s,
102
                            struct ofpbuf *actions);
103
104
/* Returns one the following for the action with the given OVS_ACTION_ATTR_*
105
 * 'type':
106
 *
107
 *   - For an action whose argument has a fixed length, returned that
108
 *     nonnegative length in bytes.
109
 *
110
 *   - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
111
 *
112
 *   - For an invalid 'type', returns ATTR_LEN_INVALID. */
113
static int
114
odp_action_len(uint16_t type)
115
0
{
116
0
    if (type > OVS_ACTION_ATTR_MAX) {
117
0
        return -1;
118
0
    }
119
120
0
    switch ((enum ovs_action_attr) type) {
121
0
    case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
122
0
    case OVS_ACTION_ATTR_LB_OUTPUT: return sizeof(uint32_t);
123
0
    case OVS_ACTION_ATTR_TRUNC: return sizeof(struct ovs_action_trunc);
124
0
    case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
125
0
    case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
126
0
    case OVS_ACTION_ATTR_METER: return sizeof(uint32_t);
127
0
    case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
128
0
    case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
129
0
    case OVS_ACTION_ATTR_POP_VLAN: return 0;
130
0
    case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
131
0
    case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
132
0
    case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
133
0
    case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
134
0
    case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
135
0
    case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
136
0
    case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
137
0
    case OVS_ACTION_ATTR_CT: return ATTR_LEN_VARIABLE;
138
0
    case OVS_ACTION_ATTR_CT_CLEAR: return 0;
139
0
    case OVS_ACTION_ATTR_PUSH_ETH: return sizeof(struct ovs_action_push_eth);
140
0
    case OVS_ACTION_ATTR_POP_ETH: return 0;
141
0
    case OVS_ACTION_ATTR_CLONE: return ATTR_LEN_VARIABLE;
142
0
    case OVS_ACTION_ATTR_PUSH_NSH: return ATTR_LEN_VARIABLE;
143
0
    case OVS_ACTION_ATTR_POP_NSH: return 0;
144
0
    case OVS_ACTION_ATTR_CHECK_PKT_LEN: return ATTR_LEN_VARIABLE;
145
0
    case OVS_ACTION_ATTR_ADD_MPLS: return sizeof(struct ovs_action_add_mpls);
146
0
    case OVS_ACTION_ATTR_DEC_TTL: return ATTR_LEN_VARIABLE;
147
0
    case OVS_ACTION_ATTR_DROP: return sizeof(uint32_t);
148
0
    case OVS_ACTION_ATTR_PSAMPLE: return ATTR_LEN_VARIABLE;
149
150
0
    case OVS_ACTION_ATTR_UNSPEC:
151
0
    case __OVS_ACTION_ATTR_MAX:
152
0
        return ATTR_LEN_INVALID;
153
0
    }
154
155
0
    return ATTR_LEN_INVALID;
156
0
}
157
158
/* Returns a string form of 'attr'.  The return value is either a statically
159
 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'.  'bufsize'
160
 * should be at least OVS_KEY_ATTR_BUFSIZE. */
161
enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
162
static const char *
163
ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
164
0
{
165
0
    switch (attr) {
166
0
    case OVS_KEY_ATTR_UNSPEC: return "unspec";
167
0
    case OVS_KEY_ATTR_ENCAP: return "encap";
168
0
    case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
169
0
    case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
170
0
    case OVS_KEY_ATTR_CT_STATE: return "ct_state";
171
0
    case OVS_KEY_ATTR_CT_ZONE: return "ct_zone";
172
0
    case OVS_KEY_ATTR_CT_MARK: return "ct_mark";
173
0
    case OVS_KEY_ATTR_CT_LABELS: return "ct_label";
174
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: return "ct_tuple4";
175
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: return "ct_tuple6";
176
0
    case OVS_KEY_ATTR_TUNNEL: return "tunnel";
177
0
    case OVS_KEY_ATTR_IN_PORT: return "in_port";
178
0
    case OVS_KEY_ATTR_ETHERNET: return "eth";
179
0
    case OVS_KEY_ATTR_VLAN: return "vlan";
180
0
    case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
181
0
    case OVS_KEY_ATTR_IPV4: return "ipv4";
182
0
    case OVS_KEY_ATTR_IPV6: return "ipv6";
183
0
    case OVS_KEY_ATTR_TCP: return "tcp";
184
0
    case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
185
0
    case OVS_KEY_ATTR_UDP: return "udp";
186
0
    case OVS_KEY_ATTR_SCTP: return "sctp";
187
0
    case OVS_KEY_ATTR_ICMP: return "icmp";
188
0
    case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
189
0
    case OVS_KEY_ATTR_ARP: return "arp";
190
0
    case OVS_KEY_ATTR_ND: return "nd";
191
0
    case OVS_KEY_ATTR_ND_EXTENSIONS: return "nd_ext";
192
0
    case OVS_KEY_ATTR_MPLS: return "mpls";
193
0
    case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
194
0
    case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
195
0
    case OVS_KEY_ATTR_PACKET_TYPE: return "packet_type";
196
0
    case OVS_KEY_ATTR_NSH: return "nsh";
197
198
0
    case OVS_KEY_ATTR_TUNNEL_INFO: return "<error: kernel-only tunnel_info>";
199
0
    case __OVS_KEY_ATTR_MAX:
200
0
    default:
201
0
        snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
202
0
        return namebuf;
203
0
    }
204
0
}
205
206
static void
207
format_generic_odp_action(struct ds *ds, const struct nlattr *a)
208
0
{
209
0
    size_t len = nl_attr_get_size(a);
210
211
0
    ds_put_format(ds, "action%d", nl_attr_type(a));
212
0
    if (len) {
213
0
        const uint8_t *unspec;
214
0
        unsigned int i;
215
216
0
        unspec = nl_attr_get(a);
217
0
        for (i = 0; i < len; i++) {
218
0
            ds_put_char(ds, i ? ' ': '(');
219
0
            ds_put_format(ds, "%02x", unspec[i]);
220
0
        }
221
0
        ds_put_char(ds, ')');
222
0
    }
223
0
}
224
225
static void
226
format_odp_sample_action(struct ds *ds, const struct nlattr *attr,
227
                         const struct hmap *portno_names)
228
0
{
229
0
    static const struct nl_policy ovs_sample_policy[] = {
230
0
        [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
231
0
        [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
232
0
    };
233
0
    struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
234
0
    double percentage;
235
0
    const struct nlattr *nla_acts;
236
0
    int len;
237
238
0
    ds_put_cstr(ds, "sample");
239
240
0
    if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
241
0
        ds_put_cstr(ds, "(error)");
242
0
        return;
243
0
    }
244
245
0
    percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
246
0
                        UINT32_MAX;
247
248
0
    ds_put_format(ds, "(sample=%.1f%%,", percentage);
249
250
0
    ds_put_cstr(ds, "actions(");
251
0
    nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
252
0
    len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
253
0
    format_odp_actions(ds, nla_acts, len, portno_names);
254
0
    ds_put_format(ds, "))");
255
0
}
256
257
static void
258
format_odp_clone_action(struct ds *ds, const struct nlattr *attr,
259
                        const struct hmap *portno_names)
260
0
{
261
0
    const struct nlattr *nla_acts = nl_attr_get(attr);
262
0
    int len = nl_attr_get_size(attr);
263
264
0
    ds_put_cstr(ds, "clone");
265
0
    ds_put_format(ds, "(");
266
0
    format_odp_actions(ds, nla_acts, len, portno_names);
267
0
    ds_put_format(ds, ")");
268
0
}
269
270
static void
271
format_nsh_key(struct ds *ds, const struct ovs_key_nsh *key)
272
0
{
273
0
    ds_put_format(ds, "flags=%d", key->flags);
274
0
    ds_put_format(ds, ",ttl=%d", key->ttl);
275
0
    ds_put_format(ds, ",mdtype=%d", key->mdtype);
276
0
    ds_put_format(ds, ",np=%d", key->np);
277
0
    ds_put_format(ds, ",spi=0x%x",
278
0
                  nsh_path_hdr_to_spi_uint32(key->path_hdr));
279
0
    ds_put_format(ds, ",si=%d",
280
0
                  nsh_path_hdr_to_si(key->path_hdr));
281
282
0
    switch (key->mdtype) {
283
0
        case NSH_M_TYPE1:
284
0
            for (int i = 0; i < 4; i++) {
285
0
                ds_put_format(ds, ",c%d=0x%x", i + 1, ntohl(key->context[i]));
286
0
            }
287
0
            break;
288
0
        case NSH_M_TYPE2:
289
0
        default:
290
            /* No support for matching other metadata formats yet. */
291
0
            break;
292
0
    }
293
0
}
294
295
static void
296
format_uint8_masked(struct ds *s, bool *first, const char *name,
297
                    uint8_t value, uint8_t mask)
298
0
{
299
0
    if (mask != 0) {
300
0
        if (!*first) {
301
0
            ds_put_char(s, ',');
302
0
        }
303
0
        ds_put_format(s, "%s=", name);
304
0
        if (mask == UINT8_MAX) {
305
0
            ds_put_format(s, "%"PRIu8, value);
306
0
        } else {
307
0
            ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask);
308
0
        }
309
0
        *first = false;
310
0
    }
311
0
}
312
313
static void
314
format_be32_masked(struct ds *s, bool *first, const char *name,
315
                   ovs_be32 value, ovs_be32 mask)
316
0
{
317
0
    if (mask != htonl(0)) {
318
0
        if (!*first) {
319
0
            ds_put_char(s, ',');
320
0
        }
321
0
        ds_put_format(s, "%s=", name);
322
0
        if (mask == OVS_BE32_MAX) {
323
0
            ds_put_format(s, "0x%"PRIx32, ntohl(value));
324
0
        } else {
325
0
            ds_put_format(s, "0x%"PRIx32"/0x%08"PRIx32,
326
0
                          ntohl(value), ntohl(mask));
327
0
        }
328
0
        *first = false;
329
0
    }
330
0
}
331
332
static void
333
format_nsh_key_mask(struct ds *ds, const struct ovs_key_nsh *key,
334
                    const struct ovs_key_nsh *mask)
335
0
{
336
0
    if (!mask) {
337
0
        format_nsh_key(ds, key);
338
0
    } else {
339
0
        bool first = true;
340
0
        uint32_t spi = nsh_path_hdr_to_spi_uint32(key->path_hdr);
341
0
        uint32_t spi_mask = nsh_path_hdr_to_spi_uint32(mask->path_hdr);
342
0
        if (spi_mask == (NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
343
0
            spi_mask = UINT32_MAX;
344
0
        }
345
0
        uint8_t si = nsh_path_hdr_to_si(key->path_hdr);
346
0
        uint8_t si_mask = nsh_path_hdr_to_si(mask->path_hdr);
347
348
0
        format_uint8_masked(ds, &first, "flags", key->flags, mask->flags);
349
0
        format_uint8_masked(ds, &first, "ttl", key->ttl, mask->ttl);
350
0
        format_uint8_masked(ds, &first, "mdtype", key->mdtype, mask->mdtype);
351
0
        format_uint8_masked(ds, &first, "np", key->np, mask->np);
352
0
        format_be32_masked(ds, &first, "spi", htonl(spi), htonl(spi_mask));
353
0
        format_uint8_masked(ds, &first, "si", si, si_mask);
354
0
        format_be32_masked(ds, &first, "c1", key->context[0],
355
0
                           mask->context[0]);
356
0
        format_be32_masked(ds, &first, "c2", key->context[1],
357
0
                           mask->context[1]);
358
0
        format_be32_masked(ds, &first, "c3", key->context[2],
359
0
                           mask->context[2]);
360
0
        format_be32_masked(ds, &first, "c4", key->context[3],
361
0
                           mask->context[3]);
362
0
    }
363
0
}
364
365
static void
366
format_odp_push_nsh_action(struct ds *ds,
367
                           const struct nsh_hdr *nsh_hdr)
368
0
 {
369
0
    size_t mdlen = nsh_hdr_len(nsh_hdr) - NSH_BASE_HDR_LEN;
370
0
    uint32_t spi = ntohl(nsh_get_spi(nsh_hdr));
371
0
    uint8_t si = nsh_get_si(nsh_hdr);
372
0
    uint8_t flags = nsh_get_flags(nsh_hdr);
373
0
    uint8_t ttl = nsh_get_ttl(nsh_hdr);
374
375
0
    ds_put_cstr(ds, "push_nsh(");
376
0
    ds_put_format(ds, "flags=%d", flags);
377
0
    ds_put_format(ds, ",ttl=%d", ttl);
378
0
    ds_put_format(ds, ",mdtype=%d", nsh_hdr->md_type);
379
0
    ds_put_format(ds, ",np=%d", nsh_hdr->next_proto);
380
0
    ds_put_format(ds, ",spi=0x%x", spi);
381
0
    ds_put_format(ds, ",si=%d", si);
382
0
    switch (nsh_hdr->md_type) {
383
0
    case NSH_M_TYPE1: {
384
0
        const struct nsh_md1_ctx *md1_ctx = &nsh_hdr->md1;
385
0
        for (int i = 0; i < 4; i++) {
386
0
            ds_put_format(ds, ",c%d=0x%x", i + 1,
387
0
                          ntohl(get_16aligned_be32(&md1_ctx->context[i])));
388
0
        }
389
0
        break;
390
0
    }
391
0
    case NSH_M_TYPE2: {
392
0
        const struct nsh_md2_tlv *md2_ctx = &nsh_hdr->md2;
393
0
        ds_put_cstr(ds, ",md2=");
394
0
        ds_put_hex(ds, md2_ctx, mdlen);
395
0
        break;
396
0
    }
397
0
    default:
398
0
        ds_put_cstr(ds, ",<error: unknown mdtype>");
399
0
        break;
400
0
    }
401
0
    ds_put_format(ds, ")");
402
0
}
403
404
static const char *
405
slow_path_reason_to_string(uint32_t reason)
406
0
{
407
0
    switch ((enum slow_path_reason) reason) {
408
0
#define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
409
0
        SLOW_PATH_REASONS
410
0
#undef SPR
411
0
    }
412
413
0
    return NULL;
414
0
}
415
416
const char *
417
slow_path_reason_to_explanation(enum slow_path_reason reason)
418
0
{
419
0
    switch (reason) {
420
0
#define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
421
0
        SLOW_PATH_REASONS
422
0
#undef SPR
423
0
    }
424
425
0
    return "<unknown>";
426
0
}
427
428
static int
429
parse_odp_flags(const char *s, const char *(*bit_to_string)(uint32_t),
430
                uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
431
0
{
432
0
    return parse_flags(s, bit_to_string, ')', NULL, NULL,
433
0
                       res_flags, allowed, res_mask);
434
0
}
435
436
static void
437
format_odp_userspace_action(struct ds *ds, const struct nlattr *attr,
438
                            const struct hmap *portno_names)
439
0
{
440
0
    static const struct nl_policy ovs_userspace_policy[] = {
441
0
        [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
442
0
        [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
443
0
                                          .optional = true },
444
0
        [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
445
0
                                                 .optional = true },
446
0
        [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC,
447
0
                                                 .optional = true },
448
0
    };
449
0
    struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
450
0
    const struct nlattr *userdata_attr;
451
0
    const struct nlattr *tunnel_out_port_attr;
452
453
0
    if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
454
0
        ds_put_cstr(ds, "userspace(error)");
455
0
        return;
456
0
    }
457
458
0
    ds_put_format(ds, "userspace(pid=%"PRIu32,
459
0
                  nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
460
461
0
    userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
462
463
0
    if (userdata_attr) {
464
0
        const uint8_t *userdata = nl_attr_get(userdata_attr);
465
0
        size_t userdata_len = nl_attr_get_size(userdata_attr);
466
0
        bool userdata_unspec = true;
467
0
        struct user_action_cookie cookie;
468
469
0
        if (userdata_len == sizeof cookie) {
470
0
            memcpy(&cookie, userdata, sizeof cookie);
471
472
0
            userdata_unspec = false;
473
474
0
            if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
475
0
                ds_put_format(ds, ",sFlow("
476
0
                              "vid=%"PRIu16",pcp=%d,output=%"PRIu32")",
477
0
                              vlan_tci_to_vid(cookie.sflow.vlan_tci),
478
0
                              vlan_tci_to_pcp(cookie.sflow.vlan_tci),
479
0
                              cookie.sflow.output);
480
0
            } else if (cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
481
0
                ds_put_cstr(ds, ",slow_path(");
482
0
                format_flags(ds, slow_path_reason_to_string,
483
0
                             cookie.slow_path.reason, ',');
484
0
                ds_put_format(ds, ")");
485
0
            } else if (cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
486
0
                ds_put_format(ds, ",flow_sample(probability=%"PRIu16
487
0
                              ",collector_set_id=%"PRIu32
488
0
                              ",obs_domain_id=%"PRIu32
489
0
                              ",obs_point_id=%"PRIu32
490
0
                              ",output_port=",
491
0
                              cookie.flow_sample.probability,
492
0
                              cookie.flow_sample.collector_set_id,
493
0
                              cookie.flow_sample.obs_domain_id,
494
0
                              cookie.flow_sample.obs_point_id);
495
0
                odp_portno_name_format(portno_names,
496
0
                                       cookie.flow_sample.output_odp_port, ds);
497
0
                if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_INGRESS) {
498
0
                    ds_put_cstr(ds, ",ingress");
499
0
                } else if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_EGRESS) {
500
0
                    ds_put_cstr(ds, ",egress");
501
0
                }
502
0
                ds_put_char(ds, ')');
503
0
            } else if (cookie.type == USER_ACTION_COOKIE_IPFIX) {
504
0
                ds_put_format(ds, ",ipfix(output_port=");
505
0
                odp_portno_name_format(portno_names,
506
0
                                       cookie.ipfix.output_odp_port, ds);
507
0
                ds_put_char(ds, ')');
508
0
            } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
509
0
                ds_put_format(ds, ",controller(reason=%"PRIu16
510
0
                              ",dont_send=%d"
511
0
                              ",continuation=%d"
512
0
                              ",recirc_id=%"PRIu32
513
0
                              ",rule_cookie=%#"PRIx64
514
0
                              ",controller_id=%"PRIu16
515
0
                              ",max_len=%"PRIu16,
516
0
                              cookie.controller.reason,
517
0
                              !!cookie.controller.dont_send,
518
0
                              !!cookie.controller.continuation,
519
0
                              cookie.controller.recirc_id,
520
0
                              ntohll(get_32aligned_be64(
521
0
                                         &cookie.controller.rule_cookie)),
522
0
                              cookie.controller.controller_id,
523
0
                              cookie.controller.max_len);
524
0
                ds_put_char(ds, ')');
525
0
            } else {
526
0
                userdata_unspec = true;
527
0
            }
528
0
        }
529
530
0
        if (userdata_unspec) {
531
0
            size_t i;
532
0
            ds_put_format(ds, ",userdata(");
533
0
            for (i = 0; i < userdata_len; i++) {
534
0
                ds_put_format(ds, "%02x", userdata[i]);
535
0
            }
536
0
            ds_put_char(ds, ')');
537
0
        }
538
0
    }
539
540
0
    if (a[OVS_USERSPACE_ATTR_ACTIONS]) {
541
0
        ds_put_cstr(ds, ",actions");
542
0
    }
543
544
0
    tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
545
0
    if (tunnel_out_port_attr) {
546
0
        ds_put_format(ds, ",tunnel_out_port=");
547
0
        odp_portno_name_format(portno_names,
548
0
                               nl_attr_get_odp_port(tunnel_out_port_attr), ds);
549
0
    }
550
551
0
    ds_put_char(ds, ')');
552
0
}
553
554
static void
555
format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
556
0
{
557
0
    if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
558
0
        ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
559
0
        if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
560
0
            ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
561
0
        };
562
0
        ds_put_char(ds, ',');
563
0
    }
564
0
    if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
565
0
        ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
566
0
        if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
567
0
            ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
568
0
        }
569
0
        ds_put_char(ds, ',');
570
0
    }
571
0
    if (!(tci & htons(VLAN_CFI))) {
572
0
        ds_put_cstr(ds, "cfi=0");
573
0
        ds_put_char(ds, ',');
574
0
    }
575
0
    ds_chomp(ds, ',');
576
0
}
577
578
static void
579
format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
580
0
{
581
0
    ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
582
0
                  mpls_lse_to_label(mpls_lse),
583
0
                  mpls_lse_to_tc(mpls_lse),
584
0
                  mpls_lse_to_ttl(mpls_lse),
585
0
                  mpls_lse_to_bos(mpls_lse));
586
0
}
587
588
static void
589
format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
590
            const struct ovs_key_mpls *mpls_mask, int n)
591
0
{
592
0
    for (int i = 0; i < n; i++) {
593
0
        ovs_be32 key = mpls_key[i].mpls_lse;
594
595
0
        if (mpls_mask == NULL) {
596
0
            format_mpls_lse(ds, key);
597
0
        } else {
598
0
            ovs_be32 mask = mpls_mask[i].mpls_lse;
599
600
0
            ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
601
0
                          mpls_lse_to_label(key), mpls_lse_to_label(mask),
602
0
                          mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
603
0
                          mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
604
0
                          mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
605
0
        }
606
0
        ds_put_char(ds, ',');
607
0
    }
608
0
    ds_chomp(ds, ',');
609
0
}
610
611
static void
612
format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
613
0
{
614
0
    ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
615
0
}
616
617
static void
618
format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
619
0
{
620
0
    ds_put_format(ds, "hash(");
621
622
0
    if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
623
0
        ds_put_format(ds, "l4(%"PRIu32")", hash_act->hash_basis);
624
0
    } else if (hash_act->hash_alg == OVS_HASH_ALG_SYM_L4) {
625
0
        ds_put_format(ds, "sym_l4(%"PRIu32")", hash_act->hash_basis);
626
0
    } else {
627
0
        ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
628
0
                      hash_act->hash_alg);
629
0
    }
630
0
    ds_put_format(ds, ")");
631
0
}
632
633
static const void *
634
format_udp_tnl_push_header(struct ds *ds, const struct udp_header *udp,
635
                           uint32_t bytes_left)
636
0
{
637
0
    if (bytes_left < sizeof *udp) {
638
0
        ds_put_cstr(ds, "udp(truncated header))");
639
0
        return NULL;
640
0
    }
641
642
0
    ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
643
0
                  ntohs(udp->udp_src), ntohs(udp->udp_dst),
644
0
                  ntohs(udp->udp_csum));
645
646
0
    return udp + 1;
647
0
}
648
649
static void
650
format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
651
0
{
652
0
    const struct eth_header *eth;
653
0
    const struct udp_header *udp;
654
0
    uint32_t bytes_left;
655
0
    const void *l3;
656
0
    const void *l4;
657
658
0
    eth = (const struct eth_header *)data->header;
659
660
0
    if (data->header_len < sizeof *eth) {
661
0
        ds_put_format(ds,
662
0
                      "header(size=%"PRIu32",type=%"PRIu32
663
0
                      ", eth(truncated header))",
664
0
                      data->header_len, data->tnl_type);
665
0
        return;
666
0
    }
667
668
0
    l3 = eth + 1;
669
0
    bytes_left = data->header_len - sizeof *eth;
670
671
    /* Ethernet */
672
0
    ds_put_format(ds, "header(size=%"PRIu32",type=%"PRIu32",eth(dst=",
673
0
                  data->header_len, data->tnl_type);
674
0
    ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
675
0
    ds_put_format(ds, ",src=");
676
0
    ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
677
0
    ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
678
679
0
    if (eth->eth_type == htons(ETH_TYPE_IP)) {
680
        /* IPv4 */
681
0
        const struct ip_header *ip = l3;
682
683
0
        if (bytes_left < sizeof *ip) {
684
0
            ds_put_cstr(ds, "ipv4(truncated header))");
685
0
            return;
686
0
        }
687
688
0
        ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
689
0
                      ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
690
0
                      IP_ARGS(get_16aligned_be32(&ip->ip_src)),
691
0
                      IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
692
0
                      ip->ip_proto, ip->ip_tos,
693
0
                      ip->ip_ttl,
694
0
                      ntohs(ip->ip_frag_off));
695
0
        l4 = (ip + 1);
696
0
        bytes_left -= sizeof *ip;
697
0
    } else {
698
0
        const struct ovs_16aligned_ip6_hdr *ip6 = l3;
699
0
        struct in6_addr src, dst;
700
0
        memcpy(&src, &ip6->ip6_src, sizeof src);
701
0
        memcpy(&dst, &ip6->ip6_dst, sizeof dst);
702
0
        uint32_t ipv6_flow = ntohl(get_16aligned_be32(&ip6->ip6_flow));
703
704
0
        if (bytes_left < sizeof *ip6) {
705
0
            ds_put_cstr(ds, "ipv6(truncated header))");
706
0
            return;
707
0
        }
708
709
0
        ds_put_format(ds, "ipv6(src=");
710
0
        ipv6_format_addr(&src, ds);
711
0
        ds_put_format(ds, ",dst=");
712
0
        ipv6_format_addr(&dst, ds);
713
0
        ds_put_format(ds, ",label=%i,proto=%"PRIu8",tclass=0x%"PRIx32
714
0
                          ",hlimit=%"PRIu8"),",
715
0
                      ipv6_flow & IPV6_LABEL_MASK, ip6->ip6_nxt,
716
0
                      (ipv6_flow >> 20) & 0xff, ip6->ip6_hlim);
717
0
        l4 = (ip6 + 1);
718
0
        bytes_left -= sizeof *ip6;
719
0
    }
720
721
0
    udp = (const struct udp_header *) l4;
722
723
0
    if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
724
0
        const struct vxlanhdr *vxh;
725
726
0
        vxh = format_udp_tnl_push_header(ds, udp, bytes_left);
727
0
        if (!vxh) {
728
0
            return;
729
0
        }
730
731
0
        bytes_left -= sizeof *udp;
732
0
        if (bytes_left < sizeof *vxh) {
733
0
            ds_put_cstr(ds, "vxlan(truncated header))");
734
0
            return;
735
0
        }
736
737
0
        ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
738
0
                      ntohl(get_16aligned_be32(&vxh->vx_flags)),
739
0
                      ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
740
0
    } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
741
0
        const struct genevehdr *gnh;
742
743
0
        gnh = format_udp_tnl_push_header(ds, udp, bytes_left);
744
0
        if (!gnh) {
745
0
            return;
746
0
        }
747
748
0
        bytes_left -= sizeof *udp;
749
0
        if (bytes_left < sizeof *gnh) {
750
0
            ds_put_cstr(ds, "geneve(truncated header))");
751
0
            return;
752
0
        }
753
754
0
        ds_put_format(ds, "geneve(%s%svni=0x%"PRIx32,
755
0
                      gnh->oam ? "oam," : "",
756
0
                      gnh->critical ? "crit," : "",
757
0
                      ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
758
759
0
        bytes_left -= sizeof *gnh;
760
0
        if (gnh->opt_len) {
761
0
            if (bytes_left < gnh->opt_len * sizeof(uint32_t)) {
762
0
                ds_put_cstr(ds, ",options(invalid length)))");
763
0
                return;
764
0
            }
765
766
0
            ds_put_cstr(ds, ",options(");
767
0
            format_geneve_opts(gnh->options, NULL, gnh->opt_len * 4,
768
0
                               ds, false);
769
0
            ds_put_char(ds, ')');
770
0
        }
771
772
0
        ds_put_char(ds, ')');
773
0
    } else if (data->tnl_type == OVS_VPORT_TYPE_SRV6) {
774
0
        const struct srv6_base_hdr *srh;
775
0
        struct in6_addr *segs;
776
0
        int nr_segs;
777
0
        int i;
778
779
0
        srh = (const struct srv6_base_hdr *) l4;
780
0
        if (bytes_left < sizeof *srh) {
781
0
            ds_put_cstr(ds, "srv6(truncated header))");
782
0
            return;
783
0
        }
784
785
0
        segs = ALIGNED_CAST(struct in6_addr *, srh + 1);
786
0
        nr_segs = srh->last_entry + 1;
787
0
        bytes_left -= sizeof *srh;
788
789
0
        ds_put_format(ds, "srv6(");
790
0
        ds_put_format(ds, "segments_left=%d", srh->rt_hdr.segments_left);
791
0
        ds_put_format(ds, ",segs(");
792
0
        if (bytes_left < nr_segs * sizeof *segs) {
793
0
            ds_put_cstr(ds, "truncated segs)))");
794
0
            return;
795
0
        }
796
797
0
        for (i = 0; i < nr_segs; i++) {
798
0
            ds_put_format(ds, i > 0 ? "," : "");
799
0
            ipv6_format_addr(&segs[nr_segs - i - 1], ds);
800
0
        }
801
0
        ds_put_format(ds, "))");
802
0
    } else if (data->tnl_type == OVS_VPORT_TYPE_GRE ||
803
0
               data->tnl_type == OVS_VPORT_TYPE_IP6GRE) {
804
0
        const struct gre_base_hdr *greh;
805
0
        ovs_16aligned_be32 *options;
806
807
0
        greh = (const struct gre_base_hdr *) l4;
808
0
        if (bytes_left < sizeof *greh) {
809
0
            ds_put_cstr(ds, "gre(truncated header))");
810
0
            return;
811
0
        }
812
813
0
        ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
814
0
                           ntohs(greh->flags), ntohs(greh->protocol));
815
0
        bytes_left -= sizeof *greh;
816
0
        options = (ovs_16aligned_be32 *)(greh + 1);
817
0
        if (greh->flags & htons(GRE_CSUM)) {
818
0
            if (bytes_left < sizeof(uint16_t)) {
819
0
                ds_put_cstr(ds, ",csum=<truncated>))");
820
0
                return;
821
0
            }
822
823
0
            bytes_left -= sizeof(uint16_t);
824
0
            ds_put_format(ds, ",csum=0x%"PRIx16,
825
0
                          ntohs(*((ovs_be16 *) options)));
826
0
            options++;
827
0
        }
828
0
        if (greh->flags & htons(GRE_KEY)) {
829
0
            if (bytes_left < sizeof(uint32_t)) {
830
0
                ds_put_cstr(ds, ",key=<truncated>))");
831
0
                return;
832
0
            }
833
834
0
            bytes_left -= sizeof(uint32_t);
835
0
            ds_put_format(ds, ",key=0x%"PRIx32,
836
0
                          ntohl(get_16aligned_be32(options)));
837
0
            options++;
838
0
        }
839
0
        if (greh->flags & htons(GRE_SEQ)) {
840
0
            if (bytes_left < sizeof(uint32_t)) {
841
0
                ds_put_cstr(ds, ",seq=<truncated>))");
842
0
                return;
843
0
            }
844
845
0
            ds_put_format(ds, ",seq=0x%"PRIx32,
846
0
                          ntohl(get_16aligned_be32(options)));
847
0
            options++;
848
0
        }
849
0
        ds_put_format(ds, ")");
850
0
    } else if (data->tnl_type == OVS_VPORT_TYPE_ERSPAN ||
851
0
               data->tnl_type == OVS_VPORT_TYPE_IP6ERSPAN) {
852
0
        const struct gre_base_hdr *greh;
853
0
        const struct erspan_base_hdr *ersh;
854
855
0
        greh = (const struct gre_base_hdr *) l4;
856
0
        if (bytes_left < ERSPAN_GREHDR_LEN + sizeof *ersh) {
857
0
            ds_put_cstr(ds, "erspan(truncated header))");
858
0
            return;
859
0
        }
860
861
0
        ersh = ERSPAN_HDR(greh);
862
0
        bytes_left -= ERSPAN_GREHDR_LEN + sizeof *ersh;
863
864
0
        if (ersh->ver == 1) {
865
0
            ovs_16aligned_be32 *index = ALIGNED_CAST(ovs_16aligned_be32 *,
866
0
                                                     ersh + 1);
867
868
0
            if (bytes_left < sizeof *index) {
869
0
                ds_put_cstr(ds, "erspan(ver=1,truncated header))");
870
0
                return;
871
0
            }
872
0
            ds_put_format(ds, "erspan(ver=1,sid=0x%"PRIx16",idx=0x%"PRIx32")",
873
0
                          get_sid(ersh), ntohl(get_16aligned_be32(index)));
874
0
        } else if (ersh->ver == 2) {
875
0
            struct erspan_md2 *md2 = ALIGNED_CAST(struct erspan_md2 *,
876
0
                                                  ersh + 1);
877
878
0
            if (bytes_left < sizeof *md2) {
879
0
                ds_put_cstr(ds, "erspan(ver=2, truncated header))");
880
0
                return;
881
0
            }
882
0
            ds_put_format(ds, "erspan(ver=2,sid=0x%"PRIx16
883
0
                          ",dir=%"PRIu8",hwid=0x%"PRIx8")",
884
0
                          get_sid(ersh), md2->dir, get_hwid(md2));
885
0
        } else {
886
0
            VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__, ersh->ver);
887
0
        }
888
0
    } else if (data->tnl_type == OVS_VPORT_TYPE_GTPU) {
889
0
        const struct gtpuhdr *gtph;
890
891
0
        gtph = format_udp_tnl_push_header(ds, udp, bytes_left);
892
0
        if (!gtph) {
893
0
            return;
894
0
        }
895
896
0
        bytes_left -= sizeof *udp;
897
0
         if (bytes_left < sizeof gtph) {
898
0
            ds_put_cstr(ds, "gtpu(truncated header))");
899
0
            return;
900
0
        }
901
902
0
        ds_put_format(ds, "gtpu(flags=0x%"PRIx8
903
0
                          ",msgtype=%"PRIu8",teid=0x%"PRIx32")",
904
0
                      gtph->md.flags, gtph->md.msgtype,
905
0
                      ntohl(get_16aligned_be32(&gtph->teid)));
906
0
    }
907
908
0
    ds_put_format(ds, ")");
909
0
}
910
911
static void
912
format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr,
913
                           const struct hmap *portno_names)
914
0
{
915
0
    struct ovs_action_push_tnl *data;
916
917
0
    data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
918
919
0
    ds_put_cstr(ds, "tnl_push(tnl_port(");
920
0
    odp_portno_name_format(portno_names, data->tnl_port, ds);
921
0
    ds_put_cstr(ds, "),");
922
0
    format_odp_tnl_push_header(ds, data);
923
0
    ds_put_format(ds, ",out_port(");
924
0
    odp_portno_name_format(portno_names, data->out_port, ds);
925
0
    ds_put_cstr(ds, "))");
926
0
}
927
928
static const struct nl_policy ovs_nat_policy[] = {
929
    [OVS_NAT_ATTR_SRC] = { .type = NL_A_FLAG, .optional = true, },
930
    [OVS_NAT_ATTR_DST] = { .type = NL_A_FLAG, .optional = true, },
931
    [OVS_NAT_ATTR_IP_MIN] = { .type = NL_A_UNSPEC, .optional = true,
932
                              .min_len = sizeof(struct in_addr),
933
                              .max_len = sizeof(struct in6_addr)},
934
    [OVS_NAT_ATTR_IP_MAX] = { .type = NL_A_UNSPEC, .optional = true,
935
                              .min_len = sizeof(struct in_addr),
936
                              .max_len = sizeof(struct in6_addr)},
937
    [OVS_NAT_ATTR_PROTO_MIN] = { .type = NL_A_U16, .optional = true, },
938
    [OVS_NAT_ATTR_PROTO_MAX] = { .type = NL_A_U16, .optional = true, },
939
    [OVS_NAT_ATTR_PERSISTENT] = { .type = NL_A_FLAG, .optional = true, },
940
    [OVS_NAT_ATTR_PROTO_HASH] = { .type = NL_A_FLAG, .optional = true, },
941
    [OVS_NAT_ATTR_PROTO_RANDOM] = { .type = NL_A_FLAG, .optional = true, },
942
};
943
944
static void
945
format_odp_ct_nat(struct ds *ds, const struct nlattr *attr)
946
0
{
947
0
    struct nlattr *a[ARRAY_SIZE(ovs_nat_policy)];
948
0
    size_t addr_len;
949
0
    ovs_be32 ip_min, ip_max;
950
0
    struct in6_addr ip6_min, ip6_max;
951
0
    uint16_t proto_min, proto_max;
952
953
0
    if (!nl_parse_nested(attr, ovs_nat_policy, a, ARRAY_SIZE(a))) {
954
0
        ds_put_cstr(ds, "nat(error: nl_parse_nested() failed.)");
955
0
        return;
956
0
    }
957
    /* If no type, then nothing else either. */
958
0
    if (!(a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST])
959
0
        && (a[OVS_NAT_ATTR_IP_MIN] || a[OVS_NAT_ATTR_IP_MAX]
960
0
            || a[OVS_NAT_ATTR_PROTO_MIN] || a[OVS_NAT_ATTR_PROTO_MAX]
961
0
            || a[OVS_NAT_ATTR_PERSISTENT] || a[OVS_NAT_ATTR_PROTO_HASH]
962
0
            || a[OVS_NAT_ATTR_PROTO_RANDOM])) {
963
0
        ds_put_cstr(ds, "nat(error: options allowed only with \"src\" or \"dst\")");
964
0
        return;
965
0
    }
966
    /* Both SNAT & DNAT may not be specified. */
967
0
    if (a[OVS_NAT_ATTR_SRC] && a[OVS_NAT_ATTR_DST]) {
968
0
        ds_put_cstr(ds, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
969
0
        return;
970
0
    }
971
    /* proto may not appear without ip. */
972
0
    if (!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_PROTO_MIN]) {
973
0
        ds_put_cstr(ds, "nat(error: proto but no IP.)");
974
0
        return;
975
0
    }
976
    /* MAX may not appear without MIN. */
977
0
    if ((!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX])
978
0
        || (!a[OVS_NAT_ATTR_PROTO_MIN] && a[OVS_NAT_ATTR_PROTO_MAX])) {
979
0
        ds_put_cstr(ds, "nat(error: range max without min.)");
980
0
        return;
981
0
    }
982
    /* Address sizes must match. */
983
0
    if ((a[OVS_NAT_ATTR_IP_MIN]
984
0
         && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(ovs_be32) &&
985
0
             nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(struct in6_addr)))
986
0
        || (a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX]
987
0
            && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN])
988
0
                != nl_attr_get_size(a[OVS_NAT_ATTR_IP_MAX])))) {
989
0
        ds_put_cstr(ds, "nat(error: IP address sizes do not match)");
990
0
        return;
991
0
    }
992
993
0
    addr_len = a[OVS_NAT_ATTR_IP_MIN]
994
0
        ? nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) : 0;
995
0
    ip_min = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MIN]
996
0
        ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MIN]) : 0;
997
0
    ip_max = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MAX]
998
0
        ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MAX]) : 0;
999
0
    if (addr_len == sizeof ip6_min) {
1000
0
        ip6_min = a[OVS_NAT_ATTR_IP_MIN]
1001
0
            ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MIN])
1002
0
            : in6addr_any;
1003
0
        ip6_max = a[OVS_NAT_ATTR_IP_MAX]
1004
0
            ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MAX])
1005
0
            : in6addr_any;
1006
0
    }
1007
0
    proto_min = a[OVS_NAT_ATTR_PROTO_MIN]
1008
0
        ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MIN]) : 0;
1009
0
    proto_max = a[OVS_NAT_ATTR_PROTO_MAX]
1010
0
        ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MAX]) : 0;
1011
1012
0
    if ((addr_len == sizeof(ovs_be32)
1013
0
         && ip_max && ntohl(ip_min) > ntohl(ip_max))
1014
0
        || (addr_len == sizeof(struct in6_addr)
1015
0
            && !ipv6_mask_is_any(&ip6_max)
1016
0
            && memcmp(&ip6_min, &ip6_max, sizeof ip6_min) > 0)
1017
0
        || (proto_max && proto_min > proto_max)) {
1018
0
        ds_put_cstr(ds, "nat(range error)");
1019
0
        return;
1020
0
    }
1021
1022
0
    ds_put_cstr(ds, "nat");
1023
0
    if (a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) {
1024
0
        ds_put_char(ds, '(');
1025
0
        if (a[OVS_NAT_ATTR_SRC]) {
1026
0
            ds_put_cstr(ds, "src");
1027
0
        } else if (a[OVS_NAT_ATTR_DST]) {
1028
0
            ds_put_cstr(ds, "dst");
1029
0
        }
1030
1031
0
        if (addr_len > 0) {
1032
0
            ds_put_cstr(ds, "=");
1033
1034
0
            if (addr_len == sizeof ip_min) {
1035
0
                ds_put_format(ds, IP_FMT, IP_ARGS(ip_min));
1036
1037
0
                if (ip_max && ip_max != ip_min) {
1038
0
                    ds_put_format(ds, "-"IP_FMT, IP_ARGS(ip_max));
1039
0
                }
1040
0
            } else if (addr_len == sizeof ip6_min) {
1041
0
                ipv6_format_addr_bracket(&ip6_min, ds, proto_min);
1042
1043
0
                if (!ipv6_mask_is_any(&ip6_max) &&
1044
0
                    memcmp(&ip6_max, &ip6_min, sizeof ip6_max) != 0) {
1045
0
                    ds_put_char(ds, '-');
1046
0
                    ipv6_format_addr_bracket(&ip6_max, ds, proto_min);
1047
0
                }
1048
0
            }
1049
0
            if (proto_min) {
1050
0
                ds_put_format(ds, ":%"PRIu16, proto_min);
1051
1052
0
                if (proto_max && proto_max != proto_min) {
1053
0
                    ds_put_format(ds, "-%"PRIu16, proto_max);
1054
0
                }
1055
0
            }
1056
0
        }
1057
0
        ds_put_char(ds, ',');
1058
0
        if (a[OVS_NAT_ATTR_PERSISTENT]) {
1059
0
            ds_put_cstr(ds, "persistent,");
1060
0
        }
1061
0
        if (a[OVS_NAT_ATTR_PROTO_HASH]) {
1062
0
            ds_put_cstr(ds, "hash,");
1063
0
        }
1064
0
        if (a[OVS_NAT_ATTR_PROTO_RANDOM]) {
1065
0
            ds_put_cstr(ds, "random,");
1066
0
        }
1067
0
        ds_chomp(ds, ',');
1068
0
        ds_put_char(ds, ')');
1069
0
    }
1070
0
}
1071
1072
static const struct nl_policy ovs_conntrack_policy[] = {
1073
    [OVS_CT_ATTR_COMMIT] = { .type = NL_A_FLAG, .optional = true, },
1074
    [OVS_CT_ATTR_FORCE_COMMIT] = { .type = NL_A_FLAG, .optional = true, },
1075
    [OVS_CT_ATTR_ZONE] = { .type = NL_A_U16, .optional = true, },
1076
    [OVS_CT_ATTR_MARK] = { .type = NL_A_UNSPEC, .optional = true,
1077
                           .min_len = sizeof(uint32_t) * 2 },
1078
    [OVS_CT_ATTR_LABELS] = { .type = NL_A_UNSPEC, .optional = true,
1079
                             .min_len = sizeof(struct ovs_key_ct_labels) * 2 },
1080
    [OVS_CT_ATTR_HELPER] = { .type = NL_A_STRING, .optional = true,
1081
                             .min_len = 1, .max_len = 16 },
1082
    [OVS_CT_ATTR_NAT] = { .type = NL_A_UNSPEC, .optional = true },
1083
    [OVS_CT_ATTR_TIMEOUT] = { .type = NL_A_STRING, .optional = true,
1084
                              .min_len = 1, .max_len = 32 },
1085
};
1086
1087
static void
1088
format_odp_conntrack_action(struct ds *ds, const struct nlattr *attr)
1089
0
{
1090
0
    struct nlattr *a[ARRAY_SIZE(ovs_conntrack_policy)];
1091
0
    const struct {
1092
0
        ovs_32aligned_u128 value;
1093
0
        ovs_32aligned_u128 mask;
1094
0
    } *label;
1095
0
    const uint32_t *mark;
1096
0
    const char *helper, *timeout;
1097
0
    uint16_t zone;
1098
0
    bool commit, force;
1099
0
    const struct nlattr *nat;
1100
1101
0
    if (!nl_parse_nested(attr, ovs_conntrack_policy, a, ARRAY_SIZE(a))) {
1102
0
        ds_put_cstr(ds, "ct(error)");
1103
0
        return;
1104
0
    }
1105
1106
0
    commit = a[OVS_CT_ATTR_COMMIT] ? true : false;
1107
0
    force = a[OVS_CT_ATTR_FORCE_COMMIT] ? true : false;
1108
0
    zone = a[OVS_CT_ATTR_ZONE] ? nl_attr_get_u16(a[OVS_CT_ATTR_ZONE]) : 0;
1109
0
    mark = a[OVS_CT_ATTR_MARK] ? nl_attr_get(a[OVS_CT_ATTR_MARK]) : NULL;
1110
0
    label = a[OVS_CT_ATTR_LABELS] ? nl_attr_get(a[OVS_CT_ATTR_LABELS]): NULL;
1111
0
    helper = a[OVS_CT_ATTR_HELPER] ? nl_attr_get(a[OVS_CT_ATTR_HELPER]) : NULL;
1112
0
    timeout = a[OVS_CT_ATTR_TIMEOUT] ?
1113
0
                nl_attr_get(a[OVS_CT_ATTR_TIMEOUT]) : NULL;
1114
0
    nat = a[OVS_CT_ATTR_NAT];
1115
1116
0
    ds_put_format(ds, "ct");
1117
0
    if (commit || force || zone || mark || label || helper || timeout || nat) {
1118
0
        ds_put_cstr(ds, "(");
1119
0
        if (commit) {
1120
0
            ds_put_format(ds, "commit,");
1121
0
        }
1122
0
        if (force) {
1123
0
            ds_put_format(ds, "force_commit,");
1124
0
        }
1125
0
        if (zone) {
1126
0
            ds_put_format(ds, "zone=%"PRIu16",", zone);
1127
0
        }
1128
0
        if (mark) {
1129
0
            ds_put_format(ds, "mark=%#"PRIx32"/%#"PRIx32",", *mark,
1130
0
                          *(mark + 1));
1131
0
        }
1132
0
        if (label) {
1133
0
            ds_put_format(ds, "label=");
1134
0
            format_u128(ds, &label->value, &label->mask, true);
1135
0
            ds_put_char(ds, ',');
1136
0
        }
1137
0
        if (helper) {
1138
0
            ds_put_format(ds, "helper=%s,", helper);
1139
0
        }
1140
0
        if (timeout) {
1141
0
            ds_put_format(ds, "timeout=%s,", timeout);
1142
0
        }
1143
0
        if (nat) {
1144
0
            format_odp_ct_nat(ds, nat);
1145
0
        }
1146
0
        ds_chomp(ds, ',');
1147
0
        ds_put_cstr(ds, ")");
1148
0
    }
1149
0
}
1150
1151
static const struct attr_len_tbl
1152
ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
1153
    [OVS_NSH_KEY_ATTR_BASE]     = { .len = 8 },
1154
    [OVS_NSH_KEY_ATTR_MD1]      = { .len = 16 },
1155
    [OVS_NSH_KEY_ATTR_MD2]      = { .len = ATTR_LEN_VARIABLE },
1156
};
1157
1158
static void
1159
format_odp_set_nsh(struct ds *ds, const struct nlattr *attr)
1160
0
{
1161
0
    unsigned int left;
1162
0
    const struct nlattr *a;
1163
0
    struct ovs_key_nsh nsh;
1164
0
    struct ovs_key_nsh nsh_mask;
1165
1166
0
    memset(&nsh, 0, sizeof nsh);
1167
0
    memset(&nsh_mask, 0, sizeof nsh_mask);
1168
1169
0
    NL_NESTED_FOR_EACH (a, left, attr) {
1170
0
        enum ovs_nsh_key_attr type = nl_attr_type(a);
1171
0
        size_t len = nl_attr_get_size(a);
1172
1173
0
        if (type > OVS_NSH_KEY_ATTR_MAX) {
1174
0
            return;
1175
0
        }
1176
1177
0
        int expected_len = ovs_nsh_key_attr_lens[type].len;
1178
0
        if ((expected_len != ATTR_LEN_VARIABLE) && (len != 2 * expected_len)) {
1179
0
            return;
1180
0
        }
1181
1182
0
        switch (type) {
1183
0
        case OVS_NSH_KEY_ATTR_UNSPEC:
1184
0
            break;
1185
0
        case OVS_NSH_KEY_ATTR_BASE: {
1186
0
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
1187
0
            const struct ovs_nsh_key_base *base_mask = base + 1;
1188
0
            memcpy(&nsh, base, sizeof(*base));
1189
0
            memcpy(&nsh_mask, base_mask, sizeof(*base_mask));
1190
0
            break;
1191
0
        }
1192
0
        case OVS_NSH_KEY_ATTR_MD1: {
1193
0
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
1194
0
            const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1195
0
            memcpy(&nsh.context, &md1->context, sizeof(*md1));
1196
0
            memcpy(&nsh_mask.context, &md1_mask->context, sizeof(*md1_mask));
1197
0
            break;
1198
0
        }
1199
0
        case OVS_NSH_KEY_ATTR_MD2:
1200
0
        case __OVS_NSH_KEY_ATTR_MAX:
1201
0
        default:
1202
            /* No support for matching other metadata formats yet. */
1203
0
            break;
1204
0
        }
1205
0
    }
1206
1207
0
    ds_put_cstr(ds, "set(nsh(");
1208
0
    format_nsh_key_mask(ds, &nsh, &nsh_mask);
1209
0
    ds_put_cstr(ds, "))");
1210
0
}
1211
1212
static void
1213
format_odp_check_pkt_len_action(struct ds *ds, const struct nlattr *attr,
1214
                                const struct hmap *portno_names OVS_UNUSED)
1215
0
{
1216
0
    static const struct nl_policy ovs_cpl_policy[] = {
1217
0
        [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = { .type = NL_A_U16 },
1218
0
        [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = { .type = NL_A_NESTED },
1219
0
        [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL]
1220
0
            = { .type = NL_A_NESTED },
1221
0
    };
1222
0
    struct nlattr *a[ARRAY_SIZE(ovs_cpl_policy)];
1223
0
    ds_put_cstr(ds, "check_pkt_len");
1224
0
    if (!nl_parse_nested(attr, ovs_cpl_policy, a, ARRAY_SIZE(a))) {
1225
0
        ds_put_cstr(ds, "(error)");
1226
0
        return;
1227
0
    }
1228
1229
0
    if (!a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] ||
1230
0
        !a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL]) {
1231
0
        ds_put_cstr(ds, "(error)");
1232
0
        return;
1233
0
    }
1234
1235
0
    uint16_t pkt_len = nl_attr_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
1236
0
    ds_put_format(ds, "(size=%u,gt(", pkt_len);
1237
0
    const struct nlattr *acts;
1238
0
    acts = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
1239
0
    format_odp_actions(ds, nl_attr_get(acts), nl_attr_get_size(acts),
1240
0
                       portno_names);
1241
1242
0
    ds_put_cstr(ds, "),le(");
1243
0
    acts = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
1244
0
    format_odp_actions(ds, nl_attr_get(acts), nl_attr_get_size(acts),
1245
0
                           portno_names);
1246
0
    ds_put_cstr(ds, "))");
1247
0
}
1248
1249
static void
1250
format_dec_ttl_action(struct ds *ds, const struct nlattr *attr,
1251
                      const struct hmap *portno_names)
1252
0
{
1253
0
    const struct nlattr *a;
1254
0
    unsigned int left;
1255
1256
0
    ds_put_cstr(ds,"dec_ttl(le_1(");
1257
0
    NL_ATTR_FOR_EACH (a, left,
1258
0
                      nl_attr_get(attr), nl_attr_get_size(attr)) {
1259
0
        if (nl_attr_type(a) == OVS_DEC_TTL_ATTR_ACTION) {
1260
0
           format_odp_actions(ds, nl_attr_get(a),
1261
0
                              nl_attr_get_size(a), portno_names);
1262
0
           break;
1263
0
        }
1264
0
    }
1265
0
    ds_put_format(ds, "))");
1266
0
}
1267
1268
static void
1269
format_odp_psample_action(struct ds *ds, const struct nlattr *attr)
1270
0
{
1271
0
    const struct nlattr *a;
1272
0
    unsigned int left;
1273
1274
0
    ds_put_cstr(ds, "psample(");
1275
0
    NL_NESTED_FOR_EACH (a, left, attr) {
1276
0
        switch (a->nla_type) {
1277
0
        case OVS_PSAMPLE_ATTR_GROUP:
1278
0
            ds_put_format(ds, "group=%"PRIu32",", nl_attr_get_u32(a));
1279
0
            break;
1280
0
        case OVS_PSAMPLE_ATTR_COOKIE:
1281
0
            ds_put_cstr(ds, "cookie=");
1282
0
            ds_put_hex(ds, nl_attr_get(a), nl_attr_get_size(a));
1283
0
            break;
1284
0
        }
1285
0
    }
1286
0
    ds_chomp(ds, ',');
1287
0
    ds_put_char(ds, ')');
1288
0
}
1289
1290
static void
1291
format_odp_action(struct ds *ds, const struct nlattr *a,
1292
                  const struct hmap *portno_names)
1293
0
{
1294
0
    int expected_len;
1295
0
    enum ovs_action_attr type = nl_attr_type(a);
1296
0
    size_t size;
1297
1298
0
    expected_len = odp_action_len(nl_attr_type(a));
1299
0
    if (expected_len != ATTR_LEN_VARIABLE &&
1300
0
        nl_attr_get_size(a) != expected_len) {
1301
0
        ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
1302
0
                      nl_attr_get_size(a), expected_len);
1303
0
        format_generic_odp_action(ds, a);
1304
0
        return;
1305
0
    }
1306
1307
0
    switch (type) {
1308
0
    case OVS_ACTION_ATTR_METER:
1309
0
        ds_put_format(ds, "meter(%"PRIu32")", nl_attr_get_u32(a));
1310
0
        break;
1311
0
    case OVS_ACTION_ATTR_OUTPUT:
1312
0
        odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
1313
0
        break;
1314
0
    case OVS_ACTION_ATTR_LB_OUTPUT:
1315
0
        ds_put_format(ds, "lb_output(%"PRIu32")", nl_attr_get_u32(a));
1316
0
        break;
1317
0
    case OVS_ACTION_ATTR_TRUNC: {
1318
0
        const struct ovs_action_trunc *trunc =
1319
0
                       nl_attr_get_unspec(a, sizeof *trunc);
1320
1321
0
        ds_put_format(ds, "trunc(%"PRIu32")", trunc->max_len);
1322
0
        break;
1323
0
    }
1324
0
    case OVS_ACTION_ATTR_TUNNEL_POP:
1325
0
        ds_put_cstr(ds, "tnl_pop(");
1326
0
        odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
1327
0
        ds_put_char(ds, ')');
1328
0
        break;
1329
0
    case OVS_ACTION_ATTR_TUNNEL_PUSH:
1330
0
        format_odp_tnl_push_action(ds, a, portno_names);
1331
0
        break;
1332
0
    case OVS_ACTION_ATTR_USERSPACE:
1333
0
        format_odp_userspace_action(ds, a, portno_names);
1334
0
        break;
1335
0
    case OVS_ACTION_ATTR_RECIRC:
1336
0
        format_odp_recirc_action(ds, nl_attr_get_u32(a));
1337
0
        break;
1338
0
    case OVS_ACTION_ATTR_HASH:
1339
0
        format_odp_hash_action(ds, nl_attr_get(a));
1340
0
        break;
1341
0
    case OVS_ACTION_ATTR_SET_MASKED:
1342
0
        a = nl_attr_get(a);
1343
        /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1344
0
        if (nl_attr_type(a) == OVS_KEY_ATTR_NSH) {
1345
0
            format_odp_set_nsh(ds, a);
1346
0
            break;
1347
0
        }
1348
0
        size = nl_attr_get_size(a) / 2;
1349
0
        ds_put_cstr(ds, "set(");
1350
1351
        /* Masked set action not supported for tunnel key, which is bigger. */
1352
0
        if (size <= sizeof(struct ovs_key_ipv6)) {
1353
0
            struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
1354
0
                                                sizeof(struct nlattr))];
1355
0
            struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
1356
0
                                                sizeof(struct nlattr))];
1357
1358
0
            mask->nla_type = attr->nla_type = nl_attr_type(a);
1359
0
            mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
1360
0
            memcpy(attr + 1, (char *)(a + 1), size);
1361
0
            memcpy(mask + 1, (char *)(a + 1) + size, size);
1362
0
            format_odp_key_attr(attr, mask, NULL, ds, false);
1363
0
        } else {
1364
0
            format_odp_key_attr(a, NULL, NULL, ds, false);
1365
0
        }
1366
0
        ds_put_cstr(ds, ")");
1367
0
        break;
1368
0
    case OVS_ACTION_ATTR_SET:
1369
0
        ds_put_cstr(ds, "set(");
1370
0
        format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
1371
0
        ds_put_cstr(ds, ")");
1372
0
        break;
1373
0
    case OVS_ACTION_ATTR_PUSH_ETH: {
1374
0
        const struct ovs_action_push_eth *eth = nl_attr_get(a);
1375
0
        ds_put_format(ds, "push_eth(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
1376
0
                      ETH_ADDR_ARGS(eth->addresses.eth_src),
1377
0
                      ETH_ADDR_ARGS(eth->addresses.eth_dst));
1378
0
        break;
1379
0
    }
1380
0
    case OVS_ACTION_ATTR_POP_ETH:
1381
0
        ds_put_cstr(ds, "pop_eth");
1382
0
        break;
1383
0
    case OVS_ACTION_ATTR_PUSH_VLAN: {
1384
0
        const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
1385
0
        ds_put_cstr(ds, "push_vlan(");
1386
0
        if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
1387
0
            ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
1388
0
        }
1389
0
        format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
1390
0
        ds_put_char(ds, ')');
1391
0
        break;
1392
0
    }
1393
0
    case OVS_ACTION_ATTR_POP_VLAN:
1394
0
        ds_put_cstr(ds, "pop_vlan");
1395
0
        break;
1396
0
    case OVS_ACTION_ATTR_PUSH_MPLS: {
1397
0
        const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
1398
0
        ds_put_cstr(ds, "push_mpls(");
1399
0
        format_mpls_lse(ds, mpls->mpls_lse);
1400
0
        ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
1401
0
        break;
1402
0
    }
1403
0
    case OVS_ACTION_ATTR_POP_MPLS: {
1404
0
        ovs_be16 ethertype = nl_attr_get_be16(a);
1405
0
        ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
1406
0
        break;
1407
0
    }
1408
0
    case OVS_ACTION_ATTR_SAMPLE:
1409
0
        format_odp_sample_action(ds, a, portno_names);
1410
0
        break;
1411
0
    case OVS_ACTION_ATTR_CT:
1412
0
        format_odp_conntrack_action(ds, a);
1413
0
        break;
1414
0
    case OVS_ACTION_ATTR_CT_CLEAR:
1415
0
        ds_put_cstr(ds, "ct_clear");
1416
0
        break;
1417
0
    case OVS_ACTION_ATTR_CLONE:
1418
0
        format_odp_clone_action(ds, a, portno_names);
1419
0
        break;
1420
0
    case OVS_ACTION_ATTR_PUSH_NSH: {
1421
0
        uint32_t buffer[NSH_HDR_MAX_LEN / 4];
1422
0
        struct nsh_hdr *nsh_hdr = ALIGNED_CAST(struct nsh_hdr *, buffer);
1423
0
        nsh_reset_ver_flags_ttl_len(nsh_hdr);
1424
0
        odp_nsh_hdr_from_attr(nl_attr_get(a), nsh_hdr, NSH_HDR_MAX_LEN);
1425
0
        format_odp_push_nsh_action(ds, nsh_hdr);
1426
0
        break;
1427
0
    }
1428
0
    case OVS_ACTION_ATTR_POP_NSH:
1429
0
        ds_put_cstr(ds, "pop_nsh()");
1430
0
        break;
1431
0
    case OVS_ACTION_ATTR_CHECK_PKT_LEN:
1432
0
        format_odp_check_pkt_len_action(ds, a, portno_names);
1433
0
        break;
1434
0
    case OVS_ACTION_ATTR_ADD_MPLS: {
1435
0
        const struct ovs_action_add_mpls *mpls = nl_attr_get(a);
1436
1437
0
        ds_put_cstr(ds, "add_mpls(");
1438
0
        format_mpls_lse(ds, mpls->mpls_lse);
1439
0
        ds_put_format(ds, ",eth_type=0x%"PRIx16")",
1440
0
                      ntohs(mpls->mpls_ethertype));
1441
0
        break;
1442
0
    }
1443
0
    case OVS_ACTION_ATTR_DEC_TTL:
1444
0
        format_dec_ttl_action(ds, a, portno_names);
1445
0
        break;
1446
0
    case OVS_ACTION_ATTR_DROP:
1447
0
        ds_put_cstr(ds, "drop");
1448
0
        break;
1449
0
    case OVS_ACTION_ATTR_PSAMPLE:
1450
0
        format_odp_psample_action(ds, a);
1451
0
        break;
1452
0
    case OVS_ACTION_ATTR_UNSPEC:
1453
0
    case __OVS_ACTION_ATTR_MAX:
1454
0
    default:
1455
0
        format_generic_odp_action(ds, a);
1456
0
        break;
1457
0
    }
1458
0
}
1459
1460
void
1461
format_odp_actions(struct ds *ds, const struct nlattr *actions,
1462
                   size_t actions_len, const struct hmap *portno_names)
1463
0
{
1464
0
    if (actions_len) {
1465
0
        const struct nlattr *a;
1466
0
        unsigned int left;
1467
1468
0
        NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
1469
0
            if (a != actions) {
1470
0
                ds_put_char(ds, ',');
1471
0
            }
1472
0
            format_odp_action(ds, a, portno_names);
1473
0
        }
1474
0
        if (left) {
1475
0
            int i;
1476
1477
0
            if (left == actions_len) {
1478
0
                ds_put_cstr(ds, "<empty>");
1479
0
            }
1480
0
            ds_put_format(ds, ",***%u leftover bytes*** (", left);
1481
0
            for (i = 0; i < left; i++) {
1482
0
                ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1483
0
            }
1484
0
            ds_put_char(ds, ')');
1485
0
        }
1486
0
    } else {
1487
0
        ds_put_cstr(ds, "drop");
1488
0
    }
1489
0
}
1490
1491
/* Separate out parse_odp_userspace_action() function. */
1492
static int
1493
parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
1494
0
{
1495
0
    uint32_t pid;
1496
0
    struct user_action_cookie cookie;
1497
0
    struct ofpbuf buf;
1498
0
    odp_port_t tunnel_out_port;
1499
0
    int n = -1;
1500
0
    void *user_data = NULL;
1501
0
    size_t user_data_size = 0;
1502
0
    bool include_actions = false;
1503
0
    int res;
1504
1505
0
    if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
1506
0
        return -EINVAL;
1507
0
    }
1508
1509
0
    ofpbuf_init(&buf, 16);
1510
0
    memset(&cookie, 0, sizeof cookie);
1511
1512
0
    user_data = &cookie;
1513
0
    user_data_size = sizeof cookie;
1514
0
    {
1515
0
        uint32_t output;
1516
0
        uint32_t probability;
1517
0
        uint32_t collector_set_id;
1518
0
        uint32_t obs_domain_id;
1519
0
        uint32_t obs_point_id;
1520
1521
        /* USER_ACTION_COOKIE_CONTROLLER. */
1522
0
        uint8_t dont_send;
1523
0
        uint8_t continuation;
1524
0
        uint16_t reason;
1525
0
        uint32_t recirc_id;
1526
0
        uint64_t rule_cookie;
1527
0
        uint16_t controller_id;
1528
0
        uint16_t max_len;
1529
1530
0
        int vid, pcp;
1531
0
        int n1 = -1;
1532
0
        if (ovs_scan(&s[n], ",sFlow(vid=%i,"
1533
0
                     "pcp=%i,output=%"SCNi32")%n",
1534
0
                     &vid, &pcp, &output, &n1)) {
1535
0
            uint16_t tci;
1536
1537
0
            n += n1;
1538
0
            tci = vid | (pcp << VLAN_PCP_SHIFT);
1539
0
            if (tci) {
1540
0
                tci |= VLAN_CFI;
1541
0
            }
1542
1543
0
            cookie.type = USER_ACTION_COOKIE_SFLOW;
1544
0
            cookie.ofp_in_port = OFPP_NONE;
1545
0
            cookie.ofproto_uuid = UUID_ZERO;
1546
0
            cookie.sflow.vlan_tci = htons(tci);
1547
0
            cookie.sflow.output = output;
1548
0
        } else if (ovs_scan(&s[n], ",slow_path(%n",
1549
0
                            &n1)) {
1550
0
            n += n1;
1551
0
            cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
1552
0
            cookie.ofp_in_port = OFPP_NONE;
1553
0
            cookie.ofproto_uuid = UUID_ZERO;
1554
0
            cookie.slow_path.reason = 0;
1555
1556
0
            res = parse_odp_flags(&s[n], slow_path_reason_to_string,
1557
0
                                  &cookie.slow_path.reason,
1558
0
                                  SLOW_PATH_REASON_MASK, NULL);
1559
0
            if (res < 0 || s[n + res] != ')') {
1560
0
                goto out;
1561
0
            }
1562
0
            n += res + 1;
1563
0
        } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
1564
0
                            "collector_set_id=%"SCNi32","
1565
0
                            "obs_domain_id=%"SCNi32","
1566
0
                            "obs_point_id=%"SCNi32","
1567
0
                            "output_port=%"SCNi32"%n",
1568
0
                            &probability, &collector_set_id,
1569
0
                            &obs_domain_id, &obs_point_id,
1570
0
                            &output, &n1)) {
1571
0
            n += n1;
1572
1573
0
            cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1574
0
            cookie.ofp_in_port = OFPP_NONE;
1575
0
            cookie.ofproto_uuid = UUID_ZERO;
1576
0
            cookie.flow_sample.probability = probability;
1577
0
            cookie.flow_sample.collector_set_id = collector_set_id;
1578
0
            cookie.flow_sample.obs_domain_id = obs_domain_id;
1579
0
            cookie.flow_sample.obs_point_id = obs_point_id;
1580
0
            cookie.flow_sample.output_odp_port = u32_to_odp(output);
1581
1582
0
            if (ovs_scan(&s[n], ",ingress%n", &n1)) {
1583
0
                cookie.flow_sample.direction = NX_ACTION_SAMPLE_INGRESS;
1584
0
                n += n1;
1585
0
            } else if (ovs_scan(&s[n], ",egress%n", &n1)) {
1586
0
                cookie.flow_sample.direction = NX_ACTION_SAMPLE_EGRESS;
1587
0
                n += n1;
1588
0
            } else {
1589
0
                cookie.flow_sample.direction = NX_ACTION_SAMPLE_DEFAULT;
1590
0
            }
1591
0
            if (s[n] != ')') {
1592
0
                res = -EINVAL;
1593
0
                goto out;
1594
0
            }
1595
0
            n++;
1596
0
        } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
1597
0
                            &output, &n1) ) {
1598
0
            n += n1;
1599
0
            cookie.type = USER_ACTION_COOKIE_IPFIX;
1600
0
            cookie.ofp_in_port = OFPP_NONE;
1601
0
            cookie.ofproto_uuid = UUID_ZERO;
1602
0
            cookie.ipfix.output_odp_port = u32_to_odp(output);
1603
0
        } else if (ovs_scan(&s[n], ",controller(reason=%"SCNu16
1604
0
                              ",dont_send=%"SCNu8
1605
0
                              ",continuation=%"SCNu8
1606
0
                              ",recirc_id=%"SCNu32
1607
0
                              ",rule_cookie=%"SCNx64
1608
0
                              ",controller_id=%"SCNu16
1609
0
                              ",max_len=%"SCNu16")%n",
1610
0
                              &reason, &dont_send, &continuation, &recirc_id,
1611
0
                              &rule_cookie, &controller_id, &max_len, &n1)) {
1612
0
            n += n1;
1613
0
            cookie.type = USER_ACTION_COOKIE_CONTROLLER;
1614
0
            cookie.ofp_in_port = OFPP_NONE;
1615
0
            cookie.ofproto_uuid = UUID_ZERO;
1616
0
            cookie.controller.dont_send = dont_send ? true : false;
1617
0
            cookie.controller.continuation = continuation ? true : false;
1618
0
            cookie.controller.reason = reason;
1619
0
            cookie.controller.recirc_id = recirc_id;
1620
0
            put_32aligned_be64(&cookie.controller.rule_cookie,
1621
0
                               htonll(rule_cookie));
1622
0
            cookie.controller.controller_id = controller_id;
1623
0
            cookie.controller.max_len = max_len;
1624
0
       } else if (ovs_scan(&s[n], ",userdata(%n", &n1)) {
1625
0
            char *end;
1626
1627
0
            n += n1;
1628
0
            end = ofpbuf_put_hex(&buf, &s[n], NULL);
1629
0
            if (end[0] != ')') {
1630
0
                res = -EINVAL;
1631
0
                goto out;
1632
0
            }
1633
0
            user_data = buf.data;
1634
0
            user_data_size = buf.size;
1635
0
            n = (end + 1) - s;
1636
0
        }
1637
0
    }
1638
1639
0
    {
1640
0
        int n1 = -1;
1641
0
        if (ovs_scan(&s[n], ",actions%n", &n1)) {
1642
0
            n += n1;
1643
0
            include_actions = true;
1644
0
        }
1645
0
    }
1646
1647
0
    {
1648
0
        int n1 = -1;
1649
0
        if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
1650
0
                     &tunnel_out_port, &n1)) {
1651
0
            res = odp_put_userspace_action(pid, user_data, user_data_size,
1652
0
                                           tunnel_out_port, include_actions,
1653
0
                                           actions, NULL);
1654
0
            if (!res) {
1655
0
                res = n + n1;
1656
0
            }
1657
0
            goto out;
1658
0
        } else if (s[n] == ')') {
1659
0
            res = odp_put_userspace_action(pid, user_data, user_data_size,
1660
0
                                           ODPP_NONE, include_actions,
1661
0
                                           actions, NULL);
1662
0
            if (!res) {
1663
0
                res = n + 1;
1664
0
            }
1665
0
            goto out;
1666
0
        }
1667
0
    }
1668
1669
0
    {
1670
0
        struct ovs_action_push_eth push;
1671
0
        int eth_type = 0;
1672
0
        int n1 = -1;
1673
1674
0
        if (ovs_scan(&s[n], "push_eth(src="ETH_ADDR_SCAN_FMT","
1675
0
                     "dst="ETH_ADDR_SCAN_FMT",type=%i)%n",
1676
0
                     ETH_ADDR_SCAN_ARGS(push.addresses.eth_src),
1677
0
                     ETH_ADDR_SCAN_ARGS(push.addresses.eth_dst),
1678
0
                     &eth_type, &n1)) {
1679
1680
0
            nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_ETH,
1681
0
                              &push, sizeof push);
1682
1683
0
            res = n + n1;
1684
0
            goto out;
1685
0
        }
1686
0
    }
1687
1688
0
    if (!strncmp(&s[n], "pop_eth", 7)) {
1689
0
        nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_ETH);
1690
0
        res = 7;
1691
0
        goto out;
1692
0
    }
1693
1694
0
    res = -EINVAL;
1695
0
out:
1696
0
    ofpbuf_uninit(&buf);
1697
0
    return res;
1698
0
}
1699
1700
static int
1701
ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
1702
0
{
1703
0
    struct eth_header *eth;
1704
0
    struct ip_header *ip;
1705
0
    struct ovs_16aligned_ip6_hdr *ip6;
1706
0
    struct udp_header *udp;
1707
0
    struct gre_base_hdr *greh;
1708
0
    struct erspan_base_hdr *ersh;
1709
0
    struct erspan_md2 *md2;
1710
0
    uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, udp_csum, sid;
1711
0
    ovs_be32 sip, dip;
1712
0
    uint32_t tnl_type = 0, header_len = 0, ip_len = 0, erspan_idx = 0;
1713
0
    void *l3, *l4;
1714
0
    int n = 0;
1715
0
    uint8_t hwid, dir;
1716
0
    uint32_t teid;
1717
0
    uint8_t gtpu_flags, gtpu_msgtype;
1718
0
    uint8_t segments_left;
1719
1720
0
    if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
1721
0
        return -EINVAL;
1722
0
    }
1723
0
    eth = (struct eth_header *) data->header;
1724
0
    l3 = (struct ip_header *) (eth + 1);
1725
0
    ip = (struct ip_header *) l3;
1726
0
    ip6 = (struct ovs_16aligned_ip6_hdr *) l3;
1727
0
    if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
1728
0
                      "eth(dst="ETH_ADDR_SCAN_FMT",",
1729
0
                      &data->header_len,
1730
0
                      &data->tnl_type,
1731
0
                      ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
1732
0
        return -EINVAL;
1733
0
    }
1734
1735
0
    if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
1736
0
                      ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
1737
0
        return -EINVAL;
1738
0
    }
1739
0
    if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
1740
0
        return -EINVAL;
1741
0
    }
1742
0
    eth->eth_type = htons(dl_type);
1743
1744
0
    if (eth->eth_type == htons(ETH_TYPE_IP)) {
1745
        /* IPv4 */
1746
0
        uint16_t ip_frag_off;
1747
0
        memset(ip, 0, sizeof(*ip));
1748
0
        if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
1749
0
                          ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
1750
0
                          IP_SCAN_ARGS(&sip),
1751
0
                          IP_SCAN_ARGS(&dip),
1752
0
                          &ip->ip_proto, &ip->ip_tos,
1753
0
                          &ip->ip_ttl, &ip_frag_off)) {
1754
0
            return -EINVAL;
1755
0
        }
1756
0
        put_16aligned_be32(&ip->ip_src, sip);
1757
0
        put_16aligned_be32(&ip->ip_dst, dip);
1758
0
        ip->ip_frag_off = htons(ip_frag_off);
1759
0
        ip->ip_ihl_ver = IP_IHL_VER(5, 4);
1760
0
        ip_len = sizeof *ip;
1761
0
        ip->ip_csum = csum(ip, ip_len);
1762
0
    } else {
1763
0
        char sip6_s[IPV6_SCAN_LEN + 1];
1764
0
        char dip6_s[IPV6_SCAN_LEN + 1];
1765
0
        struct in6_addr sip6, dip6;
1766
0
        uint8_t tclass;
1767
0
        uint32_t label;
1768
0
        if (!ovs_scan_len(s, &n, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT
1769
0
                             ",label=%i,proto=%"SCNi8",tclass=0x%"SCNx8
1770
0
                             ",hlimit=%"SCNi8"),",
1771
0
                             sip6_s, dip6_s, &label, &ip6->ip6_nxt,
1772
0
                             &tclass, &ip6->ip6_hlim)
1773
0
            || (label & ~IPV6_LABEL_MASK) != 0
1774
0
            || inet_pton(AF_INET6, sip6_s, &sip6) != 1
1775
0
            || inet_pton(AF_INET6, dip6_s, &dip6) != 1) {
1776
0
            return -EINVAL;
1777
0
        }
1778
0
        put_16aligned_be32(&ip6->ip6_flow, htonl(6 << 28) |
1779
0
                           htonl(tclass << 20) | htonl(label));
1780
0
        memcpy(&ip6->ip6_src, &sip6, sizeof(ip6->ip6_src));
1781
0
        memcpy(&ip6->ip6_dst, &dip6, sizeof(ip6->ip6_dst));
1782
0
        ip_len = sizeof *ip6;
1783
0
    }
1784
1785
    /* Tunnel header */
1786
0
    l4 = ((uint8_t *) l3 + ip_len);
1787
0
    udp = (struct udp_header *) l4;
1788
0
    greh = (struct gre_base_hdr *) l4;
1789
0
    if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
1790
0
                     &udp_src, &udp_dst, &udp_csum)) {
1791
0
        uint32_t vx_flags, vni;
1792
1793
0
        udp->udp_src = htons(udp_src);
1794
0
        udp->udp_dst = htons(udp_dst);
1795
0
        udp->udp_len = 0;
1796
0
        udp->udp_csum = htons(udp_csum);
1797
1798
0
        if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
1799
0
                         &vx_flags, &vni)) {
1800
0
            struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
1801
1802
0
            put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
1803
0
            put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
1804
0
            tnl_type = OVS_VPORT_TYPE_VXLAN;
1805
0
            header_len = sizeof *eth + ip_len +
1806
0
                         sizeof *udp + sizeof *vxh;
1807
0
        } else if (ovs_scan_len(s, &n, "geneve(")) {
1808
0
            struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
1809
1810
0
            memset(gnh, 0, sizeof *gnh);
1811
0
            header_len = sizeof *eth + ip_len +
1812
0
                         sizeof *udp + sizeof *gnh;
1813
1814
0
            if (ovs_scan_len(s, &n, "oam,")) {
1815
0
                gnh->oam = 1;
1816
0
            }
1817
0
            if (ovs_scan_len(s, &n, "crit,")) {
1818
0
                gnh->critical = 1;
1819
0
            }
1820
0
            if (!ovs_scan_len(s, &n, "vni=%"SCNi32, &vni)) {
1821
0
                return -EINVAL;
1822
0
            }
1823
0
            if (ovs_scan_len(s, &n, ",options(")) {
1824
0
                struct geneve_scan options;
1825
0
                int len;
1826
1827
0
                memset(&options, 0, sizeof options);
1828
0
                len = scan_geneve(s + n, &options, NULL);
1829
0
                if (!len) {
1830
0
                    return -EINVAL;
1831
0
                }
1832
1833
0
                memcpy(gnh->options, options.d, options.len);
1834
0
                gnh->opt_len = options.len / 4;
1835
0
                header_len += options.len;
1836
1837
0
                n += len;
1838
0
            }
1839
0
            if (!ovs_scan_len(s, &n, "))")) {
1840
0
                return -EINVAL;
1841
0
            }
1842
1843
0
            gnh->proto_type = htons(ETH_TYPE_TEB);
1844
0
            put_16aligned_be32(&gnh->vni, htonl(vni << 8));
1845
0
            tnl_type = OVS_VPORT_TYPE_GENEVE;
1846
0
        } else {
1847
0
            return -EINVAL;
1848
0
        }
1849
0
    } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
1850
0
                            &gre_flags, &gre_proto)){
1851
1852
0
        if (eth->eth_type == htons(ETH_TYPE_IP)) {
1853
0
            tnl_type = OVS_VPORT_TYPE_GRE;
1854
0
        } else {
1855
0
            tnl_type = OVS_VPORT_TYPE_IP6GRE;
1856
0
        }
1857
0
        greh->flags = htons(gre_flags);
1858
0
        greh->protocol = htons(gre_proto);
1859
0
        ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
1860
1861
0
        if (greh->flags & htons(GRE_CSUM)) {
1862
0
            uint16_t csum;
1863
0
            if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
1864
0
                return -EINVAL;
1865
0
            }
1866
1867
0
            memset(options, 0, sizeof *options);
1868
0
            *((ovs_be16 *)options) = htons(csum);
1869
0
            options++;
1870
0
        }
1871
0
        if (greh->flags & htons(GRE_KEY)) {
1872
0
            uint32_t key;
1873
1874
0
            if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
1875
0
                return -EINVAL;
1876
0
            }
1877
1878
0
            put_16aligned_be32(options, htonl(key));
1879
0
            options++;
1880
0
        }
1881
0
        if (greh->flags & htons(GRE_SEQ)) {
1882
0
            uint32_t seq;
1883
1884
0
            if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
1885
0
                return -EINVAL;
1886
0
            }
1887
0
            put_16aligned_be32(options, htonl(seq));
1888
0
            options++;
1889
0
        }
1890
1891
0
        if (!ovs_scan_len(s, &n, "))")) {
1892
0
            return -EINVAL;
1893
0
        }
1894
1895
0
        header_len = sizeof *eth + ip_len +
1896
0
                     ((uint8_t *) options - (uint8_t *) greh);
1897
0
    } else if (ovs_scan_len(s, &n, "erspan(ver=1,sid="SCNx16",idx=0x"SCNx32")",
1898
0
                            &sid, &erspan_idx)) {
1899
0
        ersh = ERSPAN_HDR(greh);
1900
0
        ovs_16aligned_be32 *index = ALIGNED_CAST(ovs_16aligned_be32 *,
1901
0
                                                 ersh + 1);
1902
1903
0
        if (eth->eth_type == htons(ETH_TYPE_IP)) {
1904
0
            tnl_type = OVS_VPORT_TYPE_ERSPAN;
1905
0
        } else {
1906
0
            tnl_type = OVS_VPORT_TYPE_IP6ERSPAN;
1907
0
        }
1908
1909
0
        greh->flags = htons(GRE_SEQ);
1910
0
        greh->protocol = htons(ETH_TYPE_ERSPAN1);
1911
1912
0
        ersh->ver = 1;
1913
0
        set_sid(ersh, sid);
1914
0
        put_16aligned_be32(index, htonl(erspan_idx));
1915
1916
0
        if (!ovs_scan_len(s, &n, ")")) {
1917
0
            return -EINVAL;
1918
0
        }
1919
0
        header_len = sizeof *eth + ip_len + ERSPAN_GREHDR_LEN +
1920
0
                     sizeof *ersh + ERSPAN_V1_MDSIZE;
1921
1922
0
    } else if (ovs_scan_len(s, &n, "erspan(ver=2,sid="SCNx16"dir="SCNu8
1923
0
                            ",hwid=0x"SCNx8")", &sid, &dir, &hwid)) {
1924
1925
0
        ersh = ERSPAN_HDR(greh);
1926
0
        md2 = ALIGNED_CAST(struct erspan_md2 *, ersh + 1);
1927
1928
0
        if (eth->eth_type == htons(ETH_TYPE_IP)) {
1929
0
            tnl_type = OVS_VPORT_TYPE_ERSPAN;
1930
0
        } else {
1931
0
            tnl_type = OVS_VPORT_TYPE_IP6ERSPAN;
1932
0
        }
1933
1934
0
        greh->flags = htons(GRE_SEQ);
1935
0
        greh->protocol = htons(ETH_TYPE_ERSPAN2);
1936
1937
0
        ersh->ver = 2;
1938
0
        set_sid(ersh, sid);
1939
0
        set_hwid(md2, hwid);
1940
0
        md2->dir = dir;
1941
1942
0
        if (!ovs_scan_len(s, &n, ")")) {
1943
0
            return -EINVAL;
1944
0
        }
1945
1946
0
        header_len = sizeof *eth + ip_len + ERSPAN_GREHDR_LEN +
1947
0
                     sizeof *ersh + ERSPAN_V2_MDSIZE;
1948
1949
0
    } else if (ovs_scan_len(s, &n, "gtpu(flags=%"SCNi8",msgtype=%"
1950
0
                SCNu8",teid=0x%"SCNx32"))",
1951
0
                &gtpu_flags, &gtpu_msgtype, &teid)) {
1952
0
        struct gtpuhdr *gtph = (struct gtpuhdr *) (udp + 1);
1953
1954
0
        gtph->md.flags = gtpu_flags;
1955
0
        gtph->md.msgtype = gtpu_msgtype;
1956
0
        put_16aligned_be32(&gtph->teid, htonl(teid));
1957
0
        tnl_type = OVS_VPORT_TYPE_GTPU;
1958
0
        header_len = sizeof *eth + ip_len +
1959
0
                     sizeof *udp + sizeof *gtph;
1960
0
    } else if (ovs_scan_len(s, &n, "srv6(segments_left=%"SCNu8,
1961
0
                            &segments_left)) {
1962
0
        struct srv6_base_hdr *srh = (struct srv6_base_hdr *) (ip6 + 1);
1963
0
        union ovs_16aligned_in6_addr *segs;
1964
0
        char seg_s[IPV6_SCAN_LEN + 1];
1965
0
        struct in6_addr seg;
1966
0
        uint8_t n_segs = 0;
1967
1968
0
        if (segments_left + 1 > SRV6_MAX_SEGS) {
1969
0
            return -EINVAL;
1970
0
        }
1971
1972
0
        ip6->ip6_nxt = IPPROTO_ROUTING;
1973
1974
0
        srh->rt_hdr.hdrlen = 2 * (segments_left + 1);
1975
0
        srh->rt_hdr.segments_left = segments_left;
1976
0
        srh->rt_hdr.type = IPV6_SRCRT_TYPE_4;
1977
0
        srh->last_entry = segments_left;
1978
1979
0
        tnl_type = OVS_VPORT_TYPE_SRV6;
1980
0
        header_len = sizeof *eth + ip_len +
1981
0
                     sizeof *srh + 8 * srh->rt_hdr.hdrlen;
1982
        /* Parse segment list. */
1983
0
        if (!ovs_scan_len(s, &n, ",segs(")) {
1984
0
            return -EINVAL;
1985
0
        }
1986
1987
0
        segs = (union ovs_16aligned_in6_addr *) (srh + 1);
1988
0
        segs += segments_left;
1989
1990
0
        while (ovs_scan_len(s, &n, IPV6_SCAN_FMT, seg_s)
1991
0
               && inet_pton(AF_INET6, seg_s, &seg) == 1) {
1992
0
            if (n_segs == segments_left + 1) {
1993
0
                return -EINVAL;
1994
0
            }
1995
1996
0
            memcpy(segs--, &seg, sizeof *segs);
1997
0
            n_segs++;
1998
1999
0
            if (s[n] == ',') {
2000
0
                n++;
2001
0
            }
2002
0
        }
2003
2004
0
        if (!ovs_scan_len(s, &n, ")))")) {
2005
0
            return -EINVAL;
2006
0
        }
2007
2008
0
        if (n_segs != segments_left + 1) {
2009
0
            return -EINVAL;
2010
0
        }
2011
0
    } else {
2012
0
        return -EINVAL;
2013
0
    }
2014
2015
    /* check tunnel meta data. */
2016
0
    if (data->tnl_type != tnl_type) {
2017
0
        return -EINVAL;
2018
0
    }
2019
0
    if (data->header_len != header_len) {
2020
0
        return -EINVAL;
2021
0
    }
2022
2023
    /* Out port */
2024
0
    if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
2025
0
        return -EINVAL;
2026
0
    }
2027
2028
0
    return n;
2029
0
}
2030
2031
struct ct_nat_params {
2032
    bool snat;
2033
    bool dnat;
2034
    size_t addr_len;
2035
    union {
2036
        ovs_be32 ip;
2037
        struct in6_addr ip6;
2038
    } addr_min;
2039
    union {
2040
        ovs_be32 ip;
2041
        struct in6_addr ip6;
2042
    } addr_max;
2043
    uint16_t proto_min;
2044
    uint16_t proto_max;
2045
    bool persistent;
2046
    bool proto_hash;
2047
    bool proto_random;
2048
};
2049
2050
static int
2051
scan_ct_nat_range(const char *s, int *n, struct ct_nat_params *p)
2052
0
{
2053
0
    if (ovs_scan_len(s, n, "=")) {
2054
0
        char ipv6_s[IPV6_SCAN_LEN + 1];
2055
0
        struct in6_addr ipv6;
2056
2057
0
        if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(&p->addr_min.ip))) {
2058
0
            p->addr_len = sizeof p->addr_min.ip;
2059
0
            if (ovs_scan_len(s, n, "-")) {
2060
0
                if (!ovs_scan_len(s, n, IP_SCAN_FMT,
2061
0
                                  IP_SCAN_ARGS(&p->addr_max.ip))) {
2062
0
                    return -EINVAL;
2063
0
                }
2064
0
            }
2065
0
        } else if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
2066
0
                    || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
2067
0
                   && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
2068
0
            p->addr_len = sizeof p->addr_min.ip6;
2069
0
            p->addr_min.ip6 = ipv6;
2070
0
            if (ovs_scan_len(s, n, "-")) {
2071
0
                if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
2072
0
                     || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
2073
0
                    && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
2074
0
                    p->addr_max.ip6 = ipv6;
2075
0
                } else {
2076
0
                    return -EINVAL;
2077
0
                }
2078
0
            }
2079
0
        } else {
2080
0
            return -EINVAL;
2081
0
        }
2082
0
        if (ovs_scan_len(s, n, ":%"SCNu16, &p->proto_min)) {
2083
0
            if (ovs_scan_len(s, n, "-")) {
2084
0
                if (!ovs_scan_len(s, n, "%"SCNu16, &p->proto_max)) {
2085
0
                    return -EINVAL;
2086
0
                }
2087
0
            }
2088
0
        }
2089
0
    }
2090
0
    return 0;
2091
0
}
2092
2093
static int
2094
scan_ct_nat(const char *s, struct ct_nat_params *p)
2095
0
{
2096
0
    int n = 0;
2097
2098
0
    if (ovs_scan_len(s, &n, "nat")) {
2099
0
        memset(p, 0, sizeof *p);
2100
2101
0
        if (ovs_scan_len(s, &n, "(")) {
2102
0
            const char *end;
2103
0
            int end_n;
2104
2105
0
            end = strchr(s + n, ')');
2106
0
            if (!end) {
2107
0
                return -EINVAL;
2108
0
            }
2109
0
            end_n = end - s;
2110
2111
0
            while (n < end_n) {
2112
0
                n += strspn(s + n, delimiters);
2113
0
                if (ovs_scan_len(s, &n, "src")) {
2114
0
                    int err = scan_ct_nat_range(s, &n, p);
2115
0
                    if (err) {
2116
0
                        return err;
2117
0
                    }
2118
0
                    p->snat = true;
2119
0
                    continue;
2120
0
                }
2121
0
                if (ovs_scan_len(s, &n, "dst")) {
2122
0
                    int err = scan_ct_nat_range(s, &n, p);
2123
0
                    if (err) {
2124
0
                        return err;
2125
0
                    }
2126
0
                    p->dnat = true;
2127
0
                    continue;
2128
0
                }
2129
0
                if (ovs_scan_len(s, &n, "persistent")) {
2130
0
                    p->persistent = true;
2131
0
                    continue;
2132
0
                }
2133
0
                if (ovs_scan_len(s, &n, "hash")) {
2134
0
                    p->proto_hash = true;
2135
0
                    continue;
2136
0
                }
2137
0
                if (ovs_scan_len(s, &n, "random")) {
2138
0
                    p->proto_random = true;
2139
0
                    continue;
2140
0
                }
2141
0
                return -EINVAL;
2142
0
            }
2143
2144
0
            if (p->snat && p->dnat) {
2145
0
                return -EINVAL;
2146
0
            }
2147
0
            if ((p->addr_len != 0 &&
2148
0
                 memcmp(&p->addr_max, &in6addr_any, p->addr_len) &&
2149
0
                 memcmp(&p->addr_max, &p->addr_min, p->addr_len) < 0) ||
2150
0
                (p->proto_max && p->proto_max < p->proto_min)) {
2151
0
                return -EINVAL;
2152
0
            }
2153
0
            if (p->proto_hash && p->proto_random) {
2154
0
                return -EINVAL;
2155
0
            }
2156
0
            n++;
2157
0
        }
2158
0
    }
2159
0
    return n;
2160
0
}
2161
2162
static void
2163
nl_msg_put_ct_nat(struct ct_nat_params *p, struct ofpbuf *actions)
2164
0
{
2165
0
    size_t start = nl_msg_start_nested(actions, OVS_CT_ATTR_NAT);
2166
2167
0
    if (p->snat) {
2168
0
        nl_msg_put_flag(actions, OVS_NAT_ATTR_SRC);
2169
0
    } else if (p->dnat) {
2170
0
        nl_msg_put_flag(actions, OVS_NAT_ATTR_DST);
2171
0
    } else {
2172
0
        goto out;
2173
0
    }
2174
0
    if (p->addr_len != 0) {
2175
0
        nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MIN, &p->addr_min,
2176
0
                          p->addr_len);
2177
0
        if (memcmp(&p->addr_max, &p->addr_min, p->addr_len) > 0) {
2178
0
            nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MAX, &p->addr_max,
2179
0
                              p->addr_len);
2180
0
        }
2181
0
        if (p->proto_min) {
2182
0
            nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MIN, p->proto_min);
2183
0
            if (p->proto_max && p->proto_max > p->proto_min) {
2184
0
                nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MAX, p->proto_max);
2185
0
            }
2186
0
        }
2187
0
        if (p->persistent) {
2188
0
            nl_msg_put_flag(actions, OVS_NAT_ATTR_PERSISTENT);
2189
0
        }
2190
0
        if (p->proto_hash) {
2191
0
            nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_HASH);
2192
0
        }
2193
0
        if (p->proto_random) {
2194
0
            nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_RANDOM);
2195
0
        }
2196
0
    }
2197
0
out:
2198
0
    nl_msg_end_nested(actions, start);
2199
0
}
2200
2201
static int
2202
parse_conntrack_action(const char *s_, struct ofpbuf *actions)
2203
0
{
2204
0
    const char *s = s_;
2205
2206
0
    if (ovs_scan(s, "ct")) {
2207
0
        const char *helper = NULL, *timeout = NULL;
2208
0
        size_t helper_len = 0, timeout_len = 0;
2209
0
        bool commit = false;
2210
0
        bool force_commit = false;
2211
0
        uint16_t zone = 0;
2212
0
        struct {
2213
0
            uint32_t value;
2214
0
            uint32_t mask;
2215
0
        } ct_mark = { 0, 0 };
2216
0
        struct {
2217
0
            ovs_u128 value;
2218
0
            ovs_u128 mask;
2219
0
        } ct_label;
2220
0
        struct ct_nat_params nat_params = {
2221
0
            .snat = false,
2222
0
            .dnat = false,
2223
0
        };
2224
0
        bool have_nat = false;
2225
0
        const char *end;
2226
0
        size_t start;
2227
2228
0
        memset(&ct_label, 0, sizeof(ct_label));
2229
2230
0
        s += 2;
2231
0
        if (ovs_scan(s, "(")) {
2232
0
            s++;
2233
0
find_end:
2234
0
            end = strchr(s, ')');
2235
0
            if (!end) {
2236
0
                return -EINVAL;
2237
0
            }
2238
2239
0
            while (s != end) {
2240
0
                int n;
2241
2242
0
                s += strspn(s, delimiters);
2243
0
                if (ovs_scan(s, "commit%n", &n)) {
2244
0
                    commit = true;
2245
0
                    s += n;
2246
0
                    continue;
2247
0
                }
2248
0
                if (ovs_scan(s, "force_commit%n", &n)) {
2249
0
                    force_commit = true;
2250
0
                    s += n;
2251
0
                    continue;
2252
0
                }
2253
0
                if (ovs_scan(s, "zone=%"SCNu16"%n", &zone, &n)) {
2254
0
                    s += n;
2255
0
                    continue;
2256
0
                }
2257
0
                if (ovs_scan(s, "mark=%"SCNx32"%n", &ct_mark.value, &n)) {
2258
0
                    s += n;
2259
0
                    n = -1;
2260
0
                    if (ovs_scan(s, "/%"SCNx32"%n", &ct_mark.mask, &n)) {
2261
0
                        s += n;
2262
0
                    } else {
2263
0
                        ct_mark.mask = UINT32_MAX;
2264
0
                    }
2265
0
                    continue;
2266
0
                }
2267
0
                if (ovs_scan(s, "label=%n", &n)) {
2268
0
                    int retval;
2269
2270
0
                    s += n;
2271
0
                    retval = scan_u128(s, &ct_label.value, &ct_label.mask);
2272
0
                    if (retval == 0) {
2273
0
                        return -EINVAL;
2274
0
                    }
2275
0
                    s += retval;
2276
0
                    continue;
2277
0
                }
2278
0
                if (ovs_scan(s, "helper=%n", &n)) {
2279
0
                    s += n;
2280
0
                    helper_len = strcspn(s, delimiters_end);
2281
0
                    if (!helper_len || helper_len > 15) {
2282
0
                        return -EINVAL;
2283
0
                    }
2284
0
                    helper = s;
2285
0
                    s += helper_len;
2286
0
                    continue;
2287
0
                }
2288
0
                if (ovs_scan(s, "timeout=%n", &n)) {
2289
0
                    s += n;
2290
0
                    timeout_len = strcspn(s, delimiters_end);
2291
0
                    if (!timeout_len || timeout_len > 31) {
2292
0
                        return -EINVAL;
2293
0
                    }
2294
0
                    timeout = s;
2295
0
                    s += timeout_len;
2296
0
                    continue;
2297
0
                }
2298
2299
0
                n = scan_ct_nat(s, &nat_params);
2300
0
                if (n > 0) {
2301
0
                    s += n;
2302
0
                    have_nat = true;
2303
2304
                    /* end points to the end of the nested, nat action.
2305
                     * find the real end. */
2306
0
                    goto find_end;
2307
0
                }
2308
                /* Nothing matched. */
2309
0
                return -EINVAL;
2310
0
            }
2311
0
            s++;
2312
0
        }
2313
0
        if (commit && force_commit) {
2314
0
            return -EINVAL;
2315
0
        }
2316
2317
0
        start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CT);
2318
0
        if (commit) {
2319
0
            nl_msg_put_flag(actions, OVS_CT_ATTR_COMMIT);
2320
0
        } else if (force_commit) {
2321
0
            nl_msg_put_flag(actions, OVS_CT_ATTR_FORCE_COMMIT);
2322
0
        }
2323
0
        if (zone) {
2324
0
            nl_msg_put_u16(actions, OVS_CT_ATTR_ZONE, zone);
2325
0
        }
2326
0
        if (ct_mark.mask) {
2327
0
            nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark,
2328
0
                              sizeof(ct_mark));
2329
0
        }
2330
0
        if (!ovs_u128_is_zero(ct_label.mask)) {
2331
0
            nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label,
2332
0
                              sizeof ct_label);
2333
0
        }
2334
0
        if (helper) {
2335
0
            nl_msg_put_string__(actions, OVS_CT_ATTR_HELPER, helper,
2336
0
                                helper_len);
2337
0
        }
2338
0
        if (timeout) {
2339
0
            nl_msg_put_string__(actions, OVS_CT_ATTR_TIMEOUT, timeout,
2340
0
                                timeout_len);
2341
0
        }
2342
0
        if (have_nat) {
2343
0
            nl_msg_put_ct_nat(&nat_params, actions);
2344
0
        }
2345
0
        nl_msg_end_nested(actions, start);
2346
0
    }
2347
2348
0
    return s - s_;
2349
0
}
2350
2351
static void
2352
nsh_key_to_attr(struct ofpbuf *buf, const struct ovs_key_nsh *nsh,
2353
                uint8_t * metadata, size_t md_size,
2354
                bool is_mask)
2355
0
{
2356
0
    size_t nsh_key_ofs;
2357
0
    struct ovs_nsh_key_base base;
2358
2359
0
    base.flags = nsh->flags;
2360
0
    base.ttl = nsh->ttl;
2361
0
    base.mdtype = nsh->mdtype;
2362
0
    base.np = nsh->np;
2363
0
    base.path_hdr = nsh->path_hdr;
2364
2365
0
    nsh_key_ofs = nl_msg_start_nested(buf, OVS_KEY_ATTR_NSH);
2366
0
    nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_BASE, &base, sizeof base);
2367
2368
0
    if (is_mask) {
2369
0
        nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context,
2370
0
                          sizeof nsh->context);
2371
0
    } else {
2372
0
        switch (nsh->mdtype) {
2373
0
        case NSH_M_TYPE1:
2374
0
            nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context,
2375
0
                              sizeof nsh->context);
2376
0
            break;
2377
0
        case NSH_M_TYPE2:
2378
0
            if (metadata && md_size > 0) {
2379
0
                nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD2, metadata,
2380
0
                                  md_size);
2381
0
            }
2382
0
            break;
2383
0
        default:
2384
            /* No match support for other MD formats yet. */
2385
0
            break;
2386
0
        }
2387
0
    }
2388
0
    nl_msg_end_nested(buf, nsh_key_ofs);
2389
0
}
2390
2391
2392
static int
2393
parse_odp_push_nsh_action(const char *s, struct ofpbuf *actions)
2394
0
{
2395
0
    int n = 0;
2396
0
    int ret = 0;
2397
0
    uint32_t spi = 0;
2398
0
    uint8_t si = 255;
2399
0
    uint32_t cd;
2400
0
    struct ovs_key_nsh nsh;
2401
0
    uint8_t metadata[NSH_CTX_HDRS_MAX_LEN];
2402
0
    uint8_t md_size = 0;
2403
2404
0
    if (!ovs_scan_len(s, &n, "push_nsh(")) {
2405
0
        ret = -EINVAL;
2406
0
        goto out;
2407
0
    }
2408
2409
    /* The default is NSH_M_TYPE1 */
2410
0
    nsh.flags = 0;
2411
0
    nsh.ttl = 63;
2412
0
    nsh.mdtype = NSH_M_TYPE1;
2413
0
    nsh.np = NSH_P_ETHERNET;
2414
0
    nsh.path_hdr = nsh_spi_si_to_path_hdr(0, 255);
2415
0
    memset(nsh.context, 0, NSH_M_TYPE1_MDLEN);
2416
2417
0
    for (;;) {
2418
0
        n += strspn(s + n, delimiters);
2419
0
        if (s[n] == ')') {
2420
0
            break;
2421
0
        }
2422
2423
0
        if (ovs_scan_len(s, &n, "flags=%"SCNi8, &nsh.flags)) {
2424
0
            continue;
2425
0
        }
2426
0
        if (ovs_scan_len(s, &n, "ttl=%"SCNi8, &nsh.ttl)) {
2427
0
            continue;
2428
0
        }
2429
0
        if (ovs_scan_len(s, &n, "mdtype=%"SCNi8, &nsh.mdtype)) {
2430
0
            switch (nsh.mdtype) {
2431
0
            case NSH_M_TYPE1:
2432
0
                /* This is the default format. */;
2433
0
                break;
2434
0
            case NSH_M_TYPE2:
2435
                /* Length will be updated later. */
2436
0
                md_size = 0;
2437
0
                break;
2438
0
            default:
2439
0
                ret = -EINVAL;
2440
0
                goto out;
2441
0
            }
2442
0
            continue;
2443
0
        }
2444
0
        if (ovs_scan_len(s, &n, "np=%"SCNi8, &nsh.np)) {
2445
0
            continue;
2446
0
        }
2447
0
        if (ovs_scan_len(s, &n, "spi=0x%"SCNx32, &spi)) {
2448
0
            continue;
2449
0
        }
2450
0
        if (ovs_scan_len(s, &n, "si=%"SCNi8, &si)) {
2451
0
            continue;
2452
0
        }
2453
0
        if (nsh.mdtype == NSH_M_TYPE1) {
2454
0
            if (ovs_scan_len(s, &n, "c1=0x%"SCNx32, &cd)) {
2455
0
                nsh.context[0] = htonl(cd);
2456
0
                continue;
2457
0
            }
2458
0
            if (ovs_scan_len(s, &n, "c2=0x%"SCNx32, &cd)) {
2459
0
                nsh.context[1] = htonl(cd);
2460
0
                continue;
2461
0
            }
2462
0
            if (ovs_scan_len(s, &n, "c3=0x%"SCNx32, &cd)) {
2463
0
                nsh.context[2] = htonl(cd);
2464
0
                continue;
2465
0
            }
2466
0
            if (ovs_scan_len(s, &n, "c4=0x%"SCNx32, &cd)) {
2467
0
                nsh.context[3] = htonl(cd);
2468
0
                continue;
2469
0
            }
2470
0
        }
2471
0
        else if (nsh.mdtype == NSH_M_TYPE2) {
2472
0
            struct ofpbuf b;
2473
0
            char buf[512];
2474
0
            size_t mdlen, padding;
2475
0
            if (ovs_scan_len(s, &n, "md2=0x%511[0-9a-fA-F]", buf)
2476
0
                && n/2 <= sizeof metadata) {
2477
0
                ofpbuf_use_stub(&b, metadata, sizeof metadata);
2478
0
                ofpbuf_put_hex(&b, buf, &mdlen);
2479
                /* Pad metadata to 4 bytes. */
2480
0
                padding = PAD_SIZE(mdlen, 4);
2481
0
                if (padding > 0) {
2482
0
                    ofpbuf_put_zeros(&b, padding);
2483
0
                }
2484
0
                md_size = mdlen + padding;
2485
0
                ofpbuf_uninit(&b);
2486
0
                continue;
2487
0
            }
2488
0
        }
2489
2490
0
        ret = -EINVAL;
2491
0
        goto out;
2492
0
    }
2493
0
out:
2494
0
    if (ret >= 0) {
2495
0
        nsh.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
2496
0
        size_t offset = nl_msg_start_nested(actions, OVS_ACTION_ATTR_PUSH_NSH);
2497
0
        nsh_key_to_attr(actions, &nsh, metadata, md_size, false);
2498
0
        nl_msg_end_nested(actions, offset);
2499
        /* Consume the closing ')'. */
2500
0
        ret = n + 1;
2501
0
    }
2502
0
    return ret;
2503
0
}
2504
2505
static int
2506
parse_odp_psample_action(const char *s, struct ofpbuf *actions)
2507
0
{
2508
0
    char buf[2 * OVS_PSAMPLE_COOKIE_MAX_SIZE + 1];
2509
0
    uint8_t cookie[OVS_PSAMPLE_COOKIE_MAX_SIZE];
2510
0
    bool has_group = false;
2511
0
    size_t cookie_len = 0;
2512
0
    uint32_t group;
2513
0
    int n = 0;
2514
2515
0
    if (!ovs_scan_len(s, &n, "psample(")) {
2516
0
        return -EINVAL;
2517
0
    }
2518
2519
0
    while (s[n] != ')') {
2520
0
        n += strspn(s + n, delimiters);
2521
2522
0
        if (!has_group && ovs_scan_len(s, &n, "group=%"SCNi32, &group)) {
2523
0
            has_group = true;
2524
0
            continue;
2525
0
        }
2526
2527
0
        if (!cookie_len &&
2528
0
            ovs_scan_len(s, &n, "cookie=0x%32[0-9a-fA-F]", buf) && n > 7) {
2529
0
            struct ofpbuf b;
2530
2531
0
            ofpbuf_use_stub(&b, cookie, OVS_PSAMPLE_COOKIE_MAX_SIZE);
2532
0
            ofpbuf_put_hex(&b, buf, &cookie_len);
2533
0
            ofpbuf_uninit(&b);
2534
0
            continue;
2535
0
        }
2536
0
        return -EINVAL;
2537
0
    }
2538
0
    n++;
2539
2540
0
    if (!has_group) {
2541
0
        return -EINVAL;
2542
0
    }
2543
2544
0
    odp_put_psample_action(actions, group, cookie_len ? cookie : NULL,
2545
0
                           cookie_len);
2546
0
    return n;
2547
0
}
2548
2549
static int
2550
parse_action_list(struct parse_odp_context *context, const char *s,
2551
                  struct ofpbuf *actions)
2552
0
{
2553
0
    int n = 0;
2554
2555
0
    for (;;) {
2556
0
        int retval;
2557
2558
0
        n += strspn(s + n, delimiters);
2559
0
        if (s[n] == ')') {
2560
0
            break;
2561
0
        }
2562
0
        retval = parse_odp_action(context, s + n, actions);
2563
0
        if (retval < 0) {
2564
0
            return retval;
2565
0
        } else if (nl_attr_oversized(actions->size - NLA_HDRLEN)) {
2566
0
            return -E2BIG;
2567
0
        }
2568
0
        n += retval;
2569
0
    }
2570
2571
0
    return n;
2572
0
}
2573
2574
2575
static int
2576
parse_odp_action(struct parse_odp_context *context, const char *s,
2577
                 struct ofpbuf *actions)
2578
0
{
2579
0
    int retval;
2580
2581
0
    context->depth++;
2582
2583
0
    if (context->depth == MAX_ODP_NESTED) {
2584
0
        retval = -EINVAL;
2585
0
    } else {
2586
0
        retval = parse_odp_action__(context, s, actions);
2587
0
    }
2588
2589
0
    context->depth--;
2590
2591
0
    return retval;
2592
0
}
2593
2594
2595
static int
2596
parse_odp_action__(struct parse_odp_context *context, const char *s,
2597
                   struct ofpbuf *actions)
2598
0
{
2599
0
    {
2600
0
        uint32_t port;
2601
0
        int n;
2602
2603
0
        if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
2604
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
2605
0
            return n;
2606
0
        }
2607
0
    }
2608
2609
0
    {
2610
0
        uint32_t bond_id;
2611
0
        int n;
2612
2613
0
        if (ovs_scan(s, "lb_output(%"PRIu32")%n", &bond_id, &n)) {
2614
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_LB_OUTPUT, bond_id);
2615
0
            return n;
2616
0
        }
2617
0
    }
2618
2619
0
    {
2620
0
        uint32_t max_len;
2621
0
        int n;
2622
2623
0
        if (ovs_scan(s, "trunc(%"SCNi32")%n", &max_len, &n)) {
2624
0
            struct ovs_action_trunc *trunc;
2625
2626
0
            trunc = nl_msg_put_unspec_uninit(actions,
2627
0
                     OVS_ACTION_ATTR_TRUNC, sizeof *trunc);
2628
0
            trunc->max_len = max_len;
2629
0
            return n;
2630
0
        }
2631
0
    }
2632
2633
0
    if (context->port_names) {
2634
0
        int len = strcspn(s, delimiters);
2635
0
        struct simap_node *node;
2636
2637
0
        node = simap_find_len(context->port_names, s, len);
2638
0
        if (node) {
2639
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
2640
0
            return len;
2641
0
        }
2642
0
    }
2643
2644
0
    {
2645
0
        uint32_t recirc_id;
2646
0
        int n = -1;
2647
2648
0
        if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
2649
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
2650
0
            return n;
2651
0
        }
2652
0
    }
2653
2654
0
    if (!strncmp(s, "userspace(", 10)) {
2655
0
        return parse_odp_userspace_action(s, actions);
2656
0
    }
2657
2658
0
    if (!strncmp(s, "set(", 4)) {
2659
0
        size_t start_ofs;
2660
0
        int retval;
2661
0
        struct nlattr mask[1024 / sizeof(struct nlattr)];
2662
0
        struct ofpbuf maskbuf = OFPBUF_STUB_INITIALIZER(mask);
2663
0
        struct nlattr *nested, *key;
2664
0
        size_t size;
2665
2666
0
        start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
2667
0
        retval = parse_odp_key_mask_attr(context, s + 4, actions, &maskbuf);
2668
0
        if (retval < 0) {
2669
0
            ofpbuf_uninit(&maskbuf);
2670
0
            return retval;
2671
0
        }
2672
0
        if (s[retval + 4] != ')') {
2673
0
            ofpbuf_uninit(&maskbuf);
2674
0
            return -EINVAL;
2675
0
        }
2676
2677
0
        nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2678
0
        key = nested + 1;
2679
2680
0
        size = nl_attr_get_size(mask);
2681
0
        if (size == nl_attr_get_size(key)) {
2682
            /* Change to masked set action if not fully masked. */
2683
0
            if (!is_all_ones(mask + 1, size)) {
2684
                /* Remove padding of eariler key payload  */
2685
0
                actions->size -= NLA_ALIGN(key->nla_len) - key->nla_len;
2686
2687
                /* Put mask payload right after key payload */
2688
0
                key->nla_len += size;
2689
0
                ofpbuf_put(actions, mask + 1, size);
2690
2691
                /* 'actions' may have been reallocated by ofpbuf_put(). */
2692
0
                nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2693
0
                nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
2694
2695
0
                key = nested + 1;
2696
                /* Add new padding as needed */
2697
0
                ofpbuf_put_zeros(actions, NLA_ALIGN(key->nla_len) -
2698
0
                                          key->nla_len);
2699
0
            }
2700
0
        }
2701
0
        ofpbuf_uninit(&maskbuf);
2702
2703
0
        nl_msg_end_nested(actions, start_ofs);
2704
0
        return retval + 5;
2705
0
    }
2706
2707
0
    {
2708
0
        struct ovs_action_push_vlan push;
2709
0
        int tpid = ETH_TYPE_VLAN;
2710
0
        int vid, pcp;
2711
0
        int cfi = 1;
2712
0
        int n = -1;
2713
2714
0
        if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
2715
0
            || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2716
0
                        &vid, &pcp, &cfi, &n)
2717
0
            || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2718
0
                        &tpid, &vid, &pcp, &n)
2719
0
            || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2720
0
                        &tpid, &vid, &pcp, &cfi, &n)) {
2721
0
            if ((vid & ~(VLAN_VID_MASK >> VLAN_VID_SHIFT)) != 0
2722
0
                || (pcp & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) != 0) {
2723
0
                return -EINVAL;
2724
0
            }
2725
0
            push.vlan_tpid = htons(tpid);
2726
0
            push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
2727
0
                                  | (pcp << VLAN_PCP_SHIFT)
2728
0
                                  | (cfi ? VLAN_CFI : 0));
2729
0
            nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
2730
0
                              &push, sizeof push);
2731
2732
0
            return n;
2733
0
        }
2734
0
    }
2735
2736
0
    if (!strncmp(s, "pop_vlan", 8)) {
2737
0
        nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
2738
0
        return 8;
2739
0
    }
2740
2741
0
    {
2742
0
        unsigned long long int meter_id;
2743
0
        int n = -1;
2744
2745
0
        if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) {
2746
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id);
2747
0
            return n;
2748
0
        }
2749
0
    }
2750
2751
0
    {
2752
0
        double percentage;
2753
0
        int n = -1;
2754
2755
0
        if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
2756
0
            && percentage >= 0. && percentage <= 100.0) {
2757
0
            size_t sample_ofs, actions_ofs;
2758
0
            double probability;
2759
2760
0
            probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
2761
0
            sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
2762
0
            nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
2763
0
                           (probability <= 0 ? 0
2764
0
                            : probability >= UINT32_MAX ? UINT32_MAX
2765
0
                            : probability));
2766
2767
0
            actions_ofs = nl_msg_start_nested(actions,
2768
0
                                              OVS_SAMPLE_ATTR_ACTIONS);
2769
0
            int retval = parse_action_list(context, s + n, actions);
2770
0
            if (retval < 0) {
2771
0
                return retval;
2772
0
            }
2773
2774
2775
0
            n += retval;
2776
0
            nl_msg_end_nested(actions, actions_ofs);
2777
0
            nl_msg_end_nested(actions, sample_ofs);
2778
2779
0
            return s[n + 1] == ')' ? n + 2 : -EINVAL;
2780
0
        }
2781
0
    }
2782
2783
0
    {
2784
0
        if (!strncmp(s, "clone(", 6)) {
2785
0
            size_t actions_ofs;
2786
0
            int n = 6;
2787
2788
0
            actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE);
2789
0
            int retval = parse_action_list(context, s + n, actions);
2790
0
            if (retval < 0) {
2791
0
                return retval;
2792
0
            }
2793
0
            n += retval;
2794
0
            nl_msg_end_nested(actions, actions_ofs);
2795
0
            return n + 1;
2796
0
        }
2797
0
    }
2798
2799
0
    if (!strncmp(s, "push_nsh(", 9)) {
2800
0
        return parse_odp_push_nsh_action(s, actions);
2801
0
    }
2802
2803
0
    {
2804
0
        int n;
2805
0
        if (ovs_scan(s, "pop_nsh()%n", &n)) {
2806
0
            nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_NSH);
2807
0
            return n;
2808
0
        }
2809
0
    }
2810
2811
0
    {
2812
0
        uint32_t port;
2813
0
        int n;
2814
2815
0
        if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
2816
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
2817
0
            return n;
2818
0
        }
2819
0
    }
2820
2821
0
    {
2822
0
        if (!strncmp(s, "ct_clear", 8)) {
2823
0
            nl_msg_put_flag(actions, OVS_ACTION_ATTR_CT_CLEAR);
2824
0
            return 8;
2825
0
        }
2826
0
    }
2827
2828
0
    {
2829
0
        uint16_t pkt_len;
2830
0
        int n = -1;
2831
0
        if (ovs_scan(s, "check_pkt_len(size=%"SCNi16",gt(%n", &pkt_len, &n)) {
2832
0
            size_t cpl_ofs, actions_ofs;
2833
0
            cpl_ofs = nl_msg_start_nested(actions,
2834
0
                                          OVS_ACTION_ATTR_CHECK_PKT_LEN);
2835
0
            nl_msg_put_u16(actions, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, pkt_len);
2836
0
            actions_ofs = nl_msg_start_nested(
2837
0
                actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
2838
2839
0
            int retval;
2840
0
            if (!strncasecmp(s + n, "drop", 4)) {
2841
0
                n += 4;
2842
0
            } else {
2843
0
                retval = parse_action_list(context, s + n, actions);
2844
0
                if (retval < 0) {
2845
0
                    return retval;
2846
0
                }
2847
2848
0
                n += retval;
2849
0
            }
2850
0
            nl_msg_end_nested(actions, actions_ofs);
2851
0
            retval = -1;
2852
0
            if (!ovs_scan(s + n, "),le(%n", &retval)) {
2853
0
                return -EINVAL;
2854
0
            }
2855
0
            n += retval;
2856
2857
0
            actions_ofs = nl_msg_start_nested(
2858
0
                actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
2859
0
            if (!strncasecmp(s + n, "drop", 4)) {
2860
0
                n += 4;
2861
0
            } else {
2862
0
                retval = parse_action_list(context, s + n, actions);
2863
0
                if (retval < 0) {
2864
0
                    return retval;
2865
0
                }
2866
0
                n += retval;
2867
0
            }
2868
0
            nl_msg_end_nested(actions, actions_ofs);
2869
0
            nl_msg_end_nested(actions, cpl_ofs);
2870
0
            return s[n + 1] == ')' ? n + 2 : -EINVAL;
2871
0
        }
2872
0
    }
2873
2874
0
    {
2875
0
        int retval;
2876
2877
0
        retval = parse_conntrack_action(s, actions);
2878
0
        if (retval) {
2879
0
            return retval;
2880
0
        }
2881
0
    }
2882
0
    {
2883
0
        struct ovs_action_add_mpls mpls;
2884
0
        uint8_t ttl, tc, bos;
2885
0
        uint16_t eth_type;
2886
0
        uint32_t lse;
2887
0
        int n = -1;
2888
2889
0
        if (ovs_scan(s, "add_mpls(label=%"SCNi32",tc=%"SCNd8",ttl=%"SCNd8","
2890
0
                                 "bos=%"SCNd8",eth_type=0x%"SCNx16")%n",
2891
0
                     &lse, &tc, &ttl, &bos, &eth_type, &n)) {
2892
0
            mpls.mpls_ethertype = htons(eth_type);
2893
0
            mpls.mpls_lse = htonl((lse << MPLS_LABEL_SHIFT) |
2894
0
                                  (tc  << MPLS_TC_SHIFT)    |
2895
0
                                  (ttl << MPLS_TTL_SHIFT)   |
2896
0
                                  (bos << MPLS_BOS_SHIFT));
2897
0
            mpls.tun_flags = 0;
2898
0
            nl_msg_put_unspec(actions, OVS_ACTION_ATTR_ADD_MPLS,
2899
0
                              &mpls, sizeof mpls);
2900
0
            return n;
2901
0
        }
2902
0
    }
2903
2904
0
    if (!strncmp(s, "psample(", 8)) {
2905
0
        return parse_odp_psample_action(s, actions);
2906
0
    }
2907
2908
0
    {
2909
0
        struct ovs_action_push_tnl data;
2910
0
        int n;
2911
2912
0
        n = ovs_parse_tnl_push(s, &data);
2913
0
        if (n > 0) {
2914
0
            odp_put_tnl_push_action(actions, &data);
2915
0
            return n;
2916
0
        } else if (n < 0) {
2917
0
            return n;
2918
0
        }
2919
0
    }
2920
2921
0
    return -EINVAL;
2922
0
}
2923
2924
/* Parses the string representation of datapath actions, in the format output
2925
 * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
2926
 * value.  On success, the ODP actions are appended to 'actions' as a series of
2927
 * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
2928
 * way, 'actions''s data might be reallocated. */
2929
int
2930
odp_actions_from_string(const char *s, const struct simap *port_names,
2931
                        struct ofpbuf *actions)
2932
0
{
2933
0
    size_t old_size;
2934
2935
0
    if (!strcasecmp(s, "drop")) {
2936
0
        nl_msg_put_u32(actions, OVS_ACTION_ATTR_DROP, XLATE_OK);
2937
0
        return 0;
2938
0
    }
2939
2940
0
    struct parse_odp_context context = (struct parse_odp_context) {
2941
0
        .port_names = port_names,
2942
0
    };
2943
2944
0
    old_size = actions->size;
2945
0
    for (;;) {
2946
0
        int retval;
2947
2948
0
        s += strspn(s, delimiters);
2949
0
        if (!*s) {
2950
0
            return 0;
2951
0
        }
2952
2953
0
        retval = parse_odp_action(&context, s, actions);
2954
2955
0
        if (retval >= 0 && nl_attr_oversized(actions->size - NLA_HDRLEN)) {
2956
0
            retval = -E2BIG;
2957
0
        }
2958
2959
0
        if (retval < 0 || !strchr(delimiters, s[retval])) {
2960
0
            actions->size = old_size;
2961
0
            return -retval;
2962
0
        }
2963
0
        s += retval;
2964
0
    }
2965
2966
0
    return 0;
2967
0
}
2968

2969
static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
2970
    [OVS_VXLAN_EXT_GBP]                 = { .len = 4 },
2971
};
2972
2973
static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
2974
    [OVS_TUNNEL_KEY_ATTR_ID]            = { .len = 8 },
2975
    [OVS_TUNNEL_KEY_ATTR_IPV4_SRC]      = { .len = 4 },
2976
    [OVS_TUNNEL_KEY_ATTR_IPV4_DST]      = { .len = 4 },
2977
    [OVS_TUNNEL_KEY_ATTR_TOS]           = { .len = 1 },
2978
    [OVS_TUNNEL_KEY_ATTR_TTL]           = { .len = 1 },
2979
    [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
2980
    [OVS_TUNNEL_KEY_ATTR_CSUM]          = { .len = 0 },
2981
    [OVS_TUNNEL_KEY_ATTR_TP_SRC]        = { .len = 2 },
2982
    [OVS_TUNNEL_KEY_ATTR_TP_DST]        = { .len = 2 },
2983
    [OVS_TUNNEL_KEY_ATTR_OAM]           = { .len = 0 },
2984
    [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS]   = { .len = ATTR_LEN_VARIABLE },
2985
    [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS]    = { .len = ATTR_LEN_NESTED,
2986
                                            .next = ovs_vxlan_ext_attr_lens ,
2987
                                            .next_max = OVS_VXLAN_EXT_MAX},
2988
    [OVS_TUNNEL_KEY_ATTR_IPV6_SRC]      = { .len = 16 },
2989
    [OVS_TUNNEL_KEY_ATTR_IPV6_DST]      = { .len = 16 },
2990
    [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS]   = { .len = ATTR_LEN_VARIABLE },
2991
    [OVS_TUNNEL_KEY_ATTR_GTPU_OPTS]   = { .len = ATTR_LEN_VARIABLE },
2992
};
2993
2994
const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
2995
    [OVS_KEY_ATTR_ENCAP]     = { .len = ATTR_LEN_NESTED },
2996
    [OVS_KEY_ATTR_PRIORITY]  = { .len = 4 },
2997
    [OVS_KEY_ATTR_SKB_MARK]  = { .len = 4 },
2998
    [OVS_KEY_ATTR_DP_HASH]   = { .len = 4 },
2999
    [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
3000
    [OVS_KEY_ATTR_TUNNEL]    = { .len = ATTR_LEN_NESTED,
3001
                                 .next = ovs_tun_key_attr_lens,
3002
                                 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
3003
    [OVS_KEY_ATTR_IN_PORT]   = { .len = 4  },
3004
    [OVS_KEY_ATTR_ETHERNET]  = { .len = sizeof(struct ovs_key_ethernet) },
3005
    [OVS_KEY_ATTR_VLAN]      = { .len = 2 },
3006
    [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
3007
    [OVS_KEY_ATTR_MPLS]      = { .len = ATTR_LEN_VARIABLE },
3008
    [OVS_KEY_ATTR_IPV4]      = { .len = sizeof(struct ovs_key_ipv4) },
3009
    [OVS_KEY_ATTR_IPV6]      = { .len = sizeof(struct ovs_key_ipv6) },
3010
    [OVS_KEY_ATTR_TCP]       = { .len = sizeof(struct ovs_key_tcp) },
3011
    [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
3012
    [OVS_KEY_ATTR_UDP]       = { .len = sizeof(struct ovs_key_udp) },
3013
    [OVS_KEY_ATTR_SCTP]      = { .len = sizeof(struct ovs_key_sctp) },
3014
    [OVS_KEY_ATTR_ICMP]      = { .len = sizeof(struct ovs_key_icmp) },
3015
    [OVS_KEY_ATTR_ICMPV6]    = { .len = sizeof(struct ovs_key_icmpv6) },
3016
    [OVS_KEY_ATTR_ARP]       = { .len = sizeof(struct ovs_key_arp) },
3017
    [OVS_KEY_ATTR_ND]        = { .len = sizeof(struct ovs_key_nd) },
3018
    [OVS_KEY_ATTR_ND_EXTENSIONS] = { .len = sizeof(struct ovs_key_nd_extensions) },
3019
    [OVS_KEY_ATTR_CT_STATE]  = { .len = 4 },
3020
    [OVS_KEY_ATTR_CT_ZONE]   = { .len = 2 },
3021
    [OVS_KEY_ATTR_CT_MARK]   = { .len = 4 },
3022
    [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
3023
    [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = { .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
3024
    [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = { .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
3025
    [OVS_KEY_ATTR_PACKET_TYPE] = { .len = 4  },
3026
    [OVS_KEY_ATTR_NSH]       = { .len = ATTR_LEN_NESTED,
3027
                                 .next = ovs_nsh_key_attr_lens,
3028
                                 .next_max = OVS_NSH_KEY_ATTR_MAX },
3029
};
3030
3031
/* Returns the correct length of the payload for a flow key attribute of the
3032
 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
3033
 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
3034
 * payload is a nested type. */
3035
static int
3036
odp_key_attr_len(const struct attr_len_tbl tbl[], int max_type, uint16_t type)
3037
0
{
3038
0
    if (type > max_type) {
3039
0
        return ATTR_LEN_INVALID;
3040
0
    }
3041
3042
0
    return tbl[type].len;
3043
0
}
3044
3045
static void
3046
format_generic_odp_key(const struct nlattr *a, struct ds *ds)
3047
0
{
3048
0
    size_t len = nl_attr_get_size(a);
3049
0
    if (len) {
3050
0
        const uint8_t *unspec;
3051
0
        unsigned int i;
3052
3053
0
        unspec = nl_attr_get(a);
3054
0
        for (i = 0; i < len; i++) {
3055
0
            if (i) {
3056
0
                ds_put_char(ds, ' ');
3057
0
            }
3058
0
            ds_put_format(ds, "%02x", unspec[i]);
3059
0
        }
3060
0
    }
3061
0
}
3062
3063
static const char *
3064
ovs_frag_type_to_string(enum ovs_frag_type type)
3065
0
{
3066
0
    switch (type) {
3067
0
    case OVS_FRAG_TYPE_NONE:
3068
0
        return "no";
3069
0
    case OVS_FRAG_TYPE_FIRST:
3070
0
        return "first";
3071
0
    case OVS_FRAG_TYPE_LATER:
3072
0
        return "later";
3073
0
    case __OVS_FRAG_TYPE_MAX:
3074
0
    default:
3075
0
        return "<error>";
3076
0
    }
3077
0
}
3078
3079
enum odp_key_fitness
3080
odp_nsh_hdr_from_attr(const struct nlattr *attr,
3081
                      struct nsh_hdr *nsh_hdr, size_t size)
3082
0
{
3083
0
    unsigned int left;
3084
0
    const struct nlattr *a;
3085
0
    bool unknown = false;
3086
0
    uint8_t flags = 0;
3087
0
    uint8_t ttl = 63;
3088
0
    size_t mdlen = 0;
3089
0
    bool has_md1 = false;
3090
0
    bool has_md2 = false;
3091
3092
0
    memset(nsh_hdr, 0, size);
3093
3094
0
    NL_NESTED_FOR_EACH (a, left, attr) {
3095
0
        uint16_t type = nl_attr_type(a);
3096
0
        size_t len = nl_attr_get_size(a);
3097
0
        int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
3098
0
                                            OVS_NSH_KEY_ATTR_MAX, type);
3099
3100
0
        if (len != expected_len && expected_len >= 0) {
3101
0
            return ODP_FIT_ERROR;
3102
0
        }
3103
3104
0
        switch (type) {
3105
0
        case OVS_NSH_KEY_ATTR_BASE: {
3106
0
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
3107
0
            nsh_hdr->next_proto = base->np;
3108
0
            nsh_hdr->md_type = base->mdtype;
3109
0
            put_16aligned_be32(&nsh_hdr->path_hdr, base->path_hdr);
3110
0
            flags = base->flags;
3111
0
            ttl = base->ttl;
3112
0
            break;
3113
0
        }
3114
0
        case OVS_NSH_KEY_ATTR_MD1: {
3115
0
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
3116
0
            struct nsh_md1_ctx *md1_dst = &nsh_hdr->md1;
3117
0
            has_md1 = true;
3118
0
            mdlen = nl_attr_get_size(a);
3119
0
            if ((mdlen + NSH_BASE_HDR_LEN != NSH_M_TYPE1_LEN) ||
3120
0
                (mdlen + NSH_BASE_HDR_LEN > size)) {
3121
0
                return ODP_FIT_ERROR;
3122
0
            }
3123
0
            memcpy(md1_dst, md1, mdlen);
3124
0
            break;
3125
0
        }
3126
0
        case OVS_NSH_KEY_ATTR_MD2: {
3127
0
            struct nsh_md2_tlv *md2_dst = &nsh_hdr->md2;
3128
0
            const uint8_t *md2 = nl_attr_get(a);
3129
0
            has_md2 = true;
3130
0
            mdlen = nl_attr_get_size(a);
3131
0
            if (mdlen + NSH_BASE_HDR_LEN > size) {
3132
0
                return ODP_FIT_ERROR;
3133
0
            }
3134
0
            memcpy(md2_dst, md2, mdlen);
3135
0
            break;
3136
0
        }
3137
0
        default:
3138
            /* Allow this to show up as unexpected, if there are unknown
3139
             * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3140
0
            unknown = true;
3141
0
            break;
3142
0
        }
3143
0
    }
3144
3145
0
    if (unknown) {
3146
0
        return ODP_FIT_TOO_MUCH;
3147
0
    }
3148
3149
0
    if ((has_md1 && nsh_hdr->md_type != NSH_M_TYPE1)
3150
0
        || (has_md2 && nsh_hdr->md_type != NSH_M_TYPE2)) {
3151
0
        return ODP_FIT_ERROR;
3152
0
    }
3153
3154
    /* nsh header length  = NSH_BASE_HDR_LEN + mdlen */
3155
0
    nsh_set_flags_ttl_len(nsh_hdr, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
3156
3157
0
    return ODP_FIT_PERFECT;
3158
0
}
3159
3160
/* Reports the error 'msg', which is formatted as with printf().
3161
 *
3162
 * If 'errorp' is nonnull, then some the wants the error report to come
3163
 * directly back to it, so the function stores the error message into '*errorp'
3164
 * (after first freeing it in case there's something there already).
3165
 *
3166
 * Otherwise, logs the message at WARN level, rate-limited. */
3167
static void OVS_PRINTF_FORMAT(3, 4)
3168
odp_parse_error(struct vlog_rate_limit *rl, char **errorp,
3169
                const char *msg, ...)
3170
0
{
3171
0
    if (OVS_UNLIKELY(errorp)) {
3172
0
        free(*errorp);
3173
3174
0
        va_list args;
3175
0
        va_start(args, msg);
3176
0
        *errorp = xvasprintf(msg, args);
3177
0
        va_end(args);
3178
0
    } else if (!VLOG_DROP_WARN(rl)) {
3179
0
        va_list args;
3180
0
        va_start(args, msg);
3181
0
        char *error = xvasprintf(msg, args);
3182
0
        va_end(args);
3183
3184
0
        VLOG_WARN("%s", error);
3185
3186
0
        free(error);
3187
0
    }
3188
0
}
3189
3190
/* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
3191
 * returns fitness.  If the attribute is a key, 'is_mask' should be false;
3192
 * if it is a mask, 'is_mask' should be true.  If 'errorp' is nonnull and the
3193
 * function returns ODP_FIT_ERROR, stores a malloc()'d error message in
3194
 * '*errorp'. */
3195
static enum odp_key_fitness
3196
odp_nsh_key_from_attr__(const struct nlattr *attr, bool is_mask,
3197
                        struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask,
3198
                        char **errorp)
3199
0
{
3200
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3201
0
    if (errorp) {
3202
0
        *errorp = NULL;
3203
0
    }
3204
3205
0
    unsigned int left;
3206
0
    const struct nlattr *a;
3207
0
    bool unknown = false;
3208
0
    bool has_md1 = false;
3209
3210
0
    NL_NESTED_FOR_EACH (a, left, attr) {
3211
0
        uint16_t type = nl_attr_type(a);
3212
0
        size_t len = nl_attr_get_size(a);
3213
0
        int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
3214
0
                                            OVS_NSH_KEY_ATTR_MAX, type);
3215
0
        if (expected_len) {
3216
0
            if (nsh_mask) {
3217
0
                expected_len *= 2;
3218
0
            }
3219
0
            if (len != expected_len) {
3220
0
                odp_parse_error(&rl, errorp, "NSH %s attribute %"PRIu16" "
3221
0
                                "should have length %d but actually has "
3222
0
                                "%"PRIuSIZE,
3223
0
                                nsh_mask ? "mask" : "key",
3224
0
                                type, expected_len, len);
3225
0
                return ODP_FIT_ERROR;
3226
0
            }
3227
0
        }
3228
3229
0
        switch (type) {
3230
0
        case OVS_NSH_KEY_ATTR_UNSPEC:
3231
0
            break;
3232
0
        case OVS_NSH_KEY_ATTR_BASE: {
3233
0
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
3234
0
            nsh->flags = base->flags;
3235
0
            nsh->ttl = base->ttl;
3236
0
            nsh->mdtype = base->mdtype;
3237
0
            nsh->np = base->np;
3238
0
            nsh->path_hdr = base->path_hdr;
3239
0
            if (nsh_mask && (len == 2 * sizeof(*base))) {
3240
0
                const struct ovs_nsh_key_base *base_mask = base + 1;
3241
0
                nsh_mask->flags = base_mask->flags;
3242
0
                nsh_mask->ttl = base_mask->ttl;
3243
0
                nsh_mask->mdtype = base_mask->mdtype;
3244
0
                nsh_mask->np = base_mask->np;
3245
0
                nsh_mask->path_hdr = base_mask->path_hdr;
3246
0
            }
3247
0
            break;
3248
0
        }
3249
0
        case OVS_NSH_KEY_ATTR_MD1: {
3250
0
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
3251
0
            has_md1 = true;
3252
0
            memcpy(nsh->context, md1->context, sizeof md1->context);
3253
0
            if (nsh_mask && (len == 2 * sizeof *md1)) {
3254
0
                const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
3255
0
                memcpy(nsh_mask->context, md1_mask->context,
3256
0
                       sizeof(*md1_mask));
3257
0
            }
3258
0
            break;
3259
0
        }
3260
0
        case OVS_NSH_KEY_ATTR_MD2:
3261
0
        default:
3262
            /* Allow this to show up as unexpected, if there are unknown
3263
             * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3264
0
            unknown = true;
3265
0
            break;
3266
0
        }
3267
0
    }
3268
3269
0
    if (unknown) {
3270
0
        return ODP_FIT_TOO_MUCH;
3271
0
    }
3272
3273
0
    if (!is_mask && has_md1 && nsh->mdtype != NSH_M_TYPE1 && !nsh_mask) {
3274
0
        odp_parse_error(&rl, errorp, "OVS_NSH_KEY_ATTR_MD1 present but "
3275
0
                        "declared mdtype %"PRIu8" is not %d (NSH_M_TYPE1)",
3276
0
                        nsh->mdtype, NSH_M_TYPE1);
3277
0
        return ODP_FIT_ERROR;
3278
0
    }
3279
3280
0
    return ODP_FIT_PERFECT;
3281
0
}
3282
3283
/* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
3284
 * returns fitness.  The attribute should be a key (not a mask).  If 'errorp'
3285
 * is nonnull and the function returns ODP_FIT_ERROR, stores a malloc()'d error
3286
 * message in '*errorp'. */
3287
enum odp_key_fitness
3288
odp_nsh_key_from_attr(const struct nlattr *attr, struct ovs_key_nsh *nsh,
3289
                      struct ovs_key_nsh *nsh_mask, char **errorp)
3290
0
{
3291
0
    return odp_nsh_key_from_attr__(attr, false, nsh, nsh_mask, errorp);
3292
0
}
3293
3294
/* Parses OVS_KEY_ATTR_TUNNEL attribute 'attr' into 'tun' and returns fitness.
3295
 * If the attribute is a key, 'is_mask' should be false; if it is a mask,
3296
 * 'is_mask' should be true.  If 'errorp' is nonnull and the function returns
3297
 * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */
3298
static enum odp_key_fitness
3299
odp_tun_key_from_attr__(const struct nlattr *attr, bool is_mask,
3300
                        struct flow_tnl *tun, char **errorp)
3301
0
{
3302
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3303
0
    unsigned int left;
3304
0
    const struct nlattr *a;
3305
0
    bool ttl = false;
3306
0
    bool unknown = false;
3307
3308
0
    NL_NESTED_FOR_EACH(a, left, attr) {
3309
0
        uint16_t type = nl_attr_type(a);
3310
0
        size_t len = nl_attr_get_size(a);
3311
0
        int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
3312
0
                                            OVS_TUNNEL_KEY_ATTR_MAX, type);
3313
3314
0
        if (len != expected_len && expected_len >= 0) {
3315
0
            odp_parse_error(&rl, errorp, "tunnel key attribute %"PRIu16" "
3316
0
                            "should have length %d but actually has %"PRIuSIZE,
3317
0
                            type, expected_len, len);
3318
0
            return ODP_FIT_ERROR;
3319
0
        }
3320
3321
0
        switch (type) {
3322
0
        case OVS_TUNNEL_KEY_ATTR_ID:
3323
0
            tun->tun_id = nl_attr_get_be64(a);
3324
0
            tun->flags |= FLOW_TNL_F_KEY;
3325
0
            break;
3326
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
3327
0
            tun->ip_src = nl_attr_get_be32(a);
3328
0
            break;
3329
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
3330
0
            tun->ip_dst = nl_attr_get_be32(a);
3331
0
            break;
3332
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
3333
0
            tun->ipv6_src = nl_attr_get_in6_addr(a);
3334
0
            break;
3335
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
3336
0
            tun->ipv6_dst = nl_attr_get_in6_addr(a);
3337
0
            break;
3338
0
        case OVS_TUNNEL_KEY_ATTR_TOS:
3339
0
            tun->ip_tos = nl_attr_get_u8(a);
3340
0
            break;
3341
0
        case OVS_TUNNEL_KEY_ATTR_TTL:
3342
0
            tun->ip_ttl = nl_attr_get_u8(a);
3343
0
            ttl = true;
3344
0
            break;
3345
0
        case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3346
0
            tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
3347
0
            break;
3348
0
        case OVS_TUNNEL_KEY_ATTR_CSUM:
3349
0
            tun->flags |= FLOW_TNL_F_CSUM;
3350
0
            break;
3351
0
        case OVS_TUNNEL_KEY_ATTR_TP_SRC:
3352
0
            tun->tp_src = nl_attr_get_be16(a);
3353
0
            break;
3354
0
        case OVS_TUNNEL_KEY_ATTR_TP_DST:
3355
0
            tun->tp_dst = nl_attr_get_be16(a);
3356
0
            break;
3357
0
        case OVS_TUNNEL_KEY_ATTR_OAM:
3358
0
            tun->flags |= FLOW_TNL_F_OAM;
3359
0
            break;
3360
0
        case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
3361
0
            if (odp_vxlan_tun_opts_from_attr(a, &tun->gbp_id,
3362
0
                                             &tun->gbp_flags,
3363
0
                                             NULL)) {
3364
0
                odp_parse_error(&rl, errorp, "error parsing VXLAN options");
3365
0
                return ODP_FIT_ERROR;
3366
0
            }
3367
0
            break;
3368
0
        }
3369
0
        case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
3370
0
            tun_metadata_from_geneve_nlattr(a, is_mask, tun);
3371
0
            break;
3372
0
        case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: {
3373
0
            const struct erspan_metadata *opts = nl_attr_get(a);
3374
3375
0
            tun->erspan_ver = opts->version;
3376
0
            if (tun->erspan_ver == 1) {
3377
0
                tun->erspan_idx = ntohl(opts->u.index);
3378
0
            } else if (tun->erspan_ver == 2) {
3379
0
                tun->erspan_dir = opts->u.md2.dir;
3380
0
                tun->erspan_hwid = get_hwid(&opts->u.md2);
3381
0
            } else {
3382
0
                VLOG_WARN("%s invalid erspan version\n", __func__);
3383
0
            }
3384
0
            break;
3385
0
        }
3386
0
        case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS: {
3387
0
            const struct gtpu_metadata *opts = nl_attr_get(a);
3388
3389
0
            tun->gtpu_flags = opts->flags;
3390
0
            tun->gtpu_msgtype = opts->msgtype;
3391
0
            break;
3392
0
        }
3393
3394
0
        default:
3395
            /* Allow this to show up as unexpected, if there are unknown
3396
             * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3397
0
            unknown = true;
3398
0
            break;
3399
0
        }
3400
0
    }
3401
3402
0
    if (!ttl) {
3403
0
        odp_parse_error(&rl, errorp, "tunnel options missing TTL");
3404
0
        return ODP_FIT_ERROR;
3405
0
    }
3406
0
    if (unknown) {
3407
0
        return ODP_FIT_TOO_MUCH;
3408
0
    }
3409
0
    return ODP_FIT_PERFECT;
3410
0
}
3411
3412
/* Parses OVS_KEY_ATTR_TUNNEL key attribute 'attr' into 'tun' and returns
3413
 * fitness.  The attribute should be a key (not a mask).  If 'errorp' is
3414
 * nonnull, stores NULL into '*errorp' on success, otherwise a malloc()'d error
3415
 * message. */
3416
enum odp_key_fitness
3417
odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun,
3418
                      char **errorp)
3419
0
{
3420
0
    if (errorp) {
3421
0
        *errorp = NULL;
3422
0
    }
3423
0
    memset(tun, 0, sizeof *tun);
3424
0
    return odp_tun_key_from_attr__(attr, false, tun, errorp);
3425
0
}
3426
3427
static void
3428
tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
3429
                const struct flow_tnl *tun_flow_key,
3430
                const struct ofpbuf *key_buf, const char *tnl_type)
3431
0
{
3432
0
    size_t tun_key_ofs;
3433
3434
0
    tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
3435
3436
    /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
3437
0
    if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
3438
0
        nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
3439
0
    }
3440
0
    if (tun_key->ip_src) {
3441
0
        nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
3442
0
    }
3443
0
    if (tun_key->ip_dst) {
3444
0
        nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
3445
0
    }
3446
0
    if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
3447
0
        nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
3448
0
    }
3449
0
    if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
3450
0
        nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
3451
0
    }
3452
0
    if (tun_key->ip_tos) {
3453
0
        nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
3454
0
    }
3455
0
    nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
3456
0
    if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
3457
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3458
0
    }
3459
0
    if (tun_key->flags & FLOW_TNL_F_CSUM) {
3460
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3461
0
    }
3462
0
    if (tun_key->tp_src) {
3463
0
        nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
3464
0
    }
3465
0
    if (tun_key->tp_dst) {
3466
0
        nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
3467
0
    }
3468
0
    if (tun_key->flags & FLOW_TNL_F_OAM) {
3469
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3470
0
    }
3471
3472
    /* If tnl_type is set to a particular type of output tunnel,
3473
     * only put its relevant tunnel metadata to the nlattr.
3474
     * If tnl_type is NULL, put tunnel metadata according to the
3475
     * 'tun_key'.
3476
     */
3477
0
    if ((!tnl_type || !strcmp(tnl_type, "vxlan")) &&
3478
0
        (tun_key->gbp_flags || tun_key->gbp_id)) {
3479
0
        size_t vxlan_opts_ofs;
3480
0
        uint32_t gbp_raw;
3481
3482
0
        vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3483
0
        gbp_raw = odp_encode_gbp_raw(tun_key->gbp_flags, tun_key->gbp_id);
3484
0
        nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, gbp_raw);
3485
0
        nl_msg_end_nested(a, vxlan_opts_ofs);
3486
0
    }
3487
3488
0
    if (!tnl_type || !strcmp(tnl_type, "geneve")) {
3489
0
        tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
3490
0
    }
3491
3492
0
    if ((!tnl_type || !strcmp(tnl_type, "erspan") ||
3493
0
        !strcmp(tnl_type, "ip6erspan")) &&
3494
0
        (tun_key->erspan_ver == 1 || tun_key->erspan_ver == 2)) {
3495
0
        struct erspan_metadata *opts;
3496
3497
0
        opts = nl_msg_put_unspec_zero(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
3498
0
                                      sizeof *opts);
3499
0
        opts->version = tun_key->erspan_ver;
3500
0
        if (opts->version == 1) {
3501
0
            opts->u.index = htonl(tun_key->erspan_idx);
3502
0
        } else {
3503
0
            opts->u.md2.dir = tun_key->erspan_dir;
3504
0
            set_hwid(&opts->u.md2, tun_key->erspan_hwid);
3505
0
        }
3506
0
    }
3507
3508
0
    if ((!tnl_type || !strcmp(tnl_type, "gtpu")) &&
3509
0
        (tun_key->gtpu_flags && tun_key->gtpu_msgtype)) {
3510
0
        struct gtpu_metadata opts;
3511
3512
0
        opts.flags = tun_key->gtpu_flags;
3513
0
        opts.msgtype = tun_key->gtpu_msgtype;
3514
0
        nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS,
3515
0
                          &opts, sizeof(opts));
3516
0
    }
3517
0
    nl_msg_end_nested(a, tun_key_ofs);
3518
0
}
3519
3520
static bool
3521
odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size,
3522
                       int constant)
3523
0
{
3524
    /* Convert 'constant' to all the widths we need.  C conversion rules ensure
3525
     * that -1 becomes all-1-bits and 0 does not change. */
3526
0
    ovs_be16 be16 = (OVS_FORCE ovs_be16) constant;
3527
0
    uint32_t u32 = constant;
3528
0
    uint8_t u8 = constant;
3529
0
    const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any;
3530
3531
0
    switch (attr) {
3532
0
    case OVS_KEY_ATTR_UNSPEC:
3533
0
    case OVS_KEY_ATTR_ENCAP:
3534
0
    case OVS_KEY_ATTR_TUNNEL_INFO:
3535
0
    case __OVS_KEY_ATTR_MAX:
3536
0
    default:
3537
0
        return false;
3538
3539
0
    case OVS_KEY_ATTR_PRIORITY:
3540
0
    case OVS_KEY_ATTR_IN_PORT:
3541
0
    case OVS_KEY_ATTR_ETHERNET:
3542
0
    case OVS_KEY_ATTR_VLAN:
3543
0
    case OVS_KEY_ATTR_ETHERTYPE:
3544
0
    case OVS_KEY_ATTR_IPV4:
3545
0
    case OVS_KEY_ATTR_TCP:
3546
0
    case OVS_KEY_ATTR_UDP:
3547
0
    case OVS_KEY_ATTR_ICMP:
3548
0
    case OVS_KEY_ATTR_ICMPV6:
3549
0
    case OVS_KEY_ATTR_ND:
3550
0
    case OVS_KEY_ATTR_ND_EXTENSIONS:
3551
0
    case OVS_KEY_ATTR_SKB_MARK:
3552
0
    case OVS_KEY_ATTR_TUNNEL:
3553
0
    case OVS_KEY_ATTR_SCTP:
3554
0
    case OVS_KEY_ATTR_DP_HASH:
3555
0
    case OVS_KEY_ATTR_RECIRC_ID:
3556
0
    case OVS_KEY_ATTR_MPLS:
3557
0
    case OVS_KEY_ATTR_CT_STATE:
3558
0
    case OVS_KEY_ATTR_CT_ZONE:
3559
0
    case OVS_KEY_ATTR_CT_MARK:
3560
0
    case OVS_KEY_ATTR_CT_LABELS:
3561
0
    case OVS_KEY_ATTR_PACKET_TYPE:
3562
0
    case OVS_KEY_ATTR_NSH:
3563
0
        return is_all_byte(mask, size, u8);
3564
3565
0
    case OVS_KEY_ATTR_TCP_FLAGS:
3566
0
        return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16);
3567
3568
0
    case OVS_KEY_ATTR_IPV6: {
3569
0
        const struct ovs_key_ipv6 *ipv6_mask = mask;
3570
0
        return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
3571
0
                == htonl(IPV6_LABEL_MASK & u32)
3572
0
                && ipv6_mask->ipv6_proto == u8
3573
0
                && ipv6_mask->ipv6_tclass == u8
3574
0
                && ipv6_mask->ipv6_hlimit == u8
3575
0
                && ipv6_mask->ipv6_frag == u8
3576
0
                && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6)
3577
0
                && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6));
3578
0
    }
3579
3580
0
    case OVS_KEY_ATTR_ARP:
3581
0
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8);
3582
3583
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
3584
0
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4,
3585
0
                                             ipv4_proto), u8);
3586
3587
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
3588
0
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6,
3589
0
                                             ipv6_proto), u8);
3590
0
    }
3591
0
}
3592
3593
/* The caller must already have verified that 'ma' has a correct length.
3594
 *
3595
 * The main purpose of this function is formatting, to allow code to figure out
3596
 * whether the mask can be omitted.  It doesn't try hard for attributes that
3597
 * contain sub-attributes, etc., because normally those would be broken down
3598
 * further for formatting. */
3599
static bool
3600
odp_mask_attr_is_wildcard(const struct nlattr *ma)
3601
0
{
3602
0
    return odp_mask_is_constant__(nl_attr_type(ma),
3603
0
                                  nl_attr_get(ma), nl_attr_get_size(ma), 0);
3604
0
}
3605
3606
/* The caller must already have verified that 'size' is a correct length for
3607
 * 'attr'.
3608
 *
3609
 * The main purpose of this function is formatting, to allow code to figure out
3610
 * whether the mask can be omitted.  It doesn't try hard for attributes that
3611
 * contain sub-attributes, etc., because normally those would be broken down
3612
 * further for formatting. */
3613
static bool
3614
odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
3615
0
{
3616
0
    return odp_mask_is_constant__(attr, mask, size, -1);
3617
0
}
3618
3619
/* The caller must already have verified that 'ma' has a correct length. */
3620
static bool
3621
odp_mask_attr_is_exact(const struct nlattr *ma)
3622
0
{
3623
0
    enum ovs_key_attr attr = nl_attr_type(ma);
3624
0
    return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma));
3625
0
}
3626
3627
void
3628
odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
3629
                     char *port_name)
3630
0
{
3631
0
    struct odp_portno_names *odp_portno_names;
3632
3633
0
    odp_portno_names = xmalloc(sizeof *odp_portno_names);
3634
0
    odp_portno_names->port_no = port_no;
3635
0
    odp_portno_names->name = xstrdup(port_name);
3636
0
    hmap_insert(portno_names, &odp_portno_names->hmap_node,
3637
0
                hash_odp_port(port_no));
3638
0
}
3639
3640
static char *
3641
odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
3642
0
{
3643
0
    if (portno_names) {
3644
0
        struct odp_portno_names *odp_portno_names;
3645
3646
0
        HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
3647
0
                                 hash_odp_port(port_no), portno_names) {
3648
0
            if (odp_portno_names->port_no == port_no) {
3649
0
                return odp_portno_names->name;
3650
0
            }
3651
0
        }
3652
0
    }
3653
0
    return NULL;
3654
0
}
3655
3656
void
3657
odp_portno_names_destroy(struct hmap *portno_names)
3658
0
{
3659
0
    struct odp_portno_names *odp_portno_names;
3660
3661
0
    HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) {
3662
0
        free(odp_portno_names->name);
3663
0
        free(odp_portno_names);
3664
0
    }
3665
0
}
3666
3667
void
3668
odp_portno_name_format(const struct hmap *portno_names, odp_port_t port_no,
3669
                       struct ds *s)
3670
0
{
3671
0
    const char *name = odp_portno_names_get(portno_names, port_no);
3672
0
    if (name) {
3673
0
        ds_put_cstr(s, name);
3674
0
    } else {
3675
0
        ds_put_format(s, "%"PRIu32, port_no);
3676
0
    }
3677
0
}
3678
3679
/* Format helpers. */
3680
3681
static void
3682
format_eth(struct ds *ds, const char *name, const struct eth_addr key,
3683
           const struct eth_addr *mask, bool verbose)
3684
0
{
3685
0
    bool mask_empty = mask && eth_addr_is_zero(*mask);
3686
3687
0
    if (verbose || !mask_empty) {
3688
0
        bool mask_full = !mask || eth_mask_is_exact(*mask);
3689
3690
0
        if (mask_full) {
3691
0
            ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
3692
0
        } else {
3693
0
            ds_put_format(ds, "%s=", name);
3694
0
            eth_format_masked(key, mask, ds);
3695
0
            ds_put_char(ds, ',');
3696
0
        }
3697
0
    }
3698
0
}
3699
3700
3701
static void
3702
format_be64(struct ds *ds, const char *name, ovs_be64 key,
3703
            const ovs_32aligned_be64 *mask_, bool verbose)
3704
0
{
3705
0
    ovs_be64 mask = mask_ ? get_32aligned_be64(mask_) : htonll(0);
3706
3707
0
    if (verbose || mask) {
3708
0
        bool mask_full = !mask_ || mask == OVS_BE64_MAX;
3709
3710
0
        ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
3711
0
        if (!mask_full) { /* Partially masked. */
3712
0
            ds_put_format(ds, "/%#"PRIx64, ntohll(mask));
3713
0
        }
3714
0
        ds_put_char(ds, ',');
3715
0
    }
3716
0
}
3717
3718
static void
3719
format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
3720
            const ovs_be32 *mask, bool verbose)
3721
0
{
3722
0
    bool mask_empty = mask && !*mask;
3723
3724
0
    if (verbose || !mask_empty) {
3725
0
        bool mask_full = !mask || *mask == OVS_BE32_MAX;
3726
3727
0
        ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
3728
0
        if (!mask_full) { /* Partially masked. */
3729
0
            ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
3730
0
        }
3731
0
        ds_put_char(ds, ',');
3732
0
    }
3733
0
}
3734
3735
static void
3736
format_in6_addr(struct ds *ds, const char *name,
3737
                const struct in6_addr *key,
3738
                const struct in6_addr *mask,
3739
                bool verbose)
3740
0
{
3741
0
    char buf[INET6_ADDRSTRLEN];
3742
0
    bool mask_empty = mask && ipv6_mask_is_any(mask);
3743
3744
0
    if (verbose || !mask_empty) {
3745
0
        bool mask_full = !mask || ipv6_mask_is_exact(mask);
3746
3747
0
        inet_ntop(AF_INET6, key, buf, sizeof buf);
3748
0
        ds_put_format(ds, "%s=%s", name, buf);
3749
0
        if (!mask_full) { /* Partially masked. */
3750
0
            inet_ntop(AF_INET6, mask, buf, sizeof buf);
3751
0
            ds_put_format(ds, "/%s", buf);
3752
0
        }
3753
0
        ds_put_char(ds, ',');
3754
0
    }
3755
0
}
3756
3757
static void
3758
format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
3759
                  const ovs_be32 *mask, bool verbose)
3760
0
{
3761
0
    bool mask_empty = mask && !*mask;
3762
3763
0
    if (verbose || !mask_empty) {
3764
0
        bool mask_full = !mask
3765
0
            || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
3766
3767
0
        ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
3768
0
        if (!mask_full) { /* Partially masked. */
3769
0
            ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
3770
0
        }
3771
0
        ds_put_char(ds, ',');
3772
0
    }
3773
0
}
3774
3775
static void
3776
format_u8x(struct ds *ds, const char *name, uint8_t key,
3777
           const uint8_t *mask, bool verbose)
3778
0
{
3779
0
    bool mask_empty = mask && !*mask;
3780
3781
0
    if (verbose || !mask_empty) {
3782
0
        bool mask_full = !mask || *mask == UINT8_MAX;
3783
3784
0
        ds_put_format(ds, "%s=%#"PRIx8, name, key);
3785
0
        if (!mask_full) { /* Partially masked. */
3786
0
            ds_put_format(ds, "/%#"PRIx8, *mask);
3787
0
        }
3788
0
        ds_put_char(ds, ',');
3789
0
    }
3790
0
}
3791
3792
static void
3793
format_u8u(struct ds *ds, const char *name, uint8_t key,
3794
           const uint8_t *mask, bool verbose)
3795
0
{
3796
0
    bool mask_empty = mask && !*mask;
3797
3798
0
    if (verbose || !mask_empty) {
3799
0
        bool mask_full = !mask || *mask == UINT8_MAX;
3800
3801
0
        ds_put_format(ds, "%s=%"PRIu8, name, key);
3802
0
        if (!mask_full) { /* Partially masked. */
3803
0
            ds_put_format(ds, "/%#"PRIx8, *mask);
3804
0
        }
3805
0
        ds_put_char(ds, ',');
3806
0
    }
3807
0
}
3808
3809
static void
3810
format_be16(struct ds *ds, const char *name, ovs_be16 key,
3811
            const ovs_be16 *mask, bool verbose)
3812
0
{
3813
0
    bool mask_empty = mask && !*mask;
3814
3815
0
    if (verbose || !mask_empty) {
3816
0
        bool mask_full = !mask || *mask == OVS_BE16_MAX;
3817
3818
0
        ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
3819
0
        if (!mask_full) { /* Partially masked. */
3820
0
            ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3821
0
        }
3822
0
        ds_put_char(ds, ',');
3823
0
    }
3824
0
}
3825
3826
static void
3827
format_be16x(struct ds *ds, const char *name, ovs_be16 key,
3828
             const ovs_be16 *mask, bool verbose)
3829
0
{
3830
0
    bool mask_empty = mask && !*mask;
3831
3832
0
    if (verbose || !mask_empty) {
3833
0
        bool mask_full = !mask || *mask == OVS_BE16_MAX;
3834
3835
0
        ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
3836
0
        if (!mask_full) { /* Partially masked. */
3837
0
            ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3838
0
        }
3839
0
        ds_put_char(ds, ',');
3840
0
    }
3841
0
}
3842
3843
static void
3844
format_tun_flags(struct ds *ds, const char *name, uint16_t key,
3845
                 const uint16_t *mask, bool verbose)
3846
0
{
3847
0
    bool mask_empty = mask && !*mask;
3848
3849
0
    if (verbose || !mask_empty) {
3850
0
        ds_put_cstr(ds, name);
3851
0
        ds_put_char(ds, '(');
3852
0
        if (mask) {
3853
0
            format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
3854
0
                                *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
3855
0
        } else { /* Fully masked. */
3856
0
            format_flags(ds, flow_tun_flag_to_string, key, '|');
3857
0
        }
3858
0
        ds_put_cstr(ds, "),");
3859
0
    }
3860
0
}
3861
3862
static bool
3863
check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
3864
               const struct attr_len_tbl tbl[], int max_type, bool need_key)
3865
0
{
3866
0
    uint16_t type = nl_attr_type(a);
3867
0
    int expected_len;
3868
3869
0
    if (type > max_type) {
3870
        /* Unknown attribute, can't check the length. */
3871
0
        return true;
3872
0
    }
3873
3874
0
    expected_len = odp_key_attr_len(tbl, max_type, type);
3875
3876
0
    if (expected_len != ATTR_LEN_VARIABLE &&
3877
0
        expected_len != ATTR_LEN_NESTED) {
3878
3879
0
        bool bad_key_len = nl_attr_get_size(a) != expected_len;
3880
0
        bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
3881
3882
0
        if (bad_key_len || bad_mask_len) {
3883
0
            if (need_key) {
3884
0
                ds_put_format(ds, "key%u", type);
3885
0
            }
3886
0
            if (bad_key_len) {
3887
0
                ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
3888
0
                              nl_attr_get_size(a), expected_len);
3889
0
            }
3890
0
            format_generic_odp_key(a, ds);
3891
0
            if (ma) {
3892
0
                ds_put_char(ds, '/');
3893
0
                if (bad_mask_len) {
3894
0
                    ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
3895
0
                                  nl_attr_get_size(ma), expected_len);
3896
0
                }
3897
0
                format_generic_odp_key(ma, ds);
3898
0
            }
3899
0
            ds_put_char(ds, ')');
3900
0
            return false;
3901
0
        }
3902
0
    }
3903
3904
0
    return true;
3905
0
}
3906
3907
static void
3908
format_unknown_key(struct ds *ds, const struct nlattr *a,
3909
                   const struct nlattr *ma)
3910
0
{
3911
0
    ds_put_format(ds, "key%u(", nl_attr_type(a));
3912
0
    format_generic_odp_key(a, ds);
3913
0
    if (ma && !odp_mask_attr_is_exact(ma)) {
3914
0
        ds_put_char(ds, '/');
3915
0
        format_generic_odp_key(ma, ds);
3916
0
    }
3917
0
    ds_put_cstr(ds, "),");
3918
0
}
3919
3920
static void
3921
format_odp_tun_vxlan_opt(const struct nlattr *attr,
3922
                         const struct nlattr *mask_attr, struct ds *ds,
3923
                         bool verbose)
3924
0
{
3925
0
    unsigned int left;
3926
0
    const struct nlattr *a;
3927
0
    struct ofpbuf ofp;
3928
3929
0
    ofpbuf_init(&ofp, 100);
3930
0
    NL_NESTED_FOR_EACH(a, left, attr) {
3931
0
        uint16_t type = nl_attr_type(a);
3932
0
        const struct nlattr *ma = NULL;
3933
3934
0
        if (mask_attr) {
3935
0
            ma = nl_attr_find__(nl_attr_get(mask_attr),
3936
0
                                nl_attr_get_size(mask_attr), type);
3937
0
            if (!ma) {
3938
0
                ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
3939
0
                                                OVS_VXLAN_EXT_MAX,
3940
0
                                                &ofp, a);
3941
0
            }
3942
0
        }
3943
3944
0
        if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
3945
0
                            OVS_VXLAN_EXT_MAX, true)) {
3946
0
            continue;
3947
0
        }
3948
3949
0
        switch (type) {
3950
0
        case OVS_VXLAN_EXT_GBP: {
3951
0
            uint32_t key = nl_attr_get_u32(a);
3952
0
            ovs_be16 id, id_mask;
3953
0
            uint8_t flags, flags_mask = 0;
3954
3955
0
            odp_decode_gbp_raw(key, &id, &flags);
3956
0
            if (ma) {
3957
0
                uint32_t mask = nl_attr_get_u32(ma);
3958
0
                odp_decode_gbp_raw(mask, &id_mask, &flags_mask);
3959
0
            }
3960
3961
0
            ds_put_cstr(ds, "gbp(");
3962
0
            format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
3963
0
            format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
3964
0
            ds_chomp(ds, ',');
3965
0
            ds_put_cstr(ds, "),");
3966
0
            break;
3967
0
        }
3968
3969
0
        default:
3970
0
            format_unknown_key(ds, a, ma);
3971
0
        }
3972
0
        ofpbuf_clear(&ofp);
3973
0
    }
3974
3975
0
    ds_chomp(ds, ',');
3976
0
    ofpbuf_uninit(&ofp);
3977
0
}
3978
3979
static void
3980
format_odp_tun_erspan_opt(const struct nlattr *attr,
3981
                          const struct nlattr *mask_attr, struct ds *ds,
3982
                          bool verbose)
3983
0
{
3984
0
    const struct erspan_metadata *opts, *mask;
3985
0
    uint8_t ver, ver_ma, dir, dir_ma, hwid, hwid_ma;
3986
3987
0
    opts = nl_attr_get(attr);
3988
0
    mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
3989
3990
0
    ver = (uint8_t)opts->version;
3991
0
    if (mask) {
3992
0
        ver_ma = (uint8_t)mask->version;
3993
0
    }
3994
3995
0
    format_u8u(ds, "ver", ver, mask ? &ver_ma : NULL, verbose);
3996
3997
0
    if (opts->version == 1) {
3998
0
        if (mask) {
3999
0
            ds_put_format(ds, "idx=%#"PRIx32"/%#"PRIx32",",
4000
0
                          ntohl(opts->u.index),
4001
0
                          ntohl(mask->u.index));
4002
0
        } else {
4003
0
            ds_put_format(ds, "idx=%#"PRIx32",", ntohl(opts->u.index));
4004
0
        }
4005
0
    } else if (opts->version == 2) {
4006
0
        dir = opts->u.md2.dir;
4007
0
        hwid = opts->u.md2.hwid;
4008
0
        if (mask) {
4009
0
            dir_ma = mask->u.md2.dir;
4010
0
            hwid_ma = mask->u.md2.hwid;
4011
0
        }
4012
4013
0
        format_u8u(ds, "dir", dir, mask ? &dir_ma : NULL, verbose);
4014
0
        format_u8x(ds, "hwid", hwid, mask ? &hwid_ma : NULL, verbose);
4015
0
    }
4016
0
    ds_chomp(ds, ',');
4017
0
}
4018
4019
static void
4020
format_odp_tun_gtpu_opt(const struct nlattr *attr,
4021
                        const struct nlattr *mask_attr, struct ds *ds,
4022
                        bool verbose)
4023
0
{
4024
0
    const struct gtpu_metadata *opts, *mask;
4025
4026
0
    opts = nl_attr_get(attr);
4027
0
    mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
4028
4029
0
    format_u8x(ds, "flags", opts->flags, mask ? &mask->flags : NULL, verbose);
4030
0
    format_u8u(ds, "msgtype", opts->msgtype, mask ? &mask->msgtype : NULL,
4031
0
               verbose);
4032
0
    ds_chomp(ds, ',');
4033
0
}
4034
4035
0
#define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
4036
4037
static void
4038
format_geneve_opts(const struct geneve_opt *opt,
4039
                   const struct geneve_opt *mask, int opts_len,
4040
                   struct ds *ds, bool verbose)
4041
0
{
4042
0
    while (opts_len > 0) {
4043
0
        unsigned int len;
4044
0
        uint8_t data_len, data_len_mask;
4045
4046
0
        if (opts_len < sizeof *opt) {
4047
0
            ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
4048
0
                          opts_len, sizeof *opt);
4049
0
            return;
4050
0
        }
4051
4052
0
        data_len = opt->length * 4;
4053
0
        if (mask) {
4054
0
            if (mask->length == 0x1f) {
4055
0
                data_len_mask = UINT8_MAX;
4056
0
            } else {
4057
0
                data_len_mask = mask->length;
4058
0
            }
4059
0
        }
4060
0
        len = sizeof *opt + data_len;
4061
0
        if (len > opts_len) {
4062
0
            ds_put_format(ds, "opt len %u greater than remaining %u",
4063
0
                          len, opts_len);
4064
0
            return;
4065
0
        }
4066
4067
0
        ds_put_char(ds, '{');
4068
0
        format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
4069
0
                    verbose);
4070
0
        format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
4071
0
        format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
4072
0
        if (data_len &&
4073
0
            (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
4074
0
            ds_put_hex(ds, opt + 1, data_len);
4075
0
            if (mask && !is_all_ones(mask + 1, data_len)) {
4076
0
                ds_put_char(ds, '/');
4077
0
                ds_put_hex(ds, mask + 1, data_len);
4078
0
            }
4079
0
        } else {
4080
0
            ds_chomp(ds, ',');
4081
0
        }
4082
0
        ds_put_char(ds, '}');
4083
4084
0
        opt += len / sizeof(*opt);
4085
0
        if (mask) {
4086
0
            mask += len / sizeof(*opt);
4087
0
        }
4088
0
        opts_len -= len;
4089
0
    };
4090
0
}
4091
4092
static void
4093
format_odp_tun_geneve(const struct nlattr *attr,
4094
                      const struct nlattr *mask_attr, struct ds *ds,
4095
                      bool verbose)
4096
0
{
4097
0
    int opts_len = nl_attr_get_size(attr);
4098
0
    const struct geneve_opt *opt = nl_attr_get(attr);
4099
0
    const struct geneve_opt *mask = mask_attr ?
4100
0
                                    nl_attr_get(mask_attr) : NULL;
4101
4102
0
    if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
4103
0
        ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
4104
0
                      nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
4105
0
        return;
4106
0
    }
4107
4108
0
    format_geneve_opts(opt, mask, opts_len, ds, verbose);
4109
0
}
4110
4111
static void
4112
format_odp_nsh_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
4113
                    struct ds *ds)
4114
0
{
4115
0
    unsigned int left;
4116
0
    const struct nlattr *a;
4117
0
    struct ovs_key_nsh nsh;
4118
0
    struct ovs_key_nsh nsh_mask;
4119
4120
0
    memset(&nsh, 0, sizeof nsh);
4121
0
    memset(&nsh_mask, 0xff, sizeof nsh_mask);
4122
4123
0
    NL_NESTED_FOR_EACH (a, left, attr) {
4124
0
        enum ovs_nsh_key_attr type = nl_attr_type(a);
4125
0
        const struct nlattr *ma = NULL;
4126
4127
0
        if (mask_attr) {
4128
0
            ma = nl_attr_find__(nl_attr_get(mask_attr),
4129
0
                                nl_attr_get_size(mask_attr), type);
4130
0
        }
4131
4132
0
        if (!check_attr_len(ds, a, ma, ovs_nsh_key_attr_lens,
4133
0
                            OVS_NSH_KEY_ATTR_MAX, true)) {
4134
0
            continue;
4135
0
        }
4136
4137
0
        switch (type) {
4138
0
        case OVS_NSH_KEY_ATTR_UNSPEC:
4139
0
            break;
4140
0
        case OVS_NSH_KEY_ATTR_BASE: {
4141
0
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
4142
0
            const struct ovs_nsh_key_base *base_mask
4143
0
                = ma ? nl_attr_get(ma) : NULL;
4144
0
            nsh.flags = base->flags;
4145
0
            nsh.ttl = base->ttl;
4146
0
            nsh.mdtype = base->mdtype;
4147
0
            nsh.np = base->np;
4148
0
            nsh.path_hdr = base->path_hdr;
4149
0
            if (base_mask) {
4150
0
                nsh_mask.flags = base_mask->flags;
4151
0
                nsh_mask.ttl = base_mask->ttl;
4152
0
                nsh_mask.mdtype = base_mask->mdtype;
4153
0
                nsh_mask.np = base_mask->np;
4154
0
                nsh_mask.path_hdr = base_mask->path_hdr;
4155
0
            }
4156
0
            break;
4157
0
        }
4158
0
        case OVS_NSH_KEY_ATTR_MD1: {
4159
0
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
4160
0
            const struct ovs_nsh_key_md1 *md1_mask
4161
0
                = ma ? nl_attr_get(ma) : NULL;
4162
0
            memcpy(nsh.context, md1->context, sizeof md1->context);
4163
0
            if (md1_mask) {
4164
0
                memcpy(nsh_mask.context, md1_mask->context,
4165
0
                       sizeof md1_mask->context);
4166
0
            }
4167
0
            break;
4168
0
        }
4169
0
        case OVS_NSH_KEY_ATTR_MD2:
4170
0
        case __OVS_NSH_KEY_ATTR_MAX:
4171
0
        default:
4172
            /* No support for matching other metadata formats yet. */
4173
0
            break;
4174
0
        }
4175
0
    }
4176
4177
0
    if (mask_attr) {
4178
0
        format_nsh_key_mask(ds, &nsh, &nsh_mask);
4179
0
    } else {
4180
0
        format_nsh_key(ds, &nsh);
4181
0
    }
4182
0
}
4183
4184
static void
4185
format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
4186
                    struct ds *ds, bool verbose)
4187
0
{
4188
0
    unsigned int left;
4189
0
    const struct nlattr *a;
4190
0
    uint16_t flags = 0;
4191
0
    uint16_t mask_flags = 0;
4192
0
    struct ofpbuf ofp;
4193
4194
0
    ofpbuf_init(&ofp, 100);
4195
0
    NL_NESTED_FOR_EACH(a, left, attr) {
4196
0
        enum ovs_tunnel_key_attr type = nl_attr_type(a);
4197
0
        const struct nlattr *ma = NULL;
4198
4199
0
        if (mask_attr) {
4200
0
            ma = nl_attr_find__(nl_attr_get(mask_attr),
4201
0
                                nl_attr_get_size(mask_attr), type);
4202
0
            if (!ma) {
4203
0
                ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
4204
0
                                                OVS_TUNNEL_KEY_ATTR_MAX,
4205
0
                                                &ofp, a);
4206
0
            }
4207
0
        }
4208
4209
0
        if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
4210
0
                            OVS_TUNNEL_KEY_ATTR_MAX, true)) {
4211
0
            continue;
4212
0
        }
4213
4214
0
        switch (type) {
4215
0
        case OVS_TUNNEL_KEY_ATTR_ID:
4216
0
            format_be64(ds, "tun_id", nl_attr_get_be64(a),
4217
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4218
0
            flags |= FLOW_TNL_F_KEY;
4219
0
            if (ma) {
4220
0
                mask_flags |= FLOW_TNL_F_KEY;
4221
0
            }
4222
0
            break;
4223
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
4224
0
            format_ipv4(ds, "src", nl_attr_get_be32(a),
4225
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4226
0
            break;
4227
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
4228
0
            format_ipv4(ds, "dst", nl_attr_get_be32(a),
4229
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4230
0
            break;
4231
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
4232
0
            struct in6_addr ipv6_src;
4233
0
            ipv6_src = nl_attr_get_in6_addr(a);
4234
0
            format_in6_addr(ds, "ipv6_src", &ipv6_src,
4235
0
                            ma ? nl_attr_get(ma) : NULL, verbose);
4236
0
            break;
4237
0
        }
4238
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
4239
0
            struct in6_addr ipv6_dst;
4240
0
            ipv6_dst = nl_attr_get_in6_addr(a);
4241
0
            format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
4242
0
                            ma ? nl_attr_get(ma) : NULL, verbose);
4243
0
            break;
4244
0
        }
4245
0
        case OVS_TUNNEL_KEY_ATTR_TOS:
4246
0
            format_u8x(ds, "tos", nl_attr_get_u8(a),
4247
0
                       ma ? nl_attr_get(ma) : NULL, verbose);
4248
0
            break;
4249
0
        case OVS_TUNNEL_KEY_ATTR_TTL:
4250
0
            format_u8u(ds, "ttl", nl_attr_get_u8(a),
4251
0
                       ma ? nl_attr_get(ma) : NULL, verbose);
4252
0
            break;
4253
0
        case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
4254
0
            flags |= FLOW_TNL_F_DONT_FRAGMENT;
4255
0
            break;
4256
0
        case OVS_TUNNEL_KEY_ATTR_CSUM:
4257
0
            flags |= FLOW_TNL_F_CSUM;
4258
0
            break;
4259
0
        case OVS_TUNNEL_KEY_ATTR_TP_SRC:
4260
0
            format_be16(ds, "tp_src", nl_attr_get_be16(a),
4261
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4262
0
            break;
4263
0
        case OVS_TUNNEL_KEY_ATTR_TP_DST:
4264
0
            format_be16(ds, "tp_dst", nl_attr_get_be16(a),
4265
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4266
0
            break;
4267
0
        case OVS_TUNNEL_KEY_ATTR_OAM:
4268
0
            flags |= FLOW_TNL_F_OAM;
4269
0
            break;
4270
0
        case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
4271
0
            ds_put_cstr(ds, "vxlan(");
4272
0
            format_odp_tun_vxlan_opt(a, ma, ds, verbose);
4273
0
            ds_put_cstr(ds, "),");
4274
0
            break;
4275
0
        case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
4276
0
            ds_put_cstr(ds, "geneve(");
4277
0
            format_odp_tun_geneve(a, ma, ds, verbose);
4278
0
            ds_put_cstr(ds, "),");
4279
0
            break;
4280
0
        case OVS_TUNNEL_KEY_ATTR_PAD:
4281
0
            break;
4282
0
        case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
4283
0
            ds_put_cstr(ds, "erspan(");
4284
0
            format_odp_tun_erspan_opt(a, ma, ds, verbose);
4285
0
            ds_put_cstr(ds, "),");
4286
0
            break;
4287
0
        case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS:
4288
0
            ds_put_cstr(ds, "gtpu(");
4289
0
            format_odp_tun_gtpu_opt(a, ma, ds, verbose);
4290
0
            ds_put_cstr(ds, "),");
4291
0
            break;
4292
0
        case __OVS_TUNNEL_KEY_ATTR_MAX:
4293
0
        default:
4294
0
            format_unknown_key(ds, a, ma);
4295
0
        }
4296
0
        ofpbuf_clear(&ofp);
4297
0
    }
4298
4299
    /* Flags can have a valid mask even if the attribute is not set, so
4300
     * we need to collect these separately. */
4301
0
    if (mask_attr) {
4302
0
        NL_NESTED_FOR_EACH(a, left, mask_attr) {
4303
0
            switch (nl_attr_type(a)) {
4304
0
            case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
4305
0
                mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
4306
0
                break;
4307
0
            case OVS_TUNNEL_KEY_ATTR_CSUM:
4308
0
                mask_flags |= FLOW_TNL_F_CSUM;
4309
0
                break;
4310
0
            case OVS_TUNNEL_KEY_ATTR_OAM:
4311
0
                mask_flags |= FLOW_TNL_F_OAM;
4312
0
                break;
4313
0
            }
4314
0
        }
4315
0
    }
4316
4317
0
    format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
4318
0
                     verbose);
4319
0
    ds_chomp(ds, ',');
4320
0
    ofpbuf_uninit(&ofp);
4321
0
}
4322
4323
static const char *
4324
odp_ct_state_to_string(uint32_t flag)
4325
0
{
4326
0
    switch (flag) {
4327
0
    case OVS_CS_F_REPLY_DIR:
4328
0
        return "rpl";
4329
0
    case OVS_CS_F_TRACKED:
4330
0
        return "trk";
4331
0
    case OVS_CS_F_NEW:
4332
0
        return "new";
4333
0
    case OVS_CS_F_ESTABLISHED:
4334
0
        return "est";
4335
0
    case OVS_CS_F_RELATED:
4336
0
        return "rel";
4337
0
    case OVS_CS_F_INVALID:
4338
0
        return "inv";
4339
0
    case OVS_CS_F_SRC_NAT:
4340
0
        return "snat";
4341
0
    case OVS_CS_F_DST_NAT:
4342
0
        return "dnat";
4343
0
    default:
4344
0
        return NULL;
4345
0
    }
4346
0
}
4347
4348
static void
4349
format_frag(struct ds *ds, const char *name, uint8_t key,
4350
            const uint8_t *mask, bool verbose OVS_UNUSED)
4351
0
{
4352
0
    bool mask_empty = mask && !*mask;
4353
0
    bool mask_full = !mask || *mask == UINT8_MAX;
4354
4355
    /* ODP frag is an enumeration field; partial masks are not meaningful. */
4356
0
    if (!mask_empty && !mask_full) {
4357
0
        ds_put_format(ds, "error: partial mask not supported for frag (%#"
4358
0
                      PRIx8"),", *mask);
4359
0
    } else if (!mask_empty) {
4360
0
        ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
4361
0
    }
4362
0
}
4363
4364
static bool
4365
mask_empty(const struct nlattr *ma)
4366
0
{
4367
0
    const void *mask;
4368
0
    size_t n;
4369
4370
0
    if (!ma) {
4371
0
        return true;
4372
0
    }
4373
0
    mask = nl_attr_get(ma);
4374
0
    n = nl_attr_get_size(ma);
4375
4376
0
    return is_all_zeros(mask, n);
4377
0
}
4378
4379
/* The caller must have already verified that 'a' and 'ma' have correct
4380
 * lengths. */
4381
static void
4382
format_odp_key_attr__(const struct nlattr *a, const struct nlattr *ma,
4383
                      const struct hmap *portno_names, struct ds *ds,
4384
                      bool verbose, bool skip_no_mask)
4385
0
{
4386
0
    enum ovs_key_attr attr = nl_attr_type(a);
4387
0
    char namebuf[OVS_KEY_ATTR_BUFSIZE];
4388
0
    bool is_exact;
4389
4390
0
    is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
4391
4392
0
    ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
4393
4394
0
    ds_put_char(ds, '(');
4395
0
    switch (attr) {
4396
0
    case OVS_KEY_ATTR_ENCAP:
4397
0
        if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
4398
0
            odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
4399
0
                            nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
4400
0
                            verbose, skip_no_mask);
4401
0
        } else if (nl_attr_get_size(a)) {
4402
0
            odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
4403
0
                            ds, verbose, skip_no_mask);
4404
0
        }
4405
0
        break;
4406
4407
0
    case OVS_KEY_ATTR_PRIORITY:
4408
0
    case OVS_KEY_ATTR_SKB_MARK:
4409
0
    case OVS_KEY_ATTR_DP_HASH:
4410
0
    case OVS_KEY_ATTR_RECIRC_ID:
4411
0
        ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4412
0
        if (!is_exact) {
4413
0
            ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4414
0
        }
4415
0
        break;
4416
4417
0
    case OVS_KEY_ATTR_CT_MARK:
4418
0
        if (verbose || !mask_empty(ma)) {
4419
0
            ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4420
0
            if (!is_exact) {
4421
0
                ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4422
0
            }
4423
0
        }
4424
0
        break;
4425
4426
0
    case OVS_KEY_ATTR_CT_STATE:
4427
0
        if (verbose) {
4428
0
                ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4429
0
                if (!is_exact) {
4430
0
                    ds_put_format(ds, "/%#"PRIx32,
4431
0
                                  mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
4432
0
                }
4433
0
        } else if (!is_exact) {
4434
0
            format_flags_masked(ds, NULL, odp_ct_state_to_string,
4435
0
                                nl_attr_get_u32(a),
4436
0
                                mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
4437
0
                                UINT32_MAX);
4438
0
        } else {
4439
0
            format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
4440
0
        }
4441
0
        break;
4442
4443
0
    case OVS_KEY_ATTR_CT_ZONE:
4444
0
        if (verbose || !mask_empty(ma)) {
4445
0
            ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
4446
0
            if (!is_exact) {
4447
0
                ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
4448
0
            }
4449
0
        }
4450
0
        break;
4451
4452
0
    case OVS_KEY_ATTR_CT_LABELS: {
4453
0
        const ovs_32aligned_u128 *value = nl_attr_get(a);
4454
0
        const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL;
4455
4456
0
        format_u128(ds, value, mask, verbose);
4457
0
        break;
4458
0
    }
4459
4460
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
4461
0
        const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a);
4462
0
        const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4463
4464
0
        format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4465
0
        format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4466
0
        format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4467
0
                   verbose);
4468
0
        format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port),
4469
0
                    verbose);
4470
0
        format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port),
4471
0
                    verbose);
4472
0
        ds_chomp(ds, ',');
4473
0
        break;
4474
0
    }
4475
4476
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
4477
0
        const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a);
4478
0
        const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4479
4480
0
        format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4481
0
                        verbose);
4482
0
        format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4483
0
                        verbose);
4484
0
        format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4485
0
                   verbose);
4486
0
        format_be16(ds, "src_port", key->src_port, MASK(mask, src_port),
4487
0
                    verbose);
4488
0
        format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port),
4489
0
                    verbose);
4490
0
        ds_chomp(ds, ',');
4491
0
        break;
4492
0
    }
4493
4494
0
    case OVS_KEY_ATTR_TUNNEL:
4495
0
        format_odp_tun_attr(a, ma, ds, verbose);
4496
0
        break;
4497
4498
0
    case OVS_KEY_ATTR_IN_PORT:
4499
0
        if (is_exact) {
4500
0
            odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
4501
0
        } else {
4502
0
            ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
4503
0
            if (!is_exact) {
4504
0
                ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4505
0
            }
4506
0
        }
4507
0
        break;
4508
4509
0
    case OVS_KEY_ATTR_PACKET_TYPE: {
4510
0
        ovs_be32 value = nl_attr_get_be32(a);
4511
0
        ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX;
4512
4513
0
        ovs_be16 ns = htons(pt_ns(value));
4514
0
        ovs_be16 ns_mask = htons(pt_ns(mask));
4515
0
        format_be16(ds, "ns", ns, &ns_mask, verbose);
4516
4517
0
        ovs_be16 ns_type = pt_ns_type_be(value);
4518
0
        ovs_be16 ns_type_mask = pt_ns_type_be(mask);
4519
0
        format_be16x(ds, "id", ns_type, &ns_type_mask, verbose);
4520
4521
0
        ds_chomp(ds, ',');
4522
0
        break;
4523
0
    }
4524
4525
0
    case OVS_KEY_ATTR_ETHERNET: {
4526
0
        const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
4527
0
        const struct ovs_key_ethernet *key = nl_attr_get(a);
4528
4529
0
        format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
4530
0
        format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
4531
0
        ds_chomp(ds, ',');
4532
0
        break;
4533
0
    }
4534
0
    case OVS_KEY_ATTR_VLAN:
4535
0
        format_vlan_tci(ds, nl_attr_get_be16(a),
4536
0
                        ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
4537
0
        break;
4538
4539
0
    case OVS_KEY_ATTR_MPLS: {
4540
0
        const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
4541
0
        const struct ovs_key_mpls *mpls_mask = NULL;
4542
0
        size_t size = nl_attr_get_size(a);
4543
4544
0
        if (!size || size % sizeof *mpls_key) {
4545
0
            ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
4546
0
            return;
4547
0
        }
4548
0
        if (!is_exact) {
4549
0
            mpls_mask = nl_attr_get(ma);
4550
0
            if (size != nl_attr_get_size(ma)) {
4551
0
                ds_put_format(ds, "(key length %"PRIuSIZE" != "
4552
0
                              "mask length %"PRIuSIZE")",
4553
0
                              size, nl_attr_get_size(ma));
4554
0
                return;
4555
0
            }
4556
0
        }
4557
0
        format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
4558
0
        break;
4559
0
    }
4560
0
    case OVS_KEY_ATTR_ETHERTYPE:
4561
0
        ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
4562
0
        if (!is_exact) {
4563
0
            ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
4564
0
        }
4565
0
        break;
4566
4567
0
    case OVS_KEY_ATTR_IPV4: {
4568
0
        const struct ovs_key_ipv4 *key = nl_attr_get(a);
4569
0
        const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4570
4571
0
        format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4572
0
        format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4573
0
        format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4574
0
                      verbose);
4575
0
        format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
4576
0
        format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
4577
0
        format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
4578
0
                    verbose);
4579
0
        ds_chomp(ds, ',');
4580
0
        break;
4581
0
    }
4582
0
    case OVS_KEY_ATTR_IPV6: {
4583
0
        const struct ovs_key_ipv6 *key = nl_attr_get(a);
4584
0
        const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4585
4586
0
        format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4587
0
                        verbose);
4588
0
        format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4589
0
                        verbose);
4590
0
        format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
4591
0
                          verbose);
4592
0
        format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4593
0
                   verbose);
4594
0
        format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
4595
0
                   verbose);
4596
0
        format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
4597
0
                   verbose);
4598
0
        format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
4599
0
                    verbose);
4600
0
        ds_chomp(ds, ',');
4601
0
        break;
4602
0
    }
4603
        /* These have the same structure and format. */
4604
0
    case OVS_KEY_ATTR_TCP:
4605
0
    case OVS_KEY_ATTR_UDP:
4606
0
    case OVS_KEY_ATTR_SCTP: {
4607
0
        const struct ovs_key_tcp *key = nl_attr_get(a);
4608
0
        const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
4609
4610
0
        format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
4611
0
        format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
4612
0
        ds_chomp(ds, ',');
4613
0
        break;
4614
0
    }
4615
0
    case OVS_KEY_ATTR_TCP_FLAGS:
4616
0
        if (!is_exact) {
4617
0
            format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
4618
0
                                ntohs(nl_attr_get_be16(a)),
4619
0
                                TCP_FLAGS(nl_attr_get_be16(ma)),
4620
0
                                TCP_FLAGS(OVS_BE16_MAX));
4621
0
        } else {
4622
0
            format_flags(ds, packet_tcp_flag_to_string,
4623
0
                         ntohs(nl_attr_get_be16(a)), '|');
4624
0
        }
4625
0
        break;
4626
4627
0
    case OVS_KEY_ATTR_ICMP: {
4628
0
        const struct ovs_key_icmp *key = nl_attr_get(a);
4629
0
        const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
4630
4631
0
        format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
4632
0
        format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
4633
0
        ds_chomp(ds, ',');
4634
0
        break;
4635
0
    }
4636
0
    case OVS_KEY_ATTR_ICMPV6: {
4637
0
        const struct ovs_key_icmpv6 *key = nl_attr_get(a);
4638
0
        const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
4639
4640
0
        format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
4641
0
                   verbose);
4642
0
        format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
4643
0
                   verbose);
4644
0
        ds_chomp(ds, ',');
4645
0
        break;
4646
0
    }
4647
0
    case OVS_KEY_ATTR_ARP: {
4648
0
        const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
4649
0
        const struct ovs_key_arp *key = nl_attr_get(a);
4650
4651
0
        format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
4652
0
        format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
4653
0
        format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
4654
0
        format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
4655
0
        format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
4656
0
        ds_chomp(ds, ',');
4657
0
        break;
4658
0
    }
4659
0
    case OVS_KEY_ATTR_ND: {
4660
0
        const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
4661
0
        const struct ovs_key_nd *key = nl_attr_get(a);
4662
4663
0
        format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target),
4664
0
                        verbose);
4665
0
        format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
4666
0
        format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
4667
4668
0
        ds_chomp(ds, ',');
4669
0
        break;
4670
0
    }
4671
0
    case OVS_KEY_ATTR_ND_EXTENSIONS: {
4672
0
        const struct ovs_key_nd_extensions *mask = ma ? nl_attr_get(ma) : NULL;
4673
0
        const struct ovs_key_nd_extensions *key = nl_attr_get(a);
4674
4675
0
        bool first = true;
4676
0
        format_be32_masked(ds, &first, "nd_reserved", key->nd_reserved,
4677
0
                           OVS_BE32_MAX);
4678
0
        ds_put_char(ds, ',');
4679
4680
0
        format_u8u(ds, "nd_options_type", key->nd_options_type,
4681
0
                   MASK(mask, nd_options_type), verbose);
4682
4683
0
        ds_chomp(ds, ',');
4684
0
        break;
4685
0
    }
4686
0
    case OVS_KEY_ATTR_NSH: {
4687
0
        format_odp_nsh_attr(a, ma, ds);
4688
0
        break;
4689
0
    }
4690
0
    case OVS_KEY_ATTR_UNSPEC:
4691
0
    case OVS_KEY_ATTR_TUNNEL_INFO:
4692
0
    case __OVS_KEY_ATTR_MAX:
4693
0
    default:
4694
0
        format_generic_odp_key(a, ds);
4695
0
        if (!is_exact) {
4696
0
            ds_put_char(ds, '/');
4697
0
            format_generic_odp_key(ma, ds);
4698
0
        }
4699
0
        break;
4700
0
    }
4701
0
    ds_put_char(ds, ')');
4702
0
}
4703
4704
static void
4705
format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
4706
                    const struct hmap *portno_names, struct ds *ds,
4707
                    bool verbose)
4708
0
{
4709
0
    if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4710
0
                        OVS_KEY_ATTR_MAX, false)) {
4711
0
        format_odp_key_attr__(a, ma, portno_names, ds, verbose, false);
4712
0
    }
4713
0
}
4714
4715
static struct nlattr *
4716
generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
4717
                           struct ofpbuf *ofp, const struct nlattr *key)
4718
0
{
4719
0
    const struct nlattr *a;
4720
0
    unsigned int left;
4721
0
    int type = nl_attr_type(key);
4722
0
    int size = nl_attr_get_size(key);
4723
4724
0
    if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
4725
0
        nl_msg_put_unspec_zero(ofp, type, size);
4726
0
    } else {
4727
0
        size_t nested_mask;
4728
4729
0
        if (tbl[type].next) {
4730
0
            const struct attr_len_tbl *entry = &tbl[type];
4731
0
            tbl = entry->next;
4732
0
            max = entry->next_max;
4733
0
        }
4734
4735
0
        nested_mask = nl_msg_start_nested(ofp, type);
4736
0
        NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
4737
0
            generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
4738
0
        }
4739
0
        nl_msg_end_nested(ofp, nested_mask);
4740
0
    }
4741
4742
0
    return ofp->base;
4743
0
}
4744
4745
static void
4746
format_u128(struct ds *ds, const ovs_32aligned_u128 *key,
4747
            const ovs_32aligned_u128 *mask, bool verbose)
4748
0
{
4749
0
    if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) {
4750
0
        ovs_be128 value = hton128(get_32aligned_u128(key));
4751
0
        ds_put_hex(ds, &value, sizeof value);
4752
0
        if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) {
4753
0
            value = hton128(get_32aligned_u128(mask));
4754
0
            ds_put_char(ds, '/');
4755
0
            ds_put_hex(ds, &value, sizeof value);
4756
0
        }
4757
0
    }
4758
0
}
4759
4760
/* Read the string from 's_' as a 128-bit value.  If the string contains
4761
 * a "/", the rest of the string will be treated as a 128-bit mask.
4762
 *
4763
 * If either the value or mask is larger than 64 bits, the string must
4764
 * be in hexadecimal.
4765
 */
4766
static int
4767
scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
4768
0
{
4769
0
    char *s = CONST_CAST(char *, s_);
4770
0
    ovs_be128 be_value;
4771
0
    ovs_be128 be_mask;
4772
4773
0
    if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
4774
0
        *value = ntoh128(be_value);
4775
4776
0
        if (mask) {
4777
0
            int n;
4778
4779
0
            if (ovs_scan(s, "/%n", &n)) {
4780
0
                int error;
4781
4782
0
                s += n;
4783
0
                error = parse_int_string(s, (uint8_t *)&be_mask,
4784
0
                                         sizeof be_mask, &s);
4785
0
                if (error) {
4786
0
                    return 0;
4787
0
                }
4788
0
                *mask = ntoh128(be_mask);
4789
0
            } else {
4790
0
                *mask = OVS_U128_MAX;
4791
0
            }
4792
0
        }
4793
0
        return s - s_;
4794
0
    }
4795
4796
0
    return 0;
4797
0
}
4798
4799
int
4800
odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
4801
0
{
4802
0
    const char *s = s_;
4803
4804
0
    if (ovs_scan(s, "ufid:")) {
4805
0
        s += 5;
4806
4807
0
        if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
4808
0
            return -EINVAL;
4809
0
        }
4810
0
        s += UUID_LEN;
4811
4812
0
        return s - s_;
4813
0
    }
4814
4815
0
    return 0;
4816
0
}
4817
4818
void
4819
odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
4820
0
{
4821
0
    ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
4822
0
}
4823
4824
/* Appends to 'ds' a string representation of the 'key_len' bytes of
4825
 * OVS_KEY_ATTR_* attributes in 'key'.  If non-null, additionally formats the
4826
 * 'mask_len' bytes of 'mask' which apply to 'key'.  If 'portno_names' is
4827
 * non-null, translates odp port number to its name.  If 'skip_no_mask' is set
4828
 * to true, OVS_KEY_ATTR_* entries without a mask will not be printed, even
4829
 * when verbose mode is 'true'. */
4830
void
4831
odp_flow_format(const struct nlattr *key, size_t key_len,
4832
                const struct nlattr *mask, size_t mask_len,
4833
                const struct hmap *portno_names, struct ds *ds, bool verbose,
4834
                bool skip_no_mask)
4835
0
{
4836
0
    if (key_len) {
4837
0
        const struct nlattr *a;
4838
0
        unsigned int left;
4839
0
        bool has_ethtype_key = false;
4840
0
        bool has_packet_type_key = false;
4841
0
        struct ofpbuf ofp;
4842
0
        bool first_field = true;
4843
4844
0
        ofpbuf_init(&ofp, 100);
4845
0
        NL_ATTR_FOR_EACH (a, left, key, key_len) {
4846
0
            int attr_type = nl_attr_type(a);
4847
0
            const struct nlattr *ma = (mask && mask_len
4848
0
                                       ? nl_attr_find__(mask, mask_len,
4849
0
                                                        attr_type)
4850
0
                                       : NULL);
4851
0
            if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4852
0
                                OVS_KEY_ATTR_MAX, false)
4853
0
                || (skip_no_mask && !ma)) {
4854
0
                continue;
4855
0
            }
4856
4857
0
            bool is_nested_attr;
4858
0
            bool is_wildcard = false;
4859
4860
0
            if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
4861
0
                has_ethtype_key = true;
4862
0
            } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE) {
4863
0
                has_packet_type_key = true;
4864
0
            }
4865
4866
0
            is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
4867
0
                                              OVS_KEY_ATTR_MAX, attr_type) ==
4868
0
                             ATTR_LEN_NESTED;
4869
4870
0
            if (mask && mask_len) {
4871
0
                ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
4872
0
                is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
4873
0
            }
4874
4875
0
            if (verbose || !is_wildcard  || is_nested_attr) {
4876
0
                if (is_wildcard && !ma) {
4877
0
                    ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
4878
0
                                                    OVS_KEY_ATTR_MAX,
4879
0
                                                    &ofp, a);
4880
0
                }
4881
0
                if (!first_field) {
4882
0
                    ds_put_char(ds, ',');
4883
0
                }
4884
0
                format_odp_key_attr__(a, ma, portno_names, ds, verbose,
4885
0
                                      skip_no_mask);
4886
0
                first_field = false;
4887
0
            } else if (attr_type == OVS_KEY_ATTR_ETHERNET
4888
0
                       && !has_packet_type_key) {
4889
                /* This special case reflects differences between the kernel
4890
                 * and userspace datapaths regarding the root type of the
4891
                 * packet being matched (typically Ethernet but some tunnels
4892
                 * can encapsulate IPv4 etc.).  The kernel datapath does not
4893
                 * have an explicit way to indicate packet type; instead:
4894
                 *
4895
                 *   - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4896
                 *     Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4897
                 *     Ethertype encoded in the Ethernet header.
4898
                 *
4899
                 *   - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4900
                 *     root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4901
                 *     (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4902
                 *     packet is an IPv4 packet).
4903
                 *
4904
                 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4905
                 * all-wildcarded, it is important to print it.
4906
                 *
4907
                 * On the other hand, the userspace datapath supports
4908
                 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4909
                 * type.  Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4910
                 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4911
0
                if (!first_field) {
4912
0
                    ds_put_char(ds, ',');
4913
0
                }
4914
0
                ds_put_cstr(ds, "eth()");
4915
0
            } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE && is_wildcard) {
4916
                /* See the above help text, however in the case where the
4917
                 * packet type is not shown, we still need to display the
4918
                 * eth() header if the packets type is wildcarded. */
4919
0
                has_packet_type_key = false;
4920
0
            }
4921
0
            ofpbuf_clear(&ofp);
4922
0
        }
4923
0
        ofpbuf_uninit(&ofp);
4924
4925
0
        if (left) {
4926
0
            int i;
4927
4928
0
            if (left == key_len) {
4929
0
                ds_put_cstr(ds, "<empty>");
4930
0
            }
4931
0
            ds_put_format(ds, ",***%u leftover bytes*** (", left);
4932
0
            for (i = 0; i < left; i++) {
4933
0
                ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
4934
0
            }
4935
0
            ds_put_char(ds, ')');
4936
0
        }
4937
0
        if (!has_ethtype_key && mask) {
4938
0
            const struct nlattr *ma = nl_attr_find__(mask, mask_len,
4939
0
                                                     OVS_KEY_ATTR_ETHERTYPE);
4940
0
            if (ma) {
4941
0
                ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
4942
0
                              ntohs(nl_attr_get_be16(ma)));
4943
0
            }
4944
0
        }
4945
0
    } else {
4946
0
        ds_put_cstr(ds, "<empty>");
4947
0
    }
4948
0
}
4949
4950
/* Appends to 'ds' a string representation of the 'key_len' bytes of
4951
 * OVS_KEY_ATTR_* attributes in 'key'. */
4952
void
4953
odp_flow_key_format(const struct nlattr *key,
4954
                    size_t key_len, struct ds *ds)
4955
0
{
4956
0
    odp_flow_format(key, key_len, NULL, 0, NULL, ds, true, false);
4957
0
}
4958
4959
static bool
4960
ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
4961
0
{
4962
0
    if (!strcasecmp(s, "no")) {
4963
0
        *type = OVS_FRAG_TYPE_NONE;
4964
0
    } else if (!strcasecmp(s, "first")) {
4965
0
        *type = OVS_FRAG_TYPE_FIRST;
4966
0
    } else if (!strcasecmp(s, "later")) {
4967
0
        *type = OVS_FRAG_TYPE_LATER;
4968
0
    } else {
4969
0
        return false;
4970
0
    }
4971
0
    return true;
4972
0
}
4973
4974
/* Parsing. */
4975
4976
static int
4977
scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
4978
0
{
4979
0
    int n;
4980
4981
0
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
4982
0
                 ETH_ADDR_SCAN_ARGS(*key), &n)) {
4983
0
        int len = n;
4984
4985
0
        if (mask) {
4986
0
            if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
4987
0
                         ETH_ADDR_SCAN_ARGS(*mask), &n)) {
4988
0
                len += n;
4989
0
            } else {
4990
0
                memset(mask, 0xff, sizeof *mask);
4991
0
            }
4992
0
        }
4993
0
        return len;
4994
0
    }
4995
0
    return 0;
4996
0
}
4997
4998
static int
4999
scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
5000
0
{
5001
0
    int n;
5002
5003
0
    if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
5004
0
        int len = n;
5005
5006
0
        if (mask) {
5007
0
            if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
5008
0
                         IP_SCAN_ARGS(mask), &n)) {
5009
0
                len += n;
5010
0
            } else {
5011
0
                *mask = OVS_BE32_MAX;
5012
0
            }
5013
0
        }
5014
0
        return len;
5015
0
    }
5016
0
    return 0;
5017
0
}
5018
5019
static int
5020
scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
5021
0
{
5022
0
    int n;
5023
0
    char ipv6_s[IPV6_SCAN_LEN + 1];
5024
5025
0
    if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
5026
0
        && inet_pton(AF_INET6, ipv6_s, key) == 1) {
5027
0
        int len = n;
5028
5029
0
        if (mask) {
5030
0
            if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
5031
0
                && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
5032
0
                len += n;
5033
0
            } else {
5034
0
                memset(mask, 0xff, sizeof *mask);
5035
0
            }
5036
0
        }
5037
0
        return len;
5038
0
    }
5039
0
    return 0;
5040
0
}
5041
5042
static int
5043
scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
5044
0
{
5045
0
    int key_, mask_;
5046
0
    int n;
5047
5048
0
    if (ovs_scan(s, "%i%n", &key_, &n)
5049
0
        && (key_ & ~IPV6_LABEL_MASK) == 0) {
5050
0
        int len = n;
5051
5052
0
        *key = htonl(key_);
5053
0
        if (mask) {
5054
0
            if (ovs_scan(s + len, "/%i%n", &mask_, &n)
5055
0
                && (mask_ & ~IPV6_LABEL_MASK) == 0) {
5056
0
                len += n;
5057
0
                *mask = htonl(mask_);
5058
0
            } else {
5059
0
                *mask = htonl(IPV6_LABEL_MASK);
5060
0
            }
5061
0
        }
5062
0
        return len;
5063
0
    }
5064
0
    return 0;
5065
0
}
5066
5067
static int
5068
scan_u8(const char *s, uint8_t *key, uint8_t *mask)
5069
0
{
5070
0
    int n;
5071
5072
0
    if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
5073
0
        int len = n;
5074
5075
0
        if (mask) {
5076
0
            if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
5077
0
                len += n;
5078
0
            } else {
5079
0
                *mask = UINT8_MAX;
5080
0
            }
5081
0
        }
5082
0
        return len;
5083
0
    }
5084
0
    return 0;
5085
0
}
5086
5087
static int
5088
scan_u16(const char *s, uint16_t *key, uint16_t *mask)
5089
0
{
5090
0
    int n;
5091
5092
0
    if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
5093
0
        int len = n;
5094
5095
0
        if (mask) {
5096
0
            if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
5097
0
                len += n;
5098
0
            } else {
5099
0
                *mask = UINT16_MAX;
5100
0
            }
5101
0
        }
5102
0
        return len;
5103
0
    }
5104
0
    return 0;
5105
0
}
5106
5107
static int
5108
scan_u32(const char *s, uint32_t *key, uint32_t *mask)
5109
0
{
5110
0
    int n;
5111
5112
0
    if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
5113
0
        int len = n;
5114
5115
0
        if (mask) {
5116
0
            if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
5117
0
                len += n;
5118
0
            } else {
5119
0
                *mask = UINT32_MAX;
5120
0
            }
5121
0
        }
5122
0
        return len;
5123
0
    }
5124
0
    return 0;
5125
0
}
5126
5127
static int
5128
scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
5129
0
{
5130
0
    uint16_t key_, mask_;
5131
0
    int n;
5132
5133
0
    if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
5134
0
        int len = n;
5135
5136
0
        *key = htons(key_);
5137
0
        if (mask) {
5138
0
            if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
5139
0
                len += n;
5140
0
                *mask = htons(mask_);
5141
0
            } else {
5142
0
                *mask = OVS_BE16_MAX;
5143
0
            }
5144
0
        }
5145
0
        return len;
5146
0
    }
5147
0
    return 0;
5148
0
}
5149
5150
static int
5151
scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask)
5152
0
{
5153
0
    uint32_t key_, mask_;
5154
0
    int n;
5155
5156
0
    if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
5157
0
        int len = n;
5158
5159
0
        *key = htonl(key_);
5160
0
        if (mask) {
5161
0
            if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
5162
0
                len += n;
5163
0
                *mask = htonl(mask_);
5164
0
            } else {
5165
0
                *mask = OVS_BE32_MAX;
5166
0
            }
5167
0
        }
5168
0
        return len;
5169
0
    }
5170
0
    return 0;
5171
0
}
5172
5173
static int
5174
scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
5175
0
{
5176
0
    uint64_t key_, mask_;
5177
0
    int n;
5178
5179
0
    if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
5180
0
        int len = n;
5181
5182
0
        *key = htonll(key_);
5183
0
        if (mask) {
5184
0
            if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
5185
0
                len += n;
5186
0
                *mask = htonll(mask_);
5187
0
            } else {
5188
0
                *mask = OVS_BE64_MAX;
5189
0
            }
5190
0
        }
5191
0
        return len;
5192
0
    }
5193
0
    return 0;
5194
0
}
5195
5196
static int
5197
scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
5198
0
{
5199
0
    uint32_t flags, fmask;
5200
0
    int n;
5201
5202
0
    n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
5203
0
                        FLOW_TNL_F_MASK, mask ? &fmask : NULL);
5204
0
    if (n >= 0 && s[n] == ')') {
5205
0
        *key = flags;
5206
0
        if (mask) {
5207
0
            *mask = fmask;
5208
0
        }
5209
0
        return n + 1;
5210
0
    }
5211
0
    return 0;
5212
0
}
5213
5214
static int
5215
scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
5216
0
{
5217
0
    uint32_t flags, fmask;
5218
0
    int n;
5219
5220
0
    n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
5221
0
                        TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
5222
0
    if (n >= 0) {
5223
0
        *key = htons(flags);
5224
0
        if (mask) {
5225
0
            *mask = htons(fmask);
5226
0
        }
5227
0
        return n;
5228
0
    }
5229
0
    return 0;
5230
0
}
5231
5232
static uint32_t
5233
ovs_to_odp_ct_state(uint8_t state)
5234
0
{
5235
0
    uint32_t odp = 0;
5236
5237
0
#define CS_STATE(ENUM, INDEX, NAME)             \
5238
0
    if (state & CS_##ENUM) {                    \
5239
0
        odp |= OVS_CS_F_##ENUM;                 \
5240
0
    }
5241
0
    CS_STATES
5242
0
#undef CS_STATE
5243
5244
0
    return odp;
5245
0
}
5246
5247
static uint8_t
5248
odp_to_ovs_ct_state(uint32_t flags)
5249
0
{
5250
0
    uint32_t state = 0;
5251
5252
0
#define CS_STATE(ENUM, INDEX, NAME) \
5253
0
    if (flags & OVS_CS_F_##ENUM) {  \
5254
0
        state |= CS_##ENUM;         \
5255
0
    }
5256
0
    CS_STATES
5257
0
#undef CS_STATE
5258
5259
0
    return state;
5260
0
}
5261
5262
static int
5263
scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
5264
0
{
5265
0
    uint32_t flags, fmask;
5266
0
    int n;
5267
5268
0
    n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
5269
0
                    ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
5270
0
                    mask ? &fmask : NULL);
5271
5272
0
    if (n >= 0) {
5273
0
        *key = flags;
5274
0
        if (mask) {
5275
0
            *mask = fmask;
5276
0
        }
5277
0
        return n;
5278
0
    }
5279
0
    return 0;
5280
0
}
5281
5282
static int
5283
scan_frag(const char *s, uint8_t *key, uint8_t *mask)
5284
0
{
5285
0
    int n;
5286
0
    char frag[8];
5287
0
    enum ovs_frag_type frag_type;
5288
5289
0
    if (ovs_scan(s, "%7[a-z]%n", frag, &n)
5290
0
        && ovs_frag_type_from_string(frag, &frag_type)) {
5291
0
        int len = n;
5292
5293
0
        *key = frag_type;
5294
0
        if (mask) {
5295
0
            *mask = UINT8_MAX;
5296
0
        }
5297
0
        return len;
5298
0
    }
5299
0
    return 0;
5300
0
}
5301
5302
static int
5303
scan_port(const char *s, uint32_t *key, uint32_t *mask,
5304
          const struct simap *port_names)
5305
0
{
5306
0
    int n;
5307
5308
0
    if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
5309
0
        int len = n;
5310
5311
0
        if (mask) {
5312
0
            if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
5313
0
                len += n;
5314
0
            } else {
5315
0
                *mask = UINT32_MAX;
5316
0
            }
5317
0
        }
5318
0
        return len;
5319
0
    } else if (port_names) {
5320
0
        const struct simap_node *node;
5321
0
        int len;
5322
5323
0
        len = strcspn(s, ")");
5324
0
        node = simap_find_len(port_names, s, len);
5325
0
        if (node) {
5326
0
            *key = node->data;
5327
5328
0
            if (mask) {
5329
0
                *mask = UINT32_MAX;
5330
0
            }
5331
0
            return len;
5332
0
        }
5333
0
    }
5334
0
    return 0;
5335
0
}
5336
5337
/* Helper for vlan parsing. */
5338
struct ovs_key_vlan__ {
5339
    ovs_be16 tci;
5340
};
5341
5342
static bool
5343
set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
5344
0
{
5345
0
    const uint16_t mask = ((1U << bits) - 1) << offset;
5346
5347
0
    if (value >> bits) {
5348
0
        return false;
5349
0
    }
5350
5351
0
    *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
5352
0
    return true;
5353
0
}
5354
5355
static int
5356
scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
5357
             uint8_t offset)
5358
0
{
5359
0
    uint16_t key_, mask_;
5360
0
    int n;
5361
5362
0
    if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
5363
0
        int len = n;
5364
5365
0
        if (set_be16_bf(key, bits, offset, key_)) {
5366
0
            if (mask) {
5367
0
                if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
5368
0
                    len += n;
5369
5370
0
                    if (!set_be16_bf(mask, bits, offset, mask_)) {
5371
0
                        return 0;
5372
0
                    }
5373
0
                } else {
5374
0
                    *mask |= htons(((1U << bits) - 1) << offset);
5375
0
                }
5376
0
            }
5377
0
            return len;
5378
0
        }
5379
0
    }
5380
0
    return 0;
5381
0
}
5382
5383
static int
5384
scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
5385
0
{
5386
0
    return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
5387
0
}
5388
5389
static int
5390
scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
5391
0
{
5392
0
    return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
5393
0
}
5394
5395
static int
5396
scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
5397
0
{
5398
0
    return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
5399
0
}
5400
5401
/* For MPLS. */
5402
static bool
5403
set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
5404
0
{
5405
0
    const uint32_t mask = ((1U << bits) - 1) << offset;
5406
5407
0
    if (value >> bits) {
5408
0
        return false;
5409
0
    }
5410
5411
0
    *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
5412
0
    return true;
5413
0
}
5414
5415
static int
5416
scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
5417
             uint8_t offset)
5418
0
{
5419
0
    uint32_t key_, mask_;
5420
0
    int n;
5421
5422
0
    if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
5423
0
        int len = n;
5424
5425
0
        if (set_be32_bf(key, bits, offset, key_)) {
5426
0
            if (mask) {
5427
0
                if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
5428
0
                    len += n;
5429
5430
0
                    if (!set_be32_bf(mask, bits, offset, mask_)) {
5431
0
                        return 0;
5432
0
                    }
5433
0
                } else {
5434
0
                    *mask |= htonl(((1U << bits) - 1) << offset);
5435
0
                }
5436
0
            }
5437
0
            return len;
5438
0
        }
5439
0
    }
5440
0
    return 0;
5441
0
}
5442
5443
static int
5444
scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
5445
0
{
5446
0
    return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
5447
0
}
5448
5449
static int
5450
scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
5451
0
{
5452
0
    return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
5453
0
}
5454
5455
static int
5456
scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
5457
0
{
5458
0
    return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
5459
0
}
5460
5461
static int
5462
scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
5463
0
{
5464
0
    return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
5465
0
}
5466
5467
static int
5468
scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
5469
0
{
5470
0
    const char *s_base = s;
5471
0
    ovs_be16 id = 0, id_mask = 0;
5472
0
    uint8_t flags = 0, flags_mask = 0;
5473
0
    int len;
5474
5475
0
    if (!strncmp(s, "id=", 3)) {
5476
0
        s += 3;
5477
0
        len = scan_be16(s, &id, mask ? &id_mask : NULL);
5478
0
        if (len == 0) {
5479
0
            return 0;
5480
0
        }
5481
0
        s += len;
5482
0
    }
5483
5484
0
    if (s[0] == ',') {
5485
0
        s++;
5486
0
    }
5487
0
    if (!strncmp(s, "flags=", 6)) {
5488
0
        s += 6;
5489
0
        len = scan_u8(s, &flags, mask ? &flags_mask : NULL);
5490
0
        if (len == 0) {
5491
0
            return 0;
5492
0
        }
5493
0
        s += len;
5494
0
    }
5495
5496
0
    if (!strncmp(s, "))", 2)) {
5497
0
        s += 2;
5498
5499
0
        *key = (flags << 16) | ntohs(id);
5500
0
        if (mask) {
5501
0
            *mask = (flags_mask << 16) | ntohs(id_mask);
5502
0
        }
5503
5504
0
        return s - s_base;
5505
0
    }
5506
5507
0
    return 0;
5508
0
}
5509
5510
static int
5511
scan_gtpu_metadata(const char *s,
5512
                   struct gtpu_metadata *key,
5513
                   struct gtpu_metadata *mask)
5514
0
{
5515
0
    const char *s_base = s;
5516
0
    uint8_t flags = 0, flags_ma = 0;
5517
0
    uint8_t msgtype = 0, msgtype_ma = 0;
5518
0
    int len;
5519
5520
0
    if (!strncmp(s, "flags=", 6)) {
5521
0
        s += 6;
5522
0
        len = scan_u8(s, &flags, mask ? &flags_ma : NULL);
5523
0
        if (len == 0) {
5524
0
            return 0;
5525
0
        }
5526
0
        s += len;
5527
0
    }
5528
5529
0
    if (s[0] == ',') {
5530
0
        s++;
5531
0
    }
5532
5533
0
    if (!strncmp(s, "msgtype=", 8)) {
5534
0
        s += 8;
5535
0
        len = scan_u8(s, &msgtype, mask ? &msgtype_ma : NULL);
5536
0
        if (len == 0) {
5537
0
            return 0;
5538
0
        }
5539
0
        s += len;
5540
0
    }
5541
5542
0
    if (!strncmp(s, ")", 1)) {
5543
0
        s += 1;
5544
0
        key->flags = flags;
5545
0
        key->msgtype = msgtype;
5546
0
        if (mask) {
5547
0
            mask->flags = flags_ma;
5548
0
            mask->msgtype = msgtype_ma;
5549
0
        }
5550
0
    }
5551
0
    return s - s_base;
5552
0
}
5553
5554
static int
5555
scan_erspan_metadata(const char *s,
5556
                     struct erspan_metadata *key,
5557
                     struct erspan_metadata *mask)
5558
0
{
5559
0
    const char *s_base = s;
5560
0
    uint32_t idx = 0, idx_mask = 0;
5561
0
    uint8_t ver = 0, dir = 0, hwid = 0;
5562
0
    uint8_t ver_mask = 0, dir_mask = 0, hwid_mask = 0;
5563
0
    int len;
5564
5565
0
    if (!strncmp(s, "ver=", 4)) {
5566
0
        s += 4;
5567
0
        len = scan_u8(s, &ver, mask ? &ver_mask : NULL);
5568
0
        if (len == 0) {
5569
0
            return 0;
5570
0
        }
5571
0
        s += len;
5572
0
    }
5573
5574
0
    if (s[0] == ',') {
5575
0
        s++;
5576
0
    }
5577
5578
0
    if (ver == 1) {
5579
0
        if (!strncmp(s, "idx=", 4)) {
5580
0
            s += 4;
5581
0
            len = scan_u32(s, &idx, mask ? &idx_mask : NULL);
5582
0
            if (len == 0) {
5583
0
                return 0;
5584
0
            }
5585
0
            s += len;
5586
0
        }
5587
5588
0
        if (!strncmp(s, ")", 1)) {
5589
0
            s += 1;
5590
0
            key->version = ver;
5591
0
            key->u.index = htonl(idx);
5592
0
            if (mask) {
5593
0
                mask->u.index = htonl(idx_mask);
5594
0
            }
5595
0
        }
5596
0
        return s - s_base;
5597
5598
0
    } else if (ver == 2) {
5599
0
        if (!strncmp(s, "dir=", 4)) {
5600
0
            s += 4;
5601
0
            len = scan_u8(s, &dir, mask ? &dir_mask : NULL);
5602
0
            if (len == 0) {
5603
0
                return 0;
5604
0
            }
5605
0
            s += len;
5606
0
        }
5607
0
        if (s[0] == ',') {
5608
0
            s++;
5609
0
        }
5610
0
        if (!strncmp(s, "hwid=", 5)) {
5611
0
            s += 5;
5612
0
            len = scan_u8(s, &hwid, mask ? &hwid_mask : NULL);
5613
0
            if (len == 0) {
5614
0
                return 0;
5615
0
            }
5616
0
            s += len;
5617
0
        }
5618
5619
0
        if (!strncmp(s, ")", 1)) {
5620
0
            s += 1;
5621
0
            key->version = ver;
5622
0
            key->u.md2.hwid = hwid;
5623
0
            key->u.md2.dir = dir;
5624
0
            if (mask) {
5625
0
                mask->u.md2.hwid = hwid_mask;
5626
0
                mask->u.md2.dir = dir_mask;
5627
0
            }
5628
0
        }
5629
0
        return s - s_base;
5630
0
    }
5631
5632
0
    return 0;
5633
0
}
5634
5635
static int
5636
scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
5637
0
{
5638
0
    const char *s_base = s;
5639
0
    struct geneve_opt *opt = key->d;
5640
0
    struct geneve_opt *opt_mask = mask ? mask->d : NULL;
5641
0
    int len_remain = sizeof key->d;
5642
0
    int len;
5643
5644
0
    while (s[0] == '{' && len_remain >= sizeof *opt) {
5645
0
        int data_len = 0;
5646
5647
0
        s++;
5648
0
        len_remain -= sizeof *opt;
5649
5650
0
        if (!strncmp(s, "class=", 6)) {
5651
0
            s += 6;
5652
0
            len = scan_be16(s, &opt->opt_class,
5653
0
                            mask ? &opt_mask->opt_class : NULL);
5654
0
            if (len == 0) {
5655
0
                return 0;
5656
0
            }
5657
0
            s += len;
5658
0
        } else if (mask) {
5659
0
            memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
5660
0
        }
5661
5662
0
        if (s[0] == ',') {
5663
0
            s++;
5664
0
        }
5665
0
        if (!strncmp(s, "type=", 5)) {
5666
0
            s += 5;
5667
0
            len = scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
5668
0
            if (len == 0) {
5669
0
                return 0;
5670
0
            }
5671
0
            s += len;
5672
0
        } else if (mask) {
5673
0
            memset(&opt_mask->type, 0, sizeof opt_mask->type);
5674
0
        }
5675
5676
0
        if (s[0] == ',') {
5677
0
            s++;
5678
0
        }
5679
0
        if (!strncmp(s, "len=", 4)) {
5680
0
            uint8_t opt_len, opt_len_mask;
5681
0
            s += 4;
5682
0
            len = scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
5683
0
            if (len == 0) {
5684
0
                return 0;
5685
0
            }
5686
0
            s += len;
5687
5688
0
            if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
5689
0
                return 0;
5690
0
            }
5691
0
            opt->length = opt_len / 4;
5692
0
            if (mask) {
5693
0
                opt_mask->length = opt_len_mask;
5694
0
            }
5695
0
            data_len = opt_len;
5696
0
        } else if (mask) {
5697
0
            memset(&opt_mask->type, 0, sizeof opt_mask->type);
5698
0
        }
5699
5700
0
        if (s[0] == ',') {
5701
0
            s++;
5702
0
            if (parse_int_string(s, (uint8_t *)(opt + 1),
5703
0
                                 data_len, (char **)&s)) {
5704
0
                return 0;
5705
0
            }
5706
0
        }
5707
0
        if (mask) {
5708
0
            if (s[0] == '/') {
5709
0
                s++;
5710
0
                if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
5711
0
                                     data_len, (char **)&s)) {
5712
0
                    return 0;
5713
0
                }
5714
0
            }
5715
0
            opt_mask->r1 = 0;
5716
0
            opt_mask->r2 = 0;
5717
0
            opt_mask->r3 = 0;
5718
0
        }
5719
5720
0
        if (s[0] == '}') {
5721
0
            s++;
5722
0
            opt += 1 + data_len / 4;
5723
0
            if (mask) {
5724
0
                opt_mask += 1 + data_len / 4;
5725
0
            }
5726
0
            len_remain -= data_len;
5727
0
        } else {
5728
0
            return 0;
5729
0
        }
5730
0
    }
5731
5732
0
    if (s[0] == ')') {
5733
0
        len = sizeof key->d - len_remain;
5734
5735
0
        s++;
5736
0
        key->len = len;
5737
0
        if (mask) {
5738
0
            mask->len = len;
5739
0
        }
5740
0
        return s - s_base;
5741
0
    }
5742
5743
0
    return 0;
5744
0
}
5745
5746
static void
5747
tun_flags_to_attr(struct ofpbuf *a, const void *data_)
5748
0
{
5749
0
    const uint16_t *flags = data_;
5750
5751
0
    if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
5752
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
5753
0
    }
5754
0
    if (*flags & FLOW_TNL_F_CSUM) {
5755
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
5756
0
    }
5757
0
    if (*flags & FLOW_TNL_F_OAM) {
5758
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
5759
0
    }
5760
0
}
5761
5762
static void
5763
vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
5764
0
{
5765
0
    const uint32_t *gbp = data_;
5766
5767
0
    if (*gbp) {
5768
0
        size_t vxlan_opts_ofs;
5769
5770
0
        vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
5771
0
        nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
5772
0
        nl_msg_end_nested(a, vxlan_opts_ofs);
5773
0
    }
5774
0
}
5775
5776
static void
5777
geneve_to_attr(struct ofpbuf *a, const void *data_)
5778
0
{
5779
0
    const struct geneve_scan *geneve = data_;
5780
5781
0
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
5782
0
                      geneve->len);
5783
0
}
5784
5785
static void
5786
erspan_to_attr(struct ofpbuf *a, const void *data_)
5787
0
{
5788
0
    const struct erspan_metadata *md = data_;
5789
5790
0
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, md,
5791
0
                      sizeof *md);
5792
0
}
5793
5794
static void
5795
gtpu_to_attr(struct ofpbuf *a, const void *data_)
5796
0
{
5797
0
    const struct gtpu_metadata *md = data_;
5798
5799
0
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS, md,
5800
0
                      sizeof *md);
5801
0
}
5802
5803
#define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC)                      \
5804
0
    {                                                             \
5805
0
        unsigned long call_fn = (unsigned long)FUNC;              \
5806
0
        if (call_fn) {                                            \
5807
0
            typedef void (*fn)(struct ofpbuf *, const void *);    \
5808
0
            fn func = FUNC;                                       \
5809
0
            func(BUF, &(DATA));                                   \
5810
0
        } else {                                                  \
5811
0
            nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5812
0
        }                                                         \
5813
0
    }
5814
5815
#define SCAN_IF(NAME)                           \
5816
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {  \
5817
0
        const char *start = s;                  \
5818
0
        int len;                                \
5819
0
                                                \
5820
0
        s += strlen(NAME)
5821
5822
/* Usually no special initialization is needed. */
5823
#define SCAN_BEGIN(NAME, TYPE)                  \
5824
0
    SCAN_IF(NAME);                              \
5825
0
        TYPE skey, smask;                       \
5826
0
        memset(&skey, 0, sizeof skey);          \
5827
0
        memset(&smask, 0, sizeof smask);        \
5828
0
        do {                                    \
5829
0
            len = 0;
5830
5831
/* Init as fully-masked as mask will not be scanned. */
5832
#define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE)     \
5833
0
    SCAN_IF(NAME);                              \
5834
0
        TYPE skey, smask;                       \
5835
0
        memset(&skey, 0, sizeof skey);          \
5836
0
        memset(&smask, 0xff, sizeof smask);     \
5837
0
        do {                                    \
5838
0
            len = 0;
5839
5840
/* VLAN needs special initialization. */
5841
#define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT)  \
5842
0
    SCAN_IF(NAME);                                        \
5843
0
        TYPE skey = KEY_INIT;                       \
5844
0
        TYPE smask = MASK_INIT;                     \
5845
0
        do {                                        \
5846
0
            len = 0;
5847
5848
/* Scan unnamed entry as 'TYPE' */
5849
#define SCAN_TYPE(TYPE, KEY, MASK)              \
5850
0
    len = scan_##TYPE(s, KEY, MASK);            \
5851
0
    if (len == 0) {                             \
5852
0
        return -EINVAL;                         \
5853
0
    }                                           \
5854
0
    s += len
5855
5856
/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5857
#define SCAN_FIELD(NAME, TYPE, FIELD)                                   \
5858
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
5859
0
        s += strlen(NAME);                                              \
5860
0
        SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL);       \
5861
0
        continue;                                                       \
5862
0
    }
5863
5864
#define SCAN_FINISH()                           \
5865
0
        } while (*s++ == ',' && len != 0);      \
5866
0
        if (s[-1] != ')') {                     \
5867
0
            return -EINVAL;                     \
5868
0
        }
5869
5870
#define SCAN_FINISH_SINGLE()                    \
5871
0
        } while (false);                        \
5872
0
        if (*s++ != ')') {                      \
5873
0
            return -EINVAL;                     \
5874
0
        }
5875
5876
/* Beginning of nested attribute. */
5877
#define SCAN_BEGIN_NESTED(NAME, ATTR)                      \
5878
0
    SCAN_IF(NAME);                                         \
5879
0
        size_t key_offset, mask_offset = 0;                \
5880
0
        key_offset = nl_msg_start_nested(key, ATTR);       \
5881
0
        if (mask) {                                        \
5882
0
            mask_offset = nl_msg_start_nested(mask, ATTR); \
5883
0
        }                                                  \
5884
0
        do {                                               \
5885
0
            len = 0;
5886
5887
#define SCAN_END_NESTED()                                                     \
5888
0
        SCAN_FINISH();                                                        \
5889
0
        if (nl_attr_oversized(key->size - key_offset - NLA_HDRLEN)) {         \
5890
0
            return -E2BIG;                                                    \
5891
0
        }                                                                     \
5892
0
        nl_msg_end_nested(key, key_offset);                                   \
5893
0
        if (mask) {                                                           \
5894
0
            nl_msg_end_nested(mask, mask_offset);                             \
5895
0
        }                                                                     \
5896
0
        return s - start;                                                     \
5897
0
    }
5898
5899
#define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC)  \
5900
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                \
5901
0
        TYPE skey, smask;                                     \
5902
0
        memset(&skey, 0, sizeof skey);                        \
5903
0
        memset(&smask, 0xff, sizeof smask);                   \
5904
0
        s += strlen(NAME);                                    \
5905
0
        SCAN_TYPE(SCAN_AS, &skey, &smask);                    \
5906
0
        SCAN_PUT(ATTR, FUNC);                                 \
5907
0
        continue;                                             \
5908
0
    }
5909
5910
#define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR)  \
5911
0
        SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5912
5913
#define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC)  \
5914
0
        SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5915
5916
#define SCAN_PUT(ATTR, FUNC)                            \
5917
0
        SCAN_PUT_ATTR(key, ATTR, skey, FUNC);           \
5918
0
        if (mask)                                       \
5919
0
            SCAN_PUT_ATTR(mask, ATTR, smask, FUNC);     \
5920
5921
#define SCAN_END(ATTR)                                  \
5922
0
        SCAN_FINISH();                                  \
5923
0
        SCAN_PUT(ATTR, NULL);                           \
5924
0
        return s - start;                               \
5925
0
    }
5926
5927
#define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT)       \
5928
0
    SCAN_IF(NAME);                              \
5929
0
        TYPE skey[CNT], smask[CNT];             \
5930
0
        memset(&skey, 0, sizeof skey);          \
5931
0
        memset(&smask, 0, sizeof smask);        \
5932
0
        int idx = 0, cnt = CNT;                 \
5933
0
        uint64_t fields = 0;                    \
5934
0
        do {                                    \
5935
0
            int field = 0;                      \
5936
0
            len = 0;
5937
5938
/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5939
#define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD)                             \
5940
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
5941
0
        if (fields & (1UL << field)) {                                  \
5942
0
            fields = 0;                                                 \
5943
0
            if (++idx == cnt) {                                         \
5944
0
                break;                                                  \
5945
0
            }                                                           \
5946
0
        }                                                               \
5947
0
        s += strlen(NAME);                                              \
5948
0
        SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5949
0
        fields |= 1UL << field;                                         \
5950
0
        continue;                                                       \
5951
0
    }                                                                   \
5952
0
    field++;
5953
5954
#define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT)                    \
5955
0
    nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5956
5957
#define SCAN_PUT_ARRAY(ATTR, CNT)                        \
5958
0
    SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT);       \
5959
0
    if (mask) {                                      \
5960
0
        SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5961
0
    }
5962
5963
#define SCAN_END_ARRAY(ATTR)             \
5964
0
        SCAN_FINISH();                   \
5965
0
        if (idx == cnt) {                \
5966
0
            return -EINVAL;              \
5967
0
        }                                \
5968
0
        SCAN_PUT_ARRAY(ATTR, idx + 1);   \
5969
0
        return s - start;                \
5970
0
    }
5971
5972
#define SCAN_END_SINGLE(ATTR)                           \
5973
0
        SCAN_FINISH_SINGLE();                           \
5974
0
        SCAN_PUT(ATTR, NULL);                           \
5975
0
        return s - start;                               \
5976
0
    }
5977
5978
#define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR)       \
5979
0
    SCAN_BEGIN(NAME, TYPE) {                         \
5980
0
        SCAN_TYPE(SCAN_AS, &skey, &smask);           \
5981
0
    } SCAN_END_SINGLE(ATTR)
5982
5983
#define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5984
0
    SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) {                   \
5985
0
        SCAN_TYPE(SCAN_AS, &skey, NULL);                    \
5986
0
    } SCAN_END_SINGLE(ATTR)
5987
5988
/* scan_port needs one extra argument. */
5989
#define SCAN_SINGLE_PORT(NAME, TYPE, ATTR)  \
5990
0
    SCAN_BEGIN(NAME, TYPE) {                            \
5991
0
        len = scan_port(s, &skey, &smask,               \
5992
0
                        context->port_names);           \
5993
0
        if (len == 0) {                                 \
5994
0
            return -EINVAL;                             \
5995
0
        }                                               \
5996
0
        s += len;                                       \
5997
0
    } SCAN_END_SINGLE(ATTR)
5998
5999
static int
6000
parse_odp_nsh_key_mask_attr(const char *s, struct ofpbuf *key,
6001
                            struct ofpbuf *mask)
6002
0
{
6003
0
    if (strncmp(s, "nsh(", 4) == 0) {
6004
0
        const char *start = s;
6005
0
        int len;
6006
0
        struct ovs_key_nsh skey, smask;
6007
0
        uint32_t spi = 0, spi_mask = 0;
6008
0
        uint8_t si = 0, si_mask = 0;
6009
6010
0
        s += 4;
6011
6012
0
        memset(&skey, 0, sizeof skey);
6013
0
        memset(&smask, 0, sizeof smask);
6014
0
        do {
6015
0
            len = 0;
6016
6017
0
            if (strncmp(s, "flags=", 6) == 0) {
6018
0
                s += 6;
6019
0
                len = scan_u8(s, &skey.flags, mask ? &smask.flags : NULL);
6020
0
                if (len == 0) {
6021
0
                    return -EINVAL;
6022
0
                }
6023
0
                s += len;
6024
0
                continue;
6025
0
            }
6026
6027
0
            if (strncmp(s, "mdtype=", 7) == 0) {
6028
0
                s += 7;
6029
0
                len = scan_u8(s, &skey.mdtype, mask ? &smask.mdtype : NULL);
6030
0
                if (len == 0) {
6031
0
                    return -EINVAL;
6032
0
                }
6033
0
                s += len;
6034
0
                continue;
6035
0
            }
6036
6037
0
            if (strncmp(s, "np=", 3) == 0) {
6038
0
                s += 3;
6039
0
                len = scan_u8(s, &skey.np, mask ? &smask.np : NULL);
6040
0
                if (len == 0) {
6041
0
                    return -EINVAL;
6042
0
                }
6043
0
                s += len;
6044
0
                continue;
6045
0
            }
6046
6047
0
            if (strncmp(s, "spi=", 4) == 0) {
6048
0
                s += 4;
6049
0
                len = scan_u32(s, &spi, mask ? &spi_mask : NULL);
6050
0
                if (len == 0) {
6051
0
                    return -EINVAL;
6052
0
                }
6053
0
                s += len;
6054
0
                continue;
6055
0
            }
6056
6057
0
            if (strncmp(s, "si=", 3) == 0) {
6058
0
                s += 3;
6059
0
                len = scan_u8(s, &si, mask ? &si_mask : NULL);
6060
0
                if (len == 0) {
6061
0
                    return -EINVAL;
6062
0
                }
6063
0
                s += len;
6064
0
                continue;
6065
0
            }
6066
6067
0
            if (strncmp(s, "c1=", 3) == 0) {
6068
0
                s += 3;
6069
0
                len = scan_be32(s, &skey.context[0],
6070
0
                                mask ? &smask.context[0] : NULL);
6071
0
                if (len == 0) {
6072
0
                    return -EINVAL;
6073
0
                }
6074
0
                s += len;
6075
0
                continue;
6076
0
            }
6077
6078
0
            if (strncmp(s, "c2=", 3) == 0) {
6079
0
                s += 3;
6080
0
                len = scan_be32(s, &skey.context[1],
6081
0
                                mask ? &smask.context[1] : NULL);
6082
0
                if (len == 0) {
6083
0
                    return -EINVAL;
6084
0
                }
6085
0
                s += len;
6086
0
                continue;
6087
0
            }
6088
6089
0
            if (strncmp(s, "c3=", 3) == 0) {
6090
0
                s += 3;
6091
0
                len = scan_be32(s, &skey.context[2],
6092
0
                                mask ? &smask.context[2] : NULL);
6093
0
                if (len == 0) {
6094
0
                    return -EINVAL;
6095
0
                }
6096
0
                s += len;
6097
0
                continue;
6098
0
            }
6099
6100
0
            if (strncmp(s, "c4=", 3) == 0) {
6101
0
                s += 3;
6102
0
                len = scan_be32(s, &skey.context[3],
6103
0
                                mask ? &smask.context[3] : NULL);
6104
0
                if (len == 0) {
6105
0
                    return -EINVAL;
6106
0
                }
6107
0
                s += len;
6108
0
                continue;
6109
0
            }
6110
0
        } while (*s++ == ',' && len != 0);
6111
0
        if (s[-1] != ')') {
6112
0
            return -EINVAL;
6113
0
        }
6114
6115
0
        skey.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
6116
0
        smask.path_hdr = nsh_spi_si_to_path_hdr(spi_mask, si_mask);
6117
6118
0
        nsh_key_to_attr(key, &skey, NULL, 0, false);
6119
0
        if (mask) {
6120
0
            nsh_key_to_attr(mask, &smask, NULL, 0, true);
6121
0
        }
6122
0
        return s - start;
6123
0
    }
6124
0
    return 0;
6125
0
}
6126
6127
static int
6128
parse_odp_key_mask_attr(struct parse_odp_context *context, const char *s,
6129
                        struct ofpbuf *key, struct ofpbuf *mask)
6130
0
{
6131
0
    int retval;
6132
6133
0
    context->depth++;
6134
6135
0
    if (context->depth == MAX_ODP_NESTED) {
6136
0
        retval = -EINVAL;
6137
0
    } else {
6138
0
        retval = parse_odp_key_mask_attr__(context, s, key, mask);
6139
0
    }
6140
6141
0
    context->depth--;
6142
6143
0
    return retval;
6144
0
}
6145
6146
static int
6147
parse_odp_key_mask_attr__(struct parse_odp_context *context, const char *s,
6148
                          struct ofpbuf *key, struct ofpbuf *mask)
6149
0
{
6150
0
    SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
6151
0
    SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
6152
0
    SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
6153
0
                             OVS_KEY_ATTR_RECIRC_ID);
6154
0
    SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
6155
6156
0
    SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
6157
0
    SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
6158
0
    SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
6159
0
    SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
6160
6161
0
    SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) {
6162
0
        SCAN_FIELD("src=", ipv4, ipv4_src);
6163
0
        SCAN_FIELD("dst=", ipv4, ipv4_dst);
6164
0
        SCAN_FIELD("proto=", u8, ipv4_proto);
6165
0
        SCAN_FIELD("tp_src=", be16, src_port);
6166
0
        SCAN_FIELD("tp_dst=", be16, dst_port);
6167
0
    } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
6168
6169
0
    SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) {
6170
0
        SCAN_FIELD("src=", in6_addr, ipv6_src);
6171
0
        SCAN_FIELD("dst=", in6_addr, ipv6_dst);
6172
0
        SCAN_FIELD("proto=", u8, ipv6_proto);
6173
0
        SCAN_FIELD("tp_src=", be16, src_port);
6174
0
        SCAN_FIELD("tp_dst=", be16, dst_port);
6175
0
    } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
6176
6177
0
    SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
6178
0
        SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
6179
0
        SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
6180
0
        SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
6181
0
        SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
6182
0
        SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
6183
0
        SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
6184
0
        SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
6185
0
        SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
6186
0
        SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
6187
0
        SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata, erspan_metadata,
6188
0
                               erspan_to_attr);
6189
0
        SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
6190
0
        SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
6191
0
                               geneve_to_attr);
6192
0
        SCAN_FIELD_NESTED_FUNC("gtpu(", struct gtpu_metadata, gtpu_metadata,
6193
0
                               gtpu_to_attr);
6194
0
        SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
6195
0
    } SCAN_END_NESTED();
6196
6197
0
    SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
6198
6199
0
    SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
6200
0
        SCAN_FIELD("src=", eth, eth_src);
6201
0
        SCAN_FIELD("dst=", eth, eth_dst);
6202
0
    } SCAN_END(OVS_KEY_ATTR_ETHERNET);
6203
6204
0
    SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
6205
0
                    { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
6206
0
        SCAN_FIELD("vid=", vid, tci);
6207
0
        SCAN_FIELD("pcp=", pcp, tci);
6208
0
        SCAN_FIELD("cfi=", cfi, tci);
6209
0
    } SCAN_END(OVS_KEY_ATTR_VLAN);
6210
6211
0
    SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
6212
6213
0
    SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
6214
0
        SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
6215
0
        SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
6216
0
        SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
6217
0
        SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
6218
0
    } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
6219
6220
0
    SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
6221
0
        SCAN_FIELD("src=", ipv4, ipv4_src);
6222
0
        SCAN_FIELD("dst=", ipv4, ipv4_dst);
6223
0
        SCAN_FIELD("proto=", u8, ipv4_proto);
6224
0
        SCAN_FIELD("tos=", u8, ipv4_tos);
6225
0
        SCAN_FIELD("ttl=", u8, ipv4_ttl);
6226
0
        SCAN_FIELD("frag=", frag, ipv4_frag);
6227
0
    } SCAN_END(OVS_KEY_ATTR_IPV4);
6228
6229
0
    SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
6230
0
        SCAN_FIELD("src=", in6_addr, ipv6_src);
6231
0
        SCAN_FIELD("dst=", in6_addr, ipv6_dst);
6232
0
        SCAN_FIELD("label=", ipv6_label, ipv6_label);
6233
0
        SCAN_FIELD("proto=", u8, ipv6_proto);
6234
0
        SCAN_FIELD("tclass=", u8, ipv6_tclass);
6235
0
        SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
6236
0
        SCAN_FIELD("frag=", frag, ipv6_frag);
6237
0
    } SCAN_END(OVS_KEY_ATTR_IPV6);
6238
6239
0
    SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
6240
0
        SCAN_FIELD("src=", be16, tcp_src);
6241
0
        SCAN_FIELD("dst=", be16, tcp_dst);
6242
0
    } SCAN_END(OVS_KEY_ATTR_TCP);
6243
6244
0
    SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
6245
6246
0
    SCAN_BEGIN("udp(", struct ovs_key_udp) {
6247
0
        SCAN_FIELD("src=", be16, udp_src);
6248
0
        SCAN_FIELD("dst=", be16, udp_dst);
6249
0
    } SCAN_END(OVS_KEY_ATTR_UDP);
6250
6251
0
    SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
6252
0
        SCAN_FIELD("src=", be16, sctp_src);
6253
0
        SCAN_FIELD("dst=", be16, sctp_dst);
6254
0
    } SCAN_END(OVS_KEY_ATTR_SCTP);
6255
6256
0
    SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
6257
0
        SCAN_FIELD("type=", u8, icmp_type);
6258
0
        SCAN_FIELD("code=", u8, icmp_code);
6259
0
    } SCAN_END(OVS_KEY_ATTR_ICMP);
6260
6261
0
    SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
6262
0
        SCAN_FIELD("type=", u8, icmpv6_type);
6263
0
        SCAN_FIELD("code=", u8, icmpv6_code);
6264
0
    } SCAN_END(OVS_KEY_ATTR_ICMPV6);
6265
6266
0
    SCAN_BEGIN("arp(", struct ovs_key_arp) {
6267
0
        SCAN_FIELD("sip=", ipv4, arp_sip);
6268
0
        SCAN_FIELD("tip=", ipv4, arp_tip);
6269
0
        SCAN_FIELD("op=", be16, arp_op);
6270
0
        SCAN_FIELD("sha=", eth, arp_sha);
6271
0
        SCAN_FIELD("tha=", eth, arp_tha);
6272
0
    } SCAN_END(OVS_KEY_ATTR_ARP);
6273
6274
0
    SCAN_BEGIN("nd(", struct ovs_key_nd) {
6275
0
        SCAN_FIELD("target=", in6_addr, nd_target);
6276
0
        SCAN_FIELD("sll=", eth, nd_sll);
6277
0
        SCAN_FIELD("tll=", eth, nd_tll);
6278
0
    } SCAN_END(OVS_KEY_ATTR_ND);
6279
6280
0
    SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions) {
6281
0
        SCAN_FIELD("nd_reserved=", be32, nd_reserved);
6282
0
        SCAN_FIELD("nd_options_type=", u8, nd_options_type);
6283
0
    } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS);
6284
6285
0
    struct packet_type {
6286
0
        ovs_be16 ns;
6287
0
        ovs_be16 id;
6288
0
    };
6289
0
    SCAN_BEGIN("packet_type(", struct packet_type) {
6290
0
        SCAN_FIELD("ns=", be16, ns);
6291
0
        SCAN_FIELD("id=", be16, id);
6292
0
    } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE);
6293
6294
    /* nsh is nested, it needs special process */
6295
0
    int ret = parse_odp_nsh_key_mask_attr(s, key, mask);
6296
0
    if (ret < 0) {
6297
0
        return ret;
6298
0
    } else {
6299
0
        s += ret;
6300
0
    }
6301
6302
    /* Encap open-coded. */
6303
0
    if (!strncmp(s, "encap(", 6)) {
6304
0
        const char *start = s;
6305
0
        size_t encap, encap_mask = 0;
6306
6307
0
        encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
6308
0
        if (mask) {
6309
0
            encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
6310
0
        }
6311
6312
0
        s += 6;
6313
0
        for (;;) {
6314
0
            int retval;
6315
6316
0
            s += strspn(s, delimiters);
6317
0
            if (!*s) {
6318
0
                return -EINVAL;
6319
0
            } else if (*s == ')') {
6320
0
                break;
6321
0
            }
6322
6323
0
            retval = parse_odp_key_mask_attr(context, s, key, mask);
6324
0
            if (retval < 0) {
6325
0
                return retval;
6326
0
            }
6327
6328
0
            if (nl_attr_oversized(key->size - encap - NLA_HDRLEN)) {
6329
0
                return -E2BIG;
6330
0
            }
6331
0
            s += retval;
6332
0
        }
6333
0
        s++;
6334
6335
0
        nl_msg_end_nested(key, encap);
6336
0
        if (mask) {
6337
0
            nl_msg_end_nested(mask, encap_mask);
6338
0
        }
6339
6340
0
        return s - start;
6341
0
    }
6342
6343
0
    return -EINVAL;
6344
0
}
6345
6346
/* Parses the string representation of a datapath flow key, in the format
6347
 * output by odp_flow_key_format().  Returns 0 if successful, otherwise a
6348
 * positive errno value.  On success, stores NULL into '*errorp' and the flow
6349
 * key is appended to 'key' as a series of Netlink attributes.  On failure,
6350
 * stores a malloc()'d error message in '*errorp' without changing the data in
6351
 * 'key'.  Either way, 'key''s data might be reallocated.
6352
 *
6353
 * If 'port_names' is nonnull, it points to an simap that maps from a port name
6354
 * to a port number.  (Port names may be used instead of port numbers in
6355
 * in_port.)
6356
 *
6357
 * On success, the attributes appended to 'key' are individually syntactically
6358
 * valid, but they may not be valid as a sequence.  'key' might, for example,
6359
 * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
6360
int
6361
odp_flow_from_string(const char *s, const struct simap *port_names,
6362
                     struct ofpbuf *key, struct ofpbuf *mask,
6363
                     char **errorp)
6364
0
{
6365
0
    if (errorp) {
6366
0
        *errorp = NULL;
6367
0
    }
6368
6369
0
    const size_t old_size = key->size;
6370
0
    struct parse_odp_context context = (struct parse_odp_context) {
6371
0
        .port_names = port_names,
6372
0
    };
6373
0
    for (;;) {
6374
0
        int retval;
6375
6376
0
        s += strspn(s, delimiters);
6377
0
        if (!*s) {
6378
0
            return 0;
6379
0
        }
6380
6381
        /* Skip UFID. */
6382
0
        ovs_u128 ufid;
6383
0
        retval = odp_ufid_from_string(s, &ufid);
6384
0
        if (retval < 0) {
6385
0
            if (errorp) {
6386
0
                *errorp = xasprintf("syntax error at %s", s);
6387
0
            }
6388
0
            key->size = old_size;
6389
0
            return -retval;
6390
0
        } else if (retval > 0) {
6391
0
            s += retval;
6392
0
            s += s[0] == ' ' ? 1 : 0;
6393
0
        }
6394
6395
0
        retval = parse_odp_key_mask_attr(&context, s, key, mask);
6396
6397
0
        if (retval >= 0) {
6398
0
            if (nl_attr_oversized(key->size - NLA_HDRLEN)) {
6399
0
                retval = -E2BIG;
6400
0
            } else if (mask && nl_attr_oversized(mask->size - NLA_HDRLEN)) {
6401
0
                retval = -E2BIG;
6402
0
            }
6403
0
        }
6404
6405
0
        if (retval < 0) {
6406
0
            if (errorp) {
6407
0
                *errorp = xasprintf("syntax error at %s", s);
6408
0
            }
6409
0
            key->size = old_size;
6410
0
            return -retval;
6411
0
        }
6412
0
        s += retval;
6413
0
    }
6414
6415
0
    return 0;
6416
0
}
6417
6418
static uint8_t
6419
ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
6420
0
{
6421
0
    if (is_mask) {
6422
        /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
6423
         * not a set of flags or bitfields. Hence, if the struct flow nw_frag
6424
         * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
6425
         * must use a zero mask for the netlink frag field, and all ones mask
6426
         * otherwise. */
6427
0
        return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
6428
0
    }
6429
0
    return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
6430
0
        : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
6431
0
        : OVS_FRAG_TYPE_FIRST;
6432
0
}
6433
6434
static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
6435
static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
6436
static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
6437
                         bool is_mask);
6438
static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
6439
                         bool is_mask);
6440
static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
6441
                         bool is_mask);
6442
static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
6443
                         bool is_mask);
6444
static void get_arp_key(const struct flow *, struct ovs_key_arp *);
6445
static void put_arp_key(const struct ovs_key_arp *, struct flow *);
6446
static void get_nd_key(const struct flow *, struct ovs_key_nd *);
6447
static void put_nd_key(const struct ovs_key_nd *, struct flow *);
6448
6449
/* These share the same layout. */
6450
union ovs_key_tp {
6451
    struct ovs_key_tcp tcp;
6452
    struct ovs_key_udp udp;
6453
    struct ovs_key_sctp sctp;
6454
};
6455
6456
static void get_tp_key(const struct flow *, union ovs_key_tp *);
6457
static void put_tp_key(const union ovs_key_tp *, struct flow *);
6458
6459
static void
6460
odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
6461
                         bool export_mask, struct ofpbuf *buf)
6462
0
{
6463
    /* New "struct flow" fields that are visible to the datapath (including all
6464
     * data fields) should be translated into equivalent datapath flow fields
6465
     * here (you will have to add a OVS_KEY_ATTR_* for them). */
6466
0
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
6467
6468
0
    struct ovs_key_ethernet *eth_key;
6469
0
    size_t encap[FLOW_MAX_VLAN_HEADERS] = {0};
6470
0
    size_t max_vlans;
6471
0
    const struct flow *flow = parms->flow;
6472
0
    const struct flow *mask = parms->mask;
6473
0
    const struct flow *data = export_mask ? mask : flow;
6474
6475
0
    if (parms->support.recirc) {
6476
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
6477
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
6478
0
    }
6479
6480
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
6481
6482
0
    if (flow_tnl_dst_is_set(&flow->tunnel) ||
6483
0
        flow_tnl_src_is_set(&flow->tunnel) || export_mask) {
6484
0
        tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
6485
0
                        parms->key_buf, NULL);
6486
0
    }
6487
6488
    /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
6489
     * is not the magical value "ODPP_NONE". */
6490
0
    if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
6491
0
        nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
6492
0
    }
6493
6494
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
6495
6496
0
    if (parms->support.ct_state) {
6497
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
6498
0
                       ovs_to_odp_ct_state(data->ct_state));
6499
0
    }
6500
0
    if (parms->support.ct_zone) {
6501
0
        nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
6502
0
    }
6503
0
    if (parms->support.ct_mark) {
6504
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
6505
0
    }
6506
0
    if (parms->support.ct_label) {
6507
0
        nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
6508
0
                          sizeof(data->ct_label));
6509
0
    }
6510
0
    if (flow->ct_nw_proto) {
6511
0
        if (parms->support.ct_orig_tuple
6512
0
            && flow->dl_type == htons(ETH_TYPE_IP)) {
6513
0
            struct ovs_key_ct_tuple_ipv4 *ct;
6514
6515
            /* 'struct ovs_key_ct_tuple_ipv4' has padding, clear it. */
6516
0
            ct = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
6517
0
                                        sizeof *ct);
6518
0
            ct->ipv4_src = data->ct_nw_src;
6519
0
            ct->ipv4_dst = data->ct_nw_dst;
6520
0
            ct->src_port = data->ct_tp_src;
6521
0
            ct->dst_port = data->ct_tp_dst;
6522
0
            ct->ipv4_proto = data->ct_nw_proto;
6523
0
        } else if (parms->support.ct_orig_tuple6
6524
0
                   && flow->dl_type == htons(ETH_TYPE_IPV6)) {
6525
0
            struct ovs_key_ct_tuple_ipv6 *ct;
6526
6527
            /* 'struct ovs_key_ct_tuple_ipv6' has padding, clear it. */
6528
0
            ct = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
6529
0
                                        sizeof *ct);
6530
0
            ct->ipv6_src = data->ct_ipv6_src;
6531
0
            ct->ipv6_dst = data->ct_ipv6_dst;
6532
0
            ct->src_port = data->ct_tp_src;
6533
0
            ct->dst_port = data->ct_tp_dst;
6534
0
            ct->ipv6_proto = data->ct_nw_proto;
6535
0
        }
6536
0
    }
6537
6538
0
    nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type);
6539
6540
0
    if (OVS_UNLIKELY(parms->probe)) {
6541
0
        max_vlans = FLOW_MAX_VLAN_HEADERS;
6542
0
    } else {
6543
0
        max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit);
6544
0
    }
6545
6546
    /* Conditionally add L2 attributes for Ethernet packets */
6547
0
    if (flow->packet_type == htonl(PT_ETH)) {
6548
0
        eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
6549
0
                                           sizeof *eth_key);
6550
0
        get_ethernet_key(data, eth_key);
6551
6552
0
        for (int encaps = 0; encaps < max_vlans; encaps++) {
6553
0
            ovs_be16 tpid = flow->vlans[encaps].tpid;
6554
6555
0
            if (flow->vlans[encaps].tci == htons(0)) {
6556
0
                if (eth_type_vlan(flow->dl_type)) {
6557
                    /* If VLAN was truncated the tpid is in dl_type */
6558
0
                    tpid = flow->dl_type;
6559
0
                } else {
6560
0
                    break;
6561
0
                }
6562
0
            }
6563
6564
0
            if (export_mask) {
6565
0
                nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6566
0
            } else {
6567
0
                nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid);
6568
0
            }
6569
0
            nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci);
6570
0
            encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
6571
0
            if (flow->vlans[encaps].tci == htons(0)) {
6572
0
                goto unencap;
6573
0
            }
6574
0
        }
6575
0
    }
6576
6577
0
    if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
6578
        /* For backwards compatibility with kernels that don't support
6579
         * wildcarding, the following convention is used to encode the
6580
         * OVS_KEY_ATTR_ETHERTYPE for key and mask:
6581
         *
6582
         *   key      mask    matches
6583
         * -------- --------  -------
6584
         *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
6585
         *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
6586
         *  <none>   0xffff   Any non-Ethernet II frame (except valid
6587
         *                    802.3 SNAP packet with valid eth_type).
6588
         */
6589
0
        if (export_mask) {
6590
0
            nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6591
0
        }
6592
0
        goto unencap;
6593
0
    }
6594
6595
0
    nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
6596
6597
0
    if (eth_type_vlan(flow->dl_type)) {
6598
0
        goto unencap;
6599
0
    }
6600
6601
0
    if (flow->dl_type == htons(ETH_TYPE_IP)) {
6602
0
        struct ovs_key_ipv4 *ipv4_key;
6603
6604
0
        ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
6605
0
                                            sizeof *ipv4_key);
6606
0
        get_ipv4_key(data, ipv4_key, export_mask);
6607
0
    } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
6608
0
        struct ovs_key_ipv6 *ipv6_key;
6609
6610
0
        ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
6611
0
                                            sizeof *ipv6_key);
6612
0
        get_ipv6_key(data, ipv6_key, export_mask);
6613
0
    } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
6614
0
               flow->dl_type == htons(ETH_TYPE_RARP)) {
6615
0
        struct ovs_key_arp *arp_key;
6616
6617
0
        arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
6618
0
                                           sizeof *arp_key);
6619
0
        get_arp_key(data, arp_key);
6620
0
    } else if (eth_type_mpls(flow->dl_type)) {
6621
0
        struct ovs_key_mpls *mpls_key;
6622
0
        int i, n;
6623
6624
0
        n = flow_count_mpls_labels(flow, NULL);
6625
0
        if (export_mask) {
6626
0
            n = MIN(n, parms->support.max_mpls_depth);
6627
0
        }
6628
0
        mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
6629
0
                                            n * sizeof *mpls_key);
6630
0
        for (i = 0; i < n; i++) {
6631
0
            mpls_key[i].mpls_lse = data->mpls_lse[i];
6632
0
        }
6633
0
    } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
6634
0
        nsh_key_to_attr(buf, &data->nsh, NULL, 0, export_mask);
6635
0
    }
6636
6637
0
    if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6638
0
        if (flow->nw_proto == IPPROTO_TCP) {
6639
0
            union ovs_key_tp *tcp_key;
6640
6641
0
            tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
6642
0
                                               sizeof *tcp_key);
6643
0
            get_tp_key(data, tcp_key);
6644
0
            if (data->tcp_flags || (mask && mask->tcp_flags)) {
6645
0
                nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
6646
0
            }
6647
0
        } else if (flow->nw_proto == IPPROTO_UDP) {
6648
0
            union ovs_key_tp *udp_key;
6649
6650
0
            udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
6651
0
                                               sizeof *udp_key);
6652
0
            get_tp_key(data, udp_key);
6653
0
        } else if (flow->nw_proto == IPPROTO_SCTP) {
6654
0
            union ovs_key_tp *sctp_key;
6655
6656
0
            sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
6657
0
                                               sizeof *sctp_key);
6658
0
            get_tp_key(data, sctp_key);
6659
0
        } else if (flow->dl_type == htons(ETH_TYPE_IP)
6660
0
                && flow->nw_proto == IPPROTO_ICMP) {
6661
0
            struct ovs_key_icmp *icmp_key;
6662
6663
0
            icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
6664
0
                                                sizeof *icmp_key);
6665
0
            icmp_key->icmp_type = ntohs(data->tp_src);
6666
0
            icmp_key->icmp_code = ntohs(data->tp_dst);
6667
0
        } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
6668
0
                && flow->nw_proto == IPPROTO_ICMPV6) {
6669
0
            struct ovs_key_icmpv6 *icmpv6_key;
6670
6671
0
            icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
6672
0
                                                  sizeof *icmpv6_key);
6673
0
            icmpv6_key->icmpv6_type = ntohs(data->tp_src);
6674
0
            icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
6675
6676
0
            if (is_nd(flow, NULL)
6677
                /* Even though 'tp_src' is 16 bits wide, ICMP type is 8 bits
6678
                 * wide.  Therefore, an exact match looks like htons(0xff),
6679
                 * not htons(0xffff).  See xlate_wc_finish() for details. */
6680
0
                && (!export_mask || data->tp_src == htons(0xff))) {
6681
0
                struct ovs_key_nd *nd_key;
6682
0
                nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
6683
0
                                                    sizeof *nd_key);
6684
0
                nd_key->nd_target = data->nd_target;
6685
0
                nd_key->nd_sll = data->arp_sha;
6686
0
                nd_key->nd_tll = data->arp_tha;
6687
6688
                /* Add ND Extensions Attr only if supported and reserved field
6689
                 * or options type is set. */
6690
0
                if (parms->support.nd_ext) {
6691
0
                    struct ovs_key_nd_extensions *nd_ext_key;
6692
6693
0
                    if (data->igmp_group_ip4 != 0 || data->tcp_flags != 0) {
6694
                        /* 'struct ovs_key_nd_extensions' has padding,
6695
                         * clear it. */
6696
0
                        nd_ext_key = nl_msg_put_unspec_zero(buf,
6697
0
                                            OVS_KEY_ATTR_ND_EXTENSIONS,
6698
0
                                            sizeof *nd_ext_key);
6699
0
                        nd_ext_key->nd_reserved = data->igmp_group_ip4;
6700
0
                        nd_ext_key->nd_options_type = ntohs(data->tcp_flags);
6701
0
                    }
6702
0
                }
6703
0
            }
6704
0
        }
6705
0
    }
6706
6707
0
unencap:
6708
0
    for (int encaps = max_vlans - 1; encaps >= 0; encaps--) {
6709
0
        if (encap[encaps]) {
6710
0
            nl_msg_end_nested(buf, encap[encaps]);
6711
0
        }
6712
0
    }
6713
0
}
6714
6715
/* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
6716
 *
6717
 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6718
 * capable of being expanded to allow for that much space. */
6719
void
6720
odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
6721
                       struct ofpbuf *buf)
6722
0
{
6723
0
    odp_flow_key_from_flow__(parms, false, buf);
6724
0
}
6725
6726
/* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
6727
 * 'buf'.
6728
 *
6729
 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6730
 * capable of being expanded to allow for that much space. */
6731
void
6732
odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
6733
                       struct ofpbuf *buf)
6734
0
{
6735
0
    odp_flow_key_from_flow__(parms, true, buf);
6736
0
}
6737
6738
/* Generate ODP flow key from the given packet metadata */
6739
void
6740
odp_key_from_dp_packet(struct ofpbuf *buf, const struct dp_packet *packet)
6741
0
{
6742
0
    const struct pkt_metadata *md = &packet->md;
6743
6744
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
6745
6746
0
    if (md->dp_hash) {
6747
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, md->dp_hash);
6748
0
    }
6749
6750
0
    if (flow_tnl_dst_is_set(&md->tunnel)) {
6751
0
        tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL, NULL);
6752
0
    }
6753
6754
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
6755
6756
0
    if (md->ct_state) {
6757
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
6758
0
                       ovs_to_odp_ct_state(md->ct_state));
6759
0
        if (md->ct_zone) {
6760
0
            nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
6761
0
        }
6762
0
        if (md->ct_mark) {
6763
0
            nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
6764
0
        }
6765
0
        if (!ovs_u128_is_zero(md->ct_label)) {
6766
0
            nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
6767
0
                              sizeof(md->ct_label));
6768
0
        }
6769
0
        if (md->ct_orig_tuple_ipv6) {
6770
0
            if (md->ct_orig_tuple.ipv6.ipv6_proto) {
6771
0
                nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
6772
0
                                  &md->ct_orig_tuple.ipv6,
6773
0
                                  sizeof md->ct_orig_tuple.ipv6);
6774
0
            }
6775
0
        } else {
6776
0
            if (md->ct_orig_tuple.ipv4.ipv4_proto) {
6777
0
                nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
6778
0
                                  &md->ct_orig_tuple.ipv4,
6779
0
                                  sizeof md->ct_orig_tuple.ipv4);
6780
0
            }
6781
0
        }
6782
0
    }
6783
6784
    /* Add an ingress port attribute if 'odp_in_port' is not the magical
6785
     * value "ODPP_NONE". */
6786
0
    if (md->in_port.odp_port != ODPP_NONE) {
6787
0
        nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
6788
0
    }
6789
6790
    /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6791
0
    if (pt_ns(packet->packet_type) == OFPHTN_ETHERTYPE) {
6792
0
        nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE,
6793
0
                        pt_ns_type_be(packet->packet_type));
6794
0
    }
6795
0
}
6796
6797
/* Generate packet metadata from the given ODP flow key. */
6798
void
6799
odp_key_to_dp_packet(const struct nlattr *key, size_t key_len,
6800
                     struct dp_packet *packet)
6801
0
{
6802
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6803
0
    const struct nlattr *nla;
6804
0
    struct pkt_metadata *md = &packet->md;
6805
0
    ovs_be32 packet_type = htonl(PT_UNKNOWN);
6806
0
    ovs_be16 ethertype = 0;
6807
0
    size_t left;
6808
6809
0
    pkt_metadata_init(md, ODPP_NONE);
6810
6811
0
    NL_ATTR_FOR_EACH (nla, left, key, key_len) {
6812
0
        enum ovs_key_attr type = nl_attr_type(nla);
6813
0
        size_t len = nl_attr_get_size(nla);
6814
0
        int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
6815
0
                                            OVS_KEY_ATTR_MAX, type);
6816
6817
0
        if (len != expected_len && expected_len >= 0) {
6818
0
            continue;
6819
0
        }
6820
6821
0
        switch (type) {
6822
0
        case OVS_KEY_ATTR_RECIRC_ID:
6823
0
            md->recirc_id = nl_attr_get_u32(nla);
6824
0
            break;
6825
0
        case OVS_KEY_ATTR_DP_HASH:
6826
0
            md->dp_hash = nl_attr_get_u32(nla);
6827
0
            break;
6828
0
        case OVS_KEY_ATTR_PRIORITY:
6829
0
            md->skb_priority = nl_attr_get_u32(nla);
6830
0
            break;
6831
0
        case OVS_KEY_ATTR_SKB_MARK:
6832
0
            md->pkt_mark = nl_attr_get_u32(nla);
6833
0
            break;
6834
0
        case OVS_KEY_ATTR_CT_STATE:
6835
0
            md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
6836
0
            break;
6837
0
        case OVS_KEY_ATTR_CT_ZONE:
6838
0
            md->ct_zone = nl_attr_get_u16(nla);
6839
0
            break;
6840
0
        case OVS_KEY_ATTR_CT_MARK:
6841
0
            md->ct_mark = nl_attr_get_u32(nla);
6842
0
            break;
6843
0
        case OVS_KEY_ATTR_CT_LABELS: {
6844
0
            md->ct_label = nl_attr_get_u128(nla);
6845
0
            break;
6846
0
        }
6847
0
        case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
6848
0
            const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(nla);
6849
0
            md->ct_orig_tuple.ipv4 = *ct;
6850
0
            md->ct_orig_tuple_ipv6 = false;
6851
0
            break;
6852
0
        }
6853
0
        case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
6854
0
            const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(nla);
6855
6856
0
            md->ct_orig_tuple.ipv6 = *ct;
6857
0
            md->ct_orig_tuple_ipv6 = true;
6858
0
            break;
6859
0
        }
6860
0
        case OVS_KEY_ATTR_TUNNEL: {
6861
0
            enum odp_key_fitness res;
6862
6863
0
            res = odp_tun_key_from_attr(nla, &md->tunnel, NULL);
6864
0
            if (res == ODP_FIT_ERROR) {
6865
0
                memset(&md->tunnel, 0, sizeof md->tunnel);
6866
0
            }
6867
0
            break;
6868
0
        }
6869
0
        case OVS_KEY_ATTR_IN_PORT:
6870
0
            md->in_port.odp_port = nl_attr_get_odp_port(nla);
6871
0
            break;
6872
0
        case OVS_KEY_ATTR_ETHERNET:
6873
            /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6874
0
            packet_type = htonl(PT_ETH);
6875
0
            break;
6876
0
        case OVS_KEY_ATTR_ETHERTYPE:
6877
0
            ethertype = nl_attr_get_be16(nla);
6878
0
            break;
6879
0
        case OVS_KEY_ATTR_UNSPEC:
6880
0
        case OVS_KEY_ATTR_ENCAP:
6881
0
        case OVS_KEY_ATTR_VLAN:
6882
0
        case OVS_KEY_ATTR_IPV4:
6883
0
        case OVS_KEY_ATTR_IPV6:
6884
0
        case OVS_KEY_ATTR_TCP:
6885
0
        case OVS_KEY_ATTR_UDP:
6886
0
        case OVS_KEY_ATTR_ICMP:
6887
0
        case OVS_KEY_ATTR_ICMPV6:
6888
0
        case OVS_KEY_ATTR_ARP:
6889
0
        case OVS_KEY_ATTR_ND:
6890
0
        case OVS_KEY_ATTR_ND_EXTENSIONS:
6891
0
        case OVS_KEY_ATTR_SCTP:
6892
0
        case OVS_KEY_ATTR_TCP_FLAGS:
6893
0
        case OVS_KEY_ATTR_MPLS:
6894
0
        case OVS_KEY_ATTR_PACKET_TYPE:
6895
0
        case OVS_KEY_ATTR_NSH:
6896
0
        case OVS_KEY_ATTR_TUNNEL_INFO:
6897
0
        case __OVS_KEY_ATTR_MAX:
6898
0
        default:
6899
0
            break;
6900
0
        }
6901
0
    }
6902
6903
0
    if (packet_type == htonl(PT_ETH)) {
6904
0
        packet->packet_type = htonl(PT_ETH);
6905
0
    } else if (packet_type == htonl(PT_UNKNOWN) && ethertype != 0) {
6906
0
        packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
6907
0
                                             ntohs(ethertype));
6908
0
    } else {
6909
0
        VLOG_ERR_RL(&rl, "Packet without ETHERTYPE. Unknown packet_type.");
6910
0
    }
6911
0
}
6912
6913
/* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'.
6914
 * Generated value has format of random UUID. */
6915
void
6916
odp_flow_key_hash(const void *key, size_t key_len, ovs_u128 *hash)
6917
0
{
6918
0
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6919
0
    static uint32_t secret;
6920
6921
0
    if (ovsthread_once_start(&once)) {
6922
0
        secret = random_uint32();
6923
0
        ovsthread_once_done(&once);
6924
0
    }
6925
0
    hash_bytes128(key, key_len, secret, hash);
6926
0
    uuid_set_bits_v4((struct uuid *)hash);
6927
0
}
6928
6929
static void
6930
log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
6931
                       uint64_t attrs, int out_of_range_attr,
6932
                       const struct nlattr *key, size_t key_len)
6933
0
{
6934
0
    struct ds s;
6935
0
    int i;
6936
6937
0
    if (VLOG_DROP_DBG(rl)) {
6938
0
        return;
6939
0
    }
6940
6941
0
    ds_init(&s);
6942
0
    for (i = 0; i < 64; i++) {
6943
0
        if (attrs & (UINT64_C(1) << i)) {
6944
0
            char namebuf[OVS_KEY_ATTR_BUFSIZE];
6945
6946
0
            ds_put_format(&s, " %s",
6947
0
                          ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
6948
0
        }
6949
0
    }
6950
0
    if (out_of_range_attr) {
6951
0
        ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
6952
0
    }
6953
6954
0
    ds_put_cstr(&s, ": ");
6955
0
    odp_flow_key_format(key, key_len, &s);
6956
6957
0
    VLOG_DBG("%s:%s", title, ds_cstr(&s));
6958
0
    ds_destroy(&s);
6959
0
}
6960
6961
static uint8_t
6962
odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
6963
0
{
6964
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6965
6966
0
    if (is_mask) {
6967
0
        return odp_frag ? FLOW_NW_FRAG_MASK : 0;
6968
0
    }
6969
6970
0
    if (odp_frag > OVS_FRAG_TYPE_LATER) {
6971
0
        VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
6972
0
        return 0xff; /* Error. */
6973
0
    }
6974
6975
0
    return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
6976
0
        : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
6977
0
        :  FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
6978
0
}
6979
6980
/* Parses the attributes in the 'key_len' bytes of 'key' into 'attrs', which
6981
 * must have OVS_KEY_ATTR_MAX + 1 elements.  Stores each attribute in 'key'
6982
 * into the corresponding element of 'attrs'.
6983
 *
6984
 * Stores a bitmask of the attributes' indexes found in 'key' into
6985
 * '*present_attrsp'.
6986
 *
6987
 * If an attribute beyond OVS_KEY_ATTR_MAX is found, stores its attribute type
6988
 * (or one of them, if more than one) into '*out_of_range_attrp', otherwise 0.
6989
 *
6990
 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6991
 * error message in '*errorp'. */
6992
static bool
6993
parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
6994
                   const struct nlattr *attrs[], uint64_t *present_attrsp,
6995
                   int *out_of_range_attrp, char **errorp)
6996
0
{
6997
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
6998
0
    const struct nlattr *nla;
6999
0
    uint64_t present_attrs;
7000
0
    size_t left;
7001
7002
0
    BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
7003
0
    present_attrs = 0;
7004
0
    *out_of_range_attrp = 0;
7005
0
    NL_ATTR_FOR_EACH (nla, left, key, key_len) {
7006
0
        uint16_t type = nl_attr_type(nla);
7007
0
        size_t len = nl_attr_get_size(nla);
7008
0
        int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
7009
0
                                            OVS_KEY_ATTR_MAX, type);
7010
7011
0
        if (len != expected_len && expected_len >= 0) {
7012
0
            char namebuf[OVS_KEY_ATTR_BUFSIZE];
7013
7014
0
            odp_parse_error(&rl, errorp, "attribute %s has length %"PRIuSIZE" "
7015
0
                            "but should have length %d",
7016
0
                            ovs_key_attr_to_string(type, namebuf,
7017
0
                                                   sizeof namebuf),
7018
0
                            len, expected_len);
7019
0
            return false;
7020
0
        }
7021
7022
0
        if (type > OVS_KEY_ATTR_MAX) {
7023
0
            *out_of_range_attrp = type;
7024
0
        } else {
7025
0
            if (present_attrs & (UINT64_C(1) << type)) {
7026
0
                char namebuf[OVS_KEY_ATTR_BUFSIZE];
7027
7028
0
                odp_parse_error(&rl, errorp,
7029
0
                                "duplicate %s attribute in flow key",
7030
0
                                ovs_key_attr_to_string(type, namebuf,
7031
0
                                                       sizeof namebuf));
7032
0
                return false;
7033
0
            }
7034
7035
0
            present_attrs |= UINT64_C(1) << type;
7036
0
            attrs[type] = nla;
7037
0
        }
7038
0
    }
7039
0
    if (left) {
7040
0
        odp_parse_error(&rl, errorp, "trailing garbage in flow key");
7041
0
        return false;
7042
0
    }
7043
7044
0
    *present_attrsp = present_attrs;
7045
0
    return true;
7046
0
}
7047
7048
static enum odp_key_fitness
7049
check_expectations(uint64_t present_attrs, int out_of_range_attr,
7050
                   uint64_t expected_attrs,
7051
                   const struct nlattr *key, size_t key_len)
7052
0
{
7053
0
    uint64_t missing_attrs;
7054
0
    uint64_t extra_attrs;
7055
7056
0
    missing_attrs = expected_attrs & ~present_attrs;
7057
0
    if (missing_attrs) {
7058
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7059
0
        log_odp_key_attributes(&rl, "expected but not present",
7060
0
                               missing_attrs, 0, key, key_len);
7061
0
        return ODP_FIT_TOO_LITTLE;
7062
0
    }
7063
7064
0
    extra_attrs = present_attrs & ~expected_attrs;
7065
0
    if (extra_attrs || out_of_range_attr) {
7066
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7067
0
        log_odp_key_attributes(&rl, "present but not expected",
7068
0
                               extra_attrs, out_of_range_attr, key, key_len);
7069
0
        return ODP_FIT_TOO_MUCH;
7070
0
    }
7071
7072
0
    return ODP_FIT_PERFECT;
7073
0
}
7074
7075
/* Initializes 'flow->dl_type' based on the attributes in 'attrs', in which the
7076
 * attributes in the bit-mask 'present_attrs' are present.  Returns true if
7077
 * successful, false on failure.
7078
 *
7079
 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
7080
 * consulted.  'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
7081
 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
7082
 * previously parsed flow key.
7083
 *
7084
 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
7085
 * error message in '*errorp'. */
7086
static bool
7087
parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
7088
                uint64_t present_attrs, uint64_t *expected_attrs,
7089
                struct flow *flow, const struct flow *src_flow,
7090
                char **errorp)
7091
0
{
7092
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7093
0
    bool is_mask = flow != src_flow;
7094
7095
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
7096
0
        flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
7097
0
        if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
7098
0
            odp_parse_error(&rl, errorp,
7099
0
                            "invalid Ethertype %"PRIu16" in flow key",
7100
0
                            ntohs(flow->dl_type));
7101
0
            return false;
7102
0
        }
7103
0
        if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
7104
0
            flow->dl_type != htons(0xffff)) {
7105
0
            odp_parse_error(&rl, errorp, "can't bitwise match non-Ethernet II "
7106
0
                            "\"Ethertype\" %#"PRIx16" (with mask %#"PRIx16")",
7107
0
                            ntohs(src_flow->dl_type), ntohs(flow->dl_type));
7108
0
            return false;
7109
0
        }
7110
0
        *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
7111
0
    } else {
7112
0
        if (!is_mask) {
7113
            /* Default ethertype for well-known L3 packets. */
7114
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
7115
0
                flow->dl_type = htons(ETH_TYPE_IP);
7116
0
            } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
7117
0
                flow->dl_type = htons(ETH_TYPE_IPV6);
7118
0
            } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7119
0
                flow->dl_type = htons(ETH_TYPE_MPLS);
7120
0
            } else {
7121
0
                flow->dl_type = htons(FLOW_DL_TYPE_NONE);
7122
0
            }
7123
0
        } else if (src_flow->packet_type != htonl(PT_ETH)) {
7124
            /* dl_type is mandatory for non-Ethernet packets */
7125
0
            flow->dl_type = htons(0xffff);
7126
0
        } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
7127
            /* See comments in odp_flow_key_from_flow__(). */
7128
0
            odp_parse_error(&rl, errorp,
7129
0
                            "mask expected for non-Ethernet II frame");
7130
0
            return false;
7131
0
        }
7132
0
    }
7133
0
    return true;
7134
0
}
7135
7136
/* Initializes MPLS, L3, and L4 fields in 'flow' based on the attributes in
7137
 * 'attrs', in which the attributes in the bit-mask 'present_attrs' are
7138
 * present.  The caller also indicates an out-of-range attribute
7139
 * 'out_of_range_attr' if one was present when parsing (if so, the fitness
7140
 * cannot be perfect).
7141
 *
7142
 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
7143
 * consulted.  'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
7144
 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
7145
 * previously parsed flow key.
7146
 *
7147
 * Returns fitness based on any discrepancies between present and expected
7148
 * attributes, except that a 'need_check' of false overrides this.
7149
 *
7150
 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
7151
 * error message in '*errorp'.  'key' and 'key_len' are just used for error
7152
 * reporting in this case. */
7153
static enum odp_key_fitness
7154
parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
7155
                  uint64_t present_attrs, int out_of_range_attr,
7156
                  uint64_t *expected_attrs, struct flow *flow,
7157
                  const struct nlattr *key, size_t key_len,
7158
                  const struct flow *src_flow, bool need_check, char **errorp)
7159
0
{
7160
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7161
0
    bool is_mask = src_flow != flow;
7162
0
    const void *check_start = NULL;
7163
0
    size_t check_len = 0;
7164
0
    enum ovs_key_attr expected_bit = 0xff;
7165
7166
0
    if (eth_type_mpls(src_flow->dl_type)) {
7167
0
        if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7168
0
            *expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
7169
0
        }
7170
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7171
0
            size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
7172
0
            const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
7173
0
            int n = size / sizeof(ovs_be32);
7174
0
            int i;
7175
7176
0
            if (!size || size % sizeof(ovs_be32)) {
7177
0
                odp_parse_error(&rl, errorp,
7178
0
                                "MPLS LSEs have invalid length %"PRIuSIZE,
7179
0
                                size);
7180
0
                return ODP_FIT_ERROR;
7181
0
            }
7182
0
            if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
7183
0
                odp_parse_error(&rl, errorp,
7184
0
                                "unexpected MPLS Ethertype mask %x"PRIx16,
7185
0
                                ntohs(flow->dl_type));
7186
0
                return ODP_FIT_ERROR;
7187
0
            }
7188
7189
0
            for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
7190
0
                flow->mpls_lse[i] = mpls_lse[i];
7191
0
            }
7192
0
            if (n > FLOW_MAX_MPLS_LABELS) {
7193
0
                return ODP_FIT_TOO_MUCH;
7194
0
            }
7195
7196
0
            if (!is_mask) {
7197
                /* BOS may be set only in the innermost label. */
7198
0
                for (i = 0; i < n - 1; i++) {
7199
0
                    if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
7200
0
                        odp_parse_error(&rl, errorp,
7201
0
                                        "MPLS BOS set in non-innermost label");
7202
0
                        return ODP_FIT_ERROR;
7203
0
                    }
7204
0
                }
7205
7206
                /* BOS must be set in the innermost label. */
7207
0
                if (n < FLOW_MAX_MPLS_LABELS
7208
0
                    && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
7209
0
                    return ODP_FIT_TOO_LITTLE;
7210
0
                }
7211
0
            }
7212
0
        }
7213
7214
0
        goto done;
7215
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
7216
0
        if (!is_mask) {
7217
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
7218
0
        }
7219
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
7220
0
            const struct ovs_key_ipv4 *ipv4_key;
7221
7222
0
            ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
7223
0
            put_ipv4_key(ipv4_key, flow, is_mask);
7224
0
            if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
7225
0
                odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV4 has invalid "
7226
0
                                "nw_frag %#"PRIx8, flow->nw_frag);
7227
0
                return ODP_FIT_ERROR;
7228
0
            }
7229
7230
0
            if (is_mask) {
7231
0
                check_start = ipv4_key;
7232
0
                check_len = sizeof *ipv4_key;
7233
0
                expected_bit = OVS_KEY_ATTR_IPV4;
7234
0
            }
7235
0
        }
7236
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
7237
0
        if (!is_mask) {
7238
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
7239
0
        }
7240
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
7241
0
            const struct ovs_key_ipv6 *ipv6_key;
7242
7243
0
            ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
7244
0
            put_ipv6_key(ipv6_key, flow, is_mask);
7245
0
            if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
7246
0
                odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV6 has invalid "
7247
0
                                "nw_frag %#"PRIx8, flow->nw_frag);
7248
0
                return ODP_FIT_ERROR;
7249
0
            }
7250
0
            if (is_mask) {
7251
0
                check_start = ipv6_key;
7252
0
                check_len = sizeof *ipv6_key;
7253
0
                expected_bit = OVS_KEY_ATTR_IPV6;
7254
0
            }
7255
0
        }
7256
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
7257
0
               src_flow->dl_type == htons(ETH_TYPE_RARP)) {
7258
0
        if (!is_mask) {
7259
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
7260
0
        }
7261
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
7262
0
            const struct ovs_key_arp *arp_key;
7263
7264
0
            arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
7265
0
            if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
7266
0
                odp_parse_error(&rl, errorp,
7267
0
                                "unsupported ARP opcode %"PRIu16" in flow "
7268
0
                                "key", ntohs(arp_key->arp_op));
7269
0
                return ODP_FIT_ERROR;
7270
0
            }
7271
0
            put_arp_key(arp_key, flow);
7272
0
            if (is_mask) {
7273
0
                check_start = arp_key;
7274
0
                check_len = sizeof *arp_key;
7275
0
                expected_bit = OVS_KEY_ATTR_ARP;
7276
0
            }
7277
0
        }
7278
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) {
7279
0
        if (!is_mask) {
7280
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH;
7281
0
        }
7282
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_NSH)) {
7283
0
            if (odp_nsh_key_from_attr__(attrs[OVS_KEY_ATTR_NSH],
7284
0
                                        is_mask, &flow->nsh,
7285
0
                                        NULL, errorp) == ODP_FIT_ERROR) {
7286
0
                return ODP_FIT_ERROR;
7287
0
            }
7288
0
            if (is_mask) {
7289
0
                check_start = nl_attr_get(attrs[OVS_KEY_ATTR_NSH]);
7290
0
                check_len = nl_attr_get_size(attrs[OVS_KEY_ATTR_NSH]);
7291
0
                expected_bit = OVS_KEY_ATTR_NSH;
7292
0
            }
7293
0
        }
7294
0
    } else {
7295
0
        goto done;
7296
0
    }
7297
0
    if (check_len > 0) { /* Happens only when 'is_mask'. */
7298
0
        if (!is_all_zeros(check_start, check_len) &&
7299
0
            flow->dl_type != htons(0xffff)) {
7300
0
            odp_parse_error(&rl, errorp, "unexpected L3 matching with "
7301
0
                            "masked Ethertype %#"PRIx16"/%#"PRIx16,
7302
0
                            ntohs(src_flow->dl_type),
7303
0
                            ntohs(flow->dl_type));
7304
0
            return ODP_FIT_ERROR;
7305
0
        } else {
7306
0
            *expected_attrs |= UINT64_C(1) << expected_bit;
7307
0
        }
7308
0
    }
7309
7310
0
    expected_bit = OVS_KEY_ATTR_UNSPEC;
7311
0
    if (src_flow->nw_proto == IPPROTO_TCP
7312
0
        && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7313
0
            src_flow->dl_type == htons(ETH_TYPE_IPV6))
7314
0
        && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7315
0
        if (!is_mask) {
7316
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
7317
0
        }
7318
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
7319
0
            const union ovs_key_tp *tcp_key;
7320
7321
0
            tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
7322
0
            put_tp_key(tcp_key, flow);
7323
0
            expected_bit = OVS_KEY_ATTR_TCP;
7324
0
        }
7325
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
7326
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
7327
0
            flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
7328
0
        }
7329
0
    } else if (src_flow->nw_proto == IPPROTO_UDP
7330
0
               && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7331
0
                   src_flow->dl_type == htons(ETH_TYPE_IPV6))
7332
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7333
0
        if (!is_mask) {
7334
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
7335
0
        }
7336
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
7337
0
            const union ovs_key_tp *udp_key;
7338
7339
0
            udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
7340
0
            put_tp_key(udp_key, flow);
7341
0
            expected_bit = OVS_KEY_ATTR_UDP;
7342
0
        }
7343
0
    } else if (src_flow->nw_proto == IPPROTO_SCTP
7344
0
               && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7345
0
                   src_flow->dl_type == htons(ETH_TYPE_IPV6))
7346
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7347
0
        if (!is_mask) {
7348
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
7349
0
        }
7350
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
7351
0
            const union ovs_key_tp *sctp_key;
7352
7353
0
            sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
7354
0
            put_tp_key(sctp_key, flow);
7355
0
            expected_bit = OVS_KEY_ATTR_SCTP;
7356
0
        }
7357
0
    } else if (src_flow->nw_proto == IPPROTO_ICMP
7358
0
               && src_flow->dl_type == htons(ETH_TYPE_IP)
7359
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7360
0
        if (!is_mask) {
7361
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
7362
0
        }
7363
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
7364
0
            const struct ovs_key_icmp *icmp_key;
7365
7366
0
            icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
7367
0
            flow->tp_src = htons(icmp_key->icmp_type);
7368
0
            flow->tp_dst = htons(icmp_key->icmp_code);
7369
0
            expected_bit = OVS_KEY_ATTR_ICMP;
7370
0
        }
7371
0
    } else if (src_flow->nw_proto == IPPROTO_ICMPV6
7372
0
               && src_flow->dl_type == htons(ETH_TYPE_IPV6)
7373
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7374
0
        if (!is_mask) {
7375
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
7376
0
        }
7377
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
7378
0
            const struct ovs_key_icmpv6 *icmpv6_key;
7379
7380
0
            icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
7381
0
            flow->tp_src = htons(icmpv6_key->icmpv6_type);
7382
0
            flow->tp_dst = htons(icmpv6_key->icmpv6_code);
7383
0
            expected_bit = OVS_KEY_ATTR_ICMPV6;
7384
0
            if (is_nd(src_flow, NULL)) {
7385
0
                if (!is_mask) {
7386
0
                    *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
7387
0
                }
7388
0
                if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
7389
0
                    const struct ovs_key_nd *nd_key;
7390
7391
0
                    nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
7392
0
                    flow->nd_target = nd_key->nd_target;
7393
0
                    flow->arp_sha = nd_key->nd_sll;
7394
0
                    flow->arp_tha = nd_key->nd_tll;
7395
0
                    if (is_mask) {
7396
                        /* Even though 'tp_src' is 16 bits wide, ICMP type
7397
                         * is 8 bits wide.  Therefore, an exact match looks
7398
                         * like htons(0xff), not htons(0xffff).  See
7399
                         * xlate_wc_finish() for details. */
7400
0
                        if (!is_all_zeros(nd_key, sizeof *nd_key) &&
7401
0
                            flow->tp_src != htons(0xff)) {
7402
0
                            odp_parse_error(&rl, errorp,
7403
0
                                            "ICMP src mask should be "
7404
0
                                            "(0xff) but is actually "
7405
0
                                            "(%#"PRIx16")",
7406
0
                                            ntohs(flow->tp_src));
7407
0
                            return ODP_FIT_ERROR;
7408
0
                        } else {
7409
0
                            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
7410
0
                        }
7411
0
                    }
7412
0
                }
7413
0
                if (present_attrs &
7414
0
                    (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS)) {
7415
0
                    const struct ovs_key_nd_extensions *nd_ext_key;
7416
0
                    if (!is_mask) {
7417
0
                        *expected_attrs |=
7418
0
                                UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS;
7419
0
                    }
7420
7421
0
                    nd_ext_key =
7422
0
                        nl_attr_get(attrs[OVS_KEY_ATTR_ND_EXTENSIONS]);
7423
0
                    flow->igmp_group_ip4 = nd_ext_key->nd_reserved;
7424
0
                    flow->tcp_flags = htons(nd_ext_key->nd_options_type);
7425
7426
0
                    if (is_mask) {
7427
                        /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
7428
                         * ICMP type and code are 8 bits wide.  Therefore, an
7429
                         * exact match looks like htons(0xff), not
7430
                         * htons(0xffff).  See xlate_wc_finish() for details.
7431
                         * */
7432
0
                        if (!is_all_zeros(nd_ext_key, sizeof *nd_ext_key) &&
7433
0
                            (flow->tp_src != htons(0xff) ||
7434
0
                             flow->tp_dst != htons(0xff))) {
7435
0
                            return ODP_FIT_ERROR;
7436
0
                        } else {
7437
0
                            *expected_attrs |=
7438
0
                                UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS;
7439
0
                        }
7440
0
                    }
7441
0
                }
7442
0
            }
7443
0
        }
7444
0
    }
7445
0
    if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
7446
0
        if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
7447
0
            odp_parse_error(&rl, errorp, "flow matches on L4 ports but does "
7448
0
                            "not define an L4 protocol");
7449
0
            return ODP_FIT_ERROR;
7450
0
        } else {
7451
0
            *expected_attrs |= UINT64_C(1) << expected_bit;
7452
0
        }
7453
0
    }
7454
7455
0
done:
7456
0
    return need_check ? check_expectations(present_attrs, out_of_range_attr,
7457
0
                            *expected_attrs, key, key_len) : ODP_FIT_PERFECT;
7458
0
}
7459
7460
/* Parse 802.1Q header then encapsulated L3 attributes. */
7461
static enum odp_key_fitness
7462
parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
7463
                   uint64_t present_attrs, int out_of_range_attr,
7464
                   uint64_t expected_attrs, struct flow *flow,
7465
                   const struct nlattr *key, size_t key_len,
7466
                   const struct flow *src_flow, char **errorp,
7467
                   bool ignore_vlan_limit)
7468
0
{
7469
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7470
0
    bool is_mask = src_flow != flow;
7471
7472
0
    const struct nlattr *encap;
7473
0
    enum odp_key_fitness encap_fitness;
7474
0
    enum odp_key_fitness fitness = ODP_FIT_ERROR;
7475
0
    int vlan_limit;
7476
0
    int encaps = 0;
7477
7478
0
    vlan_limit = ignore_vlan_limit ? FLOW_MAX_VLAN_HEADERS : flow_vlan_limit;
7479
0
    while (encaps < vlan_limit &&
7480
0
           (is_mask
7481
0
            ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0
7482
0
            : eth_type_vlan(flow->dl_type))) {
7483
7484
0
        encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
7485
0
                 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
7486
7487
        /* Calculate fitness of outer attributes. */
7488
0
        if (!is_mask) {
7489
0
            expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
7490
0
                              (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
7491
0
        } else {
7492
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
7493
0
                expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
7494
0
            }
7495
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
7496
0
                expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
7497
0
            }
7498
0
        }
7499
0
        fitness = check_expectations(present_attrs, out_of_range_attr,
7500
0
                                     expected_attrs, key, key_len);
7501
7502
        /* Set vlan_tci.
7503
         * Remove the TPID from dl_type since it's not the real Ethertype.  */
7504
0
        flow->vlans[encaps].tpid = flow->dl_type;
7505
0
        flow->dl_type = htons(0);
7506
0
        flow->vlans[encaps].tci =
7507
0
                        (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
7508
0
                        ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
7509
0
                        : htons(0));
7510
0
        if (!is_mask) {
7511
0
            if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) ||
7512
0
                !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
7513
0
                return ODP_FIT_TOO_LITTLE;
7514
0
            } else if (flow->vlans[encaps].tci == htons(0)) {
7515
                /* Corner case for a truncated 802.1Q header. */
7516
0
                if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
7517
0
                    return ODP_FIT_TOO_MUCH;
7518
0
                }
7519
0
                return fitness;
7520
0
            } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) {
7521
0
                odp_parse_error(
7522
0
                    &rl, errorp, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
7523
0
                    "but CFI bit is not set", ntohs(flow->vlans[encaps].tci));
7524
0
                return ODP_FIT_ERROR;
7525
0
            }
7526
0
        } else {
7527
0
            if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
7528
0
                return fitness;
7529
0
            }
7530
0
        }
7531
7532
        /* Now parse the encapsulated attributes. */
7533
0
        if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
7534
0
                                attrs, &present_attrs, &out_of_range_attr,
7535
0
                                errorp)) {
7536
0
            return ODP_FIT_ERROR;
7537
0
        }
7538
0
        expected_attrs = 0;
7539
7540
        /* For OVS to be backward compatible with newer datapath
7541
         * implementations, we should ignore out of range attributes. */
7542
0
        if (out_of_range_attr) {
7543
0
            VLOG_DBG("Flow key decode found unknown OVS_KEY_ATTR, %d",
7544
0
                     out_of_range_attr);
7545
0
            out_of_range_attr = 0;
7546
0
        }
7547
7548
0
        if (!parse_ethertype(attrs, present_attrs, &expected_attrs,
7549
0
                             flow, src_flow, errorp)) {
7550
0
            return ODP_FIT_ERROR;
7551
0
        }
7552
0
        encap_fitness = parse_l2_5_onward(attrs, present_attrs,
7553
0
                                          out_of_range_attr,
7554
0
                                          &expected_attrs,
7555
0
                                          flow, key, key_len,
7556
0
                                          src_flow, false, errorp);
7557
0
        if (encap_fitness != ODP_FIT_PERFECT) {
7558
0
            return encap_fitness;
7559
0
        }
7560
0
        encaps++;
7561
0
    }
7562
7563
0
    return check_expectations(present_attrs, out_of_range_attr,
7564
0
                              expected_attrs, key, key_len);
7565
0
}
7566
7567
static enum odp_key_fitness
7568
odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
7569
                       struct flow *flow, const struct flow *src_flow,
7570
                       char **errorp, bool ignore_vlan_limit)
7571
0
{
7572
    /* New "struct flow" fields that are visible to the datapath (including all
7573
     * data fields) should be translated from equivalent datapath flow fields
7574
     * here (you will have to add a OVS_KEY_ATTR_* for them).  */
7575
0
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
7576
7577
0
    enum odp_key_fitness fitness = ODP_FIT_ERROR;
7578
0
    if (errorp) {
7579
0
        *errorp = NULL;
7580
0
    }
7581
7582
0
    const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
7583
0
    uint64_t expected_attrs;
7584
0
    uint64_t present_attrs;
7585
0
    int out_of_range_attr;
7586
0
    bool is_mask = src_flow != flow;
7587
7588
0
    memset(flow, 0, sizeof *flow);
7589
7590
    /* Parse attributes. */
7591
0
    if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
7592
0
                            &out_of_range_attr, errorp)) {
7593
0
        goto exit;
7594
0
    }
7595
0
    expected_attrs = 0;
7596
7597
    /* For OVS to be backward compatible with newer datapath implementations,
7598
     * we should ignore out of range attributes. */
7599
0
    if (out_of_range_attr) {
7600
0
        VLOG_DBG("Flow key decode found unknown OVS_KEY_ATTR, %d",
7601
0
                 out_of_range_attr);
7602
0
        out_of_range_attr = 0;
7603
0
    }
7604
7605
    /* Metadata. */
7606
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
7607
0
        flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
7608
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
7609
0
    } else if (is_mask) {
7610
        /* Always exact match recirc_id if it is not specified. */
7611
0
        flow->recirc_id = UINT32_MAX;
7612
0
    }
7613
7614
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
7615
0
        flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
7616
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
7617
0
    }
7618
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
7619
0
        flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
7620
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
7621
0
    }
7622
7623
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
7624
0
        flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
7625
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
7626
0
    }
7627
7628
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
7629
0
        uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
7630
7631
0
        flow->ct_state = odp_to_ovs_ct_state(odp_state);
7632
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
7633
0
    }
7634
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
7635
0
        flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
7636
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
7637
0
    }
7638
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
7639
0
        flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
7640
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
7641
0
    }
7642
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
7643
0
        flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]);
7644
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
7645
0
    }
7646
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
7647
0
        const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
7648
0
        flow->ct_nw_src = ct->ipv4_src;
7649
0
        flow->ct_nw_dst = ct->ipv4_dst;
7650
0
        flow->ct_nw_proto = ct->ipv4_proto;
7651
0
        flow->ct_tp_src = ct->src_port;
7652
0
        flow->ct_tp_dst = ct->dst_port;
7653
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
7654
0
    }
7655
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
7656
0
        const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
7657
7658
0
        flow->ct_ipv6_src = ct->ipv6_src;
7659
0
        flow->ct_ipv6_dst = ct->ipv6_dst;
7660
0
        flow->ct_nw_proto = ct->ipv6_proto;
7661
0
        flow->ct_tp_src = ct->src_port;
7662
0
        flow->ct_tp_dst = ct->dst_port;
7663
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
7664
0
    }
7665
7666
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
7667
0
        enum odp_key_fitness res;
7668
7669
0
        res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask,
7670
0
                                      &flow->tunnel, errorp);
7671
0
        if (res == ODP_FIT_ERROR) {
7672
0
            goto exit;
7673
0
        } else if (res == ODP_FIT_PERFECT) {
7674
0
            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
7675
0
        }
7676
0
    }
7677
7678
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
7679
0
        flow->in_port.odp_port
7680
0
            = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
7681
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
7682
0
    } else if (!is_mask) {
7683
0
        flow->in_port.odp_port = ODPP_NONE;
7684
0
    }
7685
7686
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) {
7687
0
        flow->packet_type
7688
0
            = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]);
7689
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE;
7690
0
        if (pt_ns(src_flow->packet_type) == OFPHTN_ETHERTYPE) {
7691
0
            flow->dl_type = pt_ns_type_be(flow->packet_type);
7692
0
        }
7693
0
    } else if (!is_mask) {
7694
0
        flow->packet_type = htonl(PT_ETH);
7695
0
    }
7696
7697
    /* Check for Ethernet header. */
7698
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
7699
0
        const struct ovs_key_ethernet *eth_key;
7700
7701
0
        eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
7702
0
        put_ethernet_key(eth_key, flow);
7703
0
        if (!is_mask) {
7704
0
            flow->packet_type = htonl(PT_ETH);
7705
0
        }
7706
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
7707
0
    }
7708
0
    else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
7709
0
        ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
7710
0
        if (!is_mask) {
7711
0
            flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
7712
0
                                               ntohs(ethertype));
7713
0
        }
7714
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
7715
0
    }
7716
7717
    /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
7718
0
    if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
7719
0
                         src_flow, errorp)) {
7720
0
        goto exit;
7721
0
    }
7722
7723
0
    if (is_mask
7724
0
        ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0
7725
0
        : eth_type_vlan(src_flow->dl_type)) {
7726
0
        fitness = parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
7727
0
                                     expected_attrs, flow, key, key_len,
7728
0
                                     src_flow, errorp, ignore_vlan_limit);
7729
0
    } else {
7730
0
        if (is_mask) {
7731
            /* A missing VLAN mask means exact match on vlan_tci 0 (== no
7732
             * VLAN). */
7733
0
            flow->vlans[0].tpid = htons(0xffff);
7734
0
            flow->vlans[0].tci = htons(0xffff);
7735
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
7736
0
                flow->vlans[0].tci = nl_attr_get_be16(
7737
0
                    attrs[OVS_KEY_ATTR_VLAN]);
7738
0
                expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
7739
0
            }
7740
0
        }
7741
0
        fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
7742
0
                                    &expected_attrs, flow, key, key_len,
7743
0
                                    src_flow, true, errorp);
7744
0
    }
7745
7746
0
exit:;
7747
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7748
0
    if (fitness == ODP_FIT_ERROR && (errorp || !VLOG_DROP_WARN(&rl))) {
7749
0
        struct ds s = DS_EMPTY_INITIALIZER;
7750
0
        if (is_mask) {
7751
0
            ds_put_cstr(&s, "the flow mask in error is: ");
7752
0
            odp_flow_key_format(key, key_len, &s);
7753
0
            ds_put_cstr(&s, ", for the following flow key: ");
7754
0
            flow_format(&s, src_flow, NULL);
7755
0
        } else {
7756
0
            ds_put_cstr(&s, "the flow key in error is: ");
7757
0
            odp_flow_key_format(key, key_len, &s);
7758
0
        }
7759
0
        if (errorp) {
7760
0
            char *old_error = *errorp;
7761
0
            *errorp = xasprintf("%s; %s", old_error, ds_cstr(&s));
7762
0
            free(old_error);
7763
0
        } else {
7764
0
            VLOG_WARN("%s", ds_cstr(&s));
7765
0
        }
7766
0
        ds_destroy(&s);
7767
0
    }
7768
0
    return fitness;
7769
0
}
7770
7771
/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
7772
 * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
7773
 * 'key' fits our expectations for what a flow key should contain.
7774
 *
7775
 * The 'in_port' will be the datapath's understanding of the port.  The
7776
 * caller will need to translate with odp_port_to_ofp_port() if the
7777
 * OpenFlow port is needed.
7778
 *
7779
 * This function doesn't take the packet itself as an argument because none of
7780
 * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
7781
 * it is always possible to infer which additional attribute(s) should appear
7782
 * by looking at the attributes for lower-level protocols, e.g. if the network
7783
 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
7784
 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
7785
 * must be absent.
7786
 *
7787
 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7788
 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7789
 * '*errorp', otherwise NULL. */
7790
enum odp_key_fitness
7791
odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
7792
                     struct flow *flow, char **errorp)
7793
0
{
7794
0
    return odp_flow_key_to_flow__(key, key_len, flow, flow, errorp, false);
7795
0
}
7796
7797
/* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
7798
 * to a mask structure in 'mask'.  'flow' must be a previously translated flow
7799
 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
7800
 * attributes from that flow.  Returns an ODP_FIT_* value that indicates how
7801
 * well 'key' fits our expectations for what a flow key should contain.
7802
 *
7803
 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7804
 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7805
 * '*errorp', otherwise NULL. */
7806
static enum odp_key_fitness
7807
odp_flow_key_to_mask__(const struct nlattr *mask_key, size_t mask_key_len,
7808
                       struct flow_wildcards *mask,
7809
                       const struct flow *src_flow,
7810
                       char **errorp, bool ignore_vlan_limit)
7811
0
{
7812
0
    if (mask_key_len) {
7813
0
        return odp_flow_key_to_flow__(mask_key, mask_key_len,
7814
0
                                      &mask->masks, src_flow, errorp,
7815
0
                                      ignore_vlan_limit);
7816
0
    } else {
7817
0
        if (errorp) {
7818
0
            *errorp = NULL;
7819
0
        }
7820
7821
        /* A missing mask means that the flow should be exact matched.
7822
         * Generate an appropriate exact wildcard for the flow. */
7823
0
        flow_wildcards_init_for_packet(mask, src_flow);
7824
7825
0
        return ODP_FIT_PERFECT;
7826
0
    }
7827
0
}
7828
7829
enum odp_key_fitness
7830
odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
7831
                     struct flow_wildcards *mask,
7832
                     const struct flow *src_flow, char **errorp)
7833
0
{
7834
0
    return odp_flow_key_to_mask__(mask_key, mask_key_len, mask, src_flow,
7835
0
                                  errorp, false);
7836
0
}
7837
7838
/* Converts the netlink formated key/mask to match.
7839
 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
7840
 * disagree on the acceptable form of flow */
7841
int
7842
parse_key_and_mask_to_match(const struct nlattr *key, size_t key_len,
7843
                            const struct nlattr *mask, size_t mask_len,
7844
                            struct match *match)
7845
0
{
7846
0
    enum odp_key_fitness fitness;
7847
7848
0
    fitness = odp_flow_key_to_flow__(key, key_len, &match->flow, &match->flow,
7849
0
                                     NULL, true);
7850
0
    if (fitness) {
7851
        /* This will happen when the odp_flow_key_to_flow() function can't
7852
         * parse the netlink message to a match structure.  It will return
7853
         * ODP_FIT_TOO_LITTLE if there is not enough information to parse the
7854
         * content successfully, ODP_FIT_TOO_MUCH if there is too much netlink
7855
         * data and we do not know how to safely ignore it, and ODP_FIT_ERROR
7856
         * in any other case. */
7857
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7858
7859
0
        if (!VLOG_DROP_ERR(&rl)) {
7860
0
            struct ds s;
7861
7862
0
            ds_init(&s);
7863
0
            odp_flow_format(key, key_len, NULL, 0, NULL, &s, true, false);
7864
0
            VLOG_ERR("internal error parsing flow key %s (%s)",
7865
0
                     ds_cstr(&s), odp_key_fitness_to_string(fitness));
7866
0
            ds_destroy(&s);
7867
0
        }
7868
7869
0
        return EINVAL;
7870
0
    }
7871
7872
0
    fitness = odp_flow_key_to_mask__(mask, mask_len, &match->wc, &match->flow,
7873
0
                                     NULL, true);
7874
0
    if (fitness) {
7875
        /* This should not happen, see comment above. */
7876
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7877
7878
0
        if (!VLOG_DROP_ERR(&rl)) {
7879
0
            struct ds s;
7880
7881
0
            ds_init(&s);
7882
0
            odp_flow_format(key, key_len, mask, mask_len, NULL, &s,
7883
0
                            true, true);
7884
0
            VLOG_ERR("internal error parsing flow mask %s (%s)",
7885
0
                     ds_cstr(&s), odp_key_fitness_to_string(fitness));
7886
0
            ds_destroy(&s);
7887
0
        }
7888
7889
0
        return EINVAL;
7890
0
    }
7891
7892
0
    return 0;
7893
0
}
7894
7895
/* Returns 'fitness' as a string, for use in debug messages. */
7896
const char *
7897
odp_key_fitness_to_string(enum odp_key_fitness fitness)
7898
0
{
7899
0
    switch (fitness) {
7900
0
    case ODP_FIT_PERFECT:
7901
0
        return "OK";
7902
0
    case ODP_FIT_TOO_MUCH:
7903
0
        return "too_much";
7904
0
    case ODP_FIT_TOO_LITTLE:
7905
0
        return "too_little";
7906
0
    case ODP_FIT_ERROR:
7907
0
        return "error";
7908
0
    default:
7909
0
        return "<unknown>";
7910
0
    }
7911
0
}
7912
7913
/* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
7914
 * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
7915
 * whose contents are the 'userdata_size' bytes at 'userdata' and sets
7916
 * 'odp_actions_ofs' if nonnull with the offset within 'odp_actions' of the
7917
 * start of the cookie.  (If 'userdata' is null, then the 'odp_actions_ofs'
7918
 * value is not meaningful.)
7919
 *
7920
 * Returns negative error code on failure. */
7921
int
7922
odp_put_userspace_action(uint32_t pid,
7923
                         const void *userdata, size_t userdata_size,
7924
                         odp_port_t tunnel_out_port,
7925
                         bool include_actions,
7926
                         struct ofpbuf *odp_actions, size_t *odp_actions_ofs)
7927
0
{
7928
0
    size_t userdata_ofs;
7929
0
    size_t offset;
7930
7931
0
    offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
7932
0
    nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
7933
0
    if (userdata) {
7934
0
        if (nl_attr_oversized(userdata_size)) {
7935
0
            return -E2BIG;
7936
0
        }
7937
0
        userdata_ofs = odp_actions->size + NLA_HDRLEN;
7938
7939
        /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
7940
         * module before Linux 3.10 required the userdata to be exactly 8 bytes
7941
         * long:
7942
         *
7943
         *   - The kernel rejected shorter userdata with -ERANGE.
7944
         *
7945
         *   - The kernel silently dropped userdata beyond the first 8 bytes.
7946
         *
7947
         * Thus, for maximum compatibility, always put at least 8 bytes.  (We
7948
         * separately disable features that required more than 8 bytes.) */
7949
0
        memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
7950
0
                                      MAX(8, userdata_size)),
7951
0
               userdata, userdata_size);
7952
0
    } else {
7953
0
        userdata_ofs = 0;
7954
0
    }
7955
0
    if (tunnel_out_port != ODPP_NONE) {
7956
0
        nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
7957
0
                            tunnel_out_port);
7958
0
    }
7959
0
    if (include_actions) {
7960
0
        nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
7961
0
    }
7962
0
    if (nl_attr_oversized(odp_actions->size - offset - NLA_HDRLEN)) {
7963
0
        return -E2BIG;
7964
0
    }
7965
0
    nl_msg_end_nested(odp_actions, offset);
7966
7967
0
    if (odp_actions_ofs) {
7968
0
        *odp_actions_ofs = userdata_ofs;
7969
0
    }
7970
7971
0
    return 0;
7972
0
}
7973
7974
void
7975
odp_put_pop_eth_action(struct ofpbuf *odp_actions)
7976
0
{
7977
0
    nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_ETH);
7978
0
}
7979
7980
void
7981
odp_put_push_eth_action(struct ofpbuf *odp_actions,
7982
                        const struct eth_addr *eth_src,
7983
                        const struct eth_addr *eth_dst)
7984
0
{
7985
0
    struct ovs_action_push_eth eth;
7986
7987
0
    memset(&eth, 0, sizeof eth);
7988
0
    if (eth_src) {
7989
0
        eth.addresses.eth_src = *eth_src;
7990
0
    }
7991
0
    if (eth_dst) {
7992
0
        eth.addresses.eth_dst = *eth_dst;
7993
0
    }
7994
7995
0
    nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_ETH,
7996
0
                      &eth, sizeof eth);
7997
0
}
7998
7999
void
8000
odp_put_tunnel_action(const struct flow_tnl *tunnel,
8001
                      struct ofpbuf *odp_actions, const char *tnl_type)
8002
0
{
8003
0
    size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
8004
0
    tun_key_to_attr(odp_actions, tunnel, tunnel, NULL, tnl_type);
8005
0
    nl_msg_end_nested(odp_actions, offset);
8006
0
}
8007
8008
void
8009
odp_put_tnl_push_action(struct ofpbuf *odp_actions,
8010
                        struct ovs_action_push_tnl *data)
8011
0
{
8012
0
    int size = offsetof(struct ovs_action_push_tnl, header);
8013
8014
0
    size += data->header_len;
8015
0
    nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
8016
0
}
8017
8018
void
8019
odp_put_psample_action(struct ofpbuf *odp_actions, uint32_t group_id,
8020
                       uint8_t *cookie, size_t cookie_len)
8021
0
{
8022
0
    size_t offset = nl_msg_start_nested_with_flag(odp_actions,
8023
0
                                                  OVS_ACTION_ATTR_PSAMPLE);
8024
8025
0
    nl_msg_put_u32(odp_actions, OVS_PSAMPLE_ATTR_GROUP, group_id);
8026
0
    if (cookie && cookie_len) {
8027
0
        ovs_assert(cookie_len <= OVS_PSAMPLE_COOKIE_MAX_SIZE);
8028
0
        nl_msg_put_unspec(odp_actions, OVS_PSAMPLE_ATTR_COOKIE, cookie,
8029
0
                          cookie_len);
8030
0
    }
8031
8032
0
    nl_msg_end_nested(odp_actions, offset);
8033
0
}
8034
8035

8036
/* The commit_odp_actions() function and its helpers. */
8037
8038
static void
8039
commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
8040
                  const void *key, size_t key_size)
8041
0
{
8042
0
    size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
8043
0
    nl_msg_put_unspec(odp_actions, key_type, key, key_size);
8044
0
    nl_msg_end_nested(odp_actions, offset);
8045
0
}
8046
8047
/* Commit one attribute for a masked set action.  Masked set actions have
8048
 * a mask following the data within the netlink attribute.  The unmasked bits
8049
 * in the data will be cleared as the data is copied to the attribute. */
8050
static void
8051
commit_masked_attribute(struct ofpbuf *odp_actions, int key_type,
8052
                        const void *key_, const void *mask_, size_t key_size)
8053
0
{
8054
0
    char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
8055
0
    const char *key = key_, *mask = mask_;
8056
8057
0
    memcpy(data + key_size, mask, key_size);
8058
    /* Clear unmasked bits while copying. */
8059
0
    while (key_size--) {
8060
0
        *data++ = *key++ & *mask++;
8061
0
    }
8062
0
}
8063
8064
/* A helper to commit a masked set action for a single attribute. */
8065
void
8066
commit_masked_set_action(struct ofpbuf *odp_actions,
8067
                         enum ovs_key_attr key_type,
8068
                         const void *key_, const void *mask_, size_t key_size)
8069
0
{
8070
0
    size_t offset = nl_msg_start_nested(odp_actions,
8071
0
                                        OVS_ACTION_ATTR_SET_MASKED);
8072
0
    commit_masked_attribute(odp_actions, key_type, key_, mask_, key_size);
8073
0
    nl_msg_end_nested(odp_actions, offset);
8074
0
}
8075
8076
/* If any of the flow key data that ODP actions can modify are different in
8077
 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
8078
 * 'odp_actions' that change the flow tunneling information in key from
8079
 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
8080
 * same way.  In other words, operates the same as commit_odp_actions(), but
8081
 * only on tunneling information. */
8082
void
8083
commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
8084
                         struct ofpbuf *odp_actions, const char *tnl_type)
8085
0
{
8086
    /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
8087
     * must have non-zero ipv6_dst. */
8088
0
    if (flow_tnl_dst_is_set(&flow->tunnel)) {
8089
0
        if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
8090
0
            return;
8091
0
        }
8092
0
        memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
8093
0
        odp_put_tunnel_action(&base->tunnel, odp_actions, tnl_type);
8094
0
    }
8095
0
}
8096
8097
struct offsetof_sizeof {
8098
    int offset;
8099
    int size;
8100
};
8101
8102
8103
/* Performs bitwise OR over the fields in 'dst_' and 'src_' specified in
8104
 * 'offsetof_sizeof_arr' array.  Result is stored in 'dst_'. */
8105
static void
8106
or_masks(void *dst_, const void *src_,
8107
         struct offsetof_sizeof *offsetof_sizeof_arr)
8108
0
{
8109
0
    int field, size, offset;
8110
0
    const uint8_t *src = src_;
8111
0
    uint8_t *dst = dst_;
8112
8113
0
    for (field = 0; ; field++) {
8114
0
        size   = offsetof_sizeof_arr[field].size;
8115
0
        offset = offsetof_sizeof_arr[field].offset;
8116
8117
0
        if (!size) {
8118
0
            return;
8119
0
        }
8120
0
        or_bytes(dst + offset, src + offset, size);
8121
0
    }
8122
0
}
8123
8124
/* Compares each of the fields in 'key0' and 'key1'.  The fields are specified
8125
 * in 'offsetof_sizeof_arr', which is an array terminated by a 0-size field.
8126
 * Returns true if all of the fields are equal, false if at least one differs.
8127
 * As a side effect, for each field that is the same in 'key0' and 'key1',
8128
 * zeros the corresponding bytes in 'mask'. */
8129
static bool
8130
keycmp_mask(const void *key0, const void *key1,
8131
            struct offsetof_sizeof *offsetof_sizeof_arr, void *mask)
8132
0
{
8133
0
    bool differ = false;
8134
8135
0
    for (int field = 0 ; ; field++) {
8136
0
        int size = offsetof_sizeof_arr[field].size;
8137
0
        int offset = offsetof_sizeof_arr[field].offset;
8138
0
        if (size == 0) {
8139
0
            break;
8140
0
        }
8141
8142
0
        char *pkey0 = ((char *)key0) + offset;
8143
0
        char *pkey1 = ((char *)key1) + offset;
8144
0
        char *pmask = ((char *)mask) + offset;
8145
0
        if (memcmp(pkey0, pkey1, size) == 0) {
8146
0
            memset(pmask, 0, size);
8147
0
        } else {
8148
0
            differ = true;
8149
0
        }
8150
0
    }
8151
8152
0
    return differ;
8153
0
}
8154
8155
static bool
8156
commit(enum ovs_key_attr attr, bool use_masked_set,
8157
       const void *key, void *base, void *mask, size_t size,
8158
       struct offsetof_sizeof *offsetof_sizeof_arr,
8159
       struct ofpbuf *odp_actions)
8160
0
{
8161
0
    if (keycmp_mask(key, base, offsetof_sizeof_arr, mask)) {
8162
0
        bool fully_masked = odp_mask_is_exact(attr, mask, size);
8163
8164
0
        if (use_masked_set && !fully_masked) {
8165
0
            commit_masked_set_action(odp_actions, attr, key, mask, size);
8166
0
        } else {
8167
0
            if (!fully_masked) {
8168
0
                memset(mask, 0xff, size);
8169
0
            }
8170
0
            commit_set_action(odp_actions, attr, key, size);
8171
0
        }
8172
0
        memcpy(base, key, size);
8173
0
        return true;
8174
0
    } else {
8175
        /* Mask bits are set when we have either read or set the corresponding
8176
         * values.  Masked bits will be exact-matched, no need to set them
8177
         * if the value did not actually change. */
8178
0
        return false;
8179
0
    }
8180
0
}
8181
8182
static void
8183
get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
8184
0
{
8185
0
    eth->eth_src = flow->dl_src;
8186
0
    eth->eth_dst = flow->dl_dst;
8187
0
}
8188
8189
static void
8190
put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
8191
0
{
8192
0
    flow->dl_src = eth->eth_src;
8193
0
    flow->dl_dst = eth->eth_dst;
8194
0
}
8195
8196
static void
8197
commit_set_ether_action(const struct flow *flow, struct flow *base_flow,
8198
                        struct ofpbuf *odp_actions,
8199
                        struct flow_wildcards *wc,
8200
                        bool use_masked)
8201
0
{
8202
0
    struct ovs_key_ethernet key, base, mask, orig_mask;
8203
0
    struct offsetof_sizeof ovs_key_ethernet_offsetof_sizeof_arr[] =
8204
0
        OVS_KEY_ETHERNET_OFFSETOF_SIZEOF_ARR;
8205
8206
0
    if (flow->packet_type != htonl(PT_ETH) ||
8207
0
        base_flow->packet_type != htonl(PT_ETH)) {
8208
0
        return;
8209
0
    }
8210
8211
0
    get_ethernet_key(flow, &key);
8212
0
    get_ethernet_key(base_flow, &base);
8213
0
    get_ethernet_key(&wc->masks, &mask);
8214
0
    memcpy(&orig_mask, &mask, sizeof mask);
8215
8216
0
    if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
8217
0
               &key, &base, &mask, sizeof key,
8218
0
               ovs_key_ethernet_offsetof_sizeof_arr, odp_actions)) {
8219
0
        put_ethernet_key(&base, base_flow);
8220
0
        or_masks(&mask, &orig_mask, ovs_key_ethernet_offsetof_sizeof_arr);
8221
0
        put_ethernet_key(&mask, &wc->masks);
8222
0
    }
8223
0
}
8224
8225
static void
8226
commit_vlan_action(const struct flow* flow, struct flow *base,
8227
                   struct ofpbuf *odp_actions, struct flow_wildcards *wc)
8228
0
{
8229
0
    int base_n = flow_count_vlan_headers(base);
8230
0
    int flow_n = flow_count_vlan_headers(flow);
8231
0
    flow_skip_common_vlan_headers(base, &base_n, flow, &flow_n);
8232
8233
    /* Pop all mismatching vlan of base, push those of flow */
8234
0
    for (; base_n >= 0; base_n--) {
8235
0
        nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
8236
0
        wc->masks.vlans[base_n].qtag = OVS_BE32_MAX;
8237
0
    }
8238
8239
0
    for (; flow_n >= 0; flow_n--) {
8240
0
        struct ovs_action_push_vlan vlan;
8241
8242
0
        vlan.vlan_tpid = flow->vlans[flow_n].tpid;
8243
0
        vlan.vlan_tci = flow->vlans[flow_n].tci;
8244
0
        nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
8245
0
                          &vlan, sizeof vlan);
8246
0
    }
8247
0
    memcpy(base->vlans, flow->vlans, sizeof(base->vlans));
8248
0
}
8249
8250
/* Wildcarding already done at action translation time. */
8251
static void
8252
commit_mpls_action(const struct flow *flow, struct flow *base,
8253
                   struct ofpbuf *odp_actions, bool pending_encap)
8254
0
{
8255
0
    int base_n = flow_count_mpls_labels(base, NULL);
8256
0
    int flow_n = flow_count_mpls_labels(flow, NULL);
8257
0
    int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
8258
0
                                                 NULL);
8259
8260
0
    while (base_n > common_n) {
8261
0
        if (base_n - 1 == common_n && flow_n > common_n) {
8262
            /* If there is only one more LSE in base than there are common
8263
             * between base and flow; and flow has at least one more LSE than
8264
             * is common then the topmost LSE of base may be updated using
8265
             * set */
8266
0
            struct ovs_key_mpls mpls_key;
8267
8268
0
            mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
8269
0
            commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
8270
0
                              &mpls_key, sizeof mpls_key);
8271
0
            flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
8272
0
            common_n++;
8273
0
        } else {
8274
            /* Otherwise, if there more LSEs in base than are common between
8275
             * base and flow then pop the topmost one. */
8276
0
            ovs_be16 dl_type;
8277
            /* If all the LSEs are to be popped and this is not the outermost
8278
             * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
8279
             * POP_MPLS action instead of flow->dl_type.
8280
             *
8281
             * This is because the POP_MPLS action requires its ethertype
8282
             * argument to be an MPLS ethernet type but in this case
8283
             * flow->dl_type will be a non-MPLS ethernet type.
8284
             *
8285
             * When the final POP_MPLS action occurs it use flow->dl_type and
8286
             * the and the resulting packet will have the desired dl_type. */
8287
0
            if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
8288
0
                dl_type = htons(ETH_TYPE_MPLS);
8289
0
            } else {
8290
0
                dl_type = flow->dl_type;
8291
0
            }
8292
0
            nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
8293
0
            ovs_assert(flow_pop_mpls(base, base_n, flow->dl_type, NULL));
8294
0
            base_n--;
8295
0
        }
8296
0
    }
8297
8298
    /* If, after the above popping and setting, there are more LSEs in flow
8299
     * than base then some LSEs need to be pushed. */
8300
0
    while (base_n < flow_n) {
8301
8302
0
        if (pending_encap) {
8303
0
             struct ovs_action_add_mpls *mpls;
8304
8305
0
             mpls = nl_msg_put_unspec_zero(odp_actions,
8306
0
                                           OVS_ACTION_ATTR_ADD_MPLS,
8307
0
                                           sizeof *mpls);
8308
0
             mpls->mpls_ethertype = flow->dl_type;
8309
0
             mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
8310
0
        } else {
8311
0
             struct ovs_action_push_mpls *mpls;
8312
8313
0
             mpls = nl_msg_put_unspec_zero(odp_actions,
8314
0
                                           OVS_ACTION_ATTR_PUSH_MPLS,
8315
0
                                           sizeof *mpls);
8316
0
             mpls->mpls_ethertype = flow->dl_type;
8317
0
             mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
8318
0
        }
8319
        /* Update base flow's MPLS stack, but do not clear L3.  We need the L3
8320
         * headers if the flow is restored later due to returning from a patch
8321
         * port or group bucket. */
8322
0
        flow_push_mpls(base, base_n, flow->dl_type, NULL, false);
8323
0
        flow_set_mpls_lse(base, 0, flow->mpls_lse[flow_n - base_n - 1]);
8324
0
        base_n++;
8325
0
    }
8326
0
}
8327
8328
static void
8329
get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
8330
0
{
8331
0
    ipv4->ipv4_src = flow->nw_src;
8332
0
    ipv4->ipv4_dst = flow->nw_dst;
8333
0
    ipv4->ipv4_proto = flow->nw_proto;
8334
0
    ipv4->ipv4_tos = flow->nw_tos;
8335
0
    ipv4->ipv4_ttl = flow->nw_ttl;
8336
0
    ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
8337
0
}
8338
8339
static void
8340
put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
8341
0
{
8342
0
    flow->nw_src = ipv4->ipv4_src;
8343
0
    flow->nw_dst = ipv4->ipv4_dst;
8344
0
    flow->nw_proto = ipv4->ipv4_proto;
8345
0
    flow->nw_tos = ipv4->ipv4_tos;
8346
0
    flow->nw_ttl = ipv4->ipv4_ttl;
8347
0
    flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
8348
0
}
8349
8350
static void
8351
commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
8352
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8353
                       bool use_masked)
8354
0
{
8355
0
    struct ovs_key_ipv4 key, mask, orig_mask, base;
8356
0
    struct offsetof_sizeof ovs_key_ipv4_offsetof_sizeof_arr[] =
8357
0
        OVS_KEY_IPV4_OFFSETOF_SIZEOF_ARR;
8358
8359
    /* Check that nw_proto and nw_frag remain unchanged. */
8360
0
    ovs_assert(flow->nw_proto == base_flow->nw_proto &&
8361
0
               flow->nw_frag == base_flow->nw_frag);
8362
8363
0
    get_ipv4_key(flow, &key, false);
8364
0
    get_ipv4_key(base_flow, &base, false);
8365
0
    get_ipv4_key(&wc->masks, &mask, true);
8366
0
    memcpy(&orig_mask, &mask, sizeof mask);
8367
0
    mask.ipv4_proto = 0;        /* Not writeable. */
8368
0
    mask.ipv4_frag = 0;         /* Not writable. */
8369
8370
0
    if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
8371
0
        ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
8372
0
        mask.ipv4_tos &= ~IP_ECN_MASK;
8373
0
    }
8374
8375
0
    if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
8376
0
               ovs_key_ipv4_offsetof_sizeof_arr, odp_actions)) {
8377
0
        put_ipv4_key(&base, base_flow, false);
8378
0
        or_masks(&mask, &orig_mask, ovs_key_ipv4_offsetof_sizeof_arr);
8379
0
        put_ipv4_key(&mask, &wc->masks, true);
8380
0
   }
8381
0
}
8382
8383
static void
8384
get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
8385
0
{
8386
0
    ipv6->ipv6_src = flow->ipv6_src;
8387
0
    ipv6->ipv6_dst = flow->ipv6_dst;
8388
0
    ipv6->ipv6_label = flow->ipv6_label;
8389
0
    ipv6->ipv6_proto = flow->nw_proto;
8390
0
    ipv6->ipv6_tclass = flow->nw_tos;
8391
0
    ipv6->ipv6_hlimit = flow->nw_ttl;
8392
0
    ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
8393
0
}
8394
8395
static void
8396
put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
8397
0
{
8398
0
    flow->ipv6_src = ipv6->ipv6_src;
8399
0
    flow->ipv6_dst = ipv6->ipv6_dst;
8400
0
    flow->ipv6_label = ipv6->ipv6_label;
8401
0
    flow->nw_proto = ipv6->ipv6_proto;
8402
0
    flow->nw_tos = ipv6->ipv6_tclass;
8403
0
    flow->nw_ttl = ipv6->ipv6_hlimit;
8404
0
    flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
8405
0
}
8406
8407
static void
8408
commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
8409
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8410
                       bool use_masked)
8411
0
{
8412
0
    struct ovs_key_ipv6 key, mask, orig_mask, base;
8413
0
    struct offsetof_sizeof ovs_key_ipv6_offsetof_sizeof_arr[] =
8414
0
        OVS_KEY_IPV6_OFFSETOF_SIZEOF_ARR;
8415
8416
    /* Check that nw_proto and nw_frag remain unchanged. */
8417
0
    ovs_assert(flow->nw_proto == base_flow->nw_proto &&
8418
0
               flow->nw_frag == base_flow->nw_frag);
8419
8420
0
    get_ipv6_key(flow, &key, false);
8421
0
    get_ipv6_key(base_flow, &base, false);
8422
0
    get_ipv6_key(&wc->masks, &mask, true);
8423
0
    memcpy(&orig_mask, &mask, sizeof mask);
8424
0
    mask.ipv6_proto = 0;        /* Not writeable. */
8425
0
    mask.ipv6_frag = 0;         /* Not writable. */
8426
0
    mask.ipv6_label &= htonl(IPV6_LABEL_MASK); /* Not writable. */
8427
8428
0
    if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
8429
0
        ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
8430
0
        mask.ipv6_tclass &= ~IP_ECN_MASK;
8431
0
    }
8432
8433
0
    if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
8434
0
               ovs_key_ipv6_offsetof_sizeof_arr, odp_actions)) {
8435
0
        put_ipv6_key(&base, base_flow, false);
8436
0
        or_masks(&mask, &orig_mask, ovs_key_ipv6_offsetof_sizeof_arr);
8437
0
        put_ipv6_key(&mask, &wc->masks, true);
8438
0
    }
8439
0
}
8440
8441
static void
8442
get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
8443
0
{
8444
    /* ARP key has padding, clear it. */
8445
0
    memset(arp, 0, sizeof *arp);
8446
8447
0
    arp->arp_sip = flow->nw_src;
8448
0
    arp->arp_tip = flow->nw_dst;
8449
0
    arp->arp_op = flow->nw_proto == UINT8_MAX ?
8450
0
                  OVS_BE16_MAX : htons(flow->nw_proto);
8451
0
    arp->arp_sha = flow->arp_sha;
8452
0
    arp->arp_tha = flow->arp_tha;
8453
0
}
8454
8455
static void
8456
put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
8457
0
{
8458
0
    flow->nw_src = arp->arp_sip;
8459
0
    flow->nw_dst = arp->arp_tip;
8460
0
    flow->nw_proto = ntohs(arp->arp_op);
8461
0
    flow->arp_sha = arp->arp_sha;
8462
0
    flow->arp_tha = arp->arp_tha;
8463
0
}
8464
8465
static enum slow_path_reason
8466
commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
8467
                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
8468
0
{
8469
0
    struct ovs_key_arp key, mask, orig_mask, base;
8470
0
    struct offsetof_sizeof ovs_key_arp_offsetof_sizeof_arr[] =
8471
0
        OVS_KEY_ARP_OFFSETOF_SIZEOF_ARR;
8472
8473
0
    get_arp_key(flow, &key);
8474
0
    get_arp_key(base_flow, &base);
8475
0
    get_arp_key(&wc->masks, &mask);
8476
0
    memcpy(&orig_mask, &mask, sizeof mask);
8477
8478
0
    if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
8479
0
               ovs_key_arp_offsetof_sizeof_arr, odp_actions)) {
8480
0
        put_arp_key(&base, base_flow);
8481
0
        or_masks(&mask, &orig_mask, ovs_key_arp_offsetof_sizeof_arr);
8482
0
        put_arp_key(&mask, &wc->masks);
8483
0
        return SLOW_ACTION;
8484
0
    }
8485
0
    return 0;
8486
0
}
8487
8488
static void
8489
get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
8490
0
{
8491
    /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8492
0
    icmp->icmp_type = ntohs(flow->tp_src);
8493
0
    icmp->icmp_code = ntohs(flow->tp_dst);
8494
0
}
8495
8496
static void
8497
put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
8498
0
{
8499
    /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8500
0
    flow->tp_src = htons(icmp->icmp_type);
8501
0
    flow->tp_dst = htons(icmp->icmp_code);
8502
0
}
8503
8504
static enum slow_path_reason
8505
commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
8506
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc)
8507
0
{
8508
0
    struct ovs_key_icmp key, mask, orig_mask, base;
8509
0
    struct offsetof_sizeof ovs_key_icmp_offsetof_sizeof_arr[] =
8510
0
        OVS_KEY_ICMP_OFFSETOF_SIZEOF_ARR;
8511
0
    enum ovs_key_attr attr;
8512
8513
0
    if (is_icmpv4(flow, NULL)) {
8514
0
        attr = OVS_KEY_ATTR_ICMP;
8515
0
    } else if (is_icmpv6(flow, NULL)) {
8516
0
        attr = OVS_KEY_ATTR_ICMPV6;
8517
0
    } else {
8518
0
        return 0;
8519
0
    }
8520
8521
0
    get_icmp_key(flow, &key);
8522
0
    get_icmp_key(base_flow, &base);
8523
0
    get_icmp_key(&wc->masks, &mask);
8524
0
    memcpy(&orig_mask, &mask, sizeof mask);
8525
8526
0
    if (commit(attr, false, &key, &base, &mask, sizeof key,
8527
0
               ovs_key_icmp_offsetof_sizeof_arr, odp_actions)) {
8528
0
        put_icmp_key(&base, base_flow);
8529
0
        or_masks(&mask, &orig_mask, ovs_key_icmp_offsetof_sizeof_arr);
8530
0
        put_icmp_key(&mask, &wc->masks);
8531
0
        return SLOW_ACTION;
8532
0
    }
8533
0
    return 0;
8534
0
}
8535
8536
static void
8537
get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
8538
0
{
8539
0
    nd->nd_target = flow->nd_target;
8540
    /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8541
0
    nd->nd_sll = flow->arp_sha;
8542
0
    nd->nd_tll = flow->arp_tha;
8543
0
}
8544
8545
static void
8546
put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
8547
0
{
8548
0
    flow->nd_target = nd->nd_target;
8549
    /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8550
0
    flow->arp_sha = nd->nd_sll;
8551
0
    flow->arp_tha = nd->nd_tll;
8552
0
}
8553
8554
static void
8555
get_nd_extensions_key(const struct flow *flow,
8556
                      struct ovs_key_nd_extensions *nd_ext)
8557
0
{
8558
    /* ND Extensions key has padding, clear it. */
8559
0
    memset(nd_ext, 0, sizeof *nd_ext);
8560
0
    nd_ext->nd_reserved = flow->igmp_group_ip4;
8561
0
    nd_ext->nd_options_type = ntohs(flow->tcp_flags);
8562
0
}
8563
8564
static void
8565
put_nd_extensions_key(const struct ovs_key_nd_extensions *nd_ext,
8566
                      struct flow *flow)
8567
0
{
8568
0
    flow->igmp_group_ip4 = nd_ext->nd_reserved;
8569
0
    flow->tcp_flags = htons(nd_ext->nd_options_type);
8570
0
}
8571
8572
static enum slow_path_reason
8573
commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
8574
                     struct ofpbuf *odp_actions,
8575
                     struct flow_wildcards *wc, bool use_masked)
8576
0
{
8577
0
    struct ovs_key_nd key, mask, orig_mask, base;
8578
0
    struct offsetof_sizeof ovs_key_nd_offsetof_sizeof_arr[] =
8579
0
        OVS_KEY_ND_OFFSETOF_SIZEOF_ARR;
8580
8581
0
    get_nd_key(flow, &key);
8582
0
    get_nd_key(base_flow, &base);
8583
0
    get_nd_key(&wc->masks, &mask);
8584
0
    memcpy(&orig_mask, &mask, sizeof mask);
8585
8586
0
    if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
8587
0
               ovs_key_nd_offsetof_sizeof_arr, odp_actions)) {
8588
0
        put_nd_key(&base, base_flow);
8589
0
        or_masks(&mask, &orig_mask, ovs_key_nd_offsetof_sizeof_arr);
8590
0
        put_nd_key(&mask, &wc->masks);
8591
0
        return SLOW_ACTION;
8592
0
    }
8593
8594
0
    return 0;
8595
0
}
8596
8597
static enum slow_path_reason
8598
commit_set_nd_extensions_action(const struct flow *flow,
8599
                                struct flow *base_flow,
8600
                                struct ofpbuf *odp_actions,
8601
                                struct flow_wildcards *wc, bool use_masked)
8602
0
{
8603
0
    struct ovs_key_nd_extensions key, mask, orig_mask, base;
8604
0
    struct offsetof_sizeof ovs_key_nd_extensions_offsetof_sizeof_arr[] =
8605
0
        OVS_KEY_ND_EXTENSIONS_OFFSETOF_SIZEOF_ARR;
8606
8607
0
    get_nd_extensions_key(flow, &key);
8608
0
    get_nd_extensions_key(base_flow, &base);
8609
0
    get_nd_extensions_key(&wc->masks, &mask);
8610
0
    memcpy(&orig_mask, &mask, sizeof mask);
8611
8612
0
    if (commit(OVS_KEY_ATTR_ND_EXTENSIONS, use_masked, &key, &base, &mask,
8613
0
               sizeof key, ovs_key_nd_extensions_offsetof_sizeof_arr,
8614
0
               odp_actions)) {
8615
0
        put_nd_extensions_key(&base, base_flow);
8616
0
        or_masks(&mask, &orig_mask, ovs_key_nd_extensions_offsetof_sizeof_arr);
8617
0
        put_nd_extensions_key(&mask, &wc->masks);
8618
0
        return SLOW_ACTION;
8619
0
    }
8620
0
    return 0;
8621
0
}
8622
8623
static enum slow_path_reason
8624
commit_set_nw_action(const struct flow *flow, struct flow *base,
8625
                     struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8626
                     bool use_masked)
8627
0
{
8628
0
    uint32_t reason;
8629
8630
    /* Check if 'flow' really has an L3 header. */
8631
0
    if (!flow->nw_proto) {
8632
0
        return 0;
8633
0
    }
8634
8635
0
    switch (ntohs(base->dl_type)) {
8636
0
    case ETH_TYPE_IP:
8637
0
        commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
8638
0
        break;
8639
8640
0
    case ETH_TYPE_IPV6:
8641
0
        commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
8642
0
        if (base->nw_proto == IPPROTO_ICMPV6) {
8643
            /* Commit extended attrs first to make sure
8644
               correct options are added.*/
8645
0
            reason = commit_set_nd_extensions_action(flow, base,
8646
0
                                         odp_actions, wc, use_masked);
8647
0
            reason |= commit_set_nd_action(flow, base, odp_actions,
8648
0
                                         wc, use_masked);
8649
0
            return reason;
8650
0
        }
8651
0
        break;
8652
8653
0
    case ETH_TYPE_ARP:
8654
0
        return commit_set_arp_action(flow, base, odp_actions, wc);
8655
0
    }
8656
8657
0
    return 0;
8658
0
}
8659
8660
static bool
8661
commit_nsh(bool use_masked_set, const struct ovs_key_nsh *key,
8662
           struct ovs_key_nsh *base, struct ovs_key_nsh *mask, size_t size,
8663
           struct ofpbuf *odp_actions)
8664
0
{
8665
0
    if (!memcmp(key, base, size)) {
8666
        /* Mask bits are set when we have either read or set the corresponding
8667
         * values.  Masked bits will be exact-matched, no need to set them
8668
         * if the value did not actually change. */
8669
0
        return false;
8670
0
    }
8671
8672
0
    bool fully_masked = odp_mask_is_exact(OVS_KEY_ATTR_NSH, mask, size);
8673
0
    size_t set_ofs;
8674
8675
0
    if (use_masked_set && !fully_masked) {
8676
0
        struct ovs_nsh_key_base nsh_base = {
8677
0
            .flags = key->flags,
8678
0
            .ttl = key->ttl,
8679
            /* 'mdtype' and 'np' are not writable. */
8680
0
            .path_hdr = key->path_hdr,
8681
0
        };
8682
0
        struct ovs_nsh_key_base nsh_base_mask = {
8683
0
            .flags = mask->flags,
8684
0
            .ttl = mask->ttl,
8685
            /* 'mdtype' and 'np' are not writable. */
8686
0
            .path_hdr = mask->path_hdr,
8687
0
        };
8688
0
        size_t nsh_ofs;
8689
8690
0
        set_ofs = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET_MASKED);
8691
0
        nsh_ofs = nl_msg_start_nested(odp_actions, OVS_KEY_ATTR_NSH);
8692
8693
0
        if (!is_all_zeros(&nsh_base_mask, sizeof nsh_base_mask)) {
8694
0
            commit_masked_attribute(odp_actions, OVS_NSH_KEY_ATTR_BASE,
8695
0
                                    &nsh_base, &nsh_base_mask,
8696
0
                                    sizeof nsh_base);
8697
0
        }
8698
8699
        /* No match support for other MD formats yet. */
8700
0
        if (key->mdtype == NSH_M_TYPE1
8701
0
            && !is_all_zeros(mask->context, sizeof mask->context)) {
8702
0
            commit_masked_attribute(odp_actions, OVS_NSH_KEY_ATTR_MD1,
8703
0
                                    key->context, mask->context,
8704
0
                                    sizeof key->context);
8705
0
        }
8706
8707
0
        nl_msg_end_nested(odp_actions, nsh_ofs);
8708
0
        nl_msg_end_nested(odp_actions, set_ofs);
8709
0
    } else {
8710
        /* Overwriting the whole header.  Make sure that fields that wasn't
8711
         * in the mask are indeed the same as in the committed action. */
8712
0
        if (!fully_masked) {
8713
0
            memset(mask, 0xff, size);
8714
0
        }
8715
8716
0
        set_ofs = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
8717
0
        nsh_key_to_attr(odp_actions, key, NULL, 0, false);
8718
0
        nl_msg_end_nested(odp_actions, set_ofs);
8719
0
    }
8720
0
    memcpy(base, key, size);
8721
0
    return true;
8722
0
}
8723
8724
static void
8725
commit_set_nsh_action(const struct flow *flow, struct flow *base_flow,
8726
                      struct ofpbuf *odp_actions,
8727
                      struct flow_wildcards *wc,
8728
                      bool use_masked)
8729
0
{
8730
0
    struct ovs_key_nsh key, mask, base;
8731
8732
0
    if (flow->dl_type != htons(ETH_TYPE_NSH)) {
8733
0
        return;
8734
0
    }
8735
8736
    /* Check that mdtype and np remain unchanged. */
8737
0
    ovs_assert(flow->nsh.mdtype == base_flow->nsh.mdtype &&
8738
0
               flow->nsh.np == base_flow->nsh.np);
8739
8740
0
    key = flow->nsh;
8741
0
    base = base_flow->nsh;
8742
0
    mask = wc->masks.nsh;
8743
8744
0
    if (commit_nsh(use_masked, &key, &base, &mask, sizeof key, odp_actions)) {
8745
0
        base_flow->nsh = base;
8746
0
        or_bytes(&wc->masks.nsh, &mask, sizeof wc->masks.nsh);
8747
0
    }
8748
0
}
8749
8750
/* TCP, UDP, and SCTP keys have the same layout. */
8751
BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
8752
                  sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
8753
8754
static void
8755
get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
8756
0
{
8757
0
    tp->tcp.tcp_src = flow->tp_src;
8758
0
    tp->tcp.tcp_dst = flow->tp_dst;
8759
0
}
8760
8761
static void
8762
put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
8763
0
{
8764
0
    flow->tp_src = tp->tcp.tcp_src;
8765
0
    flow->tp_dst = tp->tcp.tcp_dst;
8766
0
}
8767
8768
static void
8769
commit_set_port_action(const struct flow *flow, struct flow *base_flow,
8770
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8771
                       bool use_masked)
8772
0
{
8773
0
    enum ovs_key_attr key_type;
8774
0
    union ovs_key_tp key, mask, orig_mask, base;
8775
0
    struct offsetof_sizeof ovs_key_tp_offsetof_sizeof_arr[] =
8776
0
        OVS_KEY_TCP_OFFSETOF_SIZEOF_ARR;
8777
8778
    /* Check if 'flow' really has an L3 header. */
8779
0
    if (!flow->nw_proto) {
8780
0
        return;
8781
0
    }
8782
8783
0
    if (!is_ip_any(base_flow)) {
8784
0
        return;
8785
0
    }
8786
8787
0
    if (flow->nw_proto == IPPROTO_TCP) {
8788
0
        key_type = OVS_KEY_ATTR_TCP;
8789
0
    } else if (flow->nw_proto == IPPROTO_UDP) {
8790
0
        key_type = OVS_KEY_ATTR_UDP;
8791
0
    } else if (flow->nw_proto == IPPROTO_SCTP) {
8792
0
        key_type = OVS_KEY_ATTR_SCTP;
8793
0
    } else {
8794
0
        return;
8795
0
    }
8796
8797
0
    get_tp_key(flow, &key);
8798
0
    get_tp_key(base_flow, &base);
8799
0
    get_tp_key(&wc->masks, &mask);
8800
0
    memcpy(&orig_mask, &mask, sizeof mask);
8801
8802
0
    if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
8803
0
               ovs_key_tp_offsetof_sizeof_arr, odp_actions)) {
8804
0
        put_tp_key(&base, base_flow);
8805
0
        or_masks(&mask, &orig_mask, ovs_key_tp_offsetof_sizeof_arr);
8806
0
        put_tp_key(&mask, &wc->masks);
8807
0
    }
8808
0
}
8809
8810
static void
8811
commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
8812
                           struct ofpbuf *odp_actions,
8813
                           struct flow_wildcards *wc,
8814
                           bool use_masked)
8815
0
{
8816
0
    uint32_t key, mask, base;
8817
0
    struct offsetof_sizeof ovs_key_prio_offsetof_sizeof_arr[] = {
8818
0
        {0, sizeof(uint32_t)},
8819
0
        {0, 0}
8820
0
    };
8821
8822
0
    key = flow->skb_priority;
8823
0
    base = base_flow->skb_priority;
8824
0
    mask = wc->masks.skb_priority;
8825
8826
0
    if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
8827
0
               sizeof key, ovs_key_prio_offsetof_sizeof_arr, odp_actions)) {
8828
0
        base_flow->skb_priority = base;
8829
0
        wc->masks.skb_priority |= mask;
8830
0
    }
8831
0
}
8832
8833
static void
8834
commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
8835
                           struct ofpbuf *odp_actions,
8836
                           struct flow_wildcards *wc,
8837
                           bool use_masked)
8838
0
{
8839
0
    uint32_t key, mask, base;
8840
0
    struct offsetof_sizeof ovs_key_pkt_mark_offsetof_sizeof_arr[] = {
8841
0
        {0, sizeof(uint32_t)},
8842
0
        {0, 0}
8843
0
    };
8844
8845
0
    key = flow->pkt_mark;
8846
0
    base = base_flow->pkt_mark;
8847
0
    mask = wc->masks.pkt_mark;
8848
8849
0
    if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
8850
0
               sizeof key, ovs_key_pkt_mark_offsetof_sizeof_arr,
8851
0
               odp_actions)) {
8852
0
        base_flow->pkt_mark = base;
8853
0
        wc->masks.pkt_mark |= mask;
8854
0
    }
8855
0
}
8856
8857
static void
8858
odp_put_pop_nsh_action(struct ofpbuf *odp_actions)
8859
0
{
8860
0
    nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_NSH);
8861
0
}
8862
8863
static void
8864
odp_put_push_nsh_action(struct ofpbuf *odp_actions,
8865
                         const struct flow *flow,
8866
                         struct ofpbuf *encap_data)
8867
0
{
8868
0
    uint8_t * metadata = NULL;
8869
0
    uint8_t md_size = 0;
8870
8871
0
    switch (flow->nsh.mdtype) {
8872
0
    case NSH_M_TYPE2:
8873
0
        if (encap_data) {
8874
0
            ovs_assert(encap_data->size < NSH_CTX_HDRS_MAX_LEN);
8875
0
            metadata = encap_data->data;
8876
0
            md_size = encap_data->size;
8877
0
        } else {
8878
0
            md_size = 0;
8879
0
        }
8880
0
        break;
8881
0
    default:
8882
0
        md_size = 0;
8883
0
        break;
8884
0
    }
8885
0
    size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_PUSH_NSH);
8886
0
    nsh_key_to_attr(odp_actions, &flow->nsh, metadata, md_size, false);
8887
0
    nl_msg_end_nested(odp_actions, offset);
8888
0
}
8889
8890
static void
8891
commit_encap_decap_action(const struct flow *flow,
8892
                          struct flow *base_flow,
8893
                          struct ofpbuf *odp_actions,
8894
                          struct flow_wildcards *wc,
8895
                          bool pending_encap, bool pending_decap,
8896
                          struct ofpbuf *encap_data)
8897
0
{
8898
0
    if (pending_encap) {
8899
0
        switch (ntohl(flow->packet_type)) {
8900
0
        case PT_ETH: {
8901
            /* push_eth */
8902
0
            odp_put_push_eth_action(odp_actions, &flow->dl_src,
8903
0
                                    &flow->dl_dst);
8904
0
            base_flow->packet_type = flow->packet_type;
8905
0
            base_flow->dl_src = flow->dl_src;
8906
0
            base_flow->dl_dst = flow->dl_dst;
8907
0
            break;
8908
0
        }
8909
0
        case PT_NSH:
8910
            /* push_nsh */
8911
0
            odp_put_push_nsh_action(odp_actions, flow, encap_data);
8912
0
            base_flow->packet_type = flow->packet_type;
8913
            /* Update all packet headers in base_flow. */
8914
0
            memcpy((char *) base_flow + offsetof(struct flow, dl_dst),
8915
0
                   (const char *) flow + offsetof(struct flow, dl_dst),
8916
0
                   sizeof(*flow) - offsetof(struct flow, dl_dst));
8917
0
            break;
8918
0
        case PT_MPLS:
8919
0
        case PT_MPLS_MC:
8920
0
            commit_mpls_action(flow, base_flow, odp_actions,
8921
0
                               pending_encap);
8922
0
            break;
8923
0
        default:
8924
            /* Only the above protocols are supported for encap.
8925
             * The check is done at action translation. */
8926
0
            OVS_NOT_REACHED();
8927
0
        }
8928
0
    } else if (pending_decap || flow->packet_type != base_flow->packet_type) {
8929
        /* This is an explicit or implicit decap case. */
8930
0
        if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE &&
8931
0
            base_flow->packet_type == htonl(PT_ETH)) {
8932
            /* Generate pop_eth and continue without recirculation. */
8933
0
            odp_put_pop_eth_action(odp_actions);
8934
0
            base_flow->packet_type = flow->packet_type;
8935
0
            base_flow->dl_src = eth_addr_zero;
8936
0
            base_flow->dl_dst = eth_addr_zero;
8937
0
        } else {
8938
            /* All other decap cases require recirculation.
8939
             * No need to update the base flow here. */
8940
0
            switch (ntohl(base_flow->packet_type)) {
8941
0
            case PT_NSH:
8942
                /* pop_nsh. */
8943
0
                odp_put_pop_nsh_action(odp_actions);
8944
0
                break;
8945
0
            case PT_MPLS:
8946
0
            case PT_MPLS_MC:
8947
0
                commit_mpls_action(flow, base_flow, odp_actions,
8948
0
                                   pending_encap);
8949
0
                break;
8950
0
            default:
8951
                /* Checks are done during translation. */
8952
0
                OVS_NOT_REACHED();
8953
0
            }
8954
0
        }
8955
0
    }
8956
8957
0
    wc->masks.packet_type = OVS_BE32_MAX;
8958
0
}
8959
8960
/* If any of the flow key data that ODP actions can modify are different in
8961
 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
8962
 * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
8963
 * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
8964
 * in addition to this function if needed.  Sets fields in 'wc' that are
8965
 * used as part of the action.
8966
 *
8967
 * In the common case, this function returns 0.  If the flow key modification
8968
 * requires the flow's packets to be forced into the userspace slow path, this
8969
 * function returns SLOW_ACTION.  This only happens when there is no ODP action
8970
 * to modify some field that was actually modified.  For example, there is no
8971
 * ODP action to modify any ARP field, so such a modification triggers
8972
 * SLOW_ACTION.  (When this happens, packets that need such modification get
8973
 * flushed to userspace and handled there, which works OK but much more slowly
8974
 * than if the datapath handled it directly.) */
8975
enum slow_path_reason
8976
commit_odp_actions(const struct flow *flow, struct flow *base,
8977
                   struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8978
                   bool use_masked, bool pending_encap, bool pending_decap,
8979
                   struct ofpbuf *encap_data)
8980
0
{
8981
    /* If you add a field that OpenFlow actions can change, and that is visible
8982
     * to the datapath (including all data fields), then you should also add
8983
     * code here to commit changes to the field. */
8984
0
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
8985
8986
0
    enum slow_path_reason slow1, slow2;
8987
0
    bool mpls_done = false;
8988
8989
0
    commit_encap_decap_action(flow, base, odp_actions, wc,
8990
0
                              pending_encap, pending_decap, encap_data);
8991
0
    commit_set_ether_action(flow, base, odp_actions, wc, use_masked);
8992
    /* Make packet a non-MPLS packet before committing L3/4 actions,
8993
     * which would otherwise do nothing. */
8994
0
    if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) {
8995
0
        commit_mpls_action(flow, base, odp_actions, false);
8996
0
        mpls_done = true;
8997
0
    }
8998
0
    commit_set_nsh_action(flow, base, odp_actions, wc, use_masked);
8999
0
    slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
9000
0
    commit_set_port_action(flow, base, odp_actions, wc, use_masked);
9001
0
    slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
9002
0
    if (!mpls_done) {
9003
0
        commit_mpls_action(flow, base, odp_actions, false);
9004
0
    }
9005
0
    commit_vlan_action(flow, base, odp_actions, wc);
9006
0
    commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
9007
0
    commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
9008
9009
0
    return slow1 ? slow1 : slow2;
9010
0
}
9011
9012
int
9013
odp_vxlan_tun_opts_from_attr(const struct nlattr *tun_attr, ovs_be16 *id,
9014
                             uint8_t *flags, bool *id_present)
9015
0
{
9016
0
    static const struct nl_policy vxlan_opts_policy[] = {
9017
0
        [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
9018
0
    };
9019
0
    struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
9020
9021
0
    if (!nl_parse_nested(tun_attr, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
9022
0
        return EINVAL;
9023
0
    }
9024
9025
0
    if (ext[OVS_VXLAN_EXT_GBP]) {
9026
0
        uint32_t gbp_raw = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
9027
9028
0
        odp_decode_gbp_raw(gbp_raw, id, flags);
9029
0
    }
9030
9031
0
    if (id_present) {
9032
0
        *id_present = !!ext[OVS_VXLAN_EXT_GBP];
9033
0
    }
9034
9035
0
    return 0;
9036
0
}