Coverage Report

Created: 2026-06-22 06:39

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
0
        ret = n;
2500
0
    }
2501
0
    return ret;
2502
0
}
2503
2504
static int
2505
parse_odp_psample_action(const char *s, struct ofpbuf *actions)
2506
0
{
2507
0
    char buf[2 * OVS_PSAMPLE_COOKIE_MAX_SIZE + 1];
2508
0
    uint8_t cookie[OVS_PSAMPLE_COOKIE_MAX_SIZE];
2509
0
    bool has_group = false;
2510
0
    size_t cookie_len = 0;
2511
0
    uint32_t group;
2512
0
    int n = 0;
2513
2514
0
    if (!ovs_scan_len(s, &n, "psample(")) {
2515
0
        return -EINVAL;
2516
0
    }
2517
2518
0
    while (s[n] != ')') {
2519
0
        n += strspn(s + n, delimiters);
2520
2521
0
        if (!has_group && ovs_scan_len(s, &n, "group=%"SCNi32, &group)) {
2522
0
            has_group = true;
2523
0
            continue;
2524
0
        }
2525
2526
0
        if (!cookie_len &&
2527
0
            ovs_scan_len(s, &n, "cookie=0x%32[0-9a-fA-F]", buf) && n > 7) {
2528
0
            struct ofpbuf b;
2529
2530
0
            ofpbuf_use_stub(&b, cookie, OVS_PSAMPLE_COOKIE_MAX_SIZE);
2531
0
            ofpbuf_put_hex(&b, buf, &cookie_len);
2532
0
            ofpbuf_uninit(&b);
2533
0
            continue;
2534
0
        }
2535
0
        return -EINVAL;
2536
0
    }
2537
0
    n++;
2538
2539
0
    if (!has_group) {
2540
0
        return -EINVAL;
2541
0
    }
2542
2543
0
    odp_put_psample_action(actions, group, cookie_len ? cookie : NULL,
2544
0
                           cookie_len);
2545
0
    return n;
2546
0
}
2547
2548
static int
2549
parse_action_list(struct parse_odp_context *context, const char *s,
2550
                  struct ofpbuf *actions)
2551
0
{
2552
0
    int n = 0;
2553
2554
0
    for (;;) {
2555
0
        int retval;
2556
2557
0
        n += strspn(s + n, delimiters);
2558
0
        if (s[n] == ')') {
2559
0
            break;
2560
0
        }
2561
0
        retval = parse_odp_action(context, s + n, actions);
2562
0
        if (retval < 0) {
2563
0
            return retval;
2564
0
        } else if (nl_attr_oversized(actions->size - NLA_HDRLEN)) {
2565
0
            return -E2BIG;
2566
0
        }
2567
0
        n += retval;
2568
0
    }
2569
2570
0
    return n;
2571
0
}
2572
2573
2574
static int
2575
parse_odp_action(struct parse_odp_context *context, const char *s,
2576
                 struct ofpbuf *actions)
2577
0
{
2578
0
    int retval;
2579
2580
0
    context->depth++;
2581
2582
0
    if (context->depth == MAX_ODP_NESTED) {
2583
0
        retval = -EINVAL;
2584
0
    } else {
2585
0
        retval = parse_odp_action__(context, s, actions);
2586
0
    }
2587
2588
0
    context->depth--;
2589
2590
0
    return retval;
2591
0
}
2592
2593
2594
static int
2595
parse_odp_action__(struct parse_odp_context *context, const char *s,
2596
                   struct ofpbuf *actions)
2597
0
{
2598
0
    {
2599
0
        uint32_t port;
2600
0
        int n;
2601
2602
0
        if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
2603
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
2604
0
            return n;
2605
0
        }
2606
0
    }
2607
2608
0
    {
2609
0
        uint32_t bond_id;
2610
0
        int n;
2611
2612
0
        if (ovs_scan(s, "lb_output(%"PRIu32")%n", &bond_id, &n)) {
2613
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_LB_OUTPUT, bond_id);
2614
0
            return n;
2615
0
        }
2616
0
    }
2617
2618
0
    {
2619
0
        uint32_t max_len;
2620
0
        int n;
2621
2622
0
        if (ovs_scan(s, "trunc(%"SCNi32")%n", &max_len, &n)) {
2623
0
            struct ovs_action_trunc *trunc;
2624
2625
0
            trunc = nl_msg_put_unspec_uninit(actions,
2626
0
                     OVS_ACTION_ATTR_TRUNC, sizeof *trunc);
2627
0
            trunc->max_len = max_len;
2628
0
            return n;
2629
0
        }
2630
0
    }
2631
2632
0
    if (context->port_names) {
2633
0
        int len = strcspn(s, delimiters);
2634
0
        struct simap_node *node;
2635
2636
0
        node = simap_find_len(context->port_names, s, len);
2637
0
        if (node) {
2638
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
2639
0
            return len;
2640
0
        }
2641
0
    }
2642
2643
0
    {
2644
0
        uint32_t recirc_id;
2645
0
        int n = -1;
2646
2647
0
        if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
2648
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
2649
0
            return n;
2650
0
        }
2651
0
    }
2652
2653
0
    if (!strncmp(s, "userspace(", 10)) {
2654
0
        return parse_odp_userspace_action(s, actions);
2655
0
    }
2656
2657
0
    if (!strncmp(s, "set(", 4)) {
2658
0
        size_t start_ofs;
2659
0
        int retval;
2660
0
        struct nlattr mask[1024 / sizeof(struct nlattr)];
2661
0
        struct ofpbuf maskbuf = OFPBUF_STUB_INITIALIZER(mask);
2662
0
        struct nlattr *nested, *key;
2663
0
        size_t size;
2664
2665
0
        start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
2666
0
        retval = parse_odp_key_mask_attr(context, s + 4, actions, &maskbuf);
2667
0
        if (retval < 0) {
2668
0
            ofpbuf_uninit(&maskbuf);
2669
0
            return retval;
2670
0
        }
2671
0
        if (s[retval + 4] != ')') {
2672
0
            ofpbuf_uninit(&maskbuf);
2673
0
            return -EINVAL;
2674
0
        }
2675
2676
0
        nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2677
0
        key = nested + 1;
2678
2679
0
        size = nl_attr_get_size(mask);
2680
0
        if (size == nl_attr_get_size(key)) {
2681
            /* Change to masked set action if not fully masked. */
2682
0
            if (!is_all_ones(mask + 1, size)) {
2683
                /* Remove padding of eariler key payload  */
2684
0
                actions->size -= NLA_ALIGN(key->nla_len) - key->nla_len;
2685
2686
                /* Put mask payload right after key payload */
2687
0
                key->nla_len += size;
2688
0
                ofpbuf_put(actions, mask + 1, size);
2689
2690
                /* 'actions' may have been reallocated by ofpbuf_put(). */
2691
0
                nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2692
0
                nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
2693
2694
0
                key = nested + 1;
2695
                /* Add new padding as needed */
2696
0
                ofpbuf_put_zeros(actions, NLA_ALIGN(key->nla_len) -
2697
0
                                          key->nla_len);
2698
0
            }
2699
0
        }
2700
0
        ofpbuf_uninit(&maskbuf);
2701
2702
0
        nl_msg_end_nested(actions, start_ofs);
2703
0
        return retval + 5;
2704
0
    }
2705
2706
0
    {
2707
0
        struct ovs_action_push_vlan push;
2708
0
        int tpid = ETH_TYPE_VLAN;
2709
0
        int vid, pcp;
2710
0
        int cfi = 1;
2711
0
        int n = -1;
2712
2713
0
        if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
2714
0
            || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2715
0
                        &vid, &pcp, &cfi, &n)
2716
0
            || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2717
0
                        &tpid, &vid, &pcp, &n)
2718
0
            || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2719
0
                        &tpid, &vid, &pcp, &cfi, &n)) {
2720
0
            if ((vid & ~(VLAN_VID_MASK >> VLAN_VID_SHIFT)) != 0
2721
0
                || (pcp & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) != 0) {
2722
0
                return -EINVAL;
2723
0
            }
2724
0
            push.vlan_tpid = htons(tpid);
2725
0
            push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
2726
0
                                  | (pcp << VLAN_PCP_SHIFT)
2727
0
                                  | (cfi ? VLAN_CFI : 0));
2728
0
            nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
2729
0
                              &push, sizeof push);
2730
2731
0
            return n;
2732
0
        }
2733
0
    }
2734
2735
0
    if (!strncmp(s, "pop_vlan", 8)) {
2736
0
        nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
2737
0
        return 8;
2738
0
    }
2739
2740
0
    {
2741
0
        unsigned long long int meter_id;
2742
0
        int n = -1;
2743
2744
0
        if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) {
2745
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id);
2746
0
            return n;
2747
0
        }
2748
0
    }
2749
2750
0
    {
2751
0
        double percentage;
2752
0
        int n = -1;
2753
2754
0
        if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
2755
0
            && percentage >= 0. && percentage <= 100.0) {
2756
0
            size_t sample_ofs, actions_ofs;
2757
0
            double probability;
2758
2759
0
            probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
2760
0
            sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
2761
0
            nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
2762
0
                           (probability <= 0 ? 0
2763
0
                            : probability >= UINT32_MAX ? UINT32_MAX
2764
0
                            : probability));
2765
2766
0
            actions_ofs = nl_msg_start_nested(actions,
2767
0
                                              OVS_SAMPLE_ATTR_ACTIONS);
2768
0
            int retval = parse_action_list(context, s + n, actions);
2769
0
            if (retval < 0) {
2770
0
                return retval;
2771
0
            }
2772
2773
2774
0
            n += retval;
2775
0
            nl_msg_end_nested(actions, actions_ofs);
2776
0
            nl_msg_end_nested(actions, sample_ofs);
2777
2778
0
            return s[n + 1] == ')' ? n + 2 : -EINVAL;
2779
0
        }
2780
0
    }
2781
2782
0
    {
2783
0
        if (!strncmp(s, "clone(", 6)) {
2784
0
            size_t actions_ofs;
2785
0
            int n = 6;
2786
2787
0
            actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE);
2788
0
            int retval = parse_action_list(context, s + n, actions);
2789
0
            if (retval < 0) {
2790
0
                return retval;
2791
0
            }
2792
0
            n += retval;
2793
0
            nl_msg_end_nested(actions, actions_ofs);
2794
0
            return n + 1;
2795
0
        }
2796
0
    }
2797
2798
0
    {
2799
0
        if (!strncmp(s, "push_nsh(", 9)) {
2800
0
            int retval = parse_odp_push_nsh_action(s, actions);
2801
0
            if (retval < 0) {
2802
0
                return retval;
2803
0
            }
2804
0
            return retval + 1;
2805
0
        }
2806
0
    }
2807
2808
0
    {
2809
0
        int n;
2810
0
        if (ovs_scan(s, "pop_nsh()%n", &n)) {
2811
0
            nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_NSH);
2812
0
            return n;
2813
0
        }
2814
0
    }
2815
2816
0
    {
2817
0
        uint32_t port;
2818
0
        int n;
2819
2820
0
        if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
2821
0
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
2822
0
            return n;
2823
0
        }
2824
0
    }
2825
2826
0
    {
2827
0
        if (!strncmp(s, "ct_clear", 8)) {
2828
0
            nl_msg_put_flag(actions, OVS_ACTION_ATTR_CT_CLEAR);
2829
0
            return 8;
2830
0
        }
2831
0
    }
2832
2833
0
    {
2834
0
        uint16_t pkt_len;
2835
0
        int n = -1;
2836
0
        if (ovs_scan(s, "check_pkt_len(size=%"SCNi16",gt(%n", &pkt_len, &n)) {
2837
0
            size_t cpl_ofs, actions_ofs;
2838
0
            cpl_ofs = nl_msg_start_nested(actions,
2839
0
                                          OVS_ACTION_ATTR_CHECK_PKT_LEN);
2840
0
            nl_msg_put_u16(actions, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, pkt_len);
2841
0
            actions_ofs = nl_msg_start_nested(
2842
0
                actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
2843
2844
0
            int retval;
2845
0
            if (!strncasecmp(s + n, "drop", 4)) {
2846
0
                n += 4;
2847
0
            } else {
2848
0
                retval = parse_action_list(context, s + n, actions);
2849
0
                if (retval < 0) {
2850
0
                    return retval;
2851
0
                }
2852
2853
0
                n += retval;
2854
0
            }
2855
0
            nl_msg_end_nested(actions, actions_ofs);
2856
0
            retval = -1;
2857
0
            if (!ovs_scan(s + n, "),le(%n", &retval)) {
2858
0
                return -EINVAL;
2859
0
            }
2860
0
            n += retval;
2861
2862
0
            actions_ofs = nl_msg_start_nested(
2863
0
                actions, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
2864
0
            if (!strncasecmp(s + n, "drop", 4)) {
2865
0
                n += 4;
2866
0
            } else {
2867
0
                retval = parse_action_list(context, s + n, actions);
2868
0
                if (retval < 0) {
2869
0
                    return retval;
2870
0
                }
2871
0
                n += retval;
2872
0
            }
2873
0
            nl_msg_end_nested(actions, actions_ofs);
2874
0
            nl_msg_end_nested(actions, cpl_ofs);
2875
0
            return s[n + 1] == ')' ? n + 2 : -EINVAL;
2876
0
        }
2877
0
    }
2878
2879
0
    {
2880
0
        int retval;
2881
2882
0
        retval = parse_conntrack_action(s, actions);
2883
0
        if (retval) {
2884
0
            return retval;
2885
0
        }
2886
0
    }
2887
0
    {
2888
0
        struct ovs_action_add_mpls mpls;
2889
0
        uint8_t ttl, tc, bos;
2890
0
        uint16_t eth_type;
2891
0
        uint32_t lse;
2892
0
        int n = -1;
2893
2894
0
        if (ovs_scan(s, "add_mpls(label=%"SCNi32",tc=%"SCNd8",ttl=%"SCNd8","
2895
0
                                 "bos=%"SCNd8",eth_type=0x%"SCNx16")%n",
2896
0
                     &lse, &tc, &ttl, &bos, &eth_type, &n)) {
2897
0
            mpls.mpls_ethertype = htons(eth_type);
2898
0
            mpls.mpls_lse = htonl((lse << MPLS_LABEL_SHIFT) |
2899
0
                                  (tc  << MPLS_TC_SHIFT)    |
2900
0
                                  (ttl << MPLS_TTL_SHIFT)   |
2901
0
                                  (bos << MPLS_BOS_SHIFT));
2902
0
            mpls.tun_flags = 0;
2903
0
            nl_msg_put_unspec(actions, OVS_ACTION_ATTR_ADD_MPLS,
2904
0
                              &mpls, sizeof mpls);
2905
0
            return n;
2906
0
        }
2907
0
    }
2908
2909
0
    if (!strncmp(s, "psample(", 8)) {
2910
0
        return parse_odp_psample_action(s, actions);
2911
0
    }
2912
2913
0
    {
2914
0
        struct ovs_action_push_tnl data;
2915
0
        int n;
2916
2917
0
        n = ovs_parse_tnl_push(s, &data);
2918
0
        if (n > 0) {
2919
0
            odp_put_tnl_push_action(actions, &data);
2920
0
            return n;
2921
0
        } else if (n < 0) {
2922
0
            return n;
2923
0
        }
2924
0
    }
2925
2926
0
    return -EINVAL;
2927
0
}
2928
2929
/* Parses the string representation of datapath actions, in the format output
2930
 * by format_odp_action().  Returns 0 if successful, otherwise a positive errno
2931
 * value.  On success, the ODP actions are appended to 'actions' as a series of
2932
 * Netlink attributes.  On failure, no data is appended to 'actions'.  Either
2933
 * way, 'actions''s data might be reallocated. */
2934
int
2935
odp_actions_from_string(const char *s, const struct simap *port_names,
2936
                        struct ofpbuf *actions)
2937
0
{
2938
0
    size_t old_size;
2939
2940
0
    if (!strcasecmp(s, "drop")) {
2941
0
        nl_msg_put_u32(actions, OVS_ACTION_ATTR_DROP, XLATE_OK);
2942
0
        return 0;
2943
0
    }
2944
2945
0
    struct parse_odp_context context = (struct parse_odp_context) {
2946
0
        .port_names = port_names,
2947
0
    };
2948
2949
0
    old_size = actions->size;
2950
0
    for (;;) {
2951
0
        int retval;
2952
2953
0
        s += strspn(s, delimiters);
2954
0
        if (!*s) {
2955
0
            return 0;
2956
0
        }
2957
2958
0
        retval = parse_odp_action(&context, s, actions);
2959
2960
0
        if (retval >= 0 && nl_attr_oversized(actions->size - NLA_HDRLEN)) {
2961
0
            retval = -E2BIG;
2962
0
        }
2963
2964
0
        if (retval < 0 || !strchr(delimiters, s[retval])) {
2965
0
            actions->size = old_size;
2966
0
            return -retval;
2967
0
        }
2968
0
        s += retval;
2969
0
    }
2970
2971
0
    return 0;
2972
0
}
2973

2974
static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
2975
    [OVS_VXLAN_EXT_GBP]                 = { .len = 4 },
2976
};
2977
2978
static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
2979
    [OVS_TUNNEL_KEY_ATTR_ID]            = { .len = 8 },
2980
    [OVS_TUNNEL_KEY_ATTR_IPV4_SRC]      = { .len = 4 },
2981
    [OVS_TUNNEL_KEY_ATTR_IPV4_DST]      = { .len = 4 },
2982
    [OVS_TUNNEL_KEY_ATTR_TOS]           = { .len = 1 },
2983
    [OVS_TUNNEL_KEY_ATTR_TTL]           = { .len = 1 },
2984
    [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
2985
    [OVS_TUNNEL_KEY_ATTR_CSUM]          = { .len = 0 },
2986
    [OVS_TUNNEL_KEY_ATTR_TP_SRC]        = { .len = 2 },
2987
    [OVS_TUNNEL_KEY_ATTR_TP_DST]        = { .len = 2 },
2988
    [OVS_TUNNEL_KEY_ATTR_OAM]           = { .len = 0 },
2989
    [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS]   = { .len = ATTR_LEN_VARIABLE },
2990
    [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS]    = { .len = ATTR_LEN_NESTED,
2991
                                            .next = ovs_vxlan_ext_attr_lens ,
2992
                                            .next_max = OVS_VXLAN_EXT_MAX},
2993
    [OVS_TUNNEL_KEY_ATTR_IPV6_SRC]      = { .len = 16 },
2994
    [OVS_TUNNEL_KEY_ATTR_IPV6_DST]      = { .len = 16 },
2995
    [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS]   = { .len = ATTR_LEN_VARIABLE },
2996
    [OVS_TUNNEL_KEY_ATTR_GTPU_OPTS]   = { .len = ATTR_LEN_VARIABLE },
2997
};
2998
2999
const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
3000
    [OVS_KEY_ATTR_ENCAP]     = { .len = ATTR_LEN_NESTED },
3001
    [OVS_KEY_ATTR_PRIORITY]  = { .len = 4 },
3002
    [OVS_KEY_ATTR_SKB_MARK]  = { .len = 4 },
3003
    [OVS_KEY_ATTR_DP_HASH]   = { .len = 4 },
3004
    [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
3005
    [OVS_KEY_ATTR_TUNNEL]    = { .len = ATTR_LEN_NESTED,
3006
                                 .next = ovs_tun_key_attr_lens,
3007
                                 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
3008
    [OVS_KEY_ATTR_IN_PORT]   = { .len = 4  },
3009
    [OVS_KEY_ATTR_ETHERNET]  = { .len = sizeof(struct ovs_key_ethernet) },
3010
    [OVS_KEY_ATTR_VLAN]      = { .len = 2 },
3011
    [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
3012
    [OVS_KEY_ATTR_MPLS]      = { .len = ATTR_LEN_VARIABLE },
3013
    [OVS_KEY_ATTR_IPV4]      = { .len = sizeof(struct ovs_key_ipv4) },
3014
    [OVS_KEY_ATTR_IPV6]      = { .len = sizeof(struct ovs_key_ipv6) },
3015
    [OVS_KEY_ATTR_TCP]       = { .len = sizeof(struct ovs_key_tcp) },
3016
    [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
3017
    [OVS_KEY_ATTR_UDP]       = { .len = sizeof(struct ovs_key_udp) },
3018
    [OVS_KEY_ATTR_SCTP]      = { .len = sizeof(struct ovs_key_sctp) },
3019
    [OVS_KEY_ATTR_ICMP]      = { .len = sizeof(struct ovs_key_icmp) },
3020
    [OVS_KEY_ATTR_ICMPV6]    = { .len = sizeof(struct ovs_key_icmpv6) },
3021
    [OVS_KEY_ATTR_ARP]       = { .len = sizeof(struct ovs_key_arp) },
3022
    [OVS_KEY_ATTR_ND]        = { .len = sizeof(struct ovs_key_nd) },
3023
    [OVS_KEY_ATTR_ND_EXTENSIONS] = { .len = sizeof(struct ovs_key_nd_extensions) },
3024
    [OVS_KEY_ATTR_CT_STATE]  = { .len = 4 },
3025
    [OVS_KEY_ATTR_CT_ZONE]   = { .len = 2 },
3026
    [OVS_KEY_ATTR_CT_MARK]   = { .len = 4 },
3027
    [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
3028
    [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = { .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
3029
    [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = { .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
3030
    [OVS_KEY_ATTR_PACKET_TYPE] = { .len = 4  },
3031
    [OVS_KEY_ATTR_NSH]       = { .len = ATTR_LEN_NESTED,
3032
                                 .next = ovs_nsh_key_attr_lens,
3033
                                 .next_max = OVS_NSH_KEY_ATTR_MAX },
3034
};
3035
3036
/* Returns the correct length of the payload for a flow key attribute of the
3037
 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
3038
 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
3039
 * payload is a nested type. */
3040
static int
3041
odp_key_attr_len(const struct attr_len_tbl tbl[], int max_type, uint16_t type)
3042
0
{
3043
0
    if (type > max_type) {
3044
0
        return ATTR_LEN_INVALID;
3045
0
    }
3046
3047
0
    return tbl[type].len;
3048
0
}
3049
3050
static void
3051
format_generic_odp_key(const struct nlattr *a, struct ds *ds)
3052
0
{
3053
0
    size_t len = nl_attr_get_size(a);
3054
0
    if (len) {
3055
0
        const uint8_t *unspec;
3056
0
        unsigned int i;
3057
3058
0
        unspec = nl_attr_get(a);
3059
0
        for (i = 0; i < len; i++) {
3060
0
            if (i) {
3061
0
                ds_put_char(ds, ' ');
3062
0
            }
3063
0
            ds_put_format(ds, "%02x", unspec[i]);
3064
0
        }
3065
0
    }
3066
0
}
3067
3068
static const char *
3069
ovs_frag_type_to_string(enum ovs_frag_type type)
3070
0
{
3071
0
    switch (type) {
3072
0
    case OVS_FRAG_TYPE_NONE:
3073
0
        return "no";
3074
0
    case OVS_FRAG_TYPE_FIRST:
3075
0
        return "first";
3076
0
    case OVS_FRAG_TYPE_LATER:
3077
0
        return "later";
3078
0
    case __OVS_FRAG_TYPE_MAX:
3079
0
    default:
3080
0
        return "<error>";
3081
0
    }
3082
0
}
3083
3084
enum odp_key_fitness
3085
odp_nsh_hdr_from_attr(const struct nlattr *attr,
3086
                      struct nsh_hdr *nsh_hdr, size_t size)
3087
0
{
3088
0
    unsigned int left;
3089
0
    const struct nlattr *a;
3090
0
    bool unknown = false;
3091
0
    uint8_t flags = 0;
3092
0
    uint8_t ttl = 63;
3093
0
    size_t mdlen = 0;
3094
0
    bool has_md1 = false;
3095
0
    bool has_md2 = false;
3096
3097
0
    memset(nsh_hdr, 0, size);
3098
3099
0
    NL_NESTED_FOR_EACH (a, left, attr) {
3100
0
        uint16_t type = nl_attr_type(a);
3101
0
        size_t len = nl_attr_get_size(a);
3102
0
        int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
3103
0
                                            OVS_NSH_KEY_ATTR_MAX, type);
3104
3105
0
        if (len != expected_len && expected_len >= 0) {
3106
0
            return ODP_FIT_ERROR;
3107
0
        }
3108
3109
0
        switch (type) {
3110
0
        case OVS_NSH_KEY_ATTR_BASE: {
3111
0
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
3112
0
            nsh_hdr->next_proto = base->np;
3113
0
            nsh_hdr->md_type = base->mdtype;
3114
0
            put_16aligned_be32(&nsh_hdr->path_hdr, base->path_hdr);
3115
0
            flags = base->flags;
3116
0
            ttl = base->ttl;
3117
0
            break;
3118
0
        }
3119
0
        case OVS_NSH_KEY_ATTR_MD1: {
3120
0
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
3121
0
            struct nsh_md1_ctx *md1_dst = &nsh_hdr->md1;
3122
0
            has_md1 = true;
3123
0
            mdlen = nl_attr_get_size(a);
3124
0
            if ((mdlen + NSH_BASE_HDR_LEN != NSH_M_TYPE1_LEN) ||
3125
0
                (mdlen + NSH_BASE_HDR_LEN > size)) {
3126
0
                return ODP_FIT_ERROR;
3127
0
            }
3128
0
            memcpy(md1_dst, md1, mdlen);
3129
0
            break;
3130
0
        }
3131
0
        case OVS_NSH_KEY_ATTR_MD2: {
3132
0
            struct nsh_md2_tlv *md2_dst = &nsh_hdr->md2;
3133
0
            const uint8_t *md2 = nl_attr_get(a);
3134
0
            has_md2 = true;
3135
0
            mdlen = nl_attr_get_size(a);
3136
0
            if (mdlen + NSH_BASE_HDR_LEN > size) {
3137
0
                return ODP_FIT_ERROR;
3138
0
            }
3139
0
            memcpy(md2_dst, md2, mdlen);
3140
0
            break;
3141
0
        }
3142
0
        default:
3143
            /* Allow this to show up as unexpected, if there are unknown
3144
             * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3145
0
            unknown = true;
3146
0
            break;
3147
0
        }
3148
0
    }
3149
3150
0
    if (unknown) {
3151
0
        return ODP_FIT_TOO_MUCH;
3152
0
    }
3153
3154
0
    if ((has_md1 && nsh_hdr->md_type != NSH_M_TYPE1)
3155
0
        || (has_md2 && nsh_hdr->md_type != NSH_M_TYPE2)) {
3156
0
        return ODP_FIT_ERROR;
3157
0
    }
3158
3159
    /* nsh header length  = NSH_BASE_HDR_LEN + mdlen */
3160
0
    nsh_set_flags_ttl_len(nsh_hdr, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
3161
3162
0
    return ODP_FIT_PERFECT;
3163
0
}
3164
3165
/* Reports the error 'msg', which is formatted as with printf().
3166
 *
3167
 * If 'errorp' is nonnull, then some the wants the error report to come
3168
 * directly back to it, so the function stores the error message into '*errorp'
3169
 * (after first freeing it in case there's something there already).
3170
 *
3171
 * Otherwise, logs the message at WARN level, rate-limited. */
3172
static void OVS_PRINTF_FORMAT(3, 4)
3173
odp_parse_error(struct vlog_rate_limit *rl, char **errorp,
3174
                const char *msg, ...)
3175
0
{
3176
0
    if (OVS_UNLIKELY(errorp)) {
3177
0
        free(*errorp);
3178
3179
0
        va_list args;
3180
0
        va_start(args, msg);
3181
0
        *errorp = xvasprintf(msg, args);
3182
0
        va_end(args);
3183
0
    } else if (!VLOG_DROP_WARN(rl)) {
3184
0
        va_list args;
3185
0
        va_start(args, msg);
3186
0
        char *error = xvasprintf(msg, args);
3187
0
        va_end(args);
3188
3189
0
        VLOG_WARN("%s", error);
3190
3191
0
        free(error);
3192
0
    }
3193
0
}
3194
3195
/* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
3196
 * returns fitness.  If the attribute is a key, 'is_mask' should be false;
3197
 * if it is a mask, 'is_mask' should be true.  If 'errorp' is nonnull and the
3198
 * function returns ODP_FIT_ERROR, stores a malloc()'d error message in
3199
 * '*errorp'. */
3200
static enum odp_key_fitness
3201
odp_nsh_key_from_attr__(const struct nlattr *attr, bool is_mask,
3202
                        struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask,
3203
                        char **errorp)
3204
0
{
3205
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3206
0
    if (errorp) {
3207
0
        *errorp = NULL;
3208
0
    }
3209
3210
0
    unsigned int left;
3211
0
    const struct nlattr *a;
3212
0
    bool unknown = false;
3213
0
    bool has_md1 = false;
3214
3215
0
    NL_NESTED_FOR_EACH (a, left, attr) {
3216
0
        uint16_t type = nl_attr_type(a);
3217
0
        size_t len = nl_attr_get_size(a);
3218
0
        int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
3219
0
                                            OVS_NSH_KEY_ATTR_MAX, type);
3220
0
        if (expected_len) {
3221
0
            if (nsh_mask) {
3222
0
                expected_len *= 2;
3223
0
            }
3224
0
            if (len != expected_len) {
3225
0
                odp_parse_error(&rl, errorp, "NSH %s attribute %"PRIu16" "
3226
0
                                "should have length %d but actually has "
3227
0
                                "%"PRIuSIZE,
3228
0
                                nsh_mask ? "mask" : "key",
3229
0
                                type, expected_len, len);
3230
0
                return ODP_FIT_ERROR;
3231
0
            }
3232
0
        }
3233
3234
0
        switch (type) {
3235
0
        case OVS_NSH_KEY_ATTR_UNSPEC:
3236
0
            break;
3237
0
        case OVS_NSH_KEY_ATTR_BASE: {
3238
0
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
3239
0
            nsh->flags = base->flags;
3240
0
            nsh->ttl = base->ttl;
3241
0
            nsh->mdtype = base->mdtype;
3242
0
            nsh->np = base->np;
3243
0
            nsh->path_hdr = base->path_hdr;
3244
0
            if (nsh_mask && (len == 2 * sizeof(*base))) {
3245
0
                const struct ovs_nsh_key_base *base_mask = base + 1;
3246
0
                nsh_mask->flags = base_mask->flags;
3247
0
                nsh_mask->ttl = base_mask->ttl;
3248
0
                nsh_mask->mdtype = base_mask->mdtype;
3249
0
                nsh_mask->np = base_mask->np;
3250
0
                nsh_mask->path_hdr = base_mask->path_hdr;
3251
0
            }
3252
0
            break;
3253
0
        }
3254
0
        case OVS_NSH_KEY_ATTR_MD1: {
3255
0
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
3256
0
            has_md1 = true;
3257
0
            memcpy(nsh->context, md1->context, sizeof md1->context);
3258
0
            if (nsh_mask && (len == 2 * sizeof *md1)) {
3259
0
                const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
3260
0
                memcpy(nsh_mask->context, md1_mask->context,
3261
0
                       sizeof(*md1_mask));
3262
0
            }
3263
0
            break;
3264
0
        }
3265
0
        case OVS_NSH_KEY_ATTR_MD2:
3266
0
        default:
3267
            /* Allow this to show up as unexpected, if there are unknown
3268
             * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3269
0
            unknown = true;
3270
0
            break;
3271
0
        }
3272
0
    }
3273
3274
0
    if (unknown) {
3275
0
        return ODP_FIT_TOO_MUCH;
3276
0
    }
3277
3278
0
    if (!is_mask && has_md1 && nsh->mdtype != NSH_M_TYPE1 && !nsh_mask) {
3279
0
        odp_parse_error(&rl, errorp, "OVS_NSH_KEY_ATTR_MD1 present but "
3280
0
                        "declared mdtype %"PRIu8" is not %d (NSH_M_TYPE1)",
3281
0
                        nsh->mdtype, NSH_M_TYPE1);
3282
0
        return ODP_FIT_ERROR;
3283
0
    }
3284
3285
0
    return ODP_FIT_PERFECT;
3286
0
}
3287
3288
/* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
3289
 * returns fitness.  The attribute should be a key (not a mask).  If 'errorp'
3290
 * is nonnull and the function returns ODP_FIT_ERROR, stores a malloc()'d error
3291
 * message in '*errorp'. */
3292
enum odp_key_fitness
3293
odp_nsh_key_from_attr(const struct nlattr *attr, struct ovs_key_nsh *nsh,
3294
                      struct ovs_key_nsh *nsh_mask, char **errorp)
3295
0
{
3296
0
    return odp_nsh_key_from_attr__(attr, false, nsh, nsh_mask, errorp);
3297
0
}
3298
3299
/* Parses OVS_KEY_ATTR_TUNNEL attribute 'attr' into 'tun' and returns fitness.
3300
 * If the attribute is a key, 'is_mask' should be false; if it is a mask,
3301
 * 'is_mask' should be true.  If 'errorp' is nonnull and the function returns
3302
 * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */
3303
static enum odp_key_fitness
3304
odp_tun_key_from_attr__(const struct nlattr *attr, bool is_mask,
3305
                        struct flow_tnl *tun, char **errorp)
3306
0
{
3307
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3308
0
    unsigned int left;
3309
0
    const struct nlattr *a;
3310
0
    bool ttl = false;
3311
0
    bool unknown = false;
3312
3313
0
    NL_NESTED_FOR_EACH(a, left, attr) {
3314
0
        uint16_t type = nl_attr_type(a);
3315
0
        size_t len = nl_attr_get_size(a);
3316
0
        int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
3317
0
                                            OVS_TUNNEL_ATTR_MAX, type);
3318
3319
0
        if (len != expected_len && expected_len >= 0) {
3320
0
            odp_parse_error(&rl, errorp, "tunnel key attribute %"PRIu16" "
3321
0
                            "should have length %d but actually has %"PRIuSIZE,
3322
0
                            type, expected_len, len);
3323
0
            return ODP_FIT_ERROR;
3324
0
        }
3325
3326
0
        switch (type) {
3327
0
        case OVS_TUNNEL_KEY_ATTR_ID:
3328
0
            tun->tun_id = nl_attr_get_be64(a);
3329
0
            tun->flags |= FLOW_TNL_F_KEY;
3330
0
            break;
3331
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
3332
0
            tun->ip_src = nl_attr_get_be32(a);
3333
0
            break;
3334
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
3335
0
            tun->ip_dst = nl_attr_get_be32(a);
3336
0
            break;
3337
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
3338
0
            tun->ipv6_src = nl_attr_get_in6_addr(a);
3339
0
            break;
3340
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
3341
0
            tun->ipv6_dst = nl_attr_get_in6_addr(a);
3342
0
            break;
3343
0
        case OVS_TUNNEL_KEY_ATTR_TOS:
3344
0
            tun->ip_tos = nl_attr_get_u8(a);
3345
0
            break;
3346
0
        case OVS_TUNNEL_KEY_ATTR_TTL:
3347
0
            tun->ip_ttl = nl_attr_get_u8(a);
3348
0
            ttl = true;
3349
0
            break;
3350
0
        case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3351
0
            tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
3352
0
            break;
3353
0
        case OVS_TUNNEL_KEY_ATTR_CSUM:
3354
0
            tun->flags |= FLOW_TNL_F_CSUM;
3355
0
            break;
3356
0
        case OVS_TUNNEL_KEY_ATTR_TP_SRC:
3357
0
            tun->tp_src = nl_attr_get_be16(a);
3358
0
            break;
3359
0
        case OVS_TUNNEL_KEY_ATTR_TP_DST:
3360
0
            tun->tp_dst = nl_attr_get_be16(a);
3361
0
            break;
3362
0
        case OVS_TUNNEL_KEY_ATTR_OAM:
3363
0
            tun->flags |= FLOW_TNL_F_OAM;
3364
0
            break;
3365
0
        case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
3366
0
            if (odp_vxlan_tun_opts_from_attr(a, &tun->gbp_id,
3367
0
                                             &tun->gbp_flags,
3368
0
                                             NULL)) {
3369
0
                odp_parse_error(&rl, errorp, "error parsing VXLAN options");
3370
0
                return ODP_FIT_ERROR;
3371
0
            }
3372
0
            break;
3373
0
        }
3374
0
        case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
3375
0
            tun_metadata_from_geneve_nlattr(a, is_mask, tun);
3376
0
            break;
3377
0
        case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: {
3378
0
            const struct erspan_metadata *opts = nl_attr_get(a);
3379
3380
0
            tun->erspan_ver = opts->version;
3381
0
            if (tun->erspan_ver == 1) {
3382
0
                tun->erspan_idx = ntohl(opts->u.index);
3383
0
            } else if (tun->erspan_ver == 2) {
3384
0
                tun->erspan_dir = opts->u.md2.dir;
3385
0
                tun->erspan_hwid = get_hwid(&opts->u.md2);
3386
0
            } else {
3387
0
                VLOG_WARN("%s invalid erspan version\n", __func__);
3388
0
            }
3389
0
            break;
3390
0
        }
3391
0
        case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS: {
3392
0
            const struct gtpu_metadata *opts = nl_attr_get(a);
3393
3394
0
            tun->gtpu_flags = opts->flags;
3395
0
            tun->gtpu_msgtype = opts->msgtype;
3396
0
            break;
3397
0
        }
3398
3399
0
        default:
3400
            /* Allow this to show up as unexpected, if there are unknown
3401
             * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3402
0
            unknown = true;
3403
0
            break;
3404
0
        }
3405
0
    }
3406
3407
0
    if (!ttl) {
3408
0
        odp_parse_error(&rl, errorp, "tunnel options missing TTL");
3409
0
        return ODP_FIT_ERROR;
3410
0
    }
3411
0
    if (unknown) {
3412
0
        return ODP_FIT_TOO_MUCH;
3413
0
    }
3414
0
    return ODP_FIT_PERFECT;
3415
0
}
3416
3417
/* Parses OVS_KEY_ATTR_TUNNEL key attribute 'attr' into 'tun' and returns
3418
 * fitness.  The attribute should be a key (not a mask).  If 'errorp' is
3419
 * nonnull, stores NULL into '*errorp' on success, otherwise a malloc()'d error
3420
 * message. */
3421
enum odp_key_fitness
3422
odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun,
3423
                      char **errorp)
3424
0
{
3425
0
    if (errorp) {
3426
0
        *errorp = NULL;
3427
0
    }
3428
0
    memset(tun, 0, sizeof *tun);
3429
0
    return odp_tun_key_from_attr__(attr, false, tun, errorp);
3430
0
}
3431
3432
static void
3433
tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key,
3434
                const struct flow_tnl *tun_flow_key,
3435
                const struct ofpbuf *key_buf, const char *tnl_type)
3436
0
{
3437
0
    size_t tun_key_ofs;
3438
3439
0
    tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
3440
3441
    /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
3442
0
    if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
3443
0
        nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
3444
0
    }
3445
0
    if (tun_key->ip_src) {
3446
0
        nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
3447
0
    }
3448
0
    if (tun_key->ip_dst) {
3449
0
        nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
3450
0
    }
3451
0
    if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
3452
0
        nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
3453
0
    }
3454
0
    if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
3455
0
        nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
3456
0
    }
3457
0
    if (tun_key->ip_tos) {
3458
0
        nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
3459
0
    }
3460
0
    nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
3461
0
    if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
3462
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3463
0
    }
3464
0
    if (tun_key->flags & FLOW_TNL_F_CSUM) {
3465
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3466
0
    }
3467
0
    if (tun_key->tp_src) {
3468
0
        nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
3469
0
    }
3470
0
    if (tun_key->tp_dst) {
3471
0
        nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
3472
0
    }
3473
0
    if (tun_key->flags & FLOW_TNL_F_OAM) {
3474
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3475
0
    }
3476
3477
    /* If tnl_type is set to a particular type of output tunnel,
3478
     * only put its relevant tunnel metadata to the nlattr.
3479
     * If tnl_type is NULL, put tunnel metadata according to the
3480
     * 'tun_key'.
3481
     */
3482
0
    if ((!tnl_type || !strcmp(tnl_type, "vxlan")) &&
3483
0
        (tun_key->gbp_flags || tun_key->gbp_id)) {
3484
0
        size_t vxlan_opts_ofs;
3485
0
        uint32_t gbp_raw;
3486
3487
0
        vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3488
0
        gbp_raw = odp_encode_gbp_raw(tun_key->gbp_flags, tun_key->gbp_id);
3489
0
        nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, gbp_raw);
3490
0
        nl_msg_end_nested(a, vxlan_opts_ofs);
3491
0
    }
3492
3493
0
    if (!tnl_type || !strcmp(tnl_type, "geneve")) {
3494
0
        tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
3495
0
    }
3496
3497
0
    if ((!tnl_type || !strcmp(tnl_type, "erspan") ||
3498
0
        !strcmp(tnl_type, "ip6erspan")) &&
3499
0
        (tun_key->erspan_ver == 1 || tun_key->erspan_ver == 2)) {
3500
0
        struct erspan_metadata *opts;
3501
3502
0
        opts = nl_msg_put_unspec_zero(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
3503
0
                                      sizeof *opts);
3504
0
        opts->version = tun_key->erspan_ver;
3505
0
        if (opts->version == 1) {
3506
0
            opts->u.index = htonl(tun_key->erspan_idx);
3507
0
        } else {
3508
0
            opts->u.md2.dir = tun_key->erspan_dir;
3509
0
            set_hwid(&opts->u.md2, tun_key->erspan_hwid);
3510
0
        }
3511
0
    }
3512
3513
0
    if ((!tnl_type || !strcmp(tnl_type, "gtpu")) &&
3514
0
        (tun_key->gtpu_flags && tun_key->gtpu_msgtype)) {
3515
0
        struct gtpu_metadata opts;
3516
3517
0
        opts.flags = tun_key->gtpu_flags;
3518
0
        opts.msgtype = tun_key->gtpu_msgtype;
3519
0
        nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS,
3520
0
                          &opts, sizeof(opts));
3521
0
    }
3522
0
    nl_msg_end_nested(a, tun_key_ofs);
3523
0
}
3524
3525
static bool
3526
odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size,
3527
                       int constant)
3528
0
{
3529
    /* Convert 'constant' to all the widths we need.  C conversion rules ensure
3530
     * that -1 becomes all-1-bits and 0 does not change. */
3531
0
    ovs_be16 be16 = (OVS_FORCE ovs_be16) constant;
3532
0
    uint32_t u32 = constant;
3533
0
    uint8_t u8 = constant;
3534
0
    const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any;
3535
3536
0
    switch (attr) {
3537
0
    case OVS_KEY_ATTR_UNSPEC:
3538
0
    case OVS_KEY_ATTR_ENCAP:
3539
0
    case OVS_KEY_ATTR_TUNNEL_INFO:
3540
0
    case __OVS_KEY_ATTR_MAX:
3541
0
    default:
3542
0
        return false;
3543
3544
0
    case OVS_KEY_ATTR_PRIORITY:
3545
0
    case OVS_KEY_ATTR_IN_PORT:
3546
0
    case OVS_KEY_ATTR_ETHERNET:
3547
0
    case OVS_KEY_ATTR_VLAN:
3548
0
    case OVS_KEY_ATTR_ETHERTYPE:
3549
0
    case OVS_KEY_ATTR_IPV4:
3550
0
    case OVS_KEY_ATTR_TCP:
3551
0
    case OVS_KEY_ATTR_UDP:
3552
0
    case OVS_KEY_ATTR_ICMP:
3553
0
    case OVS_KEY_ATTR_ICMPV6:
3554
0
    case OVS_KEY_ATTR_ND:
3555
0
    case OVS_KEY_ATTR_ND_EXTENSIONS:
3556
0
    case OVS_KEY_ATTR_SKB_MARK:
3557
0
    case OVS_KEY_ATTR_TUNNEL:
3558
0
    case OVS_KEY_ATTR_SCTP:
3559
0
    case OVS_KEY_ATTR_DP_HASH:
3560
0
    case OVS_KEY_ATTR_RECIRC_ID:
3561
0
    case OVS_KEY_ATTR_MPLS:
3562
0
    case OVS_KEY_ATTR_CT_STATE:
3563
0
    case OVS_KEY_ATTR_CT_ZONE:
3564
0
    case OVS_KEY_ATTR_CT_MARK:
3565
0
    case OVS_KEY_ATTR_CT_LABELS:
3566
0
    case OVS_KEY_ATTR_PACKET_TYPE:
3567
0
    case OVS_KEY_ATTR_NSH:
3568
0
        return is_all_byte(mask, size, u8);
3569
3570
0
    case OVS_KEY_ATTR_TCP_FLAGS:
3571
0
        return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16);
3572
3573
0
    case OVS_KEY_ATTR_IPV6: {
3574
0
        const struct ovs_key_ipv6 *ipv6_mask = mask;
3575
0
        return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
3576
0
                == htonl(IPV6_LABEL_MASK & u32)
3577
0
                && ipv6_mask->ipv6_proto == u8
3578
0
                && ipv6_mask->ipv6_tclass == u8
3579
0
                && ipv6_mask->ipv6_hlimit == u8
3580
0
                && ipv6_mask->ipv6_frag == u8
3581
0
                && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6)
3582
0
                && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6));
3583
0
    }
3584
3585
0
    case OVS_KEY_ATTR_ARP:
3586
0
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8);
3587
3588
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
3589
0
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4,
3590
0
                                             ipv4_proto), u8);
3591
3592
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
3593
0
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6,
3594
0
                                             ipv6_proto), u8);
3595
0
    }
3596
0
}
3597
3598
/* The caller must already have verified that 'ma' has a correct length.
3599
 *
3600
 * The main purpose of this function is formatting, to allow code to figure out
3601
 * whether the mask can be omitted.  It doesn't try hard for attributes that
3602
 * contain sub-attributes, etc., because normally those would be broken down
3603
 * further for formatting. */
3604
static bool
3605
odp_mask_attr_is_wildcard(const struct nlattr *ma)
3606
0
{
3607
0
    return odp_mask_is_constant__(nl_attr_type(ma),
3608
0
                                  nl_attr_get(ma), nl_attr_get_size(ma), 0);
3609
0
}
3610
3611
/* The caller must already have verified that 'size' is a correct length for
3612
 * 'attr'.
3613
 *
3614
 * The main purpose of this function is formatting, to allow code to figure out
3615
 * whether the mask can be omitted.  It doesn't try hard for attributes that
3616
 * contain sub-attributes, etc., because normally those would be broken down
3617
 * further for formatting. */
3618
static bool
3619
odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
3620
0
{
3621
0
    return odp_mask_is_constant__(attr, mask, size, -1);
3622
0
}
3623
3624
/* The caller must already have verified that 'ma' has a correct length. */
3625
static bool
3626
odp_mask_attr_is_exact(const struct nlattr *ma)
3627
0
{
3628
0
    enum ovs_key_attr attr = nl_attr_type(ma);
3629
0
    return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma));
3630
0
}
3631
3632
void
3633
odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
3634
                     char *port_name)
3635
0
{
3636
0
    struct odp_portno_names *odp_portno_names;
3637
3638
0
    odp_portno_names = xmalloc(sizeof *odp_portno_names);
3639
0
    odp_portno_names->port_no = port_no;
3640
0
    odp_portno_names->name = xstrdup(port_name);
3641
0
    hmap_insert(portno_names, &odp_portno_names->hmap_node,
3642
0
                hash_odp_port(port_no));
3643
0
}
3644
3645
static char *
3646
odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
3647
0
{
3648
0
    if (portno_names) {
3649
0
        struct odp_portno_names *odp_portno_names;
3650
3651
0
        HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
3652
0
                                 hash_odp_port(port_no), portno_names) {
3653
0
            if (odp_portno_names->port_no == port_no) {
3654
0
                return odp_portno_names->name;
3655
0
            }
3656
0
        }
3657
0
    }
3658
0
    return NULL;
3659
0
}
3660
3661
void
3662
odp_portno_names_destroy(struct hmap *portno_names)
3663
0
{
3664
0
    struct odp_portno_names *odp_portno_names;
3665
3666
0
    HMAP_FOR_EACH_POP (odp_portno_names, hmap_node, portno_names) {
3667
0
        free(odp_portno_names->name);
3668
0
        free(odp_portno_names);
3669
0
    }
3670
0
}
3671
3672
void
3673
odp_portno_name_format(const struct hmap *portno_names, odp_port_t port_no,
3674
                       struct ds *s)
3675
0
{
3676
0
    const char *name = odp_portno_names_get(portno_names, port_no);
3677
0
    if (name) {
3678
0
        ds_put_cstr(s, name);
3679
0
    } else {
3680
0
        ds_put_format(s, "%"PRIu32, port_no);
3681
0
    }
3682
0
}
3683
3684
/* Format helpers. */
3685
3686
static void
3687
format_eth(struct ds *ds, const char *name, const struct eth_addr key,
3688
           const struct eth_addr *mask, bool verbose)
3689
0
{
3690
0
    bool mask_empty = mask && eth_addr_is_zero(*mask);
3691
3692
0
    if (verbose || !mask_empty) {
3693
0
        bool mask_full = !mask || eth_mask_is_exact(*mask);
3694
3695
0
        if (mask_full) {
3696
0
            ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
3697
0
        } else {
3698
0
            ds_put_format(ds, "%s=", name);
3699
0
            eth_format_masked(key, mask, ds);
3700
0
            ds_put_char(ds, ',');
3701
0
        }
3702
0
    }
3703
0
}
3704
3705
3706
static void
3707
format_be64(struct ds *ds, const char *name, ovs_be64 key,
3708
            const ovs_32aligned_be64 *mask_, bool verbose)
3709
0
{
3710
0
    ovs_be64 mask = mask_ ? get_32aligned_be64(mask_) : htonll(0);
3711
3712
0
    if (verbose || mask) {
3713
0
        bool mask_full = !mask_ || mask == OVS_BE64_MAX;
3714
3715
0
        ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
3716
0
        if (!mask_full) { /* Partially masked. */
3717
0
            ds_put_format(ds, "/%#"PRIx64, ntohll(mask));
3718
0
        }
3719
0
        ds_put_char(ds, ',');
3720
0
    }
3721
0
}
3722
3723
static void
3724
format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
3725
            const ovs_be32 *mask, bool verbose)
3726
0
{
3727
0
    bool mask_empty = mask && !*mask;
3728
3729
0
    if (verbose || !mask_empty) {
3730
0
        bool mask_full = !mask || *mask == OVS_BE32_MAX;
3731
3732
0
        ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
3733
0
        if (!mask_full) { /* Partially masked. */
3734
0
            ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
3735
0
        }
3736
0
        ds_put_char(ds, ',');
3737
0
    }
3738
0
}
3739
3740
static void
3741
format_in6_addr(struct ds *ds, const char *name,
3742
                const struct in6_addr *key,
3743
                const struct in6_addr *mask,
3744
                bool verbose)
3745
0
{
3746
0
    char buf[INET6_ADDRSTRLEN];
3747
0
    bool mask_empty = mask && ipv6_mask_is_any(mask);
3748
3749
0
    if (verbose || !mask_empty) {
3750
0
        bool mask_full = !mask || ipv6_mask_is_exact(mask);
3751
3752
0
        inet_ntop(AF_INET6, key, buf, sizeof buf);
3753
0
        ds_put_format(ds, "%s=%s", name, buf);
3754
0
        if (!mask_full) { /* Partially masked. */
3755
0
            inet_ntop(AF_INET6, mask, buf, sizeof buf);
3756
0
            ds_put_format(ds, "/%s", buf);
3757
0
        }
3758
0
        ds_put_char(ds, ',');
3759
0
    }
3760
0
}
3761
3762
static void
3763
format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
3764
                  const ovs_be32 *mask, bool verbose)
3765
0
{
3766
0
    bool mask_empty = mask && !*mask;
3767
3768
0
    if (verbose || !mask_empty) {
3769
0
        bool mask_full = !mask
3770
0
            || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
3771
3772
0
        ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
3773
0
        if (!mask_full) { /* Partially masked. */
3774
0
            ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
3775
0
        }
3776
0
        ds_put_char(ds, ',');
3777
0
    }
3778
0
}
3779
3780
static void
3781
format_u8x(struct ds *ds, const char *name, uint8_t key,
3782
           const uint8_t *mask, bool verbose)
3783
0
{
3784
0
    bool mask_empty = mask && !*mask;
3785
3786
0
    if (verbose || !mask_empty) {
3787
0
        bool mask_full = !mask || *mask == UINT8_MAX;
3788
3789
0
        ds_put_format(ds, "%s=%#"PRIx8, name, key);
3790
0
        if (!mask_full) { /* Partially masked. */
3791
0
            ds_put_format(ds, "/%#"PRIx8, *mask);
3792
0
        }
3793
0
        ds_put_char(ds, ',');
3794
0
    }
3795
0
}
3796
3797
static void
3798
format_u8u(struct ds *ds, const char *name, uint8_t key,
3799
           const uint8_t *mask, bool verbose)
3800
0
{
3801
0
    bool mask_empty = mask && !*mask;
3802
3803
0
    if (verbose || !mask_empty) {
3804
0
        bool mask_full = !mask || *mask == UINT8_MAX;
3805
3806
0
        ds_put_format(ds, "%s=%"PRIu8, name, key);
3807
0
        if (!mask_full) { /* Partially masked. */
3808
0
            ds_put_format(ds, "/%#"PRIx8, *mask);
3809
0
        }
3810
0
        ds_put_char(ds, ',');
3811
0
    }
3812
0
}
3813
3814
static void
3815
format_be16(struct ds *ds, const char *name, ovs_be16 key,
3816
            const ovs_be16 *mask, bool verbose)
3817
0
{
3818
0
    bool mask_empty = mask && !*mask;
3819
3820
0
    if (verbose || !mask_empty) {
3821
0
        bool mask_full = !mask || *mask == OVS_BE16_MAX;
3822
3823
0
        ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
3824
0
        if (!mask_full) { /* Partially masked. */
3825
0
            ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3826
0
        }
3827
0
        ds_put_char(ds, ',');
3828
0
    }
3829
0
}
3830
3831
static void
3832
format_be16x(struct ds *ds, const char *name, ovs_be16 key,
3833
             const ovs_be16 *mask, bool verbose)
3834
0
{
3835
0
    bool mask_empty = mask && !*mask;
3836
3837
0
    if (verbose || !mask_empty) {
3838
0
        bool mask_full = !mask || *mask == OVS_BE16_MAX;
3839
3840
0
        ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
3841
0
        if (!mask_full) { /* Partially masked. */
3842
0
            ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3843
0
        }
3844
0
        ds_put_char(ds, ',');
3845
0
    }
3846
0
}
3847
3848
static void
3849
format_tun_flags(struct ds *ds, const char *name, uint16_t key,
3850
                 const uint16_t *mask, bool verbose)
3851
0
{
3852
0
    bool mask_empty = mask && !*mask;
3853
3854
0
    if (verbose || !mask_empty) {
3855
0
        ds_put_cstr(ds, name);
3856
0
        ds_put_char(ds, '(');
3857
0
        if (mask) {
3858
0
            format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
3859
0
                                *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
3860
0
        } else { /* Fully masked. */
3861
0
            format_flags(ds, flow_tun_flag_to_string, key, '|');
3862
0
        }
3863
0
        ds_put_cstr(ds, "),");
3864
0
    }
3865
0
}
3866
3867
static bool
3868
check_attr_len(struct ds *ds, const struct nlattr *a, const struct nlattr *ma,
3869
               const struct attr_len_tbl tbl[], int max_type, bool need_key)
3870
0
{
3871
0
    uint16_t type = nl_attr_type(a);
3872
0
    int expected_len;
3873
3874
0
    if (type > max_type) {
3875
        /* Unknown attribute, can't check the length. */
3876
0
        return true;
3877
0
    }
3878
3879
0
    expected_len = odp_key_attr_len(tbl, max_type, type);
3880
3881
0
    if (expected_len != ATTR_LEN_VARIABLE &&
3882
0
        expected_len != ATTR_LEN_NESTED) {
3883
3884
0
        bool bad_key_len = nl_attr_get_size(a) != expected_len;
3885
0
        bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
3886
3887
0
        if (bad_key_len || bad_mask_len) {
3888
0
            if (need_key) {
3889
0
                ds_put_format(ds, "key%u", type);
3890
0
            }
3891
0
            if (bad_key_len) {
3892
0
                ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
3893
0
                              nl_attr_get_size(a), expected_len);
3894
0
            }
3895
0
            format_generic_odp_key(a, ds);
3896
0
            if (ma) {
3897
0
                ds_put_char(ds, '/');
3898
0
                if (bad_mask_len) {
3899
0
                    ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
3900
0
                                  nl_attr_get_size(ma), expected_len);
3901
0
                }
3902
0
                format_generic_odp_key(ma, ds);
3903
0
            }
3904
0
            ds_put_char(ds, ')');
3905
0
            return false;
3906
0
        }
3907
0
    }
3908
3909
0
    return true;
3910
0
}
3911
3912
static void
3913
format_unknown_key(struct ds *ds, const struct nlattr *a,
3914
                   const struct nlattr *ma)
3915
0
{
3916
0
    ds_put_format(ds, "key%u(", nl_attr_type(a));
3917
0
    format_generic_odp_key(a, ds);
3918
0
    if (ma && !odp_mask_attr_is_exact(ma)) {
3919
0
        ds_put_char(ds, '/');
3920
0
        format_generic_odp_key(ma, ds);
3921
0
    }
3922
0
    ds_put_cstr(ds, "),");
3923
0
}
3924
3925
static void
3926
format_odp_tun_vxlan_opt(const struct nlattr *attr,
3927
                         const struct nlattr *mask_attr, struct ds *ds,
3928
                         bool verbose)
3929
0
{
3930
0
    unsigned int left;
3931
0
    const struct nlattr *a;
3932
0
    struct ofpbuf ofp;
3933
3934
0
    ofpbuf_init(&ofp, 100);
3935
0
    NL_NESTED_FOR_EACH(a, left, attr) {
3936
0
        uint16_t type = nl_attr_type(a);
3937
0
        const struct nlattr *ma = NULL;
3938
3939
0
        if (mask_attr) {
3940
0
            ma = nl_attr_find__(nl_attr_get(mask_attr),
3941
0
                                nl_attr_get_size(mask_attr), type);
3942
0
            if (!ma) {
3943
0
                ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
3944
0
                                                OVS_VXLAN_EXT_MAX,
3945
0
                                                &ofp, a);
3946
0
            }
3947
0
        }
3948
3949
0
        if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
3950
0
                            OVS_VXLAN_EXT_MAX, true)) {
3951
0
            continue;
3952
0
        }
3953
3954
0
        switch (type) {
3955
0
        case OVS_VXLAN_EXT_GBP: {
3956
0
            uint32_t key = nl_attr_get_u32(a);
3957
0
            ovs_be16 id, id_mask;
3958
0
            uint8_t flags, flags_mask = 0;
3959
3960
0
            odp_decode_gbp_raw(key, &id, &flags);
3961
0
            if (ma) {
3962
0
                uint32_t mask = nl_attr_get_u32(ma);
3963
0
                odp_decode_gbp_raw(mask, &id_mask, &flags_mask);
3964
0
            }
3965
3966
0
            ds_put_cstr(ds, "gbp(");
3967
0
            format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
3968
0
            format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
3969
0
            ds_chomp(ds, ',');
3970
0
            ds_put_cstr(ds, "),");
3971
0
            break;
3972
0
        }
3973
3974
0
        default:
3975
0
            format_unknown_key(ds, a, ma);
3976
0
        }
3977
0
        ofpbuf_clear(&ofp);
3978
0
    }
3979
3980
0
    ds_chomp(ds, ',');
3981
0
    ofpbuf_uninit(&ofp);
3982
0
}
3983
3984
static void
3985
format_odp_tun_erspan_opt(const struct nlattr *attr,
3986
                          const struct nlattr *mask_attr, struct ds *ds,
3987
                          bool verbose)
3988
0
{
3989
0
    const struct erspan_metadata *opts, *mask;
3990
0
    uint8_t ver, ver_ma, dir, dir_ma, hwid, hwid_ma;
3991
3992
0
    opts = nl_attr_get(attr);
3993
0
    mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
3994
3995
0
    ver = (uint8_t)opts->version;
3996
0
    if (mask) {
3997
0
        ver_ma = (uint8_t)mask->version;
3998
0
    }
3999
4000
0
    format_u8u(ds, "ver", ver, mask ? &ver_ma : NULL, verbose);
4001
4002
0
    if (opts->version == 1) {
4003
0
        if (mask) {
4004
0
            ds_put_format(ds, "idx=%#"PRIx32"/%#"PRIx32",",
4005
0
                          ntohl(opts->u.index),
4006
0
                          ntohl(mask->u.index));
4007
0
        } else {
4008
0
            ds_put_format(ds, "idx=%#"PRIx32",", ntohl(opts->u.index));
4009
0
        }
4010
0
    } else if (opts->version == 2) {
4011
0
        dir = opts->u.md2.dir;
4012
0
        hwid = opts->u.md2.hwid;
4013
0
        if (mask) {
4014
0
            dir_ma = mask->u.md2.dir;
4015
0
            hwid_ma = mask->u.md2.hwid;
4016
0
        }
4017
4018
0
        format_u8u(ds, "dir", dir, mask ? &dir_ma : NULL, verbose);
4019
0
        format_u8x(ds, "hwid", hwid, mask ? &hwid_ma : NULL, verbose);
4020
0
    }
4021
0
    ds_chomp(ds, ',');
4022
0
}
4023
4024
static void
4025
format_odp_tun_gtpu_opt(const struct nlattr *attr,
4026
                        const struct nlattr *mask_attr, struct ds *ds,
4027
                        bool verbose)
4028
0
{
4029
0
    const struct gtpu_metadata *opts, *mask;
4030
4031
0
    opts = nl_attr_get(attr);
4032
0
    mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
4033
4034
0
    format_u8x(ds, "flags", opts->flags, mask ? &mask->flags : NULL, verbose);
4035
0
    format_u8u(ds, "msgtype", opts->msgtype, mask ? &mask->msgtype : NULL,
4036
0
               verbose);
4037
0
    ds_chomp(ds, ',');
4038
0
}
4039
4040
0
#define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
4041
4042
static void
4043
format_geneve_opts(const struct geneve_opt *opt,
4044
                   const struct geneve_opt *mask, int opts_len,
4045
                   struct ds *ds, bool verbose)
4046
0
{
4047
0
    while (opts_len > 0) {
4048
0
        unsigned int len;
4049
0
        uint8_t data_len, data_len_mask;
4050
4051
0
        if (opts_len < sizeof *opt) {
4052
0
            ds_put_format(ds, "opt len %u less than minimum %"PRIuSIZE,
4053
0
                          opts_len, sizeof *opt);
4054
0
            return;
4055
0
        }
4056
4057
0
        data_len = opt->length * 4;
4058
0
        if (mask) {
4059
0
            if (mask->length == 0x1f) {
4060
0
                data_len_mask = UINT8_MAX;
4061
0
            } else {
4062
0
                data_len_mask = mask->length;
4063
0
            }
4064
0
        }
4065
0
        len = sizeof *opt + data_len;
4066
0
        if (len > opts_len) {
4067
0
            ds_put_format(ds, "opt len %u greater than remaining %u",
4068
0
                          len, opts_len);
4069
0
            return;
4070
0
        }
4071
4072
0
        ds_put_char(ds, '{');
4073
0
        format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
4074
0
                    verbose);
4075
0
        format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
4076
0
        format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
4077
0
        if (data_len &&
4078
0
            (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
4079
0
            ds_put_hex(ds, opt + 1, data_len);
4080
0
            if (mask && !is_all_ones(mask + 1, data_len)) {
4081
0
                ds_put_char(ds, '/');
4082
0
                ds_put_hex(ds, mask + 1, data_len);
4083
0
            }
4084
0
        } else {
4085
0
            ds_chomp(ds, ',');
4086
0
        }
4087
0
        ds_put_char(ds, '}');
4088
4089
0
        opt += len / sizeof(*opt);
4090
0
        if (mask) {
4091
0
            mask += len / sizeof(*opt);
4092
0
        }
4093
0
        opts_len -= len;
4094
0
    };
4095
0
}
4096
4097
static void
4098
format_odp_tun_geneve(const struct nlattr *attr,
4099
                      const struct nlattr *mask_attr, struct ds *ds,
4100
                      bool verbose)
4101
0
{
4102
0
    int opts_len = nl_attr_get_size(attr);
4103
0
    const struct geneve_opt *opt = nl_attr_get(attr);
4104
0
    const struct geneve_opt *mask = mask_attr ?
4105
0
                                    nl_attr_get(mask_attr) : NULL;
4106
4107
0
    if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
4108
0
        ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
4109
0
                      nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
4110
0
        return;
4111
0
    }
4112
4113
0
    format_geneve_opts(opt, mask, opts_len, ds, verbose);
4114
0
}
4115
4116
static void
4117
format_odp_nsh_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
4118
                    struct ds *ds)
4119
0
{
4120
0
    unsigned int left;
4121
0
    const struct nlattr *a;
4122
0
    struct ovs_key_nsh nsh;
4123
0
    struct ovs_key_nsh nsh_mask;
4124
4125
0
    memset(&nsh, 0, sizeof nsh);
4126
0
    memset(&nsh_mask, 0xff, sizeof nsh_mask);
4127
4128
0
    NL_NESTED_FOR_EACH (a, left, attr) {
4129
0
        enum ovs_nsh_key_attr type = nl_attr_type(a);
4130
0
        const struct nlattr *ma = NULL;
4131
4132
0
        if (mask_attr) {
4133
0
            ma = nl_attr_find__(nl_attr_get(mask_attr),
4134
0
                                nl_attr_get_size(mask_attr), type);
4135
0
        }
4136
4137
0
        if (!check_attr_len(ds, a, ma, ovs_nsh_key_attr_lens,
4138
0
                            OVS_NSH_KEY_ATTR_MAX, true)) {
4139
0
            continue;
4140
0
        }
4141
4142
0
        switch (type) {
4143
0
        case OVS_NSH_KEY_ATTR_UNSPEC:
4144
0
            break;
4145
0
        case OVS_NSH_KEY_ATTR_BASE: {
4146
0
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
4147
0
            const struct ovs_nsh_key_base *base_mask
4148
0
                = ma ? nl_attr_get(ma) : NULL;
4149
0
            nsh.flags = base->flags;
4150
0
            nsh.ttl = base->ttl;
4151
0
            nsh.mdtype = base->mdtype;
4152
0
            nsh.np = base->np;
4153
0
            nsh.path_hdr = base->path_hdr;
4154
0
            if (base_mask) {
4155
0
                nsh_mask.flags = base_mask->flags;
4156
0
                nsh_mask.ttl = base_mask->ttl;
4157
0
                nsh_mask.mdtype = base_mask->mdtype;
4158
0
                nsh_mask.np = base_mask->np;
4159
0
                nsh_mask.path_hdr = base_mask->path_hdr;
4160
0
            }
4161
0
            break;
4162
0
        }
4163
0
        case OVS_NSH_KEY_ATTR_MD1: {
4164
0
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
4165
0
            const struct ovs_nsh_key_md1 *md1_mask
4166
0
                = ma ? nl_attr_get(ma) : NULL;
4167
0
            memcpy(nsh.context, md1->context, sizeof md1->context);
4168
0
            if (md1_mask) {
4169
0
                memcpy(nsh_mask.context, md1_mask->context,
4170
0
                       sizeof md1_mask->context);
4171
0
            }
4172
0
            break;
4173
0
        }
4174
0
        case OVS_NSH_KEY_ATTR_MD2:
4175
0
        case __OVS_NSH_KEY_ATTR_MAX:
4176
0
        default:
4177
            /* No support for matching other metadata formats yet. */
4178
0
            break;
4179
0
        }
4180
0
    }
4181
4182
0
    if (mask_attr) {
4183
0
        format_nsh_key_mask(ds, &nsh, &nsh_mask);
4184
0
    } else {
4185
0
        format_nsh_key(ds, &nsh);
4186
0
    }
4187
0
}
4188
4189
static void
4190
format_odp_tun_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
4191
                    struct ds *ds, bool verbose)
4192
0
{
4193
0
    unsigned int left;
4194
0
    const struct nlattr *a;
4195
0
    uint16_t flags = 0;
4196
0
    uint16_t mask_flags = 0;
4197
0
    struct ofpbuf ofp;
4198
4199
0
    ofpbuf_init(&ofp, 100);
4200
0
    NL_NESTED_FOR_EACH(a, left, attr) {
4201
0
        enum ovs_tunnel_key_attr type = nl_attr_type(a);
4202
0
        const struct nlattr *ma = NULL;
4203
4204
0
        if (mask_attr) {
4205
0
            ma = nl_attr_find__(nl_attr_get(mask_attr),
4206
0
                                nl_attr_get_size(mask_attr), type);
4207
0
            if (!ma) {
4208
0
                ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
4209
0
                                                OVS_TUNNEL_KEY_ATTR_MAX,
4210
0
                                                &ofp, a);
4211
0
            }
4212
0
        }
4213
4214
0
        if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
4215
0
                            OVS_TUNNEL_KEY_ATTR_MAX, true)) {
4216
0
            continue;
4217
0
        }
4218
4219
0
        switch (type) {
4220
0
        case OVS_TUNNEL_KEY_ATTR_ID:
4221
0
            format_be64(ds, "tun_id", nl_attr_get_be64(a),
4222
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4223
0
            flags |= FLOW_TNL_F_KEY;
4224
0
            if (ma) {
4225
0
                mask_flags |= FLOW_TNL_F_KEY;
4226
0
            }
4227
0
            break;
4228
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
4229
0
            format_ipv4(ds, "src", nl_attr_get_be32(a),
4230
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4231
0
            break;
4232
0
        case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
4233
0
            format_ipv4(ds, "dst", nl_attr_get_be32(a),
4234
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4235
0
            break;
4236
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
4237
0
            struct in6_addr ipv6_src;
4238
0
            ipv6_src = nl_attr_get_in6_addr(a);
4239
0
            format_in6_addr(ds, "ipv6_src", &ipv6_src,
4240
0
                            ma ? nl_attr_get(ma) : NULL, verbose);
4241
0
            break;
4242
0
        }
4243
0
        case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
4244
0
            struct in6_addr ipv6_dst;
4245
0
            ipv6_dst = nl_attr_get_in6_addr(a);
4246
0
            format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
4247
0
                            ma ? nl_attr_get(ma) : NULL, verbose);
4248
0
            break;
4249
0
        }
4250
0
        case OVS_TUNNEL_KEY_ATTR_TOS:
4251
0
            format_u8x(ds, "tos", nl_attr_get_u8(a),
4252
0
                       ma ? nl_attr_get(ma) : NULL, verbose);
4253
0
            break;
4254
0
        case OVS_TUNNEL_KEY_ATTR_TTL:
4255
0
            format_u8u(ds, "ttl", nl_attr_get_u8(a),
4256
0
                       ma ? nl_attr_get(ma) : NULL, verbose);
4257
0
            break;
4258
0
        case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
4259
0
            flags |= FLOW_TNL_F_DONT_FRAGMENT;
4260
0
            break;
4261
0
        case OVS_TUNNEL_KEY_ATTR_CSUM:
4262
0
            flags |= FLOW_TNL_F_CSUM;
4263
0
            break;
4264
0
        case OVS_TUNNEL_KEY_ATTR_TP_SRC:
4265
0
            format_be16(ds, "tp_src", nl_attr_get_be16(a),
4266
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4267
0
            break;
4268
0
        case OVS_TUNNEL_KEY_ATTR_TP_DST:
4269
0
            format_be16(ds, "tp_dst", nl_attr_get_be16(a),
4270
0
                        ma ? nl_attr_get(ma) : NULL, verbose);
4271
0
            break;
4272
0
        case OVS_TUNNEL_KEY_ATTR_OAM:
4273
0
            flags |= FLOW_TNL_F_OAM;
4274
0
            break;
4275
0
        case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
4276
0
            ds_put_cstr(ds, "vxlan(");
4277
0
            format_odp_tun_vxlan_opt(a, ma, ds, verbose);
4278
0
            ds_put_cstr(ds, "),");
4279
0
            break;
4280
0
        case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
4281
0
            ds_put_cstr(ds, "geneve(");
4282
0
            format_odp_tun_geneve(a, ma, ds, verbose);
4283
0
            ds_put_cstr(ds, "),");
4284
0
            break;
4285
0
        case OVS_TUNNEL_KEY_ATTR_PAD:
4286
0
            break;
4287
0
        case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
4288
0
            ds_put_cstr(ds, "erspan(");
4289
0
            format_odp_tun_erspan_opt(a, ma, ds, verbose);
4290
0
            ds_put_cstr(ds, "),");
4291
0
            break;
4292
0
        case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS:
4293
0
            ds_put_cstr(ds, "gtpu(");
4294
0
            format_odp_tun_gtpu_opt(a, ma, ds, verbose);
4295
0
            ds_put_cstr(ds, "),");
4296
0
            break;
4297
0
        case __OVS_TUNNEL_KEY_ATTR_MAX:
4298
0
        default:
4299
0
            format_unknown_key(ds, a, ma);
4300
0
        }
4301
0
        ofpbuf_clear(&ofp);
4302
0
    }
4303
4304
    /* Flags can have a valid mask even if the attribute is not set, so
4305
     * we need to collect these separately. */
4306
0
    if (mask_attr) {
4307
0
        NL_NESTED_FOR_EACH(a, left, mask_attr) {
4308
0
            switch (nl_attr_type(a)) {
4309
0
            case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
4310
0
                mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
4311
0
                break;
4312
0
            case OVS_TUNNEL_KEY_ATTR_CSUM:
4313
0
                mask_flags |= FLOW_TNL_F_CSUM;
4314
0
                break;
4315
0
            case OVS_TUNNEL_KEY_ATTR_OAM:
4316
0
                mask_flags |= FLOW_TNL_F_OAM;
4317
0
                break;
4318
0
            }
4319
0
        }
4320
0
    }
4321
4322
0
    format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
4323
0
                     verbose);
4324
0
    ds_chomp(ds, ',');
4325
0
    ofpbuf_uninit(&ofp);
4326
0
}
4327
4328
static const char *
4329
odp_ct_state_to_string(uint32_t flag)
4330
0
{
4331
0
    switch (flag) {
4332
0
    case OVS_CS_F_REPLY_DIR:
4333
0
        return "rpl";
4334
0
    case OVS_CS_F_TRACKED:
4335
0
        return "trk";
4336
0
    case OVS_CS_F_NEW:
4337
0
        return "new";
4338
0
    case OVS_CS_F_ESTABLISHED:
4339
0
        return "est";
4340
0
    case OVS_CS_F_RELATED:
4341
0
        return "rel";
4342
0
    case OVS_CS_F_INVALID:
4343
0
        return "inv";
4344
0
    case OVS_CS_F_SRC_NAT:
4345
0
        return "snat";
4346
0
    case OVS_CS_F_DST_NAT:
4347
0
        return "dnat";
4348
0
    default:
4349
0
        return NULL;
4350
0
    }
4351
0
}
4352
4353
static void
4354
format_frag(struct ds *ds, const char *name, uint8_t key,
4355
            const uint8_t *mask, bool verbose OVS_UNUSED)
4356
0
{
4357
0
    bool mask_empty = mask && !*mask;
4358
0
    bool mask_full = !mask || *mask == UINT8_MAX;
4359
4360
    /* ODP frag is an enumeration field; partial masks are not meaningful. */
4361
0
    if (!mask_empty && !mask_full) {
4362
0
        ds_put_format(ds, "error: partial mask not supported for frag (%#"
4363
0
                      PRIx8"),", *mask);
4364
0
    } else if (!mask_empty) {
4365
0
        ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
4366
0
    }
4367
0
}
4368
4369
static bool
4370
mask_empty(const struct nlattr *ma)
4371
0
{
4372
0
    const void *mask;
4373
0
    size_t n;
4374
4375
0
    if (!ma) {
4376
0
        return true;
4377
0
    }
4378
0
    mask = nl_attr_get(ma);
4379
0
    n = nl_attr_get_size(ma);
4380
4381
0
    return is_all_zeros(mask, n);
4382
0
}
4383
4384
/* The caller must have already verified that 'a' and 'ma' have correct
4385
 * lengths. */
4386
static void
4387
format_odp_key_attr__(const struct nlattr *a, const struct nlattr *ma,
4388
                      const struct hmap *portno_names, struct ds *ds,
4389
                      bool verbose, bool skip_no_mask)
4390
0
{
4391
0
    enum ovs_key_attr attr = nl_attr_type(a);
4392
0
    char namebuf[OVS_KEY_ATTR_BUFSIZE];
4393
0
    bool is_exact;
4394
4395
0
    is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
4396
4397
0
    ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
4398
4399
0
    ds_put_char(ds, '(');
4400
0
    switch (attr) {
4401
0
    case OVS_KEY_ATTR_ENCAP:
4402
0
        if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
4403
0
            odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
4404
0
                            nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
4405
0
                            verbose, skip_no_mask);
4406
0
        } else if (nl_attr_get_size(a)) {
4407
0
            odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
4408
0
                            ds, verbose, skip_no_mask);
4409
0
        }
4410
0
        break;
4411
4412
0
    case OVS_KEY_ATTR_PRIORITY:
4413
0
    case OVS_KEY_ATTR_SKB_MARK:
4414
0
    case OVS_KEY_ATTR_DP_HASH:
4415
0
    case OVS_KEY_ATTR_RECIRC_ID:
4416
0
        ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4417
0
        if (!is_exact) {
4418
0
            ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4419
0
        }
4420
0
        break;
4421
4422
0
    case OVS_KEY_ATTR_CT_MARK:
4423
0
        if (verbose || !mask_empty(ma)) {
4424
0
            ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4425
0
            if (!is_exact) {
4426
0
                ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4427
0
            }
4428
0
        }
4429
0
        break;
4430
4431
0
    case OVS_KEY_ATTR_CT_STATE:
4432
0
        if (verbose) {
4433
0
                ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4434
0
                if (!is_exact) {
4435
0
                    ds_put_format(ds, "/%#"PRIx32,
4436
0
                                  mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
4437
0
                }
4438
0
        } else if (!is_exact) {
4439
0
            format_flags_masked(ds, NULL, odp_ct_state_to_string,
4440
0
                                nl_attr_get_u32(a),
4441
0
                                mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
4442
0
                                UINT32_MAX);
4443
0
        } else {
4444
0
            format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
4445
0
        }
4446
0
        break;
4447
4448
0
    case OVS_KEY_ATTR_CT_ZONE:
4449
0
        if (verbose || !mask_empty(ma)) {
4450
0
            ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
4451
0
            if (!is_exact) {
4452
0
                ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
4453
0
            }
4454
0
        }
4455
0
        break;
4456
4457
0
    case OVS_KEY_ATTR_CT_LABELS: {
4458
0
        const ovs_32aligned_u128 *value = nl_attr_get(a);
4459
0
        const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL;
4460
4461
0
        format_u128(ds, value, mask, verbose);
4462
0
        break;
4463
0
    }
4464
4465
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
4466
0
        const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a);
4467
0
        const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4468
4469
0
        format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4470
0
        format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4471
0
        format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4472
0
                   verbose);
4473
0
        format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port),
4474
0
                    verbose);
4475
0
        format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port),
4476
0
                    verbose);
4477
0
        ds_chomp(ds, ',');
4478
0
        break;
4479
0
    }
4480
4481
0
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
4482
0
        const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a);
4483
0
        const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4484
4485
0
        format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4486
0
                        verbose);
4487
0
        format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4488
0
                        verbose);
4489
0
        format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4490
0
                   verbose);
4491
0
        format_be16(ds, "src_port", key->src_port, MASK(mask, src_port),
4492
0
                    verbose);
4493
0
        format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port),
4494
0
                    verbose);
4495
0
        ds_chomp(ds, ',');
4496
0
        break;
4497
0
    }
4498
4499
0
    case OVS_KEY_ATTR_TUNNEL:
4500
0
        format_odp_tun_attr(a, ma, ds, verbose);
4501
0
        break;
4502
4503
0
    case OVS_KEY_ATTR_IN_PORT:
4504
0
        if (is_exact) {
4505
0
            odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
4506
0
        } else {
4507
0
            ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
4508
0
            if (!is_exact) {
4509
0
                ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4510
0
            }
4511
0
        }
4512
0
        break;
4513
4514
0
    case OVS_KEY_ATTR_PACKET_TYPE: {
4515
0
        ovs_be32 value = nl_attr_get_be32(a);
4516
0
        ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX;
4517
4518
0
        ovs_be16 ns = htons(pt_ns(value));
4519
0
        ovs_be16 ns_mask = htons(pt_ns(mask));
4520
0
        format_be16(ds, "ns", ns, &ns_mask, verbose);
4521
4522
0
        ovs_be16 ns_type = pt_ns_type_be(value);
4523
0
        ovs_be16 ns_type_mask = pt_ns_type_be(mask);
4524
0
        format_be16x(ds, "id", ns_type, &ns_type_mask, verbose);
4525
4526
0
        ds_chomp(ds, ',');
4527
0
        break;
4528
0
    }
4529
4530
0
    case OVS_KEY_ATTR_ETHERNET: {
4531
0
        const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
4532
0
        const struct ovs_key_ethernet *key = nl_attr_get(a);
4533
4534
0
        format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
4535
0
        format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
4536
0
        ds_chomp(ds, ',');
4537
0
        break;
4538
0
    }
4539
0
    case OVS_KEY_ATTR_VLAN:
4540
0
        format_vlan_tci(ds, nl_attr_get_be16(a),
4541
0
                        ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
4542
0
        break;
4543
4544
0
    case OVS_KEY_ATTR_MPLS: {
4545
0
        const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
4546
0
        const struct ovs_key_mpls *mpls_mask = NULL;
4547
0
        size_t size = nl_attr_get_size(a);
4548
4549
0
        if (!size || size % sizeof *mpls_key) {
4550
0
            ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
4551
0
            return;
4552
0
        }
4553
0
        if (!is_exact) {
4554
0
            mpls_mask = nl_attr_get(ma);
4555
0
            if (size != nl_attr_get_size(ma)) {
4556
0
                ds_put_format(ds, "(key length %"PRIuSIZE" != "
4557
0
                              "mask length %"PRIuSIZE")",
4558
0
                              size, nl_attr_get_size(ma));
4559
0
                return;
4560
0
            }
4561
0
        }
4562
0
        format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
4563
0
        break;
4564
0
    }
4565
0
    case OVS_KEY_ATTR_ETHERTYPE:
4566
0
        ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
4567
0
        if (!is_exact) {
4568
0
            ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
4569
0
        }
4570
0
        break;
4571
4572
0
    case OVS_KEY_ATTR_IPV4: {
4573
0
        const struct ovs_key_ipv4 *key = nl_attr_get(a);
4574
0
        const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4575
4576
0
        format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4577
0
        format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4578
0
        format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4579
0
                      verbose);
4580
0
        format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
4581
0
        format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
4582
0
        format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
4583
0
                    verbose);
4584
0
        ds_chomp(ds, ',');
4585
0
        break;
4586
0
    }
4587
0
    case OVS_KEY_ATTR_IPV6: {
4588
0
        const struct ovs_key_ipv6 *key = nl_attr_get(a);
4589
0
        const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4590
4591
0
        format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4592
0
                        verbose);
4593
0
        format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4594
0
                        verbose);
4595
0
        format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
4596
0
                          verbose);
4597
0
        format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4598
0
                   verbose);
4599
0
        format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
4600
0
                   verbose);
4601
0
        format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
4602
0
                   verbose);
4603
0
        format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
4604
0
                    verbose);
4605
0
        ds_chomp(ds, ',');
4606
0
        break;
4607
0
    }
4608
        /* These have the same structure and format. */
4609
0
    case OVS_KEY_ATTR_TCP:
4610
0
    case OVS_KEY_ATTR_UDP:
4611
0
    case OVS_KEY_ATTR_SCTP: {
4612
0
        const struct ovs_key_tcp *key = nl_attr_get(a);
4613
0
        const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
4614
4615
0
        format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
4616
0
        format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
4617
0
        ds_chomp(ds, ',');
4618
0
        break;
4619
0
    }
4620
0
    case OVS_KEY_ATTR_TCP_FLAGS:
4621
0
        if (!is_exact) {
4622
0
            format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
4623
0
                                ntohs(nl_attr_get_be16(a)),
4624
0
                                TCP_FLAGS(nl_attr_get_be16(ma)),
4625
0
                                TCP_FLAGS(OVS_BE16_MAX));
4626
0
        } else {
4627
0
            format_flags(ds, packet_tcp_flag_to_string,
4628
0
                         ntohs(nl_attr_get_be16(a)), '|');
4629
0
        }
4630
0
        break;
4631
4632
0
    case OVS_KEY_ATTR_ICMP: {
4633
0
        const struct ovs_key_icmp *key = nl_attr_get(a);
4634
0
        const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
4635
4636
0
        format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
4637
0
        format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
4638
0
        ds_chomp(ds, ',');
4639
0
        break;
4640
0
    }
4641
0
    case OVS_KEY_ATTR_ICMPV6: {
4642
0
        const struct ovs_key_icmpv6 *key = nl_attr_get(a);
4643
0
        const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
4644
4645
0
        format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
4646
0
                   verbose);
4647
0
        format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
4648
0
                   verbose);
4649
0
        ds_chomp(ds, ',');
4650
0
        break;
4651
0
    }
4652
0
    case OVS_KEY_ATTR_ARP: {
4653
0
        const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
4654
0
        const struct ovs_key_arp *key = nl_attr_get(a);
4655
4656
0
        format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
4657
0
        format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
4658
0
        format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
4659
0
        format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
4660
0
        format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
4661
0
        ds_chomp(ds, ',');
4662
0
        break;
4663
0
    }
4664
0
    case OVS_KEY_ATTR_ND: {
4665
0
        const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
4666
0
        const struct ovs_key_nd *key = nl_attr_get(a);
4667
4668
0
        format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target),
4669
0
                        verbose);
4670
0
        format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
4671
0
        format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
4672
4673
0
        ds_chomp(ds, ',');
4674
0
        break;
4675
0
    }
4676
0
    case OVS_KEY_ATTR_ND_EXTENSIONS: {
4677
0
        const struct ovs_key_nd_extensions *mask = ma ? nl_attr_get(ma) : NULL;
4678
0
        const struct ovs_key_nd_extensions *key = nl_attr_get(a);
4679
4680
0
        bool first = true;
4681
0
        format_be32_masked(ds, &first, "nd_reserved", key->nd_reserved,
4682
0
                           OVS_BE32_MAX);
4683
0
        ds_put_char(ds, ',');
4684
4685
0
        format_u8u(ds, "nd_options_type", key->nd_options_type,
4686
0
                   MASK(mask, nd_options_type), verbose);
4687
4688
0
        ds_chomp(ds, ',');
4689
0
        break;
4690
0
    }
4691
0
    case OVS_KEY_ATTR_NSH: {
4692
0
        format_odp_nsh_attr(a, ma, ds);
4693
0
        break;
4694
0
    }
4695
0
    case OVS_KEY_ATTR_UNSPEC:
4696
0
    case OVS_KEY_ATTR_TUNNEL_INFO:
4697
0
    case __OVS_KEY_ATTR_MAX:
4698
0
    default:
4699
0
        format_generic_odp_key(a, ds);
4700
0
        if (!is_exact) {
4701
0
            ds_put_char(ds, '/');
4702
0
            format_generic_odp_key(ma, ds);
4703
0
        }
4704
0
        break;
4705
0
    }
4706
0
    ds_put_char(ds, ')');
4707
0
}
4708
4709
static void
4710
format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
4711
                    const struct hmap *portno_names, struct ds *ds,
4712
                    bool verbose)
4713
0
{
4714
0
    if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4715
0
                        OVS_KEY_ATTR_MAX, false)) {
4716
0
        format_odp_key_attr__(a, ma, portno_names, ds, verbose, false);
4717
0
    }
4718
0
}
4719
4720
static struct nlattr *
4721
generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
4722
                           struct ofpbuf *ofp, const struct nlattr *key)
4723
0
{
4724
0
    const struct nlattr *a;
4725
0
    unsigned int left;
4726
0
    int type = nl_attr_type(key);
4727
0
    int size = nl_attr_get_size(key);
4728
4729
0
    if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
4730
0
        nl_msg_put_unspec_zero(ofp, type, size);
4731
0
    } else {
4732
0
        size_t nested_mask;
4733
4734
0
        if (tbl[type].next) {
4735
0
            const struct attr_len_tbl *entry = &tbl[type];
4736
0
            tbl = entry->next;
4737
0
            max = entry->next_max;
4738
0
        }
4739
4740
0
        nested_mask = nl_msg_start_nested(ofp, type);
4741
0
        NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
4742
0
            generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
4743
0
        }
4744
0
        nl_msg_end_nested(ofp, nested_mask);
4745
0
    }
4746
4747
0
    return ofp->base;
4748
0
}
4749
4750
static void
4751
format_u128(struct ds *ds, const ovs_32aligned_u128 *key,
4752
            const ovs_32aligned_u128 *mask, bool verbose)
4753
0
{
4754
0
    if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) {
4755
0
        ovs_be128 value = hton128(get_32aligned_u128(key));
4756
0
        ds_put_hex(ds, &value, sizeof value);
4757
0
        if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) {
4758
0
            value = hton128(get_32aligned_u128(mask));
4759
0
            ds_put_char(ds, '/');
4760
0
            ds_put_hex(ds, &value, sizeof value);
4761
0
        }
4762
0
    }
4763
0
}
4764
4765
/* Read the string from 's_' as a 128-bit value.  If the string contains
4766
 * a "/", the rest of the string will be treated as a 128-bit mask.
4767
 *
4768
 * If either the value or mask is larger than 64 bits, the string must
4769
 * be in hexadecimal.
4770
 */
4771
static int
4772
scan_u128(const char *s_, ovs_u128 *value, ovs_u128 *mask)
4773
0
{
4774
0
    char *s = CONST_CAST(char *, s_);
4775
0
    ovs_be128 be_value;
4776
0
    ovs_be128 be_mask;
4777
4778
0
    if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
4779
0
        *value = ntoh128(be_value);
4780
4781
0
        if (mask) {
4782
0
            int n;
4783
4784
0
            if (ovs_scan(s, "/%n", &n)) {
4785
0
                int error;
4786
4787
0
                s += n;
4788
0
                error = parse_int_string(s, (uint8_t *)&be_mask,
4789
0
                                         sizeof be_mask, &s);
4790
0
                if (error) {
4791
0
                    return 0;
4792
0
                }
4793
0
                *mask = ntoh128(be_mask);
4794
0
            } else {
4795
0
                *mask = OVS_U128_MAX;
4796
0
            }
4797
0
        }
4798
0
        return s - s_;
4799
0
    }
4800
4801
0
    return 0;
4802
0
}
4803
4804
int
4805
odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
4806
0
{
4807
0
    const char *s = s_;
4808
4809
0
    if (ovs_scan(s, "ufid:")) {
4810
0
        s += 5;
4811
4812
0
        if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
4813
0
            return -EINVAL;
4814
0
        }
4815
0
        s += UUID_LEN;
4816
4817
0
        return s - s_;
4818
0
    }
4819
4820
0
    return 0;
4821
0
}
4822
4823
void
4824
odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
4825
0
{
4826
0
    ds_put_format(ds, "ufid:"UUID_FMT, UUID_ARGS((struct uuid *)ufid));
4827
0
}
4828
4829
/* Appends to 'ds' a string representation of the 'key_len' bytes of
4830
 * OVS_KEY_ATTR_* attributes in 'key'.  If non-null, additionally formats the
4831
 * 'mask_len' bytes of 'mask' which apply to 'key'.  If 'portno_names' is
4832
 * non-null, translates odp port number to its name.  If 'skip_no_mask' is set
4833
 * to true, OVS_KEY_ATTR_* entries without a mask will not be printed, even
4834
 * when verbose mode is 'true'. */
4835
void
4836
odp_flow_format(const struct nlattr *key, size_t key_len,
4837
                const struct nlattr *mask, size_t mask_len,
4838
                const struct hmap *portno_names, struct ds *ds, bool verbose,
4839
                bool skip_no_mask)
4840
0
{
4841
0
    if (key_len) {
4842
0
        const struct nlattr *a;
4843
0
        unsigned int left;
4844
0
        bool has_ethtype_key = false;
4845
0
        bool has_packet_type_key = false;
4846
0
        struct ofpbuf ofp;
4847
0
        bool first_field = true;
4848
4849
0
        ofpbuf_init(&ofp, 100);
4850
0
        NL_ATTR_FOR_EACH (a, left, key, key_len) {
4851
0
            int attr_type = nl_attr_type(a);
4852
0
            const struct nlattr *ma = (mask && mask_len
4853
0
                                       ? nl_attr_find__(mask, mask_len,
4854
0
                                                        attr_type)
4855
0
                                       : NULL);
4856
0
            if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4857
0
                                OVS_KEY_ATTR_MAX, false)
4858
0
                || (skip_no_mask && !ma)) {
4859
0
                continue;
4860
0
            }
4861
4862
0
            bool is_nested_attr;
4863
0
            bool is_wildcard = false;
4864
4865
0
            if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
4866
0
                has_ethtype_key = true;
4867
0
            } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE) {
4868
0
                has_packet_type_key = true;
4869
0
            }
4870
4871
0
            is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
4872
0
                                              OVS_KEY_ATTR_MAX, attr_type) ==
4873
0
                             ATTR_LEN_NESTED;
4874
4875
0
            if (mask && mask_len) {
4876
0
                ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
4877
0
                is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
4878
0
            }
4879
4880
0
            if (verbose || !is_wildcard  || is_nested_attr) {
4881
0
                if (is_wildcard && !ma) {
4882
0
                    ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
4883
0
                                                    OVS_KEY_ATTR_MAX,
4884
0
                                                    &ofp, a);
4885
0
                }
4886
0
                if (!first_field) {
4887
0
                    ds_put_char(ds, ',');
4888
0
                }
4889
0
                format_odp_key_attr__(a, ma, portno_names, ds, verbose,
4890
0
                                      skip_no_mask);
4891
0
                first_field = false;
4892
0
            } else if (attr_type == OVS_KEY_ATTR_ETHERNET
4893
0
                       && !has_packet_type_key) {
4894
                /* This special case reflects differences between the kernel
4895
                 * and userspace datapaths regarding the root type of the
4896
                 * packet being matched (typically Ethernet but some tunnels
4897
                 * can encapsulate IPv4 etc.).  The kernel datapath does not
4898
                 * have an explicit way to indicate packet type; instead:
4899
                 *
4900
                 *   - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4901
                 *     Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4902
                 *     Ethertype encoded in the Ethernet header.
4903
                 *
4904
                 *   - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4905
                 *     root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4906
                 *     (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4907
                 *     packet is an IPv4 packet).
4908
                 *
4909
                 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4910
                 * all-wildcarded, it is important to print it.
4911
                 *
4912
                 * On the other hand, the userspace datapath supports
4913
                 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4914
                 * type.  Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4915
                 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4916
0
                if (!first_field) {
4917
0
                    ds_put_char(ds, ',');
4918
0
                }
4919
0
                ds_put_cstr(ds, "eth()");
4920
0
            } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE && is_wildcard) {
4921
                /* See the above help text, however in the case where the
4922
                 * packet type is not shown, we still need to display the
4923
                 * eth() header if the packets type is wildcarded. */
4924
0
                has_packet_type_key = false;
4925
0
            }
4926
0
            ofpbuf_clear(&ofp);
4927
0
        }
4928
0
        ofpbuf_uninit(&ofp);
4929
4930
0
        if (left) {
4931
0
            int i;
4932
4933
0
            if (left == key_len) {
4934
0
                ds_put_cstr(ds, "<empty>");
4935
0
            }
4936
0
            ds_put_format(ds, ",***%u leftover bytes*** (", left);
4937
0
            for (i = 0; i < left; i++) {
4938
0
                ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
4939
0
            }
4940
0
            ds_put_char(ds, ')');
4941
0
        }
4942
0
        if (!has_ethtype_key && mask) {
4943
0
            const struct nlattr *ma = nl_attr_find__(mask, mask_len,
4944
0
                                                     OVS_KEY_ATTR_ETHERTYPE);
4945
0
            if (ma) {
4946
0
                ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
4947
0
                              ntohs(nl_attr_get_be16(ma)));
4948
0
            }
4949
0
        }
4950
0
    } else {
4951
0
        ds_put_cstr(ds, "<empty>");
4952
0
    }
4953
0
}
4954
4955
/* Appends to 'ds' a string representation of the 'key_len' bytes of
4956
 * OVS_KEY_ATTR_* attributes in 'key'. */
4957
void
4958
odp_flow_key_format(const struct nlattr *key,
4959
                    size_t key_len, struct ds *ds)
4960
0
{
4961
0
    odp_flow_format(key, key_len, NULL, 0, NULL, ds, true, false);
4962
0
}
4963
4964
static bool
4965
ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
4966
0
{
4967
0
    if (!strcasecmp(s, "no")) {
4968
0
        *type = OVS_FRAG_TYPE_NONE;
4969
0
    } else if (!strcasecmp(s, "first")) {
4970
0
        *type = OVS_FRAG_TYPE_FIRST;
4971
0
    } else if (!strcasecmp(s, "later")) {
4972
0
        *type = OVS_FRAG_TYPE_LATER;
4973
0
    } else {
4974
0
        return false;
4975
0
    }
4976
0
    return true;
4977
0
}
4978
4979
/* Parsing. */
4980
4981
static int
4982
scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
4983
0
{
4984
0
    int n;
4985
4986
0
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
4987
0
                 ETH_ADDR_SCAN_ARGS(*key), &n)) {
4988
0
        int len = n;
4989
4990
0
        if (mask) {
4991
0
            if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
4992
0
                         ETH_ADDR_SCAN_ARGS(*mask), &n)) {
4993
0
                len += n;
4994
0
            } else {
4995
0
                memset(mask, 0xff, sizeof *mask);
4996
0
            }
4997
0
        }
4998
0
        return len;
4999
0
    }
5000
0
    return 0;
5001
0
}
5002
5003
static int
5004
scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
5005
0
{
5006
0
    int n;
5007
5008
0
    if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
5009
0
        int len = n;
5010
5011
0
        if (mask) {
5012
0
            if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
5013
0
                         IP_SCAN_ARGS(mask), &n)) {
5014
0
                len += n;
5015
0
            } else {
5016
0
                *mask = OVS_BE32_MAX;
5017
0
            }
5018
0
        }
5019
0
        return len;
5020
0
    }
5021
0
    return 0;
5022
0
}
5023
5024
static int
5025
scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
5026
0
{
5027
0
    int n;
5028
0
    char ipv6_s[IPV6_SCAN_LEN + 1];
5029
5030
0
    if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
5031
0
        && inet_pton(AF_INET6, ipv6_s, key) == 1) {
5032
0
        int len = n;
5033
5034
0
        if (mask) {
5035
0
            if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
5036
0
                && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
5037
0
                len += n;
5038
0
            } else {
5039
0
                memset(mask, 0xff, sizeof *mask);
5040
0
            }
5041
0
        }
5042
0
        return len;
5043
0
    }
5044
0
    return 0;
5045
0
}
5046
5047
static int
5048
scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
5049
0
{
5050
0
    int key_, mask_;
5051
0
    int n;
5052
5053
0
    if (ovs_scan(s, "%i%n", &key_, &n)
5054
0
        && (key_ & ~IPV6_LABEL_MASK) == 0) {
5055
0
        int len = n;
5056
5057
0
        *key = htonl(key_);
5058
0
        if (mask) {
5059
0
            if (ovs_scan(s + len, "/%i%n", &mask_, &n)
5060
0
                && (mask_ & ~IPV6_LABEL_MASK) == 0) {
5061
0
                len += n;
5062
0
                *mask = htonl(mask_);
5063
0
            } else {
5064
0
                *mask = htonl(IPV6_LABEL_MASK);
5065
0
            }
5066
0
        }
5067
0
        return len;
5068
0
    }
5069
0
    return 0;
5070
0
}
5071
5072
static int
5073
scan_u8(const char *s, uint8_t *key, uint8_t *mask)
5074
0
{
5075
0
    int n;
5076
5077
0
    if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
5078
0
        int len = n;
5079
5080
0
        if (mask) {
5081
0
            if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
5082
0
                len += n;
5083
0
            } else {
5084
0
                *mask = UINT8_MAX;
5085
0
            }
5086
0
        }
5087
0
        return len;
5088
0
    }
5089
0
    return 0;
5090
0
}
5091
5092
static int
5093
scan_u16(const char *s, uint16_t *key, uint16_t *mask)
5094
0
{
5095
0
    int n;
5096
5097
0
    if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
5098
0
        int len = n;
5099
5100
0
        if (mask) {
5101
0
            if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
5102
0
                len += n;
5103
0
            } else {
5104
0
                *mask = UINT16_MAX;
5105
0
            }
5106
0
        }
5107
0
        return len;
5108
0
    }
5109
0
    return 0;
5110
0
}
5111
5112
static int
5113
scan_u32(const char *s, uint32_t *key, uint32_t *mask)
5114
0
{
5115
0
    int n;
5116
5117
0
    if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
5118
0
        int len = n;
5119
5120
0
        if (mask) {
5121
0
            if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
5122
0
                len += n;
5123
0
            } else {
5124
0
                *mask = UINT32_MAX;
5125
0
            }
5126
0
        }
5127
0
        return len;
5128
0
    }
5129
0
    return 0;
5130
0
}
5131
5132
static int
5133
scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
5134
0
{
5135
0
    uint16_t key_, mask_;
5136
0
    int n;
5137
5138
0
    if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
5139
0
        int len = n;
5140
5141
0
        *key = htons(key_);
5142
0
        if (mask) {
5143
0
            if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
5144
0
                len += n;
5145
0
                *mask = htons(mask_);
5146
0
            } else {
5147
0
                *mask = OVS_BE16_MAX;
5148
0
            }
5149
0
        }
5150
0
        return len;
5151
0
    }
5152
0
    return 0;
5153
0
}
5154
5155
static int
5156
scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask)
5157
0
{
5158
0
    uint32_t key_, mask_;
5159
0
    int n;
5160
5161
0
    if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
5162
0
        int len = n;
5163
5164
0
        *key = htonl(key_);
5165
0
        if (mask) {
5166
0
            if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
5167
0
                len += n;
5168
0
                *mask = htonl(mask_);
5169
0
            } else {
5170
0
                *mask = OVS_BE32_MAX;
5171
0
            }
5172
0
        }
5173
0
        return len;
5174
0
    }
5175
0
    return 0;
5176
0
}
5177
5178
static int
5179
scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
5180
0
{
5181
0
    uint64_t key_, mask_;
5182
0
    int n;
5183
5184
0
    if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
5185
0
        int len = n;
5186
5187
0
        *key = htonll(key_);
5188
0
        if (mask) {
5189
0
            if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
5190
0
                len += n;
5191
0
                *mask = htonll(mask_);
5192
0
            } else {
5193
0
                *mask = OVS_BE64_MAX;
5194
0
            }
5195
0
        }
5196
0
        return len;
5197
0
    }
5198
0
    return 0;
5199
0
}
5200
5201
static int
5202
scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
5203
0
{
5204
0
    uint32_t flags, fmask;
5205
0
    int n;
5206
5207
0
    n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
5208
0
                        FLOW_TNL_F_MASK, mask ? &fmask : NULL);
5209
0
    if (n >= 0 && s[n] == ')') {
5210
0
        *key = flags;
5211
0
        if (mask) {
5212
0
            *mask = fmask;
5213
0
        }
5214
0
        return n + 1;
5215
0
    }
5216
0
    return 0;
5217
0
}
5218
5219
static int
5220
scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
5221
0
{
5222
0
    uint32_t flags, fmask;
5223
0
    int n;
5224
5225
0
    n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
5226
0
                        TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
5227
0
    if (n >= 0) {
5228
0
        *key = htons(flags);
5229
0
        if (mask) {
5230
0
            *mask = htons(fmask);
5231
0
        }
5232
0
        return n;
5233
0
    }
5234
0
    return 0;
5235
0
}
5236
5237
static uint32_t
5238
ovs_to_odp_ct_state(uint8_t state)
5239
0
{
5240
0
    uint32_t odp = 0;
5241
5242
0
#define CS_STATE(ENUM, INDEX, NAME)             \
5243
0
    if (state & CS_##ENUM) {                    \
5244
0
        odp |= OVS_CS_F_##ENUM;                 \
5245
0
    }
5246
0
    CS_STATES
5247
0
#undef CS_STATE
5248
5249
0
    return odp;
5250
0
}
5251
5252
static uint8_t
5253
odp_to_ovs_ct_state(uint32_t flags)
5254
0
{
5255
0
    uint32_t state = 0;
5256
5257
0
#define CS_STATE(ENUM, INDEX, NAME) \
5258
0
    if (flags & OVS_CS_F_##ENUM) {  \
5259
0
        state |= CS_##ENUM;         \
5260
0
    }
5261
0
    CS_STATES
5262
0
#undef CS_STATE
5263
5264
0
    return state;
5265
0
}
5266
5267
static int
5268
scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
5269
0
{
5270
0
    uint32_t flags, fmask;
5271
0
    int n;
5272
5273
0
    n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
5274
0
                    ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
5275
0
                    mask ? &fmask : NULL);
5276
5277
0
    if (n >= 0) {
5278
0
        *key = flags;
5279
0
        if (mask) {
5280
0
            *mask = fmask;
5281
0
        }
5282
0
        return n;
5283
0
    }
5284
0
    return 0;
5285
0
}
5286
5287
static int
5288
scan_frag(const char *s, uint8_t *key, uint8_t *mask)
5289
0
{
5290
0
    int n;
5291
0
    char frag[8];
5292
0
    enum ovs_frag_type frag_type;
5293
5294
0
    if (ovs_scan(s, "%7[a-z]%n", frag, &n)
5295
0
        && ovs_frag_type_from_string(frag, &frag_type)) {
5296
0
        int len = n;
5297
5298
0
        *key = frag_type;
5299
0
        if (mask) {
5300
0
            *mask = UINT8_MAX;
5301
0
        }
5302
0
        return len;
5303
0
    }
5304
0
    return 0;
5305
0
}
5306
5307
static int
5308
scan_port(const char *s, uint32_t *key, uint32_t *mask,
5309
          const struct simap *port_names)
5310
0
{
5311
0
    int n;
5312
5313
0
    if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
5314
0
        int len = n;
5315
5316
0
        if (mask) {
5317
0
            if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
5318
0
                len += n;
5319
0
            } else {
5320
0
                *mask = UINT32_MAX;
5321
0
            }
5322
0
        }
5323
0
        return len;
5324
0
    } else if (port_names) {
5325
0
        const struct simap_node *node;
5326
0
        int len;
5327
5328
0
        len = strcspn(s, ")");
5329
0
        node = simap_find_len(port_names, s, len);
5330
0
        if (node) {
5331
0
            *key = node->data;
5332
5333
0
            if (mask) {
5334
0
                *mask = UINT32_MAX;
5335
0
            }
5336
0
            return len;
5337
0
        }
5338
0
    }
5339
0
    return 0;
5340
0
}
5341
5342
/* Helper for vlan parsing. */
5343
struct ovs_key_vlan__ {
5344
    ovs_be16 tci;
5345
};
5346
5347
static bool
5348
set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
5349
0
{
5350
0
    const uint16_t mask = ((1U << bits) - 1) << offset;
5351
5352
0
    if (value >> bits) {
5353
0
        return false;
5354
0
    }
5355
5356
0
    *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
5357
0
    return true;
5358
0
}
5359
5360
static int
5361
scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
5362
             uint8_t offset)
5363
0
{
5364
0
    uint16_t key_, mask_;
5365
0
    int n;
5366
5367
0
    if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
5368
0
        int len = n;
5369
5370
0
        if (set_be16_bf(key, bits, offset, key_)) {
5371
0
            if (mask) {
5372
0
                if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
5373
0
                    len += n;
5374
5375
0
                    if (!set_be16_bf(mask, bits, offset, mask_)) {
5376
0
                        return 0;
5377
0
                    }
5378
0
                } else {
5379
0
                    *mask |= htons(((1U << bits) - 1) << offset);
5380
0
                }
5381
0
            }
5382
0
            return len;
5383
0
        }
5384
0
    }
5385
0
    return 0;
5386
0
}
5387
5388
static int
5389
scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
5390
0
{
5391
0
    return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
5392
0
}
5393
5394
static int
5395
scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
5396
0
{
5397
0
    return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
5398
0
}
5399
5400
static int
5401
scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
5402
0
{
5403
0
    return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
5404
0
}
5405
5406
/* For MPLS. */
5407
static bool
5408
set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
5409
0
{
5410
0
    const uint32_t mask = ((1U << bits) - 1) << offset;
5411
5412
0
    if (value >> bits) {
5413
0
        return false;
5414
0
    }
5415
5416
0
    *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
5417
0
    return true;
5418
0
}
5419
5420
static int
5421
scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
5422
             uint8_t offset)
5423
0
{
5424
0
    uint32_t key_, mask_;
5425
0
    int n;
5426
5427
0
    if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
5428
0
        int len = n;
5429
5430
0
        if (set_be32_bf(key, bits, offset, key_)) {
5431
0
            if (mask) {
5432
0
                if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
5433
0
                    len += n;
5434
5435
0
                    if (!set_be32_bf(mask, bits, offset, mask_)) {
5436
0
                        return 0;
5437
0
                    }
5438
0
                } else {
5439
0
                    *mask |= htonl(((1U << bits) - 1) << offset);
5440
0
                }
5441
0
            }
5442
0
            return len;
5443
0
        }
5444
0
    }
5445
0
    return 0;
5446
0
}
5447
5448
static int
5449
scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
5450
0
{
5451
0
    return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
5452
0
}
5453
5454
static int
5455
scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
5456
0
{
5457
0
    return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
5458
0
}
5459
5460
static int
5461
scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
5462
0
{
5463
0
    return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
5464
0
}
5465
5466
static int
5467
scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
5468
0
{
5469
0
    return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
5470
0
}
5471
5472
static int
5473
scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
5474
0
{
5475
0
    const char *s_base = s;
5476
0
    ovs_be16 id = 0, id_mask = 0;
5477
0
    uint8_t flags = 0, flags_mask = 0;
5478
0
    int len;
5479
5480
0
    if (!strncmp(s, "id=", 3)) {
5481
0
        s += 3;
5482
0
        len = scan_be16(s, &id, mask ? &id_mask : NULL);
5483
0
        if (len == 0) {
5484
0
            return 0;
5485
0
        }
5486
0
        s += len;
5487
0
    }
5488
5489
0
    if (s[0] == ',') {
5490
0
        s++;
5491
0
    }
5492
0
    if (!strncmp(s, "flags=", 6)) {
5493
0
        s += 6;
5494
0
        len = scan_u8(s, &flags, mask ? &flags_mask : NULL);
5495
0
        if (len == 0) {
5496
0
            return 0;
5497
0
        }
5498
0
        s += len;
5499
0
    }
5500
5501
0
    if (!strncmp(s, "))", 2)) {
5502
0
        s += 2;
5503
5504
0
        *key = (flags << 16) | ntohs(id);
5505
0
        if (mask) {
5506
0
            *mask = (flags_mask << 16) | ntohs(id_mask);
5507
0
        }
5508
5509
0
        return s - s_base;
5510
0
    }
5511
5512
0
    return 0;
5513
0
}
5514
5515
static int
5516
scan_gtpu_metadata(const char *s,
5517
                   struct gtpu_metadata *key,
5518
                   struct gtpu_metadata *mask)
5519
0
{
5520
0
    const char *s_base = s;
5521
0
    uint8_t flags = 0, flags_ma = 0;
5522
0
    uint8_t msgtype = 0, msgtype_ma = 0;
5523
0
    int len;
5524
5525
0
    if (!strncmp(s, "flags=", 6)) {
5526
0
        s += 6;
5527
0
        len = scan_u8(s, &flags, mask ? &flags_ma : NULL);
5528
0
        if (len == 0) {
5529
0
            return 0;
5530
0
        }
5531
0
        s += len;
5532
0
    }
5533
5534
0
    if (s[0] == ',') {
5535
0
        s++;
5536
0
    }
5537
5538
0
    if (!strncmp(s, "msgtype=", 8)) {
5539
0
        s += 8;
5540
0
        len = scan_u8(s, &msgtype, mask ? &msgtype_ma : NULL);
5541
0
        if (len == 0) {
5542
0
            return 0;
5543
0
        }
5544
0
        s += len;
5545
0
    }
5546
5547
0
    if (!strncmp(s, ")", 1)) {
5548
0
        s += 1;
5549
0
        key->flags = flags;
5550
0
        key->msgtype = msgtype;
5551
0
        if (mask) {
5552
0
            mask->flags = flags_ma;
5553
0
            mask->msgtype = msgtype_ma;
5554
0
        }
5555
0
    }
5556
0
    return s - s_base;
5557
0
}
5558
5559
static int
5560
scan_erspan_metadata(const char *s,
5561
                     struct erspan_metadata *key,
5562
                     struct erspan_metadata *mask)
5563
0
{
5564
0
    const char *s_base = s;
5565
0
    uint32_t idx = 0, idx_mask = 0;
5566
0
    uint8_t ver = 0, dir = 0, hwid = 0;
5567
0
    uint8_t ver_mask = 0, dir_mask = 0, hwid_mask = 0;
5568
0
    int len;
5569
5570
0
    if (!strncmp(s, "ver=", 4)) {
5571
0
        s += 4;
5572
0
        len = scan_u8(s, &ver, mask ? &ver_mask : NULL);
5573
0
        if (len == 0) {
5574
0
            return 0;
5575
0
        }
5576
0
        s += len;
5577
0
    }
5578
5579
0
    if (s[0] == ',') {
5580
0
        s++;
5581
0
    }
5582
5583
0
    if (ver == 1) {
5584
0
        if (!strncmp(s, "idx=", 4)) {
5585
0
            s += 4;
5586
0
            len = scan_u32(s, &idx, mask ? &idx_mask : NULL);
5587
0
            if (len == 0) {
5588
0
                return 0;
5589
0
            }
5590
0
            s += len;
5591
0
        }
5592
5593
0
        if (!strncmp(s, ")", 1)) {
5594
0
            s += 1;
5595
0
            key->version = ver;
5596
0
            key->u.index = htonl(idx);
5597
0
            if (mask) {
5598
0
                mask->u.index = htonl(idx_mask);
5599
0
            }
5600
0
        }
5601
0
        return s - s_base;
5602
5603
0
    } else if (ver == 2) {
5604
0
        if (!strncmp(s, "dir=", 4)) {
5605
0
            s += 4;
5606
0
            len = scan_u8(s, &dir, mask ? &dir_mask : NULL);
5607
0
            if (len == 0) {
5608
0
                return 0;
5609
0
            }
5610
0
            s += len;
5611
0
        }
5612
0
        if (s[0] == ',') {
5613
0
            s++;
5614
0
        }
5615
0
        if (!strncmp(s, "hwid=", 5)) {
5616
0
            s += 5;
5617
0
            len = scan_u8(s, &hwid, mask ? &hwid_mask : NULL);
5618
0
            if (len == 0) {
5619
0
                return 0;
5620
0
            }
5621
0
            s += len;
5622
0
        }
5623
5624
0
        if (!strncmp(s, ")", 1)) {
5625
0
            s += 1;
5626
0
            key->version = ver;
5627
0
            key->u.md2.hwid = hwid;
5628
0
            key->u.md2.dir = dir;
5629
0
            if (mask) {
5630
0
                mask->u.md2.hwid = hwid_mask;
5631
0
                mask->u.md2.dir = dir_mask;
5632
0
            }
5633
0
        }
5634
0
        return s - s_base;
5635
0
    }
5636
5637
0
    return 0;
5638
0
}
5639
5640
static int
5641
scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
5642
0
{
5643
0
    const char *s_base = s;
5644
0
    struct geneve_opt *opt = key->d;
5645
0
    struct geneve_opt *opt_mask = mask ? mask->d : NULL;
5646
0
    int len_remain = sizeof key->d;
5647
0
    int len;
5648
5649
0
    while (s[0] == '{' && len_remain >= sizeof *opt) {
5650
0
        int data_len = 0;
5651
5652
0
        s++;
5653
0
        len_remain -= sizeof *opt;
5654
5655
0
        if (!strncmp(s, "class=", 6)) {
5656
0
            s += 6;
5657
0
            len = scan_be16(s, &opt->opt_class,
5658
0
                            mask ? &opt_mask->opt_class : NULL);
5659
0
            if (len == 0) {
5660
0
                return 0;
5661
0
            }
5662
0
            s += len;
5663
0
        } else if (mask) {
5664
0
            memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
5665
0
        }
5666
5667
0
        if (s[0] == ',') {
5668
0
            s++;
5669
0
        }
5670
0
        if (!strncmp(s, "type=", 5)) {
5671
0
            s += 5;
5672
0
            len = scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
5673
0
            if (len == 0) {
5674
0
                return 0;
5675
0
            }
5676
0
            s += len;
5677
0
        } else if (mask) {
5678
0
            memset(&opt_mask->type, 0, sizeof opt_mask->type);
5679
0
        }
5680
5681
0
        if (s[0] == ',') {
5682
0
            s++;
5683
0
        }
5684
0
        if (!strncmp(s, "len=", 4)) {
5685
0
            uint8_t opt_len, opt_len_mask;
5686
0
            s += 4;
5687
0
            len = scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
5688
0
            if (len == 0) {
5689
0
                return 0;
5690
0
            }
5691
0
            s += len;
5692
5693
0
            if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
5694
0
                return 0;
5695
0
            }
5696
0
            opt->length = opt_len / 4;
5697
0
            if (mask) {
5698
0
                opt_mask->length = opt_len_mask;
5699
0
            }
5700
0
            data_len = opt_len;
5701
0
        } else if (mask) {
5702
0
            memset(&opt_mask->type, 0, sizeof opt_mask->type);
5703
0
        }
5704
5705
0
        if (s[0] == ',') {
5706
0
            s++;
5707
0
            if (parse_int_string(s, (uint8_t *)(opt + 1),
5708
0
                                 data_len, (char **)&s)) {
5709
0
                return 0;
5710
0
            }
5711
0
        }
5712
0
        if (mask) {
5713
0
            if (s[0] == '/') {
5714
0
                s++;
5715
0
                if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
5716
0
                                     data_len, (char **)&s)) {
5717
0
                    return 0;
5718
0
                }
5719
0
            }
5720
0
            opt_mask->r1 = 0;
5721
0
            opt_mask->r2 = 0;
5722
0
            opt_mask->r3 = 0;
5723
0
        }
5724
5725
0
        if (s[0] == '}') {
5726
0
            s++;
5727
0
            opt += 1 + data_len / 4;
5728
0
            if (mask) {
5729
0
                opt_mask += 1 + data_len / 4;
5730
0
            }
5731
0
            len_remain -= data_len;
5732
0
        } else {
5733
0
            return 0;
5734
0
        }
5735
0
    }
5736
5737
0
    if (s[0] == ')') {
5738
0
        len = sizeof key->d - len_remain;
5739
5740
0
        s++;
5741
0
        key->len = len;
5742
0
        if (mask) {
5743
0
            mask->len = len;
5744
0
        }
5745
0
        return s - s_base;
5746
0
    }
5747
5748
0
    return 0;
5749
0
}
5750
5751
static void
5752
tun_flags_to_attr(struct ofpbuf *a, const void *data_)
5753
0
{
5754
0
    const uint16_t *flags = data_;
5755
5756
0
    if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
5757
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
5758
0
    }
5759
0
    if (*flags & FLOW_TNL_F_CSUM) {
5760
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
5761
0
    }
5762
0
    if (*flags & FLOW_TNL_F_OAM) {
5763
0
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
5764
0
    }
5765
0
}
5766
5767
static void
5768
vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
5769
0
{
5770
0
    const uint32_t *gbp = data_;
5771
5772
0
    if (*gbp) {
5773
0
        size_t vxlan_opts_ofs;
5774
5775
0
        vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
5776
0
        nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
5777
0
        nl_msg_end_nested(a, vxlan_opts_ofs);
5778
0
    }
5779
0
}
5780
5781
static void
5782
geneve_to_attr(struct ofpbuf *a, const void *data_)
5783
0
{
5784
0
    const struct geneve_scan *geneve = data_;
5785
5786
0
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
5787
0
                      geneve->len);
5788
0
}
5789
5790
static void
5791
erspan_to_attr(struct ofpbuf *a, const void *data_)
5792
0
{
5793
0
    const struct erspan_metadata *md = data_;
5794
5795
0
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, md,
5796
0
                      sizeof *md);
5797
0
}
5798
5799
static void
5800
gtpu_to_attr(struct ofpbuf *a, const void *data_)
5801
0
{
5802
0
    const struct gtpu_metadata *md = data_;
5803
5804
0
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS, md,
5805
0
                      sizeof *md);
5806
0
}
5807
5808
#define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC)                      \
5809
0
    {                                                             \
5810
0
        unsigned long call_fn = (unsigned long)FUNC;              \
5811
0
        if (call_fn) {                                            \
5812
0
            typedef void (*fn)(struct ofpbuf *, const void *);    \
5813
0
            fn func = FUNC;                                       \
5814
0
            func(BUF, &(DATA));                                   \
5815
0
        } else {                                                  \
5816
0
            nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5817
0
        }                                                         \
5818
0
    }
5819
5820
#define SCAN_IF(NAME)                           \
5821
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {  \
5822
0
        const char *start = s;                  \
5823
0
        int len;                                \
5824
0
                                                \
5825
0
        s += strlen(NAME)
5826
5827
/* Usually no special initialization is needed. */
5828
#define SCAN_BEGIN(NAME, TYPE)                  \
5829
0
    SCAN_IF(NAME);                              \
5830
0
        TYPE skey, smask;                       \
5831
0
        memset(&skey, 0, sizeof skey);          \
5832
0
        memset(&smask, 0, sizeof smask);        \
5833
0
        do {                                    \
5834
0
            len = 0;
5835
5836
/* Init as fully-masked as mask will not be scanned. */
5837
#define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE)     \
5838
0
    SCAN_IF(NAME);                              \
5839
0
        TYPE skey, smask;                       \
5840
0
        memset(&skey, 0, sizeof skey);          \
5841
0
        memset(&smask, 0xff, sizeof smask);     \
5842
0
        do {                                    \
5843
0
            len = 0;
5844
5845
/* VLAN needs special initialization. */
5846
#define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT)  \
5847
0
    SCAN_IF(NAME);                                        \
5848
0
        TYPE skey = KEY_INIT;                       \
5849
0
        TYPE smask = MASK_INIT;                     \
5850
0
        do {                                        \
5851
0
            len = 0;
5852
5853
/* Scan unnamed entry as 'TYPE' */
5854
#define SCAN_TYPE(TYPE, KEY, MASK)              \
5855
0
    len = scan_##TYPE(s, KEY, MASK);            \
5856
0
    if (len == 0) {                             \
5857
0
        return -EINVAL;                         \
5858
0
    }                                           \
5859
0
    s += len
5860
5861
/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5862
#define SCAN_FIELD(NAME, TYPE, FIELD)                                   \
5863
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
5864
0
        s += strlen(NAME);                                              \
5865
0
        SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL);       \
5866
0
        continue;                                                       \
5867
0
    }
5868
5869
#define SCAN_FINISH()                           \
5870
0
        } while (*s++ == ',' && len != 0);      \
5871
0
        if (s[-1] != ')') {                     \
5872
0
            return -EINVAL;                     \
5873
0
        }
5874
5875
#define SCAN_FINISH_SINGLE()                    \
5876
0
        } while (false);                        \
5877
0
        if (*s++ != ')') {                      \
5878
0
            return -EINVAL;                     \
5879
0
        }
5880
5881
/* Beginning of nested attribute. */
5882
#define SCAN_BEGIN_NESTED(NAME, ATTR)                      \
5883
0
    SCAN_IF(NAME);                                         \
5884
0
        size_t key_offset, mask_offset = 0;                \
5885
0
        key_offset = nl_msg_start_nested(key, ATTR);       \
5886
0
        if (mask) {                                        \
5887
0
            mask_offset = nl_msg_start_nested(mask, ATTR); \
5888
0
        }                                                  \
5889
0
        do {                                               \
5890
0
            len = 0;
5891
5892
#define SCAN_END_NESTED()                                                     \
5893
0
        SCAN_FINISH();                                                        \
5894
0
        if (nl_attr_oversized(key->size - key_offset - NLA_HDRLEN)) {         \
5895
0
            return -E2BIG;                                                    \
5896
0
        }                                                                     \
5897
0
        nl_msg_end_nested(key, key_offset);                                   \
5898
0
        if (mask) {                                                           \
5899
0
            nl_msg_end_nested(mask, mask_offset);                             \
5900
0
        }                                                                     \
5901
0
        return s - start;                                                     \
5902
0
    }
5903
5904
#define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC)  \
5905
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                \
5906
0
        TYPE skey, smask;                                     \
5907
0
        memset(&skey, 0, sizeof skey);                        \
5908
0
        memset(&smask, 0xff, sizeof smask);                   \
5909
0
        s += strlen(NAME);                                    \
5910
0
        SCAN_TYPE(SCAN_AS, &skey, &smask);                    \
5911
0
        SCAN_PUT(ATTR, FUNC);                                 \
5912
0
        continue;                                             \
5913
0
    }
5914
5915
#define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR)  \
5916
0
        SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5917
5918
#define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC)  \
5919
0
        SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5920
5921
#define SCAN_PUT(ATTR, FUNC)                            \
5922
0
        SCAN_PUT_ATTR(key, ATTR, skey, FUNC);           \
5923
0
        if (mask)                                       \
5924
0
            SCAN_PUT_ATTR(mask, ATTR, smask, FUNC);     \
5925
5926
#define SCAN_END(ATTR)                                  \
5927
0
        SCAN_FINISH();                                  \
5928
0
        SCAN_PUT(ATTR, NULL);                           \
5929
0
        return s - start;                               \
5930
0
    }
5931
5932
#define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT)       \
5933
0
    SCAN_IF(NAME);                              \
5934
0
        TYPE skey[CNT], smask[CNT];             \
5935
0
        memset(&skey, 0, sizeof skey);          \
5936
0
        memset(&smask, 0, sizeof smask);        \
5937
0
        int idx = 0, cnt = CNT;                 \
5938
0
        uint64_t fields = 0;                    \
5939
0
        do {                                    \
5940
0
            int field = 0;                      \
5941
0
            len = 0;
5942
5943
/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5944
#define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD)                             \
5945
0
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
5946
0
        if (fields & (1UL << field)) {                                  \
5947
0
            fields = 0;                                                 \
5948
0
            if (++idx == cnt) {                                         \
5949
0
                break;                                                  \
5950
0
            }                                                           \
5951
0
        }                                                               \
5952
0
        s += strlen(NAME);                                              \
5953
0
        SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5954
0
        fields |= 1UL << field;                                         \
5955
0
        continue;                                                       \
5956
0
    }                                                                   \
5957
0
    field++;
5958
5959
#define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT)                    \
5960
0
    nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5961
5962
#define SCAN_PUT_ARRAY(ATTR, CNT)                        \
5963
0
    SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT);       \
5964
0
    if (mask) {                                      \
5965
0
        SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5966
0
    }
5967
5968
#define SCAN_END_ARRAY(ATTR)             \
5969
0
        SCAN_FINISH();                   \
5970
0
        if (idx == cnt) {                \
5971
0
            return -EINVAL;              \
5972
0
        }                                \
5973
0
        SCAN_PUT_ARRAY(ATTR, idx + 1);   \
5974
0
        return s - start;                \
5975
0
    }
5976
5977
#define SCAN_END_SINGLE(ATTR)                           \
5978
0
        SCAN_FINISH_SINGLE();                           \
5979
0
        SCAN_PUT(ATTR, NULL);                           \
5980
0
        return s - start;                               \
5981
0
    }
5982
5983
#define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR)       \
5984
0
    SCAN_BEGIN(NAME, TYPE) {                         \
5985
0
        SCAN_TYPE(SCAN_AS, &skey, &smask);           \
5986
0
    } SCAN_END_SINGLE(ATTR)
5987
5988
#define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5989
0
    SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) {                   \
5990
0
        SCAN_TYPE(SCAN_AS, &skey, NULL);                    \
5991
0
    } SCAN_END_SINGLE(ATTR)
5992
5993
/* scan_port needs one extra argument. */
5994
#define SCAN_SINGLE_PORT(NAME, TYPE, ATTR)  \
5995
0
    SCAN_BEGIN(NAME, TYPE) {                            \
5996
0
        len = scan_port(s, &skey, &smask,               \
5997
0
                        context->port_names);           \
5998
0
        if (len == 0) {                                 \
5999
0
            return -EINVAL;                             \
6000
0
        }                                               \
6001
0
        s += len;                                       \
6002
0
    } SCAN_END_SINGLE(ATTR)
6003
6004
static int
6005
parse_odp_nsh_key_mask_attr(const char *s, struct ofpbuf *key,
6006
                            struct ofpbuf *mask)
6007
0
{
6008
0
    if (strncmp(s, "nsh(", 4) == 0) {
6009
0
        const char *start = s;
6010
0
        int len;
6011
0
        struct ovs_key_nsh skey, smask;
6012
0
        uint32_t spi = 0, spi_mask = 0;
6013
0
        uint8_t si = 0, si_mask = 0;
6014
6015
0
        s += 4;
6016
6017
0
        memset(&skey, 0, sizeof skey);
6018
0
        memset(&smask, 0, sizeof smask);
6019
0
        do {
6020
0
            len = 0;
6021
6022
0
            if (strncmp(s, "flags=", 6) == 0) {
6023
0
                s += 6;
6024
0
                len = scan_u8(s, &skey.flags, mask ? &smask.flags : NULL);
6025
0
                if (len == 0) {
6026
0
                    return -EINVAL;
6027
0
                }
6028
0
                s += len;
6029
0
                continue;
6030
0
            }
6031
6032
0
            if (strncmp(s, "mdtype=", 7) == 0) {
6033
0
                s += 7;
6034
0
                len = scan_u8(s, &skey.mdtype, mask ? &smask.mdtype : NULL);
6035
0
                if (len == 0) {
6036
0
                    return -EINVAL;
6037
0
                }
6038
0
                s += len;
6039
0
                continue;
6040
0
            }
6041
6042
0
            if (strncmp(s, "np=", 3) == 0) {
6043
0
                s += 3;
6044
0
                len = scan_u8(s, &skey.np, mask ? &smask.np : NULL);
6045
0
                if (len == 0) {
6046
0
                    return -EINVAL;
6047
0
                }
6048
0
                s += len;
6049
0
                continue;
6050
0
            }
6051
6052
0
            if (strncmp(s, "spi=", 4) == 0) {
6053
0
                s += 4;
6054
0
                len = scan_u32(s, &spi, mask ? &spi_mask : NULL);
6055
0
                if (len == 0) {
6056
0
                    return -EINVAL;
6057
0
                }
6058
0
                s += len;
6059
0
                continue;
6060
0
            }
6061
6062
0
            if (strncmp(s, "si=", 3) == 0) {
6063
0
                s += 3;
6064
0
                len = scan_u8(s, &si, mask ? &si_mask : NULL);
6065
0
                if (len == 0) {
6066
0
                    return -EINVAL;
6067
0
                }
6068
0
                s += len;
6069
0
                continue;
6070
0
            }
6071
6072
0
            if (strncmp(s, "c1=", 3) == 0) {
6073
0
                s += 3;
6074
0
                len = scan_be32(s, &skey.context[0],
6075
0
                                mask ? &smask.context[0] : NULL);
6076
0
                if (len == 0) {
6077
0
                    return -EINVAL;
6078
0
                }
6079
0
                s += len;
6080
0
                continue;
6081
0
            }
6082
6083
0
            if (strncmp(s, "c2=", 3) == 0) {
6084
0
                s += 3;
6085
0
                len = scan_be32(s, &skey.context[1],
6086
0
                                mask ? &smask.context[1] : NULL);
6087
0
                if (len == 0) {
6088
0
                    return -EINVAL;
6089
0
                }
6090
0
                s += len;
6091
0
                continue;
6092
0
            }
6093
6094
0
            if (strncmp(s, "c3=", 3) == 0) {
6095
0
                s += 3;
6096
0
                len = scan_be32(s, &skey.context[2],
6097
0
                                mask ? &smask.context[2] : NULL);
6098
0
                if (len == 0) {
6099
0
                    return -EINVAL;
6100
0
                }
6101
0
                s += len;
6102
0
                continue;
6103
0
            }
6104
6105
0
            if (strncmp(s, "c4=", 3) == 0) {
6106
0
                s += 3;
6107
0
                len = scan_be32(s, &skey.context[3],
6108
0
                                mask ? &smask.context[3] : NULL);
6109
0
                if (len == 0) {
6110
0
                    return -EINVAL;
6111
0
                }
6112
0
                s += len;
6113
0
                continue;
6114
0
            }
6115
0
        } while (*s++ == ',' && len != 0);
6116
0
        if (s[-1] != ')') {
6117
0
            return -EINVAL;
6118
0
        }
6119
6120
0
        skey.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
6121
0
        smask.path_hdr = nsh_spi_si_to_path_hdr(spi_mask, si_mask);
6122
6123
0
        nsh_key_to_attr(key, &skey, NULL, 0, false);
6124
0
        if (mask) {
6125
0
            nsh_key_to_attr(mask, &smask, NULL, 0, true);
6126
0
        }
6127
0
        return s - start;
6128
0
    }
6129
0
    return 0;
6130
0
}
6131
6132
static int
6133
parse_odp_key_mask_attr(struct parse_odp_context *context, const char *s,
6134
                        struct ofpbuf *key, struct ofpbuf *mask)
6135
0
{
6136
0
    int retval;
6137
6138
0
    context->depth++;
6139
6140
0
    if (context->depth == MAX_ODP_NESTED) {
6141
0
        retval = -EINVAL;
6142
0
    } else {
6143
0
        retval = parse_odp_key_mask_attr__(context, s, key, mask);
6144
0
    }
6145
6146
0
    context->depth--;
6147
6148
0
    return retval;
6149
0
}
6150
6151
static int
6152
parse_odp_key_mask_attr__(struct parse_odp_context *context, const char *s,
6153
                          struct ofpbuf *key, struct ofpbuf *mask)
6154
0
{
6155
0
    SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
6156
0
    SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
6157
0
    SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
6158
0
                             OVS_KEY_ATTR_RECIRC_ID);
6159
0
    SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
6160
6161
0
    SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
6162
0
    SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
6163
0
    SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
6164
0
    SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
6165
6166
0
    SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) {
6167
0
        SCAN_FIELD("src=", ipv4, ipv4_src);
6168
0
        SCAN_FIELD("dst=", ipv4, ipv4_dst);
6169
0
        SCAN_FIELD("proto=", u8, ipv4_proto);
6170
0
        SCAN_FIELD("tp_src=", be16, src_port);
6171
0
        SCAN_FIELD("tp_dst=", be16, dst_port);
6172
0
    } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
6173
6174
0
    SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) {
6175
0
        SCAN_FIELD("src=", in6_addr, ipv6_src);
6176
0
        SCAN_FIELD("dst=", in6_addr, ipv6_dst);
6177
0
        SCAN_FIELD("proto=", u8, ipv6_proto);
6178
0
        SCAN_FIELD("tp_src=", be16, src_port);
6179
0
        SCAN_FIELD("tp_dst=", be16, dst_port);
6180
0
    } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
6181
6182
0
    SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
6183
0
        SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
6184
0
        SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
6185
0
        SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
6186
0
        SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
6187
0
        SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
6188
0
        SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
6189
0
        SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
6190
0
        SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
6191
0
        SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
6192
0
        SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata, erspan_metadata,
6193
0
                               erspan_to_attr);
6194
0
        SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
6195
0
        SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
6196
0
                               geneve_to_attr);
6197
0
        SCAN_FIELD_NESTED_FUNC("gtpu(", struct gtpu_metadata, gtpu_metadata,
6198
0
                               gtpu_to_attr);
6199
0
        SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
6200
0
    } SCAN_END_NESTED();
6201
6202
0
    SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
6203
6204
0
    SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
6205
0
        SCAN_FIELD("src=", eth, eth_src);
6206
0
        SCAN_FIELD("dst=", eth, eth_dst);
6207
0
    } SCAN_END(OVS_KEY_ATTR_ETHERNET);
6208
6209
0
    SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
6210
0
                    { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
6211
0
        SCAN_FIELD("vid=", vid, tci);
6212
0
        SCAN_FIELD("pcp=", pcp, tci);
6213
0
        SCAN_FIELD("cfi=", cfi, tci);
6214
0
    } SCAN_END(OVS_KEY_ATTR_VLAN);
6215
6216
0
    SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
6217
6218
0
    SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
6219
0
        SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
6220
0
        SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
6221
0
        SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
6222
0
        SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
6223
0
    } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
6224
6225
0
    SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
6226
0
        SCAN_FIELD("src=", ipv4, ipv4_src);
6227
0
        SCAN_FIELD("dst=", ipv4, ipv4_dst);
6228
0
        SCAN_FIELD("proto=", u8, ipv4_proto);
6229
0
        SCAN_FIELD("tos=", u8, ipv4_tos);
6230
0
        SCAN_FIELD("ttl=", u8, ipv4_ttl);
6231
0
        SCAN_FIELD("frag=", frag, ipv4_frag);
6232
0
    } SCAN_END(OVS_KEY_ATTR_IPV4);
6233
6234
0
    SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
6235
0
        SCAN_FIELD("src=", in6_addr, ipv6_src);
6236
0
        SCAN_FIELD("dst=", in6_addr, ipv6_dst);
6237
0
        SCAN_FIELD("label=", ipv6_label, ipv6_label);
6238
0
        SCAN_FIELD("proto=", u8, ipv6_proto);
6239
0
        SCAN_FIELD("tclass=", u8, ipv6_tclass);
6240
0
        SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
6241
0
        SCAN_FIELD("frag=", frag, ipv6_frag);
6242
0
    } SCAN_END(OVS_KEY_ATTR_IPV6);
6243
6244
0
    SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
6245
0
        SCAN_FIELD("src=", be16, tcp_src);
6246
0
        SCAN_FIELD("dst=", be16, tcp_dst);
6247
0
    } SCAN_END(OVS_KEY_ATTR_TCP);
6248
6249
0
    SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
6250
6251
0
    SCAN_BEGIN("udp(", struct ovs_key_udp) {
6252
0
        SCAN_FIELD("src=", be16, udp_src);
6253
0
        SCAN_FIELD("dst=", be16, udp_dst);
6254
0
    } SCAN_END(OVS_KEY_ATTR_UDP);
6255
6256
0
    SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
6257
0
        SCAN_FIELD("src=", be16, sctp_src);
6258
0
        SCAN_FIELD("dst=", be16, sctp_dst);
6259
0
    } SCAN_END(OVS_KEY_ATTR_SCTP);
6260
6261
0
    SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
6262
0
        SCAN_FIELD("type=", u8, icmp_type);
6263
0
        SCAN_FIELD("code=", u8, icmp_code);
6264
0
    } SCAN_END(OVS_KEY_ATTR_ICMP);
6265
6266
0
    SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
6267
0
        SCAN_FIELD("type=", u8, icmpv6_type);
6268
0
        SCAN_FIELD("code=", u8, icmpv6_code);
6269
0
    } SCAN_END(OVS_KEY_ATTR_ICMPV6);
6270
6271
0
    SCAN_BEGIN("arp(", struct ovs_key_arp) {
6272
0
        SCAN_FIELD("sip=", ipv4, arp_sip);
6273
0
        SCAN_FIELD("tip=", ipv4, arp_tip);
6274
0
        SCAN_FIELD("op=", be16, arp_op);
6275
0
        SCAN_FIELD("sha=", eth, arp_sha);
6276
0
        SCAN_FIELD("tha=", eth, arp_tha);
6277
0
    } SCAN_END(OVS_KEY_ATTR_ARP);
6278
6279
0
    SCAN_BEGIN("nd(", struct ovs_key_nd) {
6280
0
        SCAN_FIELD("target=", in6_addr, nd_target);
6281
0
        SCAN_FIELD("sll=", eth, nd_sll);
6282
0
        SCAN_FIELD("tll=", eth, nd_tll);
6283
0
    } SCAN_END(OVS_KEY_ATTR_ND);
6284
6285
0
    SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions) {
6286
0
        SCAN_FIELD("nd_reserved=", be32, nd_reserved);
6287
0
        SCAN_FIELD("nd_options_type=", u8, nd_options_type);
6288
0
    } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS);
6289
6290
0
    struct packet_type {
6291
0
        ovs_be16 ns;
6292
0
        ovs_be16 id;
6293
0
    };
6294
0
    SCAN_BEGIN("packet_type(", struct packet_type) {
6295
0
        SCAN_FIELD("ns=", be16, ns);
6296
0
        SCAN_FIELD("id=", be16, id);
6297
0
    } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE);
6298
6299
    /* nsh is nested, it needs special process */
6300
0
    int ret = parse_odp_nsh_key_mask_attr(s, key, mask);
6301
0
    if (ret < 0) {
6302
0
        return ret;
6303
0
    } else {
6304
0
        s += ret;
6305
0
    }
6306
6307
    /* Encap open-coded. */
6308
0
    if (!strncmp(s, "encap(", 6)) {
6309
0
        const char *start = s;
6310
0
        size_t encap, encap_mask = 0;
6311
6312
0
        encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
6313
0
        if (mask) {
6314
0
            encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
6315
0
        }
6316
6317
0
        s += 6;
6318
0
        for (;;) {
6319
0
            int retval;
6320
6321
0
            s += strspn(s, delimiters);
6322
0
            if (!*s) {
6323
0
                return -EINVAL;
6324
0
            } else if (*s == ')') {
6325
0
                break;
6326
0
            }
6327
6328
0
            retval = parse_odp_key_mask_attr(context, s, key, mask);
6329
0
            if (retval < 0) {
6330
0
                return retval;
6331
0
            }
6332
6333
0
            if (nl_attr_oversized(key->size - encap - NLA_HDRLEN)) {
6334
0
                return -E2BIG;
6335
0
            }
6336
0
            s += retval;
6337
0
        }
6338
0
        s++;
6339
6340
0
        nl_msg_end_nested(key, encap);
6341
0
        if (mask) {
6342
0
            nl_msg_end_nested(mask, encap_mask);
6343
0
        }
6344
6345
0
        return s - start;
6346
0
    }
6347
6348
0
    return -EINVAL;
6349
0
}
6350
6351
/* Parses the string representation of a datapath flow key, in the format
6352
 * output by odp_flow_key_format().  Returns 0 if successful, otherwise a
6353
 * positive errno value.  On success, stores NULL into '*errorp' and the flow
6354
 * key is appended to 'key' as a series of Netlink attributes.  On failure,
6355
 * stores a malloc()'d error message in '*errorp' without changing the data in
6356
 * 'key'.  Either way, 'key''s data might be reallocated.
6357
 *
6358
 * If 'port_names' is nonnull, it points to an simap that maps from a port name
6359
 * to a port number.  (Port names may be used instead of port numbers in
6360
 * in_port.)
6361
 *
6362
 * On success, the attributes appended to 'key' are individually syntactically
6363
 * valid, but they may not be valid as a sequence.  'key' might, for example,
6364
 * have duplicated keys.  odp_flow_key_to_flow() will detect those errors. */
6365
int
6366
odp_flow_from_string(const char *s, const struct simap *port_names,
6367
                     struct ofpbuf *key, struct ofpbuf *mask,
6368
                     char **errorp)
6369
0
{
6370
0
    if (errorp) {
6371
0
        *errorp = NULL;
6372
0
    }
6373
6374
0
    const size_t old_size = key->size;
6375
0
    struct parse_odp_context context = (struct parse_odp_context) {
6376
0
        .port_names = port_names,
6377
0
    };
6378
0
    for (;;) {
6379
0
        int retval;
6380
6381
0
        s += strspn(s, delimiters);
6382
0
        if (!*s) {
6383
0
            return 0;
6384
0
        }
6385
6386
        /* Skip UFID. */
6387
0
        ovs_u128 ufid;
6388
0
        retval = odp_ufid_from_string(s, &ufid);
6389
0
        if (retval < 0) {
6390
0
            if (errorp) {
6391
0
                *errorp = xasprintf("syntax error at %s", s);
6392
0
            }
6393
0
            key->size = old_size;
6394
0
            return -retval;
6395
0
        } else if (retval > 0) {
6396
0
            s += retval;
6397
0
            s += s[0] == ' ' ? 1 : 0;
6398
0
        }
6399
6400
0
        retval = parse_odp_key_mask_attr(&context, s, key, mask);
6401
6402
0
        if (retval >= 0) {
6403
0
            if (nl_attr_oversized(key->size - NLA_HDRLEN)) {
6404
0
                retval = -E2BIG;
6405
0
            } else if (mask && nl_attr_oversized(mask->size - NLA_HDRLEN)) {
6406
0
                retval = -E2BIG;
6407
0
            }
6408
0
        }
6409
6410
0
        if (retval < 0) {
6411
0
            if (errorp) {
6412
0
                *errorp = xasprintf("syntax error at %s", s);
6413
0
            }
6414
0
            key->size = old_size;
6415
0
            return -retval;
6416
0
        }
6417
0
        s += retval;
6418
0
    }
6419
6420
0
    return 0;
6421
0
}
6422
6423
static uint8_t
6424
ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
6425
0
{
6426
0
    if (is_mask) {
6427
        /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
6428
         * not a set of flags or bitfields. Hence, if the struct flow nw_frag
6429
         * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
6430
         * must use a zero mask for the netlink frag field, and all ones mask
6431
         * otherwise. */
6432
0
        return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
6433
0
    }
6434
0
    return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
6435
0
        : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
6436
0
        : OVS_FRAG_TYPE_FIRST;
6437
0
}
6438
6439
static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
6440
static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
6441
static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
6442
                         bool is_mask);
6443
static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
6444
                         bool is_mask);
6445
static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
6446
                         bool is_mask);
6447
static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
6448
                         bool is_mask);
6449
static void get_arp_key(const struct flow *, struct ovs_key_arp *);
6450
static void put_arp_key(const struct ovs_key_arp *, struct flow *);
6451
static void get_nd_key(const struct flow *, struct ovs_key_nd *);
6452
static void put_nd_key(const struct ovs_key_nd *, struct flow *);
6453
6454
/* These share the same layout. */
6455
union ovs_key_tp {
6456
    struct ovs_key_tcp tcp;
6457
    struct ovs_key_udp udp;
6458
    struct ovs_key_sctp sctp;
6459
};
6460
6461
static void get_tp_key(const struct flow *, union ovs_key_tp *);
6462
static void put_tp_key(const union ovs_key_tp *, struct flow *);
6463
6464
static void
6465
odp_flow_key_from_flow__(const struct odp_flow_key_parms *parms,
6466
                         bool export_mask, struct ofpbuf *buf)
6467
0
{
6468
    /* New "struct flow" fields that are visible to the datapath (including all
6469
     * data fields) should be translated into equivalent datapath flow fields
6470
     * here (you will have to add a OVS_KEY_ATTR_* for them). */
6471
0
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
6472
6473
0
    struct ovs_key_ethernet *eth_key;
6474
0
    size_t encap[FLOW_MAX_VLAN_HEADERS] = {0};
6475
0
    size_t max_vlans;
6476
0
    const struct flow *flow = parms->flow;
6477
0
    const struct flow *mask = parms->mask;
6478
0
    const struct flow *data = export_mask ? mask : flow;
6479
6480
0
    if (parms->support.recirc) {
6481
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
6482
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
6483
0
    }
6484
6485
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
6486
6487
0
    if (flow_tnl_dst_is_set(&flow->tunnel) ||
6488
0
        flow_tnl_src_is_set(&flow->tunnel) || export_mask) {
6489
0
        tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
6490
0
                        parms->key_buf, NULL);
6491
0
    }
6492
6493
    /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
6494
     * is not the magical value "ODPP_NONE". */
6495
0
    if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
6496
0
        nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
6497
0
    }
6498
6499
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
6500
6501
0
    if (parms->support.ct_state) {
6502
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
6503
0
                       ovs_to_odp_ct_state(data->ct_state));
6504
0
    }
6505
0
    if (parms->support.ct_zone) {
6506
0
        nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
6507
0
    }
6508
0
    if (parms->support.ct_mark) {
6509
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
6510
0
    }
6511
0
    if (parms->support.ct_label) {
6512
0
        nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
6513
0
                          sizeof(data->ct_label));
6514
0
    }
6515
0
    if (flow->ct_nw_proto) {
6516
0
        if (parms->support.ct_orig_tuple
6517
0
            && flow->dl_type == htons(ETH_TYPE_IP)) {
6518
0
            struct ovs_key_ct_tuple_ipv4 *ct;
6519
6520
            /* 'struct ovs_key_ct_tuple_ipv4' has padding, clear it. */
6521
0
            ct = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
6522
0
                                        sizeof *ct);
6523
0
            ct->ipv4_src = data->ct_nw_src;
6524
0
            ct->ipv4_dst = data->ct_nw_dst;
6525
0
            ct->src_port = data->ct_tp_src;
6526
0
            ct->dst_port = data->ct_tp_dst;
6527
0
            ct->ipv4_proto = data->ct_nw_proto;
6528
0
        } else if (parms->support.ct_orig_tuple6
6529
0
                   && flow->dl_type == htons(ETH_TYPE_IPV6)) {
6530
0
            struct ovs_key_ct_tuple_ipv6 *ct;
6531
6532
            /* 'struct ovs_key_ct_tuple_ipv6' has padding, clear it. */
6533
0
            ct = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
6534
0
                                        sizeof *ct);
6535
0
            ct->ipv6_src = data->ct_ipv6_src;
6536
0
            ct->ipv6_dst = data->ct_ipv6_dst;
6537
0
            ct->src_port = data->ct_tp_src;
6538
0
            ct->dst_port = data->ct_tp_dst;
6539
0
            ct->ipv6_proto = data->ct_nw_proto;
6540
0
        }
6541
0
    }
6542
6543
0
    nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type);
6544
6545
0
    if (OVS_UNLIKELY(parms->probe)) {
6546
0
        max_vlans = FLOW_MAX_VLAN_HEADERS;
6547
0
    } else {
6548
0
        max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit);
6549
0
    }
6550
6551
    /* Conditionally add L2 attributes for Ethernet packets */
6552
0
    if (flow->packet_type == htonl(PT_ETH)) {
6553
0
        eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
6554
0
                                           sizeof *eth_key);
6555
0
        get_ethernet_key(data, eth_key);
6556
6557
0
        for (int encaps = 0; encaps < max_vlans; encaps++) {
6558
0
            ovs_be16 tpid = flow->vlans[encaps].tpid;
6559
6560
0
            if (flow->vlans[encaps].tci == htons(0)) {
6561
0
                if (eth_type_vlan(flow->dl_type)) {
6562
                    /* If VLAN was truncated the tpid is in dl_type */
6563
0
                    tpid = flow->dl_type;
6564
0
                } else {
6565
0
                    break;
6566
0
                }
6567
0
            }
6568
6569
0
            if (export_mask) {
6570
0
                nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6571
0
            } else {
6572
0
                nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid);
6573
0
            }
6574
0
            nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci);
6575
0
            encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
6576
0
            if (flow->vlans[encaps].tci == htons(0)) {
6577
0
                goto unencap;
6578
0
            }
6579
0
        }
6580
0
    }
6581
6582
0
    if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
6583
        /* For backwards compatibility with kernels that don't support
6584
         * wildcarding, the following convention is used to encode the
6585
         * OVS_KEY_ATTR_ETHERTYPE for key and mask:
6586
         *
6587
         *   key      mask    matches
6588
         * -------- --------  -------
6589
         *  >0x5ff   0xffff   Specified Ethernet II Ethertype.
6590
         *  >0x5ff      0     Any Ethernet II or non-Ethernet II frame.
6591
         *  <none>   0xffff   Any non-Ethernet II frame (except valid
6592
         *                    802.3 SNAP packet with valid eth_type).
6593
         */
6594
0
        if (export_mask) {
6595
0
            nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6596
0
        }
6597
0
        goto unencap;
6598
0
    }
6599
6600
0
    nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
6601
6602
0
    if (eth_type_vlan(flow->dl_type)) {
6603
0
        goto unencap;
6604
0
    }
6605
6606
0
    if (flow->dl_type == htons(ETH_TYPE_IP)) {
6607
0
        struct ovs_key_ipv4 *ipv4_key;
6608
6609
0
        ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
6610
0
                                            sizeof *ipv4_key);
6611
0
        get_ipv4_key(data, ipv4_key, export_mask);
6612
0
    } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
6613
0
        struct ovs_key_ipv6 *ipv6_key;
6614
6615
0
        ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
6616
0
                                            sizeof *ipv6_key);
6617
0
        get_ipv6_key(data, ipv6_key, export_mask);
6618
0
    } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
6619
0
               flow->dl_type == htons(ETH_TYPE_RARP)) {
6620
0
        struct ovs_key_arp *arp_key;
6621
6622
0
        arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
6623
0
                                           sizeof *arp_key);
6624
0
        get_arp_key(data, arp_key);
6625
0
    } else if (eth_type_mpls(flow->dl_type)) {
6626
0
        struct ovs_key_mpls *mpls_key;
6627
0
        int i, n;
6628
6629
0
        n = flow_count_mpls_labels(flow, NULL);
6630
0
        if (export_mask) {
6631
0
            n = MIN(n, parms->support.max_mpls_depth);
6632
0
        }
6633
0
        mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
6634
0
                                            n * sizeof *mpls_key);
6635
0
        for (i = 0; i < n; i++) {
6636
0
            mpls_key[i].mpls_lse = data->mpls_lse[i];
6637
0
        }
6638
0
    } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
6639
0
        nsh_key_to_attr(buf, &data->nsh, NULL, 0, export_mask);
6640
0
    }
6641
6642
0
    if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6643
0
        if (flow->nw_proto == IPPROTO_TCP) {
6644
0
            union ovs_key_tp *tcp_key;
6645
6646
0
            tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
6647
0
                                               sizeof *tcp_key);
6648
0
            get_tp_key(data, tcp_key);
6649
0
            if (data->tcp_flags || (mask && mask->tcp_flags)) {
6650
0
                nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
6651
0
            }
6652
0
        } else if (flow->nw_proto == IPPROTO_UDP) {
6653
0
            union ovs_key_tp *udp_key;
6654
6655
0
            udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
6656
0
                                               sizeof *udp_key);
6657
0
            get_tp_key(data, udp_key);
6658
0
        } else if (flow->nw_proto == IPPROTO_SCTP) {
6659
0
            union ovs_key_tp *sctp_key;
6660
6661
0
            sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
6662
0
                                               sizeof *sctp_key);
6663
0
            get_tp_key(data, sctp_key);
6664
0
        } else if (flow->dl_type == htons(ETH_TYPE_IP)
6665
0
                && flow->nw_proto == IPPROTO_ICMP) {
6666
0
            struct ovs_key_icmp *icmp_key;
6667
6668
0
            icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
6669
0
                                                sizeof *icmp_key);
6670
0
            icmp_key->icmp_type = ntohs(data->tp_src);
6671
0
            icmp_key->icmp_code = ntohs(data->tp_dst);
6672
0
        } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
6673
0
                && flow->nw_proto == IPPROTO_ICMPV6) {
6674
0
            struct ovs_key_icmpv6 *icmpv6_key;
6675
6676
0
            icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
6677
0
                                                  sizeof *icmpv6_key);
6678
0
            icmpv6_key->icmpv6_type = ntohs(data->tp_src);
6679
0
            icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
6680
6681
0
            if (is_nd(flow, NULL)
6682
                /* Even though 'tp_src' is 16 bits wide, ICMP type is 8 bits
6683
                 * wide.  Therefore, an exact match looks like htons(0xff),
6684
                 * not htons(0xffff).  See xlate_wc_finish() for details. */
6685
0
                && (!export_mask || data->tp_src == htons(0xff))) {
6686
0
                struct ovs_key_nd *nd_key;
6687
0
                nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
6688
0
                                                    sizeof *nd_key);
6689
0
                nd_key->nd_target = data->nd_target;
6690
0
                nd_key->nd_sll = data->arp_sha;
6691
0
                nd_key->nd_tll = data->arp_tha;
6692
6693
                /* Add ND Extensions Attr only if supported and reserved field
6694
                 * or options type is set. */
6695
0
                if (parms->support.nd_ext) {
6696
0
                    struct ovs_key_nd_extensions *nd_ext_key;
6697
6698
0
                    if (data->igmp_group_ip4 != 0 || data->tcp_flags != 0) {
6699
                        /* 'struct ovs_key_nd_extensions' has padding,
6700
                         * clear it. */
6701
0
                        nd_ext_key = nl_msg_put_unspec_zero(buf,
6702
0
                                            OVS_KEY_ATTR_ND_EXTENSIONS,
6703
0
                                            sizeof *nd_ext_key);
6704
0
                        nd_ext_key->nd_reserved = data->igmp_group_ip4;
6705
0
                        nd_ext_key->nd_options_type = ntohs(data->tcp_flags);
6706
0
                    }
6707
0
                }
6708
0
            }
6709
0
        }
6710
0
    }
6711
6712
0
unencap:
6713
0
    for (int encaps = max_vlans - 1; encaps >= 0; encaps--) {
6714
0
        if (encap[encaps]) {
6715
0
            nl_msg_end_nested(buf, encap[encaps]);
6716
0
        }
6717
0
    }
6718
0
}
6719
6720
/* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
6721
 *
6722
 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6723
 * capable of being expanded to allow for that much space. */
6724
void
6725
odp_flow_key_from_flow(const struct odp_flow_key_parms *parms,
6726
                       struct ofpbuf *buf)
6727
0
{
6728
0
    odp_flow_key_from_flow__(parms, false, buf);
6729
0
}
6730
6731
/* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
6732
 * 'buf'.
6733
 *
6734
 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6735
 * capable of being expanded to allow for that much space. */
6736
void
6737
odp_flow_key_from_mask(const struct odp_flow_key_parms *parms,
6738
                       struct ofpbuf *buf)
6739
0
{
6740
0
    odp_flow_key_from_flow__(parms, true, buf);
6741
0
}
6742
6743
/* Generate ODP flow key from the given packet metadata */
6744
void
6745
odp_key_from_dp_packet(struct ofpbuf *buf, const struct dp_packet *packet)
6746
0
{
6747
0
    const struct pkt_metadata *md = &packet->md;
6748
6749
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
6750
6751
0
    if (md->dp_hash) {
6752
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, md->dp_hash);
6753
0
    }
6754
6755
0
    if (flow_tnl_dst_is_set(&md->tunnel)) {
6756
0
        tun_key_to_attr(buf, &md->tunnel, &md->tunnel, NULL, NULL);
6757
0
    }
6758
6759
0
    nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
6760
6761
0
    if (md->ct_state) {
6762
0
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
6763
0
                       ovs_to_odp_ct_state(md->ct_state));
6764
0
        if (md->ct_zone) {
6765
0
            nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, md->ct_zone);
6766
0
        }
6767
0
        if (md->ct_mark) {
6768
0
            nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, md->ct_mark);
6769
0
        }
6770
0
        if (!ovs_u128_is_zero(md->ct_label)) {
6771
0
            nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &md->ct_label,
6772
0
                              sizeof(md->ct_label));
6773
0
        }
6774
0
        if (md->ct_orig_tuple_ipv6) {
6775
0
            if (md->ct_orig_tuple.ipv6.ipv6_proto) {
6776
0
                nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
6777
0
                                  &md->ct_orig_tuple.ipv6,
6778
0
                                  sizeof md->ct_orig_tuple.ipv6);
6779
0
            }
6780
0
        } else {
6781
0
            if (md->ct_orig_tuple.ipv4.ipv4_proto) {
6782
0
                nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
6783
0
                                  &md->ct_orig_tuple.ipv4,
6784
0
                                  sizeof md->ct_orig_tuple.ipv4);
6785
0
            }
6786
0
        }
6787
0
    }
6788
6789
    /* Add an ingress port attribute if 'odp_in_port' is not the magical
6790
     * value "ODPP_NONE". */
6791
0
    if (md->in_port.odp_port != ODPP_NONE) {
6792
0
        nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
6793
0
    }
6794
6795
    /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6796
0
    if (pt_ns(packet->packet_type) == OFPHTN_ETHERTYPE) {
6797
0
        nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE,
6798
0
                        pt_ns_type_be(packet->packet_type));
6799
0
    }
6800
0
}
6801
6802
/* Generate packet metadata from the given ODP flow key. */
6803
void
6804
odp_key_to_dp_packet(const struct nlattr *key, size_t key_len,
6805
                     struct dp_packet *packet)
6806
0
{
6807
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6808
0
    const struct nlattr *nla;
6809
0
    struct pkt_metadata *md = &packet->md;
6810
0
    ovs_be32 packet_type = htonl(PT_UNKNOWN);
6811
0
    ovs_be16 ethertype = 0;
6812
0
    size_t left;
6813
6814
0
    pkt_metadata_init(md, ODPP_NONE);
6815
6816
0
    NL_ATTR_FOR_EACH (nla, left, key, key_len) {
6817
0
        enum ovs_key_attr type = nl_attr_type(nla);
6818
0
        size_t len = nl_attr_get_size(nla);
6819
0
        int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
6820
0
                                            OVS_KEY_ATTR_MAX, type);
6821
6822
0
        if (len != expected_len && expected_len >= 0) {
6823
0
            continue;
6824
0
        }
6825
6826
0
        switch (type) {
6827
0
        case OVS_KEY_ATTR_RECIRC_ID:
6828
0
            md->recirc_id = nl_attr_get_u32(nla);
6829
0
            break;
6830
0
        case OVS_KEY_ATTR_DP_HASH:
6831
0
            md->dp_hash = nl_attr_get_u32(nla);
6832
0
            break;
6833
0
        case OVS_KEY_ATTR_PRIORITY:
6834
0
            md->skb_priority = nl_attr_get_u32(nla);
6835
0
            break;
6836
0
        case OVS_KEY_ATTR_SKB_MARK:
6837
0
            md->pkt_mark = nl_attr_get_u32(nla);
6838
0
            break;
6839
0
        case OVS_KEY_ATTR_CT_STATE:
6840
0
            md->ct_state = odp_to_ovs_ct_state(nl_attr_get_u32(nla));
6841
0
            break;
6842
0
        case OVS_KEY_ATTR_CT_ZONE:
6843
0
            md->ct_zone = nl_attr_get_u16(nla);
6844
0
            break;
6845
0
        case OVS_KEY_ATTR_CT_MARK:
6846
0
            md->ct_mark = nl_attr_get_u32(nla);
6847
0
            break;
6848
0
        case OVS_KEY_ATTR_CT_LABELS: {
6849
0
            md->ct_label = nl_attr_get_u128(nla);
6850
0
            break;
6851
0
        }
6852
0
        case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
6853
0
            const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(nla);
6854
0
            md->ct_orig_tuple.ipv4 = *ct;
6855
0
            md->ct_orig_tuple_ipv6 = false;
6856
0
            break;
6857
0
        }
6858
0
        case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
6859
0
            const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(nla);
6860
6861
0
            md->ct_orig_tuple.ipv6 = *ct;
6862
0
            md->ct_orig_tuple_ipv6 = true;
6863
0
            break;
6864
0
        }
6865
0
        case OVS_KEY_ATTR_TUNNEL: {
6866
0
            enum odp_key_fitness res;
6867
6868
0
            res = odp_tun_key_from_attr(nla, &md->tunnel, NULL);
6869
0
            if (res == ODP_FIT_ERROR) {
6870
0
                memset(&md->tunnel, 0, sizeof md->tunnel);
6871
0
            }
6872
0
            break;
6873
0
        }
6874
0
        case OVS_KEY_ATTR_IN_PORT:
6875
0
            md->in_port.odp_port = nl_attr_get_odp_port(nla);
6876
0
            break;
6877
0
        case OVS_KEY_ATTR_ETHERNET:
6878
            /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6879
0
            packet_type = htonl(PT_ETH);
6880
0
            break;
6881
0
        case OVS_KEY_ATTR_ETHERTYPE:
6882
0
            ethertype = nl_attr_get_be16(nla);
6883
0
            break;
6884
0
        case OVS_KEY_ATTR_UNSPEC:
6885
0
        case OVS_KEY_ATTR_ENCAP:
6886
0
        case OVS_KEY_ATTR_VLAN:
6887
0
        case OVS_KEY_ATTR_IPV4:
6888
0
        case OVS_KEY_ATTR_IPV6:
6889
0
        case OVS_KEY_ATTR_TCP:
6890
0
        case OVS_KEY_ATTR_UDP:
6891
0
        case OVS_KEY_ATTR_ICMP:
6892
0
        case OVS_KEY_ATTR_ICMPV6:
6893
0
        case OVS_KEY_ATTR_ARP:
6894
0
        case OVS_KEY_ATTR_ND:
6895
0
        case OVS_KEY_ATTR_ND_EXTENSIONS:
6896
0
        case OVS_KEY_ATTR_SCTP:
6897
0
        case OVS_KEY_ATTR_TCP_FLAGS:
6898
0
        case OVS_KEY_ATTR_MPLS:
6899
0
        case OVS_KEY_ATTR_PACKET_TYPE:
6900
0
        case OVS_KEY_ATTR_NSH:
6901
0
        case OVS_KEY_ATTR_TUNNEL_INFO:
6902
0
        case __OVS_KEY_ATTR_MAX:
6903
0
        default:
6904
0
            break;
6905
0
        }
6906
0
    }
6907
6908
0
    if (packet_type == htonl(PT_ETH)) {
6909
0
        packet->packet_type = htonl(PT_ETH);
6910
0
    } else if (packet_type == htonl(PT_UNKNOWN) && ethertype != 0) {
6911
0
        packet->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
6912
0
                                             ntohs(ethertype));
6913
0
    } else {
6914
0
        VLOG_ERR_RL(&rl, "Packet without ETHERTYPE. Unknown packet_type.");
6915
0
    }
6916
0
}
6917
6918
/* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'.
6919
 * Generated value has format of random UUID. */
6920
void
6921
odp_flow_key_hash(const void *key, size_t key_len, ovs_u128 *hash)
6922
0
{
6923
0
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6924
0
    static uint32_t secret;
6925
6926
0
    if (ovsthread_once_start(&once)) {
6927
0
        secret = random_uint32();
6928
0
        ovsthread_once_done(&once);
6929
0
    }
6930
0
    hash_bytes128(key, key_len, secret, hash);
6931
0
    uuid_set_bits_v4((struct uuid *)hash);
6932
0
}
6933
6934
static void
6935
log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
6936
                       uint64_t attrs, int out_of_range_attr,
6937
                       const struct nlattr *key, size_t key_len)
6938
0
{
6939
0
    struct ds s;
6940
0
    int i;
6941
6942
0
    if (VLOG_DROP_DBG(rl)) {
6943
0
        return;
6944
0
    }
6945
6946
0
    ds_init(&s);
6947
0
    for (i = 0; i < 64; i++) {
6948
0
        if (attrs & (UINT64_C(1) << i)) {
6949
0
            char namebuf[OVS_KEY_ATTR_BUFSIZE];
6950
6951
0
            ds_put_format(&s, " %s",
6952
0
                          ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
6953
0
        }
6954
0
    }
6955
0
    if (out_of_range_attr) {
6956
0
        ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
6957
0
    }
6958
6959
0
    ds_put_cstr(&s, ": ");
6960
0
    odp_flow_key_format(key, key_len, &s);
6961
6962
0
    VLOG_DBG("%s:%s", title, ds_cstr(&s));
6963
0
    ds_destroy(&s);
6964
0
}
6965
6966
static uint8_t
6967
odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
6968
0
{
6969
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6970
6971
0
    if (is_mask) {
6972
0
        return odp_frag ? FLOW_NW_FRAG_MASK : 0;
6973
0
    }
6974
6975
0
    if (odp_frag > OVS_FRAG_TYPE_LATER) {
6976
0
        VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
6977
0
        return 0xff; /* Error. */
6978
0
    }
6979
6980
0
    return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
6981
0
        : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
6982
0
        :  FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
6983
0
}
6984
6985
/* Parses the attributes in the 'key_len' bytes of 'key' into 'attrs', which
6986
 * must have OVS_KEY_ATTR_MAX + 1 elements.  Stores each attribute in 'key'
6987
 * into the corresponding element of 'attrs'.
6988
 *
6989
 * Stores a bitmask of the attributes' indexes found in 'key' into
6990
 * '*present_attrsp'.
6991
 *
6992
 * If an attribute beyond OVS_KEY_ATTR_MAX is found, stores its attribute type
6993
 * (or one of them, if more than one) into '*out_of_range_attrp', otherwise 0.
6994
 *
6995
 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6996
 * error message in '*errorp'. */
6997
static bool
6998
parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
6999
                   const struct nlattr *attrs[], uint64_t *present_attrsp,
7000
                   int *out_of_range_attrp, char **errorp)
7001
0
{
7002
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7003
0
    const struct nlattr *nla;
7004
0
    uint64_t present_attrs;
7005
0
    size_t left;
7006
7007
0
    BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
7008
0
    present_attrs = 0;
7009
0
    *out_of_range_attrp = 0;
7010
0
    NL_ATTR_FOR_EACH (nla, left, key, key_len) {
7011
0
        uint16_t type = nl_attr_type(nla);
7012
0
        size_t len = nl_attr_get_size(nla);
7013
0
        int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
7014
0
                                            OVS_KEY_ATTR_MAX, type);
7015
7016
0
        if (len != expected_len && expected_len >= 0) {
7017
0
            char namebuf[OVS_KEY_ATTR_BUFSIZE];
7018
7019
0
            odp_parse_error(&rl, errorp, "attribute %s has length %"PRIuSIZE" "
7020
0
                            "but should have length %d",
7021
0
                            ovs_key_attr_to_string(type, namebuf,
7022
0
                                                   sizeof namebuf),
7023
0
                            len, expected_len);
7024
0
            return false;
7025
0
        }
7026
7027
0
        if (type > OVS_KEY_ATTR_MAX) {
7028
0
            *out_of_range_attrp = type;
7029
0
        } else {
7030
0
            if (present_attrs & (UINT64_C(1) << type)) {
7031
0
                char namebuf[OVS_KEY_ATTR_BUFSIZE];
7032
7033
0
                odp_parse_error(&rl, errorp,
7034
0
                                "duplicate %s attribute in flow key",
7035
0
                                ovs_key_attr_to_string(type, namebuf,
7036
0
                                                       sizeof namebuf));
7037
0
                return false;
7038
0
            }
7039
7040
0
            present_attrs |= UINT64_C(1) << type;
7041
0
            attrs[type] = nla;
7042
0
        }
7043
0
    }
7044
0
    if (left) {
7045
0
        odp_parse_error(&rl, errorp, "trailing garbage in flow key");
7046
0
        return false;
7047
0
    }
7048
7049
0
    *present_attrsp = present_attrs;
7050
0
    return true;
7051
0
}
7052
7053
static enum odp_key_fitness
7054
check_expectations(uint64_t present_attrs, int out_of_range_attr,
7055
                   uint64_t expected_attrs,
7056
                   const struct nlattr *key, size_t key_len)
7057
0
{
7058
0
    uint64_t missing_attrs;
7059
0
    uint64_t extra_attrs;
7060
7061
0
    missing_attrs = expected_attrs & ~present_attrs;
7062
0
    if (missing_attrs) {
7063
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7064
0
        log_odp_key_attributes(&rl, "expected but not present",
7065
0
                               missing_attrs, 0, key, key_len);
7066
0
        return ODP_FIT_TOO_LITTLE;
7067
0
    }
7068
7069
0
    extra_attrs = present_attrs & ~expected_attrs;
7070
0
    if (extra_attrs || out_of_range_attr) {
7071
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7072
0
        log_odp_key_attributes(&rl, "present but not expected",
7073
0
                               extra_attrs, out_of_range_attr, key, key_len);
7074
0
        return ODP_FIT_TOO_MUCH;
7075
0
    }
7076
7077
0
    return ODP_FIT_PERFECT;
7078
0
}
7079
7080
/* Initializes 'flow->dl_type' based on the attributes in 'attrs', in which the
7081
 * attributes in the bit-mask 'present_attrs' are present.  Returns true if
7082
 * successful, false on failure.
7083
 *
7084
 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
7085
 * consulted.  'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
7086
 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
7087
 * previously parsed flow key.
7088
 *
7089
 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
7090
 * error message in '*errorp'. */
7091
static bool
7092
parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
7093
                uint64_t present_attrs, uint64_t *expected_attrs,
7094
                struct flow *flow, const struct flow *src_flow,
7095
                char **errorp)
7096
0
{
7097
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7098
0
    bool is_mask = flow != src_flow;
7099
7100
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
7101
0
        flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
7102
0
        if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
7103
0
            odp_parse_error(&rl, errorp,
7104
0
                            "invalid Ethertype %"PRIu16" in flow key",
7105
0
                            ntohs(flow->dl_type));
7106
0
            return false;
7107
0
        }
7108
0
        if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
7109
0
            flow->dl_type != htons(0xffff)) {
7110
0
            odp_parse_error(&rl, errorp, "can't bitwise match non-Ethernet II "
7111
0
                            "\"Ethertype\" %#"PRIx16" (with mask %#"PRIx16")",
7112
0
                            ntohs(src_flow->dl_type), ntohs(flow->dl_type));
7113
0
            return false;
7114
0
        }
7115
0
        *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
7116
0
    } else {
7117
0
        if (!is_mask) {
7118
            /* Default ethertype for well-known L3 packets. */
7119
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
7120
0
                flow->dl_type = htons(ETH_TYPE_IP);
7121
0
            } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
7122
0
                flow->dl_type = htons(ETH_TYPE_IPV6);
7123
0
            } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7124
0
                flow->dl_type = htons(ETH_TYPE_MPLS);
7125
0
            } else {
7126
0
                flow->dl_type = htons(FLOW_DL_TYPE_NONE);
7127
0
            }
7128
0
        } else if (src_flow->packet_type != htonl(PT_ETH)) {
7129
            /* dl_type is mandatory for non-Ethernet packets */
7130
0
            flow->dl_type = htons(0xffff);
7131
0
        } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
7132
            /* See comments in odp_flow_key_from_flow__(). */
7133
0
            odp_parse_error(&rl, errorp,
7134
0
                            "mask expected for non-Ethernet II frame");
7135
0
            return false;
7136
0
        }
7137
0
    }
7138
0
    return true;
7139
0
}
7140
7141
/* Initializes MPLS, L3, and L4 fields in 'flow' based on the attributes in
7142
 * 'attrs', in which the attributes in the bit-mask 'present_attrs' are
7143
 * present.  The caller also indicates an out-of-range attribute
7144
 * 'out_of_range_attr' if one was present when parsing (if so, the fitness
7145
 * cannot be perfect).
7146
 *
7147
 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
7148
 * consulted.  'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
7149
 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
7150
 * previously parsed flow key.
7151
 *
7152
 * Returns fitness based on any discrepancies between present and expected
7153
 * attributes, except that a 'need_check' of false overrides this.
7154
 *
7155
 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
7156
 * error message in '*errorp'.  'key' and 'key_len' are just used for error
7157
 * reporting in this case. */
7158
static enum odp_key_fitness
7159
parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
7160
                  uint64_t present_attrs, int out_of_range_attr,
7161
                  uint64_t *expected_attrs, struct flow *flow,
7162
                  const struct nlattr *key, size_t key_len,
7163
                  const struct flow *src_flow, bool need_check, char **errorp)
7164
0
{
7165
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7166
0
    bool is_mask = src_flow != flow;
7167
0
    const void *check_start = NULL;
7168
0
    size_t check_len = 0;
7169
0
    enum ovs_key_attr expected_bit = 0xff;
7170
7171
0
    if (eth_type_mpls(src_flow->dl_type)) {
7172
0
        if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7173
0
            *expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
7174
0
        }
7175
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7176
0
            size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
7177
0
            const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
7178
0
            int n = size / sizeof(ovs_be32);
7179
0
            int i;
7180
7181
0
            if (!size || size % sizeof(ovs_be32)) {
7182
0
                odp_parse_error(&rl, errorp,
7183
0
                                "MPLS LSEs have invalid length %"PRIuSIZE,
7184
0
                                size);
7185
0
                return ODP_FIT_ERROR;
7186
0
            }
7187
0
            if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
7188
0
                odp_parse_error(&rl, errorp,
7189
0
                                "unexpected MPLS Ethertype mask %x"PRIx16,
7190
0
                                ntohs(flow->dl_type));
7191
0
                return ODP_FIT_ERROR;
7192
0
            }
7193
7194
0
            for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
7195
0
                flow->mpls_lse[i] = mpls_lse[i];
7196
0
            }
7197
0
            if (n > FLOW_MAX_MPLS_LABELS) {
7198
0
                return ODP_FIT_TOO_MUCH;
7199
0
            }
7200
7201
0
            if (!is_mask) {
7202
                /* BOS may be set only in the innermost label. */
7203
0
                for (i = 0; i < n - 1; i++) {
7204
0
                    if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
7205
0
                        odp_parse_error(&rl, errorp,
7206
0
                                        "MPLS BOS set in non-innermost label");
7207
0
                        return ODP_FIT_ERROR;
7208
0
                    }
7209
0
                }
7210
7211
                /* BOS must be set in the innermost label. */
7212
0
                if (n < FLOW_MAX_MPLS_LABELS
7213
0
                    && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
7214
0
                    return ODP_FIT_TOO_LITTLE;
7215
0
                }
7216
0
            }
7217
0
        }
7218
7219
0
        goto done;
7220
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
7221
0
        if (!is_mask) {
7222
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
7223
0
        }
7224
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
7225
0
            const struct ovs_key_ipv4 *ipv4_key;
7226
7227
0
            ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
7228
0
            put_ipv4_key(ipv4_key, flow, is_mask);
7229
0
            if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
7230
0
                odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV4 has invalid "
7231
0
                                "nw_frag %#"PRIx8, flow->nw_frag);
7232
0
                return ODP_FIT_ERROR;
7233
0
            }
7234
7235
0
            if (is_mask) {
7236
0
                check_start = ipv4_key;
7237
0
                check_len = sizeof *ipv4_key;
7238
0
                expected_bit = OVS_KEY_ATTR_IPV4;
7239
0
            }
7240
0
        }
7241
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
7242
0
        if (!is_mask) {
7243
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
7244
0
        }
7245
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
7246
0
            const struct ovs_key_ipv6 *ipv6_key;
7247
7248
0
            ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
7249
0
            put_ipv6_key(ipv6_key, flow, is_mask);
7250
0
            if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
7251
0
                odp_parse_error(&rl, errorp, "OVS_KEY_ATTR_IPV6 has invalid "
7252
0
                                "nw_frag %#"PRIx8, flow->nw_frag);
7253
0
                return ODP_FIT_ERROR;
7254
0
            }
7255
0
            if (is_mask) {
7256
0
                check_start = ipv6_key;
7257
0
                check_len = sizeof *ipv6_key;
7258
0
                expected_bit = OVS_KEY_ATTR_IPV6;
7259
0
            }
7260
0
        }
7261
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
7262
0
               src_flow->dl_type == htons(ETH_TYPE_RARP)) {
7263
0
        if (!is_mask) {
7264
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
7265
0
        }
7266
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
7267
0
            const struct ovs_key_arp *arp_key;
7268
7269
0
            arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
7270
0
            if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
7271
0
                odp_parse_error(&rl, errorp,
7272
0
                                "unsupported ARP opcode %"PRIu16" in flow "
7273
0
                                "key", ntohs(arp_key->arp_op));
7274
0
                return ODP_FIT_ERROR;
7275
0
            }
7276
0
            put_arp_key(arp_key, flow);
7277
0
            if (is_mask) {
7278
0
                check_start = arp_key;
7279
0
                check_len = sizeof *arp_key;
7280
0
                expected_bit = OVS_KEY_ATTR_ARP;
7281
0
            }
7282
0
        }
7283
0
    } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) {
7284
0
        if (!is_mask) {
7285
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH;
7286
0
        }
7287
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_NSH)) {
7288
0
            if (odp_nsh_key_from_attr__(attrs[OVS_KEY_ATTR_NSH],
7289
0
                                        is_mask, &flow->nsh,
7290
0
                                        NULL, errorp) == ODP_FIT_ERROR) {
7291
0
                return ODP_FIT_ERROR;
7292
0
            }
7293
0
            if (is_mask) {
7294
0
                check_start = nl_attr_get(attrs[OVS_KEY_ATTR_NSH]);
7295
0
                check_len = nl_attr_get_size(attrs[OVS_KEY_ATTR_NSH]);
7296
0
                expected_bit = OVS_KEY_ATTR_NSH;
7297
0
            }
7298
0
        }
7299
0
    } else {
7300
0
        goto done;
7301
0
    }
7302
0
    if (check_len > 0) { /* Happens only when 'is_mask'. */
7303
0
        if (!is_all_zeros(check_start, check_len) &&
7304
0
            flow->dl_type != htons(0xffff)) {
7305
0
            odp_parse_error(&rl, errorp, "unexpected L3 matching with "
7306
0
                            "masked Ethertype %#"PRIx16"/%#"PRIx16,
7307
0
                            ntohs(src_flow->dl_type),
7308
0
                            ntohs(flow->dl_type));
7309
0
            return ODP_FIT_ERROR;
7310
0
        } else {
7311
0
            *expected_attrs |= UINT64_C(1) << expected_bit;
7312
0
        }
7313
0
    }
7314
7315
0
    expected_bit = OVS_KEY_ATTR_UNSPEC;
7316
0
    if (src_flow->nw_proto == IPPROTO_TCP
7317
0
        && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7318
0
            src_flow->dl_type == htons(ETH_TYPE_IPV6))
7319
0
        && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7320
0
        if (!is_mask) {
7321
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
7322
0
        }
7323
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
7324
0
            const union ovs_key_tp *tcp_key;
7325
7326
0
            tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
7327
0
            put_tp_key(tcp_key, flow);
7328
0
            expected_bit = OVS_KEY_ATTR_TCP;
7329
0
        }
7330
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
7331
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
7332
0
            flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
7333
0
        }
7334
0
    } else if (src_flow->nw_proto == IPPROTO_UDP
7335
0
               && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7336
0
                   src_flow->dl_type == htons(ETH_TYPE_IPV6))
7337
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7338
0
        if (!is_mask) {
7339
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
7340
0
        }
7341
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
7342
0
            const union ovs_key_tp *udp_key;
7343
7344
0
            udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
7345
0
            put_tp_key(udp_key, flow);
7346
0
            expected_bit = OVS_KEY_ATTR_UDP;
7347
0
        }
7348
0
    } else if (src_flow->nw_proto == IPPROTO_SCTP
7349
0
               && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7350
0
                   src_flow->dl_type == htons(ETH_TYPE_IPV6))
7351
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7352
0
        if (!is_mask) {
7353
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
7354
0
        }
7355
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
7356
0
            const union ovs_key_tp *sctp_key;
7357
7358
0
            sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
7359
0
            put_tp_key(sctp_key, flow);
7360
0
            expected_bit = OVS_KEY_ATTR_SCTP;
7361
0
        }
7362
0
    } else if (src_flow->nw_proto == IPPROTO_ICMP
7363
0
               && src_flow->dl_type == htons(ETH_TYPE_IP)
7364
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7365
0
        if (!is_mask) {
7366
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
7367
0
        }
7368
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
7369
0
            const struct ovs_key_icmp *icmp_key;
7370
7371
0
            icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
7372
0
            flow->tp_src = htons(icmp_key->icmp_type);
7373
0
            flow->tp_dst = htons(icmp_key->icmp_code);
7374
0
            expected_bit = OVS_KEY_ATTR_ICMP;
7375
0
        }
7376
0
    } else if (src_flow->nw_proto == IPPROTO_ICMPV6
7377
0
               && src_flow->dl_type == htons(ETH_TYPE_IPV6)
7378
0
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7379
0
        if (!is_mask) {
7380
0
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
7381
0
        }
7382
0
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
7383
0
            const struct ovs_key_icmpv6 *icmpv6_key;
7384
7385
0
            icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
7386
0
            flow->tp_src = htons(icmpv6_key->icmpv6_type);
7387
0
            flow->tp_dst = htons(icmpv6_key->icmpv6_code);
7388
0
            expected_bit = OVS_KEY_ATTR_ICMPV6;
7389
0
            if (is_nd(src_flow, NULL)) {
7390
0
                if (!is_mask) {
7391
0
                    *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
7392
0
                }
7393
0
                if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
7394
0
                    const struct ovs_key_nd *nd_key;
7395
7396
0
                    nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
7397
0
                    flow->nd_target = nd_key->nd_target;
7398
0
                    flow->arp_sha = nd_key->nd_sll;
7399
0
                    flow->arp_tha = nd_key->nd_tll;
7400
0
                    if (is_mask) {
7401
                        /* Even though 'tp_src' is 16 bits wide, ICMP type
7402
                         * is 8 bits wide.  Therefore, an exact match looks
7403
                         * like htons(0xff), not htons(0xffff).  See
7404
                         * xlate_wc_finish() for details. */
7405
0
                        if (!is_all_zeros(nd_key, sizeof *nd_key) &&
7406
0
                            flow->tp_src != htons(0xff)) {
7407
0
                            odp_parse_error(&rl, errorp,
7408
0
                                            "ICMP src mask should be "
7409
0
                                            "(0xff) but is actually "
7410
0
                                            "(%#"PRIx16")",
7411
0
                                            ntohs(flow->tp_src));
7412
0
                            return ODP_FIT_ERROR;
7413
0
                        } else {
7414
0
                            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
7415
0
                        }
7416
0
                    }
7417
0
                }
7418
0
                if (present_attrs &
7419
0
                    (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS)) {
7420
0
                    const struct ovs_key_nd_extensions *nd_ext_key;
7421
0
                    if (!is_mask) {
7422
0
                        *expected_attrs |=
7423
0
                                UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS;
7424
0
                    }
7425
7426
0
                    nd_ext_key =
7427
0
                        nl_attr_get(attrs[OVS_KEY_ATTR_ND_EXTENSIONS]);
7428
0
                    flow->igmp_group_ip4 = nd_ext_key->nd_reserved;
7429
0
                    flow->tcp_flags = htons(nd_ext_key->nd_options_type);
7430
7431
0
                    if (is_mask) {
7432
                        /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
7433
                         * ICMP type and code are 8 bits wide.  Therefore, an
7434
                         * exact match looks like htons(0xff), not
7435
                         * htons(0xffff).  See xlate_wc_finish() for details.
7436
                         * */
7437
0
                        if (!is_all_zeros(nd_ext_key, sizeof *nd_ext_key) &&
7438
0
                            (flow->tp_src != htons(0xff) ||
7439
0
                             flow->tp_dst != htons(0xff))) {
7440
0
                            return ODP_FIT_ERROR;
7441
0
                        } else {
7442
0
                            *expected_attrs |=
7443
0
                                UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS;
7444
0
                        }
7445
0
                    }
7446
0
                }
7447
0
            }
7448
0
        }
7449
0
    }
7450
0
    if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
7451
0
        if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
7452
0
            odp_parse_error(&rl, errorp, "flow matches on L4 ports but does "
7453
0
                            "not define an L4 protocol");
7454
0
            return ODP_FIT_ERROR;
7455
0
        } else {
7456
0
            *expected_attrs |= UINT64_C(1) << expected_bit;
7457
0
        }
7458
0
    }
7459
7460
0
done:
7461
0
    return need_check ? check_expectations(present_attrs, out_of_range_attr,
7462
0
                            *expected_attrs, key, key_len) : ODP_FIT_PERFECT;
7463
0
}
7464
7465
/* Parse 802.1Q header then encapsulated L3 attributes. */
7466
static enum odp_key_fitness
7467
parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
7468
                   uint64_t present_attrs, int out_of_range_attr,
7469
                   uint64_t expected_attrs, struct flow *flow,
7470
                   const struct nlattr *key, size_t key_len,
7471
                   const struct flow *src_flow, char **errorp,
7472
                   bool ignore_vlan_limit)
7473
0
{
7474
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7475
0
    bool is_mask = src_flow != flow;
7476
7477
0
    const struct nlattr *encap;
7478
0
    enum odp_key_fitness encap_fitness;
7479
0
    enum odp_key_fitness fitness = ODP_FIT_ERROR;
7480
0
    int vlan_limit;
7481
0
    int encaps = 0;
7482
7483
0
    vlan_limit = ignore_vlan_limit ? FLOW_MAX_VLAN_HEADERS : flow_vlan_limit;
7484
0
    while (encaps < vlan_limit &&
7485
0
           (is_mask
7486
0
            ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0
7487
0
            : eth_type_vlan(flow->dl_type))) {
7488
7489
0
        encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
7490
0
                 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
7491
7492
        /* Calculate fitness of outer attributes. */
7493
0
        if (!is_mask) {
7494
0
            expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
7495
0
                              (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
7496
0
        } else {
7497
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
7498
0
                expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
7499
0
            }
7500
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
7501
0
                expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
7502
0
            }
7503
0
        }
7504
0
        fitness = check_expectations(present_attrs, out_of_range_attr,
7505
0
                                     expected_attrs, key, key_len);
7506
7507
        /* Set vlan_tci.
7508
         * Remove the TPID from dl_type since it's not the real Ethertype.  */
7509
0
        flow->vlans[encaps].tpid = flow->dl_type;
7510
0
        flow->dl_type = htons(0);
7511
0
        flow->vlans[encaps].tci =
7512
0
                        (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
7513
0
                        ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
7514
0
                        : htons(0));
7515
0
        if (!is_mask) {
7516
0
            if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) ||
7517
0
                !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
7518
0
                return ODP_FIT_TOO_LITTLE;
7519
0
            } else if (flow->vlans[encaps].tci == htons(0)) {
7520
                /* Corner case for a truncated 802.1Q header. */
7521
0
                if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
7522
0
                    return ODP_FIT_TOO_MUCH;
7523
0
                }
7524
0
                return fitness;
7525
0
            } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) {
7526
0
                odp_parse_error(
7527
0
                    &rl, errorp, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
7528
0
                    "but CFI bit is not set", ntohs(flow->vlans[encaps].tci));
7529
0
                return ODP_FIT_ERROR;
7530
0
            }
7531
0
        } else {
7532
0
            if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
7533
0
                return fitness;
7534
0
            }
7535
0
        }
7536
7537
        /* Now parse the encapsulated attributes. */
7538
0
        if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
7539
0
                                attrs, &present_attrs, &out_of_range_attr,
7540
0
                                errorp)) {
7541
0
            return ODP_FIT_ERROR;
7542
0
        }
7543
0
        expected_attrs = 0;
7544
7545
        /* For OVS to be backward compatible with newer datapath
7546
         * implementations, we should ignore out of range attributes. */
7547
0
        if (out_of_range_attr) {
7548
0
            VLOG_DBG("Flow key decode found unknown OVS_KEY_ATTR, %d",
7549
0
                     out_of_range_attr);
7550
0
            out_of_range_attr = 0;
7551
0
        }
7552
7553
0
        if (!parse_ethertype(attrs, present_attrs, &expected_attrs,
7554
0
                             flow, src_flow, errorp)) {
7555
0
            return ODP_FIT_ERROR;
7556
0
        }
7557
0
        encap_fitness = parse_l2_5_onward(attrs, present_attrs,
7558
0
                                          out_of_range_attr,
7559
0
                                          &expected_attrs,
7560
0
                                          flow, key, key_len,
7561
0
                                          src_flow, false, errorp);
7562
0
        if (encap_fitness != ODP_FIT_PERFECT) {
7563
0
            return encap_fitness;
7564
0
        }
7565
0
        encaps++;
7566
0
    }
7567
7568
0
    return check_expectations(present_attrs, out_of_range_attr,
7569
0
                              expected_attrs, key, key_len);
7570
0
}
7571
7572
static enum odp_key_fitness
7573
odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
7574
                       struct flow *flow, const struct flow *src_flow,
7575
                       char **errorp, bool ignore_vlan_limit)
7576
0
{
7577
    /* New "struct flow" fields that are visible to the datapath (including all
7578
     * data fields) should be translated from equivalent datapath flow fields
7579
     * here (you will have to add a OVS_KEY_ATTR_* for them).  */
7580
0
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
7581
7582
0
    enum odp_key_fitness fitness = ODP_FIT_ERROR;
7583
0
    if (errorp) {
7584
0
        *errorp = NULL;
7585
0
    }
7586
7587
0
    const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
7588
0
    uint64_t expected_attrs;
7589
0
    uint64_t present_attrs;
7590
0
    int out_of_range_attr;
7591
0
    bool is_mask = src_flow != flow;
7592
7593
0
    memset(flow, 0, sizeof *flow);
7594
7595
    /* Parse attributes. */
7596
0
    if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
7597
0
                            &out_of_range_attr, errorp)) {
7598
0
        goto exit;
7599
0
    }
7600
0
    expected_attrs = 0;
7601
7602
    /* For OVS to be backward compatible with newer datapath implementations,
7603
     * we should ignore out of range attributes. */
7604
0
    if (out_of_range_attr) {
7605
0
        VLOG_DBG("Flow key decode found unknown OVS_KEY_ATTR, %d",
7606
0
                 out_of_range_attr);
7607
0
        out_of_range_attr = 0;
7608
0
    }
7609
7610
    /* Metadata. */
7611
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
7612
0
        flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
7613
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
7614
0
    } else if (is_mask) {
7615
        /* Always exact match recirc_id if it is not specified. */
7616
0
        flow->recirc_id = UINT32_MAX;
7617
0
    }
7618
7619
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
7620
0
        flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
7621
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
7622
0
    }
7623
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
7624
0
        flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
7625
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
7626
0
    }
7627
7628
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
7629
0
        flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
7630
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
7631
0
    }
7632
7633
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
7634
0
        uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
7635
7636
0
        flow->ct_state = odp_to_ovs_ct_state(odp_state);
7637
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
7638
0
    }
7639
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
7640
0
        flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
7641
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
7642
0
    }
7643
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
7644
0
        flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
7645
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
7646
0
    }
7647
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
7648
0
        flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]);
7649
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
7650
0
    }
7651
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
7652
0
        const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
7653
0
        flow->ct_nw_src = ct->ipv4_src;
7654
0
        flow->ct_nw_dst = ct->ipv4_dst;
7655
0
        flow->ct_nw_proto = ct->ipv4_proto;
7656
0
        flow->ct_tp_src = ct->src_port;
7657
0
        flow->ct_tp_dst = ct->dst_port;
7658
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
7659
0
    }
7660
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
7661
0
        const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
7662
7663
0
        flow->ct_ipv6_src = ct->ipv6_src;
7664
0
        flow->ct_ipv6_dst = ct->ipv6_dst;
7665
0
        flow->ct_nw_proto = ct->ipv6_proto;
7666
0
        flow->ct_tp_src = ct->src_port;
7667
0
        flow->ct_tp_dst = ct->dst_port;
7668
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
7669
0
    }
7670
7671
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
7672
0
        enum odp_key_fitness res;
7673
7674
0
        res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask,
7675
0
                                      &flow->tunnel, errorp);
7676
0
        if (res == ODP_FIT_ERROR) {
7677
0
            goto exit;
7678
0
        } else if (res == ODP_FIT_PERFECT) {
7679
0
            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
7680
0
        }
7681
0
    }
7682
7683
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
7684
0
        flow->in_port.odp_port
7685
0
            = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
7686
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
7687
0
    } else if (!is_mask) {
7688
0
        flow->in_port.odp_port = ODPP_NONE;
7689
0
    }
7690
7691
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) {
7692
0
        flow->packet_type
7693
0
            = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]);
7694
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE;
7695
0
        if (pt_ns(src_flow->packet_type) == OFPHTN_ETHERTYPE) {
7696
0
            flow->dl_type = pt_ns_type_be(flow->packet_type);
7697
0
        }
7698
0
    } else if (!is_mask) {
7699
0
        flow->packet_type = htonl(PT_ETH);
7700
0
    }
7701
7702
    /* Check for Ethernet header. */
7703
0
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
7704
0
        const struct ovs_key_ethernet *eth_key;
7705
7706
0
        eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
7707
0
        put_ethernet_key(eth_key, flow);
7708
0
        if (!is_mask) {
7709
0
            flow->packet_type = htonl(PT_ETH);
7710
0
        }
7711
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
7712
0
    }
7713
0
    else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
7714
0
        ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
7715
0
        if (!is_mask) {
7716
0
            flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
7717
0
                                               ntohs(ethertype));
7718
0
        }
7719
0
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
7720
0
    }
7721
7722
    /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
7723
0
    if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
7724
0
                         src_flow, errorp)) {
7725
0
        goto exit;
7726
0
    }
7727
7728
0
    if (is_mask
7729
0
        ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0
7730
0
        : eth_type_vlan(src_flow->dl_type)) {
7731
0
        fitness = parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
7732
0
                                     expected_attrs, flow, key, key_len,
7733
0
                                     src_flow, errorp, ignore_vlan_limit);
7734
0
    } else {
7735
0
        if (is_mask) {
7736
            /* A missing VLAN mask means exact match on vlan_tci 0 (== no
7737
             * VLAN). */
7738
0
            flow->vlans[0].tpid = htons(0xffff);
7739
0
            flow->vlans[0].tci = htons(0xffff);
7740
0
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
7741
0
                flow->vlans[0].tci = nl_attr_get_be16(
7742
0
                    attrs[OVS_KEY_ATTR_VLAN]);
7743
0
                expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
7744
0
            }
7745
0
        }
7746
0
        fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
7747
0
                                    &expected_attrs, flow, key, key_len,
7748
0
                                    src_flow, true, errorp);
7749
0
    }
7750
7751
0
exit:;
7752
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7753
0
    if (fitness == ODP_FIT_ERROR && (errorp || !VLOG_DROP_WARN(&rl))) {
7754
0
        struct ds s = DS_EMPTY_INITIALIZER;
7755
0
        if (is_mask) {
7756
0
            ds_put_cstr(&s, "the flow mask in error is: ");
7757
0
            odp_flow_key_format(key, key_len, &s);
7758
0
            ds_put_cstr(&s, ", for the following flow key: ");
7759
0
            flow_format(&s, src_flow, NULL);
7760
0
        } else {
7761
0
            ds_put_cstr(&s, "the flow key in error is: ");
7762
0
            odp_flow_key_format(key, key_len, &s);
7763
0
        }
7764
0
        if (errorp) {
7765
0
            char *old_error = *errorp;
7766
0
            *errorp = xasprintf("%s; %s", old_error, ds_cstr(&s));
7767
0
            free(old_error);
7768
0
        } else {
7769
0
            VLOG_WARN("%s", ds_cstr(&s));
7770
0
        }
7771
0
        ds_destroy(&s);
7772
0
    }
7773
0
    return fitness;
7774
0
}
7775
7776
/* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
7777
 * structure in 'flow'.  Returns an ODP_FIT_* value that indicates how well
7778
 * 'key' fits our expectations for what a flow key should contain.
7779
 *
7780
 * The 'in_port' will be the datapath's understanding of the port.  The
7781
 * caller will need to translate with odp_port_to_ofp_port() if the
7782
 * OpenFlow port is needed.
7783
 *
7784
 * This function doesn't take the packet itself as an argument because none of
7785
 * the currently understood OVS_KEY_ATTR_* attributes require it.  Currently,
7786
 * it is always possible to infer which additional attribute(s) should appear
7787
 * by looking at the attributes for lower-level protocols, e.g. if the network
7788
 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
7789
 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
7790
 * must be absent.
7791
 *
7792
 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7793
 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7794
 * '*errorp', otherwise NULL. */
7795
enum odp_key_fitness
7796
odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
7797
                     struct flow *flow, char **errorp)
7798
0
{
7799
0
    return odp_flow_key_to_flow__(key, key_len, flow, flow, errorp, false);
7800
0
}
7801
7802
/* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
7803
 * to a mask structure in 'mask'.  'flow' must be a previously translated flow
7804
 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
7805
 * attributes from that flow.  Returns an ODP_FIT_* value that indicates how
7806
 * well 'key' fits our expectations for what a flow key should contain.
7807
 *
7808
 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7809
 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7810
 * '*errorp', otherwise NULL. */
7811
static enum odp_key_fitness
7812
odp_flow_key_to_mask__(const struct nlattr *mask_key, size_t mask_key_len,
7813
                       struct flow_wildcards *mask,
7814
                       const struct flow *src_flow,
7815
                       char **errorp, bool ignore_vlan_limit)
7816
0
{
7817
0
    if (mask_key_len) {
7818
0
        return odp_flow_key_to_flow__(mask_key, mask_key_len,
7819
0
                                      &mask->masks, src_flow, errorp,
7820
0
                                      ignore_vlan_limit);
7821
0
    } else {
7822
0
        if (errorp) {
7823
0
            *errorp = NULL;
7824
0
        }
7825
7826
        /* A missing mask means that the flow should be exact matched.
7827
         * Generate an appropriate exact wildcard for the flow. */
7828
0
        flow_wildcards_init_for_packet(mask, src_flow);
7829
7830
0
        return ODP_FIT_PERFECT;
7831
0
    }
7832
0
}
7833
7834
enum odp_key_fitness
7835
odp_flow_key_to_mask(const struct nlattr *mask_key, size_t mask_key_len,
7836
                     struct flow_wildcards *mask,
7837
                     const struct flow *src_flow, char **errorp)
7838
0
{
7839
0
    return odp_flow_key_to_mask__(mask_key, mask_key_len, mask, src_flow,
7840
0
                                  errorp, false);
7841
0
}
7842
7843
/* Converts the netlink formated key/mask to match.
7844
 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
7845
 * disagree on the acceptable form of flow */
7846
int
7847
parse_key_and_mask_to_match(const struct nlattr *key, size_t key_len,
7848
                            const struct nlattr *mask, size_t mask_len,
7849
                            struct match *match)
7850
0
{
7851
0
    enum odp_key_fitness fitness;
7852
7853
0
    fitness = odp_flow_key_to_flow__(key, key_len, &match->flow, &match->flow,
7854
0
                                     NULL, true);
7855
0
    if (fitness) {
7856
        /* This will happen when the odp_flow_key_to_flow() function can't
7857
         * parse the netlink message to a match structure.  It will return
7858
         * ODP_FIT_TOO_LITTLE if there is not enough information to parse the
7859
         * content successfully, ODP_FIT_TOO_MUCH if there is too much netlink
7860
         * data and we do not know how to safely ignore it, and ODP_FIT_ERROR
7861
         * in any other case. */
7862
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7863
7864
0
        if (!VLOG_DROP_ERR(&rl)) {
7865
0
            struct ds s;
7866
7867
0
            ds_init(&s);
7868
0
            odp_flow_format(key, key_len, NULL, 0, NULL, &s, true, false);
7869
0
            VLOG_ERR("internal error parsing flow key %s (%s)",
7870
0
                     ds_cstr(&s), odp_key_fitness_to_string(fitness));
7871
0
            ds_destroy(&s);
7872
0
        }
7873
7874
0
        return EINVAL;
7875
0
    }
7876
7877
0
    fitness = odp_flow_key_to_mask__(mask, mask_len, &match->wc, &match->flow,
7878
0
                                     NULL, true);
7879
0
    if (fitness) {
7880
        /* This should not happen, see comment above. */
7881
0
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7882
7883
0
        if (!VLOG_DROP_ERR(&rl)) {
7884
0
            struct ds s;
7885
7886
0
            ds_init(&s);
7887
0
            odp_flow_format(key, key_len, mask, mask_len, NULL, &s,
7888
0
                            true, true);
7889
0
            VLOG_ERR("internal error parsing flow mask %s (%s)",
7890
0
                     ds_cstr(&s), odp_key_fitness_to_string(fitness));
7891
0
            ds_destroy(&s);
7892
0
        }
7893
7894
0
        return EINVAL;
7895
0
    }
7896
7897
0
    return 0;
7898
0
}
7899
7900
/* Returns 'fitness' as a string, for use in debug messages. */
7901
const char *
7902
odp_key_fitness_to_string(enum odp_key_fitness fitness)
7903
0
{
7904
0
    switch (fitness) {
7905
0
    case ODP_FIT_PERFECT:
7906
0
        return "OK";
7907
0
    case ODP_FIT_TOO_MUCH:
7908
0
        return "too_much";
7909
0
    case ODP_FIT_TOO_LITTLE:
7910
0
        return "too_little";
7911
0
    case ODP_FIT_ERROR:
7912
0
        return "error";
7913
0
    default:
7914
0
        return "<unknown>";
7915
0
    }
7916
0
}
7917
7918
/* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
7919
 * Netlink PID 'pid'.  If 'userdata' is nonnull, adds a userdata attribute
7920
 * whose contents are the 'userdata_size' bytes at 'userdata' and sets
7921
 * 'odp_actions_ofs' if nonnull with the offset within 'odp_actions' of the
7922
 * start of the cookie.  (If 'userdata' is null, then the 'odp_actions_ofs'
7923
 * value is not meaningful.)
7924
 *
7925
 * Returns negative error code on failure. */
7926
int
7927
odp_put_userspace_action(uint32_t pid,
7928
                         const void *userdata, size_t userdata_size,
7929
                         odp_port_t tunnel_out_port,
7930
                         bool include_actions,
7931
                         struct ofpbuf *odp_actions, size_t *odp_actions_ofs)
7932
0
{
7933
0
    size_t userdata_ofs;
7934
0
    size_t offset;
7935
7936
0
    offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
7937
0
    nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
7938
0
    if (userdata) {
7939
0
        if (nl_attr_oversized(userdata_size)) {
7940
0
            return -E2BIG;
7941
0
        }
7942
0
        userdata_ofs = odp_actions->size + NLA_HDRLEN;
7943
7944
        /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
7945
         * module before Linux 3.10 required the userdata to be exactly 8 bytes
7946
         * long:
7947
         *
7948
         *   - The kernel rejected shorter userdata with -ERANGE.
7949
         *
7950
         *   - The kernel silently dropped userdata beyond the first 8 bytes.
7951
         *
7952
         * Thus, for maximum compatibility, always put at least 8 bytes.  (We
7953
         * separately disable features that required more than 8 bytes.) */
7954
0
        memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
7955
0
                                      MAX(8, userdata_size)),
7956
0
               userdata, userdata_size);
7957
0
    } else {
7958
0
        userdata_ofs = 0;
7959
0
    }
7960
0
    if (tunnel_out_port != ODPP_NONE) {
7961
0
        nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
7962
0
                            tunnel_out_port);
7963
0
    }
7964
0
    if (include_actions) {
7965
0
        nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
7966
0
    }
7967
0
    if (nl_attr_oversized(odp_actions->size - offset - NLA_HDRLEN)) {
7968
0
        return -E2BIG;
7969
0
    }
7970
0
    nl_msg_end_nested(odp_actions, offset);
7971
7972
0
    if (odp_actions_ofs) {
7973
0
        *odp_actions_ofs = userdata_ofs;
7974
0
    }
7975
7976
0
    return 0;
7977
0
}
7978
7979
void
7980
odp_put_pop_eth_action(struct ofpbuf *odp_actions)
7981
0
{
7982
0
    nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_ETH);
7983
0
}
7984
7985
void
7986
odp_put_push_eth_action(struct ofpbuf *odp_actions,
7987
                        const struct eth_addr *eth_src,
7988
                        const struct eth_addr *eth_dst)
7989
0
{
7990
0
    struct ovs_action_push_eth eth;
7991
7992
0
    memset(&eth, 0, sizeof eth);
7993
0
    if (eth_src) {
7994
0
        eth.addresses.eth_src = *eth_src;
7995
0
    }
7996
0
    if (eth_dst) {
7997
0
        eth.addresses.eth_dst = *eth_dst;
7998
0
    }
7999
8000
0
    nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_ETH,
8001
0
                      &eth, sizeof eth);
8002
0
}
8003
8004
void
8005
odp_put_tunnel_action(const struct flow_tnl *tunnel,
8006
                      struct ofpbuf *odp_actions, const char *tnl_type)
8007
0
{
8008
0
    size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
8009
0
    tun_key_to_attr(odp_actions, tunnel, tunnel, NULL, tnl_type);
8010
0
    nl_msg_end_nested(odp_actions, offset);
8011
0
}
8012
8013
void
8014
odp_put_tnl_push_action(struct ofpbuf *odp_actions,
8015
                        struct ovs_action_push_tnl *data)
8016
0
{
8017
0
    int size = offsetof(struct ovs_action_push_tnl, header);
8018
8019
0
    size += data->header_len;
8020
0
    nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
8021
0
}
8022
8023
void
8024
odp_put_psample_action(struct ofpbuf *odp_actions, uint32_t group_id,
8025
                       uint8_t *cookie, size_t cookie_len)
8026
0
{
8027
0
    size_t offset = nl_msg_start_nested_with_flag(odp_actions,
8028
0
                                                  OVS_ACTION_ATTR_PSAMPLE);
8029
8030
0
    nl_msg_put_u32(odp_actions, OVS_PSAMPLE_ATTR_GROUP, group_id);
8031
0
    if (cookie && cookie_len) {
8032
0
        ovs_assert(cookie_len <= OVS_PSAMPLE_COOKIE_MAX_SIZE);
8033
0
        nl_msg_put_unspec(odp_actions, OVS_PSAMPLE_ATTR_COOKIE, cookie,
8034
0
                          cookie_len);
8035
0
    }
8036
8037
0
    nl_msg_end_nested(odp_actions, offset);
8038
0
}
8039
8040

8041
/* The commit_odp_actions() function and its helpers. */
8042
8043
static void
8044
commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
8045
                  const void *key, size_t key_size)
8046
0
{
8047
0
    size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
8048
0
    nl_msg_put_unspec(odp_actions, key_type, key, key_size);
8049
0
    nl_msg_end_nested(odp_actions, offset);
8050
0
}
8051
8052
/* Commit one attribute for a masked set action.  Masked set actions have
8053
 * a mask following the data within the netlink attribute.  The unmasked bits
8054
 * in the data will be cleared as the data is copied to the attribute. */
8055
static void
8056
commit_masked_attribute(struct ofpbuf *odp_actions, int key_type,
8057
                        const void *key_, const void *mask_, size_t key_size)
8058
0
{
8059
0
    char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
8060
0
    const char *key = key_, *mask = mask_;
8061
8062
0
    memcpy(data + key_size, mask, key_size);
8063
    /* Clear unmasked bits while copying. */
8064
0
    while (key_size--) {
8065
0
        *data++ = *key++ & *mask++;
8066
0
    }
8067
0
}
8068
8069
/* A helper to commit a masked set action for a single attribute. */
8070
void
8071
commit_masked_set_action(struct ofpbuf *odp_actions,
8072
                         enum ovs_key_attr key_type,
8073
                         const void *key_, const void *mask_, size_t key_size)
8074
0
{
8075
0
    size_t offset = nl_msg_start_nested(odp_actions,
8076
0
                                        OVS_ACTION_ATTR_SET_MASKED);
8077
0
    commit_masked_attribute(odp_actions, key_type, key_, mask_, key_size);
8078
0
    nl_msg_end_nested(odp_actions, offset);
8079
0
}
8080
8081
/* If any of the flow key data that ODP actions can modify are different in
8082
 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
8083
 * 'odp_actions' that change the flow tunneling information in key from
8084
 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
8085
 * same way.  In other words, operates the same as commit_odp_actions(), but
8086
 * only on tunneling information. */
8087
void
8088
commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
8089
                         struct ofpbuf *odp_actions, const char *tnl_type)
8090
0
{
8091
    /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
8092
     * must have non-zero ipv6_dst. */
8093
0
    if (flow_tnl_dst_is_set(&flow->tunnel)) {
8094
0
        if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
8095
0
            return;
8096
0
        }
8097
0
        memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
8098
0
        odp_put_tunnel_action(&base->tunnel, odp_actions, tnl_type);
8099
0
    }
8100
0
}
8101
8102
struct offsetof_sizeof {
8103
    int offset;
8104
    int size;
8105
};
8106
8107
8108
/* Performs bitwise OR over the fields in 'dst_' and 'src_' specified in
8109
 * 'offsetof_sizeof_arr' array.  Result is stored in 'dst_'. */
8110
static void
8111
or_masks(void *dst_, const void *src_,
8112
         struct offsetof_sizeof *offsetof_sizeof_arr)
8113
0
{
8114
0
    int field, size, offset;
8115
0
    const uint8_t *src = src_;
8116
0
    uint8_t *dst = dst_;
8117
8118
0
    for (field = 0; ; field++) {
8119
0
        size   = offsetof_sizeof_arr[field].size;
8120
0
        offset = offsetof_sizeof_arr[field].offset;
8121
8122
0
        if (!size) {
8123
0
            return;
8124
0
        }
8125
0
        or_bytes(dst + offset, src + offset, size);
8126
0
    }
8127
0
}
8128
8129
/* Compares each of the fields in 'key0' and 'key1'.  The fields are specified
8130
 * in 'offsetof_sizeof_arr', which is an array terminated by a 0-size field.
8131
 * Returns true if all of the fields are equal, false if at least one differs.
8132
 * As a side effect, for each field that is the same in 'key0' and 'key1',
8133
 * zeros the corresponding bytes in 'mask'. */
8134
static bool
8135
keycmp_mask(const void *key0, const void *key1,
8136
            struct offsetof_sizeof *offsetof_sizeof_arr, void *mask)
8137
0
{
8138
0
    bool differ = false;
8139
8140
0
    for (int field = 0 ; ; field++) {
8141
0
        int size = offsetof_sizeof_arr[field].size;
8142
0
        int offset = offsetof_sizeof_arr[field].offset;
8143
0
        if (size == 0) {
8144
0
            break;
8145
0
        }
8146
8147
0
        char *pkey0 = ((char *)key0) + offset;
8148
0
        char *pkey1 = ((char *)key1) + offset;
8149
0
        char *pmask = ((char *)mask) + offset;
8150
0
        if (memcmp(pkey0, pkey1, size) == 0) {
8151
0
            memset(pmask, 0, size);
8152
0
        } else {
8153
0
            differ = true;
8154
0
        }
8155
0
    }
8156
8157
0
    return differ;
8158
0
}
8159
8160
static bool
8161
commit(enum ovs_key_attr attr, bool use_masked_set,
8162
       const void *key, void *base, void *mask, size_t size,
8163
       struct offsetof_sizeof *offsetof_sizeof_arr,
8164
       struct ofpbuf *odp_actions)
8165
0
{
8166
0
    if (keycmp_mask(key, base, offsetof_sizeof_arr, mask)) {
8167
0
        bool fully_masked = odp_mask_is_exact(attr, mask, size);
8168
8169
0
        if (use_masked_set && !fully_masked) {
8170
0
            commit_masked_set_action(odp_actions, attr, key, mask, size);
8171
0
        } else {
8172
0
            if (!fully_masked) {
8173
0
                memset(mask, 0xff, size);
8174
0
            }
8175
0
            commit_set_action(odp_actions, attr, key, size);
8176
0
        }
8177
0
        memcpy(base, key, size);
8178
0
        return true;
8179
0
    } else {
8180
        /* Mask bits are set when we have either read or set the corresponding
8181
         * values.  Masked bits will be exact-matched, no need to set them
8182
         * if the value did not actually change. */
8183
0
        return false;
8184
0
    }
8185
0
}
8186
8187
static void
8188
get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
8189
0
{
8190
0
    eth->eth_src = flow->dl_src;
8191
0
    eth->eth_dst = flow->dl_dst;
8192
0
}
8193
8194
static void
8195
put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
8196
0
{
8197
0
    flow->dl_src = eth->eth_src;
8198
0
    flow->dl_dst = eth->eth_dst;
8199
0
}
8200
8201
static void
8202
commit_set_ether_action(const struct flow *flow, struct flow *base_flow,
8203
                        struct ofpbuf *odp_actions,
8204
                        struct flow_wildcards *wc,
8205
                        bool use_masked)
8206
0
{
8207
0
    struct ovs_key_ethernet key, base, mask, orig_mask;
8208
0
    struct offsetof_sizeof ovs_key_ethernet_offsetof_sizeof_arr[] =
8209
0
        OVS_KEY_ETHERNET_OFFSETOF_SIZEOF_ARR;
8210
8211
0
    if (flow->packet_type != htonl(PT_ETH) ||
8212
0
        base_flow->packet_type != htonl(PT_ETH)) {
8213
0
        return;
8214
0
    }
8215
8216
0
    get_ethernet_key(flow, &key);
8217
0
    get_ethernet_key(base_flow, &base);
8218
0
    get_ethernet_key(&wc->masks, &mask);
8219
0
    memcpy(&orig_mask, &mask, sizeof mask);
8220
8221
0
    if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
8222
0
               &key, &base, &mask, sizeof key,
8223
0
               ovs_key_ethernet_offsetof_sizeof_arr, odp_actions)) {
8224
0
        put_ethernet_key(&base, base_flow);
8225
0
        or_masks(&mask, &orig_mask, ovs_key_ethernet_offsetof_sizeof_arr);
8226
0
        put_ethernet_key(&mask, &wc->masks);
8227
0
    }
8228
0
}
8229
8230
static void
8231
commit_vlan_action(const struct flow* flow, struct flow *base,
8232
                   struct ofpbuf *odp_actions, struct flow_wildcards *wc)
8233
0
{
8234
0
    int base_n = flow_count_vlan_headers(base);
8235
0
    int flow_n = flow_count_vlan_headers(flow);
8236
0
    flow_skip_common_vlan_headers(base, &base_n, flow, &flow_n);
8237
8238
    /* Pop all mismatching vlan of base, push those of flow */
8239
0
    for (; base_n >= 0; base_n--) {
8240
0
        nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
8241
0
        wc->masks.vlans[base_n].qtag = OVS_BE32_MAX;
8242
0
    }
8243
8244
0
    for (; flow_n >= 0; flow_n--) {
8245
0
        struct ovs_action_push_vlan vlan;
8246
8247
0
        vlan.vlan_tpid = flow->vlans[flow_n].tpid;
8248
0
        vlan.vlan_tci = flow->vlans[flow_n].tci;
8249
0
        nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
8250
0
                          &vlan, sizeof vlan);
8251
0
    }
8252
0
    memcpy(base->vlans, flow->vlans, sizeof(base->vlans));
8253
0
}
8254
8255
/* Wildcarding already done at action translation time. */
8256
static void
8257
commit_mpls_action(const struct flow *flow, struct flow *base,
8258
                   struct ofpbuf *odp_actions, bool pending_encap)
8259
0
{
8260
0
    int base_n = flow_count_mpls_labels(base, NULL);
8261
0
    int flow_n = flow_count_mpls_labels(flow, NULL);
8262
0
    int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
8263
0
                                                 NULL);
8264
8265
0
    while (base_n > common_n) {
8266
0
        if (base_n - 1 == common_n && flow_n > common_n) {
8267
            /* If there is only one more LSE in base than there are common
8268
             * between base and flow; and flow has at least one more LSE than
8269
             * is common then the topmost LSE of base may be updated using
8270
             * set */
8271
0
            struct ovs_key_mpls mpls_key;
8272
8273
0
            mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
8274
0
            commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
8275
0
                              &mpls_key, sizeof mpls_key);
8276
0
            flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
8277
0
            common_n++;
8278
0
        } else {
8279
            /* Otherwise, if there more LSEs in base than are common between
8280
             * base and flow then pop the topmost one. */
8281
0
            ovs_be16 dl_type;
8282
            /* If all the LSEs are to be popped and this is not the outermost
8283
             * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
8284
             * POP_MPLS action instead of flow->dl_type.
8285
             *
8286
             * This is because the POP_MPLS action requires its ethertype
8287
             * argument to be an MPLS ethernet type but in this case
8288
             * flow->dl_type will be a non-MPLS ethernet type.
8289
             *
8290
             * When the final POP_MPLS action occurs it use flow->dl_type and
8291
             * the and the resulting packet will have the desired dl_type. */
8292
0
            if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
8293
0
                dl_type = htons(ETH_TYPE_MPLS);
8294
0
            } else {
8295
0
                dl_type = flow->dl_type;
8296
0
            }
8297
0
            nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
8298
0
            ovs_assert(flow_pop_mpls(base, base_n, flow->dl_type, NULL));
8299
0
            base_n--;
8300
0
        }
8301
0
    }
8302
8303
    /* If, after the above popping and setting, there are more LSEs in flow
8304
     * than base then some LSEs need to be pushed. */
8305
0
    while (base_n < flow_n) {
8306
8307
0
        if (pending_encap) {
8308
0
             struct ovs_action_add_mpls *mpls;
8309
8310
0
             mpls = nl_msg_put_unspec_zero(odp_actions,
8311
0
                                           OVS_ACTION_ATTR_ADD_MPLS,
8312
0
                                           sizeof *mpls);
8313
0
             mpls->mpls_ethertype = flow->dl_type;
8314
0
             mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
8315
0
        } else {
8316
0
             struct ovs_action_push_mpls *mpls;
8317
8318
0
             mpls = nl_msg_put_unspec_zero(odp_actions,
8319
0
                                           OVS_ACTION_ATTR_PUSH_MPLS,
8320
0
                                           sizeof *mpls);
8321
0
             mpls->mpls_ethertype = flow->dl_type;
8322
0
             mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
8323
0
        }
8324
        /* Update base flow's MPLS stack, but do not clear L3.  We need the L3
8325
         * headers if the flow is restored later due to returning from a patch
8326
         * port or group bucket. */
8327
0
        flow_push_mpls(base, base_n, flow->dl_type, NULL, false);
8328
0
        flow_set_mpls_lse(base, 0, flow->mpls_lse[flow_n - base_n - 1]);
8329
0
        base_n++;
8330
0
    }
8331
0
}
8332
8333
static void
8334
get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
8335
0
{
8336
0
    ipv4->ipv4_src = flow->nw_src;
8337
0
    ipv4->ipv4_dst = flow->nw_dst;
8338
0
    ipv4->ipv4_proto = flow->nw_proto;
8339
0
    ipv4->ipv4_tos = flow->nw_tos;
8340
0
    ipv4->ipv4_ttl = flow->nw_ttl;
8341
0
    ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
8342
0
}
8343
8344
static void
8345
put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
8346
0
{
8347
0
    flow->nw_src = ipv4->ipv4_src;
8348
0
    flow->nw_dst = ipv4->ipv4_dst;
8349
0
    flow->nw_proto = ipv4->ipv4_proto;
8350
0
    flow->nw_tos = ipv4->ipv4_tos;
8351
0
    flow->nw_ttl = ipv4->ipv4_ttl;
8352
0
    flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
8353
0
}
8354
8355
static void
8356
commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
8357
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8358
                       bool use_masked)
8359
0
{
8360
0
    struct ovs_key_ipv4 key, mask, orig_mask, base;
8361
0
    struct offsetof_sizeof ovs_key_ipv4_offsetof_sizeof_arr[] =
8362
0
        OVS_KEY_IPV4_OFFSETOF_SIZEOF_ARR;
8363
8364
    /* Check that nw_proto and nw_frag remain unchanged. */
8365
0
    ovs_assert(flow->nw_proto == base_flow->nw_proto &&
8366
0
               flow->nw_frag == base_flow->nw_frag);
8367
8368
0
    get_ipv4_key(flow, &key, false);
8369
0
    get_ipv4_key(base_flow, &base, false);
8370
0
    get_ipv4_key(&wc->masks, &mask, true);
8371
0
    memcpy(&orig_mask, &mask, sizeof mask);
8372
0
    mask.ipv4_proto = 0;        /* Not writeable. */
8373
0
    mask.ipv4_frag = 0;         /* Not writable. */
8374
8375
0
    if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
8376
0
        ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
8377
0
        mask.ipv4_tos &= ~IP_ECN_MASK;
8378
0
    }
8379
8380
0
    if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
8381
0
               ovs_key_ipv4_offsetof_sizeof_arr, odp_actions)) {
8382
0
        put_ipv4_key(&base, base_flow, false);
8383
0
        or_masks(&mask, &orig_mask, ovs_key_ipv4_offsetof_sizeof_arr);
8384
0
        put_ipv4_key(&mask, &wc->masks, true);
8385
0
   }
8386
0
}
8387
8388
static void
8389
get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
8390
0
{
8391
0
    ipv6->ipv6_src = flow->ipv6_src;
8392
0
    ipv6->ipv6_dst = flow->ipv6_dst;
8393
0
    ipv6->ipv6_label = flow->ipv6_label;
8394
0
    ipv6->ipv6_proto = flow->nw_proto;
8395
0
    ipv6->ipv6_tclass = flow->nw_tos;
8396
0
    ipv6->ipv6_hlimit = flow->nw_ttl;
8397
0
    ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
8398
0
}
8399
8400
static void
8401
put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
8402
0
{
8403
0
    flow->ipv6_src = ipv6->ipv6_src;
8404
0
    flow->ipv6_dst = ipv6->ipv6_dst;
8405
0
    flow->ipv6_label = ipv6->ipv6_label;
8406
0
    flow->nw_proto = ipv6->ipv6_proto;
8407
0
    flow->nw_tos = ipv6->ipv6_tclass;
8408
0
    flow->nw_ttl = ipv6->ipv6_hlimit;
8409
0
    flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
8410
0
}
8411
8412
static void
8413
commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
8414
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8415
                       bool use_masked)
8416
0
{
8417
0
    struct ovs_key_ipv6 key, mask, orig_mask, base;
8418
0
    struct offsetof_sizeof ovs_key_ipv6_offsetof_sizeof_arr[] =
8419
0
        OVS_KEY_IPV6_OFFSETOF_SIZEOF_ARR;
8420
8421
    /* Check that nw_proto and nw_frag remain unchanged. */
8422
0
    ovs_assert(flow->nw_proto == base_flow->nw_proto &&
8423
0
               flow->nw_frag == base_flow->nw_frag);
8424
8425
0
    get_ipv6_key(flow, &key, false);
8426
0
    get_ipv6_key(base_flow, &base, false);
8427
0
    get_ipv6_key(&wc->masks, &mask, true);
8428
0
    memcpy(&orig_mask, &mask, sizeof mask);
8429
0
    mask.ipv6_proto = 0;        /* Not writeable. */
8430
0
    mask.ipv6_frag = 0;         /* Not writable. */
8431
0
    mask.ipv6_label &= htonl(IPV6_LABEL_MASK); /* Not writable. */
8432
8433
0
    if (flow_tnl_dst_is_set(&base_flow->tunnel) &&
8434
0
        ((base_flow->nw_tos ^ flow->nw_tos) & IP_ECN_MASK) == 0) {
8435
0
        mask.ipv6_tclass &= ~IP_ECN_MASK;
8436
0
    }
8437
8438
0
    if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
8439
0
               ovs_key_ipv6_offsetof_sizeof_arr, odp_actions)) {
8440
0
        put_ipv6_key(&base, base_flow, false);
8441
0
        or_masks(&mask, &orig_mask, ovs_key_ipv6_offsetof_sizeof_arr);
8442
0
        put_ipv6_key(&mask, &wc->masks, true);
8443
0
    }
8444
0
}
8445
8446
static void
8447
get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
8448
0
{
8449
    /* ARP key has padding, clear it. */
8450
0
    memset(arp, 0, sizeof *arp);
8451
8452
0
    arp->arp_sip = flow->nw_src;
8453
0
    arp->arp_tip = flow->nw_dst;
8454
0
    arp->arp_op = flow->nw_proto == UINT8_MAX ?
8455
0
                  OVS_BE16_MAX : htons(flow->nw_proto);
8456
0
    arp->arp_sha = flow->arp_sha;
8457
0
    arp->arp_tha = flow->arp_tha;
8458
0
}
8459
8460
static void
8461
put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
8462
0
{
8463
0
    flow->nw_src = arp->arp_sip;
8464
0
    flow->nw_dst = arp->arp_tip;
8465
0
    flow->nw_proto = ntohs(arp->arp_op);
8466
0
    flow->arp_sha = arp->arp_sha;
8467
0
    flow->arp_tha = arp->arp_tha;
8468
0
}
8469
8470
static enum slow_path_reason
8471
commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
8472
                      struct ofpbuf *odp_actions, struct flow_wildcards *wc)
8473
0
{
8474
0
    struct ovs_key_arp key, mask, orig_mask, base;
8475
0
    struct offsetof_sizeof ovs_key_arp_offsetof_sizeof_arr[] =
8476
0
        OVS_KEY_ARP_OFFSETOF_SIZEOF_ARR;
8477
8478
0
    get_arp_key(flow, &key);
8479
0
    get_arp_key(base_flow, &base);
8480
0
    get_arp_key(&wc->masks, &mask);
8481
0
    memcpy(&orig_mask, &mask, sizeof mask);
8482
8483
0
    if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
8484
0
               ovs_key_arp_offsetof_sizeof_arr, odp_actions)) {
8485
0
        put_arp_key(&base, base_flow);
8486
0
        or_masks(&mask, &orig_mask, ovs_key_arp_offsetof_sizeof_arr);
8487
0
        put_arp_key(&mask, &wc->masks);
8488
0
        return SLOW_ACTION;
8489
0
    }
8490
0
    return 0;
8491
0
}
8492
8493
static void
8494
get_icmp_key(const struct flow *flow, struct ovs_key_icmp *icmp)
8495
0
{
8496
    /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8497
0
    icmp->icmp_type = ntohs(flow->tp_src);
8498
0
    icmp->icmp_code = ntohs(flow->tp_dst);
8499
0
}
8500
8501
static void
8502
put_icmp_key(const struct ovs_key_icmp *icmp, struct flow *flow)
8503
0
{
8504
    /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8505
0
    flow->tp_src = htons(icmp->icmp_type);
8506
0
    flow->tp_dst = htons(icmp->icmp_code);
8507
0
}
8508
8509
static enum slow_path_reason
8510
commit_set_icmp_action(const struct flow *flow, struct flow *base_flow,
8511
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc)
8512
0
{
8513
0
    struct ovs_key_icmp key, mask, orig_mask, base;
8514
0
    struct offsetof_sizeof ovs_key_icmp_offsetof_sizeof_arr[] =
8515
0
        OVS_KEY_ICMP_OFFSETOF_SIZEOF_ARR;
8516
0
    enum ovs_key_attr attr;
8517
8518
0
    if (is_icmpv4(flow, NULL)) {
8519
0
        attr = OVS_KEY_ATTR_ICMP;
8520
0
    } else if (is_icmpv6(flow, NULL)) {
8521
0
        attr = OVS_KEY_ATTR_ICMPV6;
8522
0
    } else {
8523
0
        return 0;
8524
0
    }
8525
8526
0
    get_icmp_key(flow, &key);
8527
0
    get_icmp_key(base_flow, &base);
8528
0
    get_icmp_key(&wc->masks, &mask);
8529
0
    memcpy(&orig_mask, &mask, sizeof mask);
8530
8531
0
    if (commit(attr, false, &key, &base, &mask, sizeof key,
8532
0
               ovs_key_icmp_offsetof_sizeof_arr, odp_actions)) {
8533
0
        put_icmp_key(&base, base_flow);
8534
0
        or_masks(&mask, &orig_mask, ovs_key_icmp_offsetof_sizeof_arr);
8535
0
        put_icmp_key(&mask, &wc->masks);
8536
0
        return SLOW_ACTION;
8537
0
    }
8538
0
    return 0;
8539
0
}
8540
8541
static void
8542
get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
8543
0
{
8544
0
    nd->nd_target = flow->nd_target;
8545
    /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8546
0
    nd->nd_sll = flow->arp_sha;
8547
0
    nd->nd_tll = flow->arp_tha;
8548
0
}
8549
8550
static void
8551
put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
8552
0
{
8553
0
    flow->nd_target = nd->nd_target;
8554
    /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8555
0
    flow->arp_sha = nd->nd_sll;
8556
0
    flow->arp_tha = nd->nd_tll;
8557
0
}
8558
8559
static void
8560
get_nd_extensions_key(const struct flow *flow,
8561
                      struct ovs_key_nd_extensions *nd_ext)
8562
0
{
8563
    /* ND Extensions key has padding, clear it. */
8564
0
    memset(nd_ext, 0, sizeof *nd_ext);
8565
0
    nd_ext->nd_reserved = flow->igmp_group_ip4;
8566
0
    nd_ext->nd_options_type = ntohs(flow->tcp_flags);
8567
0
}
8568
8569
static void
8570
put_nd_extensions_key(const struct ovs_key_nd_extensions *nd_ext,
8571
                      struct flow *flow)
8572
0
{
8573
0
    flow->igmp_group_ip4 = nd_ext->nd_reserved;
8574
0
    flow->tcp_flags = htons(nd_ext->nd_options_type);
8575
0
}
8576
8577
static enum slow_path_reason
8578
commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
8579
                     struct ofpbuf *odp_actions,
8580
                     struct flow_wildcards *wc, bool use_masked)
8581
0
{
8582
0
    struct ovs_key_nd key, mask, orig_mask, base;
8583
0
    struct offsetof_sizeof ovs_key_nd_offsetof_sizeof_arr[] =
8584
0
        OVS_KEY_ND_OFFSETOF_SIZEOF_ARR;
8585
8586
0
    get_nd_key(flow, &key);
8587
0
    get_nd_key(base_flow, &base);
8588
0
    get_nd_key(&wc->masks, &mask);
8589
0
    memcpy(&orig_mask, &mask, sizeof mask);
8590
8591
0
    if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
8592
0
               ovs_key_nd_offsetof_sizeof_arr, odp_actions)) {
8593
0
        put_nd_key(&base, base_flow);
8594
0
        or_masks(&mask, &orig_mask, ovs_key_nd_offsetof_sizeof_arr);
8595
0
        put_nd_key(&mask, &wc->masks);
8596
0
        return SLOW_ACTION;
8597
0
    }
8598
8599
0
    return 0;
8600
0
}
8601
8602
static enum slow_path_reason
8603
commit_set_nd_extensions_action(const struct flow *flow,
8604
                                struct flow *base_flow,
8605
                                struct ofpbuf *odp_actions,
8606
                                struct flow_wildcards *wc, bool use_masked)
8607
0
{
8608
0
    struct ovs_key_nd_extensions key, mask, orig_mask, base;
8609
0
    struct offsetof_sizeof ovs_key_nd_extensions_offsetof_sizeof_arr[] =
8610
0
        OVS_KEY_ND_EXTENSIONS_OFFSETOF_SIZEOF_ARR;
8611
8612
0
    get_nd_extensions_key(flow, &key);
8613
0
    get_nd_extensions_key(base_flow, &base);
8614
0
    get_nd_extensions_key(&wc->masks, &mask);
8615
0
    memcpy(&orig_mask, &mask, sizeof mask);
8616
8617
0
    if (commit(OVS_KEY_ATTR_ND_EXTENSIONS, use_masked, &key, &base, &mask,
8618
0
               sizeof key, ovs_key_nd_extensions_offsetof_sizeof_arr,
8619
0
               odp_actions)) {
8620
0
        put_nd_extensions_key(&base, base_flow);
8621
0
        or_masks(&mask, &orig_mask, ovs_key_nd_extensions_offsetof_sizeof_arr);
8622
0
        put_nd_extensions_key(&mask, &wc->masks);
8623
0
        return SLOW_ACTION;
8624
0
    }
8625
0
    return 0;
8626
0
}
8627
8628
static enum slow_path_reason
8629
commit_set_nw_action(const struct flow *flow, struct flow *base,
8630
                     struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8631
                     bool use_masked)
8632
0
{
8633
0
    uint32_t reason;
8634
8635
    /* Check if 'flow' really has an L3 header. */
8636
0
    if (!flow->nw_proto) {
8637
0
        return 0;
8638
0
    }
8639
8640
0
    switch (ntohs(base->dl_type)) {
8641
0
    case ETH_TYPE_IP:
8642
0
        commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
8643
0
        break;
8644
8645
0
    case ETH_TYPE_IPV6:
8646
0
        commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
8647
0
        if (base->nw_proto == IPPROTO_ICMPV6) {
8648
            /* Commit extended attrs first to make sure
8649
               correct options are added.*/
8650
0
            reason = commit_set_nd_extensions_action(flow, base,
8651
0
                                         odp_actions, wc, use_masked);
8652
0
            reason |= commit_set_nd_action(flow, base, odp_actions,
8653
0
                                         wc, use_masked);
8654
0
            return reason;
8655
0
        }
8656
0
        break;
8657
8658
0
    case ETH_TYPE_ARP:
8659
0
        return commit_set_arp_action(flow, base, odp_actions, wc);
8660
0
    }
8661
8662
0
    return 0;
8663
0
}
8664
8665
static bool
8666
commit_nsh(bool use_masked_set, const struct ovs_key_nsh *key,
8667
           struct ovs_key_nsh *base, struct ovs_key_nsh *mask, size_t size,
8668
           struct ofpbuf *odp_actions)
8669
0
{
8670
0
    if (!memcmp(key, base, size)) {
8671
        /* Mask bits are set when we have either read or set the corresponding
8672
         * values.  Masked bits will be exact-matched, no need to set them
8673
         * if the value did not actually change. */
8674
0
        return false;
8675
0
    }
8676
8677
0
    bool fully_masked = odp_mask_is_exact(OVS_KEY_ATTR_NSH, mask, size);
8678
0
    size_t set_ofs;
8679
8680
0
    if (use_masked_set && !fully_masked) {
8681
0
        struct ovs_nsh_key_base nsh_base = {
8682
0
            .flags = key->flags,
8683
0
            .ttl = key->ttl,
8684
            /* 'mdtype' and 'np' are not writable. */
8685
0
            .path_hdr = key->path_hdr,
8686
0
        };
8687
0
        struct ovs_nsh_key_base nsh_base_mask = {
8688
0
            .flags = mask->flags,
8689
0
            .ttl = mask->ttl,
8690
            /* 'mdtype' and 'np' are not writable. */
8691
0
            .path_hdr = mask->path_hdr,
8692
0
        };
8693
0
        size_t nsh_ofs;
8694
8695
0
        set_ofs = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET_MASKED);
8696
0
        nsh_ofs = nl_msg_start_nested(odp_actions, OVS_KEY_ATTR_NSH);
8697
8698
0
        if (!is_all_zeros(&nsh_base_mask, sizeof nsh_base_mask)) {
8699
0
            commit_masked_attribute(odp_actions, OVS_NSH_KEY_ATTR_BASE,
8700
0
                                    &nsh_base, &nsh_base_mask,
8701
0
                                    sizeof nsh_base);
8702
0
        }
8703
8704
        /* No match support for other MD formats yet. */
8705
0
        if (key->mdtype == NSH_M_TYPE1
8706
0
            && !is_all_zeros(mask->context, sizeof mask->context)) {
8707
0
            commit_masked_attribute(odp_actions, OVS_NSH_KEY_ATTR_MD1,
8708
0
                                    key->context, mask->context,
8709
0
                                    sizeof key->context);
8710
0
        }
8711
8712
0
        nl_msg_end_nested(odp_actions, nsh_ofs);
8713
0
        nl_msg_end_nested(odp_actions, set_ofs);
8714
0
    } else {
8715
        /* Overwriting the whole header.  Make sure that fields that wasn't
8716
         * in the mask are indeed the same as in the committed action. */
8717
0
        if (!fully_masked) {
8718
0
            memset(mask, 0xff, size);
8719
0
        }
8720
8721
0
        set_ofs = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
8722
0
        nsh_key_to_attr(odp_actions, key, NULL, 0, false);
8723
0
        nl_msg_end_nested(odp_actions, set_ofs);
8724
0
    }
8725
0
    memcpy(base, key, size);
8726
0
    return true;
8727
0
}
8728
8729
static void
8730
commit_set_nsh_action(const struct flow *flow, struct flow *base_flow,
8731
                      struct ofpbuf *odp_actions,
8732
                      struct flow_wildcards *wc,
8733
                      bool use_masked)
8734
0
{
8735
0
    struct ovs_key_nsh key, mask, base;
8736
8737
0
    if (flow->dl_type != htons(ETH_TYPE_NSH)) {
8738
0
        return;
8739
0
    }
8740
8741
    /* Check that mdtype and np remain unchanged. */
8742
0
    ovs_assert(flow->nsh.mdtype == base_flow->nsh.mdtype &&
8743
0
               flow->nsh.np == base_flow->nsh.np);
8744
8745
0
    key = flow->nsh;
8746
0
    base = base_flow->nsh;
8747
0
    mask = wc->masks.nsh;
8748
8749
0
    if (commit_nsh(use_masked, &key, &base, &mask, sizeof key, odp_actions)) {
8750
0
        base_flow->nsh = base;
8751
0
        or_bytes(&wc->masks.nsh, &mask, sizeof wc->masks.nsh);
8752
0
    }
8753
0
}
8754
8755
/* TCP, UDP, and SCTP keys have the same layout. */
8756
BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
8757
                  sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
8758
8759
static void
8760
get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
8761
0
{
8762
0
    tp->tcp.tcp_src = flow->tp_src;
8763
0
    tp->tcp.tcp_dst = flow->tp_dst;
8764
0
}
8765
8766
static void
8767
put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
8768
0
{
8769
0
    flow->tp_src = tp->tcp.tcp_src;
8770
0
    flow->tp_dst = tp->tcp.tcp_dst;
8771
0
}
8772
8773
static void
8774
commit_set_port_action(const struct flow *flow, struct flow *base_flow,
8775
                       struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8776
                       bool use_masked)
8777
0
{
8778
0
    enum ovs_key_attr key_type;
8779
0
    union ovs_key_tp key, mask, orig_mask, base;
8780
0
    struct offsetof_sizeof ovs_key_tp_offsetof_sizeof_arr[] =
8781
0
        OVS_KEY_TCP_OFFSETOF_SIZEOF_ARR;
8782
8783
    /* Check if 'flow' really has an L3 header. */
8784
0
    if (!flow->nw_proto) {
8785
0
        return;
8786
0
    }
8787
8788
0
    if (!is_ip_any(base_flow)) {
8789
0
        return;
8790
0
    }
8791
8792
0
    if (flow->nw_proto == IPPROTO_TCP) {
8793
0
        key_type = OVS_KEY_ATTR_TCP;
8794
0
    } else if (flow->nw_proto == IPPROTO_UDP) {
8795
0
        key_type = OVS_KEY_ATTR_UDP;
8796
0
    } else if (flow->nw_proto == IPPROTO_SCTP) {
8797
0
        key_type = OVS_KEY_ATTR_SCTP;
8798
0
    } else {
8799
0
        return;
8800
0
    }
8801
8802
0
    get_tp_key(flow, &key);
8803
0
    get_tp_key(base_flow, &base);
8804
0
    get_tp_key(&wc->masks, &mask);
8805
0
    memcpy(&orig_mask, &mask, sizeof mask);
8806
8807
0
    if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
8808
0
               ovs_key_tp_offsetof_sizeof_arr, odp_actions)) {
8809
0
        put_tp_key(&base, base_flow);
8810
0
        or_masks(&mask, &orig_mask, ovs_key_tp_offsetof_sizeof_arr);
8811
0
        put_tp_key(&mask, &wc->masks);
8812
0
    }
8813
0
}
8814
8815
static void
8816
commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
8817
                           struct ofpbuf *odp_actions,
8818
                           struct flow_wildcards *wc,
8819
                           bool use_masked)
8820
0
{
8821
0
    uint32_t key, mask, base;
8822
0
    struct offsetof_sizeof ovs_key_prio_offsetof_sizeof_arr[] = {
8823
0
        {0, sizeof(uint32_t)},
8824
0
        {0, 0}
8825
0
    };
8826
8827
0
    key = flow->skb_priority;
8828
0
    base = base_flow->skb_priority;
8829
0
    mask = wc->masks.skb_priority;
8830
8831
0
    if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
8832
0
               sizeof key, ovs_key_prio_offsetof_sizeof_arr, odp_actions)) {
8833
0
        base_flow->skb_priority = base;
8834
0
        wc->masks.skb_priority |= mask;
8835
0
    }
8836
0
}
8837
8838
static void
8839
commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
8840
                           struct ofpbuf *odp_actions,
8841
                           struct flow_wildcards *wc,
8842
                           bool use_masked)
8843
0
{
8844
0
    uint32_t key, mask, base;
8845
0
    struct offsetof_sizeof ovs_key_pkt_mark_offsetof_sizeof_arr[] = {
8846
0
        {0, sizeof(uint32_t)},
8847
0
        {0, 0}
8848
0
    };
8849
8850
0
    key = flow->pkt_mark;
8851
0
    base = base_flow->pkt_mark;
8852
0
    mask = wc->masks.pkt_mark;
8853
8854
0
    if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
8855
0
               sizeof key, ovs_key_pkt_mark_offsetof_sizeof_arr,
8856
0
               odp_actions)) {
8857
0
        base_flow->pkt_mark = base;
8858
0
        wc->masks.pkt_mark |= mask;
8859
0
    }
8860
0
}
8861
8862
static void
8863
odp_put_pop_nsh_action(struct ofpbuf *odp_actions)
8864
0
{
8865
0
    nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_NSH);
8866
0
}
8867
8868
static void
8869
odp_put_push_nsh_action(struct ofpbuf *odp_actions,
8870
                         const struct flow *flow,
8871
                         struct ofpbuf *encap_data)
8872
0
{
8873
0
    uint8_t * metadata = NULL;
8874
0
    uint8_t md_size = 0;
8875
8876
0
    switch (flow->nsh.mdtype) {
8877
0
    case NSH_M_TYPE2:
8878
0
        if (encap_data) {
8879
0
            ovs_assert(encap_data->size < NSH_CTX_HDRS_MAX_LEN);
8880
0
            metadata = encap_data->data;
8881
0
            md_size = encap_data->size;
8882
0
        } else {
8883
0
            md_size = 0;
8884
0
        }
8885
0
        break;
8886
0
    default:
8887
0
        md_size = 0;
8888
0
        break;
8889
0
    }
8890
0
    size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_PUSH_NSH);
8891
0
    nsh_key_to_attr(odp_actions, &flow->nsh, metadata, md_size, false);
8892
0
    nl_msg_end_nested(odp_actions, offset);
8893
0
}
8894
8895
static void
8896
commit_encap_decap_action(const struct flow *flow,
8897
                          struct flow *base_flow,
8898
                          struct ofpbuf *odp_actions,
8899
                          struct flow_wildcards *wc,
8900
                          bool pending_encap, bool pending_decap,
8901
                          struct ofpbuf *encap_data)
8902
0
{
8903
0
    if (pending_encap) {
8904
0
        switch (ntohl(flow->packet_type)) {
8905
0
        case PT_ETH: {
8906
            /* push_eth */
8907
0
            odp_put_push_eth_action(odp_actions, &flow->dl_src,
8908
0
                                    &flow->dl_dst);
8909
0
            base_flow->packet_type = flow->packet_type;
8910
0
            base_flow->dl_src = flow->dl_src;
8911
0
            base_flow->dl_dst = flow->dl_dst;
8912
0
            break;
8913
0
        }
8914
0
        case PT_NSH:
8915
            /* push_nsh */
8916
0
            odp_put_push_nsh_action(odp_actions, flow, encap_data);
8917
0
            base_flow->packet_type = flow->packet_type;
8918
            /* Update all packet headers in base_flow. */
8919
0
            memcpy((char *) base_flow + offsetof(struct flow, dl_dst),
8920
0
                   (const char *) flow + offsetof(struct flow, dl_dst),
8921
0
                   sizeof(*flow) - offsetof(struct flow, dl_dst));
8922
0
            break;
8923
0
        case PT_MPLS:
8924
0
        case PT_MPLS_MC:
8925
0
            commit_mpls_action(flow, base_flow, odp_actions,
8926
0
                               pending_encap);
8927
0
            break;
8928
0
        default:
8929
            /* Only the above protocols are supported for encap.
8930
             * The check is done at action translation. */
8931
0
            OVS_NOT_REACHED();
8932
0
        }
8933
0
    } else if (pending_decap || flow->packet_type != base_flow->packet_type) {
8934
        /* This is an explicit or implicit decap case. */
8935
0
        if (pt_ns(flow->packet_type) == OFPHTN_ETHERTYPE &&
8936
0
            base_flow->packet_type == htonl(PT_ETH)) {
8937
            /* Generate pop_eth and continue without recirculation. */
8938
0
            odp_put_pop_eth_action(odp_actions);
8939
0
            base_flow->packet_type = flow->packet_type;
8940
0
            base_flow->dl_src = eth_addr_zero;
8941
0
            base_flow->dl_dst = eth_addr_zero;
8942
0
        } else {
8943
            /* All other decap cases require recirculation.
8944
             * No need to update the base flow here. */
8945
0
            switch (ntohl(base_flow->packet_type)) {
8946
0
            case PT_NSH:
8947
                /* pop_nsh. */
8948
0
                odp_put_pop_nsh_action(odp_actions);
8949
0
                break;
8950
0
            case PT_MPLS:
8951
0
            case PT_MPLS_MC:
8952
0
                commit_mpls_action(flow, base_flow, odp_actions,
8953
0
                                   pending_encap);
8954
0
                break;
8955
0
            default:
8956
                /* Checks are done during translation. */
8957
0
                OVS_NOT_REACHED();
8958
0
            }
8959
0
        }
8960
0
    }
8961
8962
0
    wc->masks.packet_type = OVS_BE32_MAX;
8963
0
}
8964
8965
/* If any of the flow key data that ODP actions can modify are different in
8966
 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
8967
 * key from 'base' into 'flow', and then changes 'base' the same way.  Does not
8968
 * commit set_tunnel actions.  Users should call commit_odp_tunnel_action()
8969
 * in addition to this function if needed.  Sets fields in 'wc' that are
8970
 * used as part of the action.
8971
 *
8972
 * In the common case, this function returns 0.  If the flow key modification
8973
 * requires the flow's packets to be forced into the userspace slow path, this
8974
 * function returns SLOW_ACTION.  This only happens when there is no ODP action
8975
 * to modify some field that was actually modified.  For example, there is no
8976
 * ODP action to modify any ARP field, so such a modification triggers
8977
 * SLOW_ACTION.  (When this happens, packets that need such modification get
8978
 * flushed to userspace and handled there, which works OK but much more slowly
8979
 * than if the datapath handled it directly.) */
8980
enum slow_path_reason
8981
commit_odp_actions(const struct flow *flow, struct flow *base,
8982
                   struct ofpbuf *odp_actions, struct flow_wildcards *wc,
8983
                   bool use_masked, bool pending_encap, bool pending_decap,
8984
                   struct ofpbuf *encap_data)
8985
0
{
8986
    /* If you add a field that OpenFlow actions can change, and that is visible
8987
     * to the datapath (including all data fields), then you should also add
8988
     * code here to commit changes to the field. */
8989
0
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
8990
8991
0
    enum slow_path_reason slow1, slow2;
8992
0
    bool mpls_done = false;
8993
8994
0
    commit_encap_decap_action(flow, base, odp_actions, wc,
8995
0
                              pending_encap, pending_decap, encap_data);
8996
0
    commit_set_ether_action(flow, base, odp_actions, wc, use_masked);
8997
    /* Make packet a non-MPLS packet before committing L3/4 actions,
8998
     * which would otherwise do nothing. */
8999
0
    if (eth_type_mpls(base->dl_type) && !eth_type_mpls(flow->dl_type)) {
9000
0
        commit_mpls_action(flow, base, odp_actions, false);
9001
0
        mpls_done = true;
9002
0
    }
9003
0
    commit_set_nsh_action(flow, base, odp_actions, wc, use_masked);
9004
0
    slow1 = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
9005
0
    commit_set_port_action(flow, base, odp_actions, wc, use_masked);
9006
0
    slow2 = commit_set_icmp_action(flow, base, odp_actions, wc);
9007
0
    if (!mpls_done) {
9008
0
        commit_mpls_action(flow, base, odp_actions, false);
9009
0
    }
9010
0
    commit_vlan_action(flow, base, odp_actions, wc);
9011
0
    commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
9012
0
    commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
9013
9014
0
    return slow1 ? slow1 : slow2;
9015
0
}
9016
9017
int
9018
odp_vxlan_tun_opts_from_attr(const struct nlattr *tun_attr, ovs_be16 *id,
9019
                             uint8_t *flags, bool *id_present)
9020
0
{
9021
0
    static const struct nl_policy vxlan_opts_policy[] = {
9022
0
        [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
9023
0
    };
9024
0
    struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
9025
9026
0
    if (!nl_parse_nested(tun_attr, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
9027
0
        return EINVAL;
9028
0
    }
9029
9030
0
    if (ext[OVS_VXLAN_EXT_GBP]) {
9031
0
        uint32_t gbp_raw = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
9032
9033
0
        odp_decode_gbp_raw(gbp_raw, id, flags);
9034
0
    }
9035
9036
0
    if (id_present) {
9037
0
        *id_present = !!ext[OVS_VXLAN_EXT_GBP];
9038
0
    }
9039
9040
0
    return 0;
9041
0
}