Coverage Report

Created: 2026-07-30 06:04

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
541k
#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
61.2k
{
116
61.2k
    if (type > OVS_ACTION_ATTR_MAX) {
117
0
        return -1;
118
0
    }
119
120
61.2k
    switch ((enum ovs_action_attr) type) {
121
27.7k
    case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
122
0
    case OVS_ACTION_ATTR_LB_OUTPUT: return sizeof(uint32_t);
123
194
    case OVS_ACTION_ATTR_TRUNC: return sizeof(struct ovs_action_trunc);
124
0
    case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
125
281
    case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
126
196
    case OVS_ACTION_ATTR_METER: return sizeof(uint32_t);
127
3.64k
    case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
128
811
    case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
129
557
    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
194
    case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
133
0
    case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
134
909
    case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
135
12.2k
    case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
136
0
    case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
137
10.0k
    case OVS_ACTION_ATTR_CT: return ATTR_LEN_VARIABLE;
138
622
    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
1.39k
    case OVS_ACTION_ATTR_CLONE: return ATTR_LEN_VARIABLE;
142
1.01k
    case OVS_ACTION_ATTR_PUSH_NSH: return ATTR_LEN_VARIABLE;
143
1.38k
    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
1
    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
61.2k
    }
154
155
0
    return ATTR_LEN_INVALID;
156
61.2k
}
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
467k
{
165
467k
    switch (attr) {
166
0
    case OVS_KEY_ATTR_UNSPEC: return "unspec";
167
44.2k
    case OVS_KEY_ATTR_ENCAP: return "encap";
168
5.00k
    case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
169
9.17k
    case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
170
5.10k
    case OVS_KEY_ATTR_CT_STATE: return "ct_state";
171
5.87k
    case OVS_KEY_ATTR_CT_ZONE: return "ct_zone";
172
4.56k
    case OVS_KEY_ATTR_CT_MARK: return "ct_mark";
173
17.7k
    case OVS_KEY_ATTR_CT_LABELS: return "ct_label";
174
4.71k
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: return "ct_tuple4";
175
4.01k
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: return "ct_tuple6";
176
12.1k
    case OVS_KEY_ATTR_TUNNEL: return "tunnel";
177
782
    case OVS_KEY_ATTR_IN_PORT: return "in_port";
178
4.94k
    case OVS_KEY_ATTR_ETHERNET: return "eth";
179
3.57k
    case OVS_KEY_ATTR_VLAN: return "vlan";
180
4.37k
    case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
181
42.1k
    case OVS_KEY_ATTR_IPV4: return "ipv4";
182
18.7k
    case OVS_KEY_ATTR_IPV6: return "ipv6";
183
1.50k
    case OVS_KEY_ATTR_TCP: return "tcp";
184
46.8k
    case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
185
1.80k
    case OVS_KEY_ATTR_UDP: return "udp";
186
2.34k
    case OVS_KEY_ATTR_SCTP: return "sctp";
187
4.15k
    case OVS_KEY_ATTR_ICMP: return "icmp";
188
1.73k
    case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
189
10.5k
    case OVS_KEY_ATTR_ARP: return "arp";
190
170k
    case OVS_KEY_ATTR_ND: return "nd";
191
5.13k
    case OVS_KEY_ATTR_ND_EXTENSIONS: return "nd_ext";
192
1.59k
    case OVS_KEY_ATTR_MPLS: return "mpls";
193
3.33k
    case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
194
3.11k
    case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
195
6.86k
    case OVS_KEY_ATTR_PACKET_TYPE: return "packet_type";
196
21.1k
    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
467k
    }
204
467k
}
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
1.39k
{
261
1.39k
    const struct nlattr *nla_acts = nl_attr_get(attr);
262
1.39k
    int len = nl_attr_get_size(attr);
263
264
1.39k
    ds_put_cstr(ds, "clone");
265
1.39k
    ds_put_format(ds, "(");
266
1.39k
    format_odp_actions(ds, nla_acts, len, portno_names);
267
1.39k
    ds_put_format(ds, ")");
268
1.39k
}
269
270
static void
271
format_nsh_key(struct ds *ds, const struct ovs_key_nsh *key)
272
3.11k
{
273
3.11k
    ds_put_format(ds, "flags=%d", key->flags);
274
3.11k
    ds_put_format(ds, ",ttl=%d", key->ttl);
275
3.11k
    ds_put_format(ds, ",mdtype=%d", key->mdtype);
276
3.11k
    ds_put_format(ds, ",np=%d", key->np);
277
3.11k
    ds_put_format(ds, ",spi=0x%x",
278
3.11k
                  nsh_path_hdr_to_spi_uint32(key->path_hdr));
279
3.11k
    ds_put_format(ds, ",si=%d",
280
3.11k
                  nsh_path_hdr_to_si(key->path_hdr));
281
282
3.11k
    switch (key->mdtype) {
283
1.32k
        case NSH_M_TYPE1:
284
6.60k
            for (int i = 0; i < 4; i++) {
285
5.28k
                ds_put_format(ds, ",c%d=0x%x", i + 1, ntohl(key->context[i]));
286
5.28k
            }
287
1.32k
            break;
288
132
        case NSH_M_TYPE2:
289
1.79k
        default:
290
            /* No support for matching other metadata formats yet. */
291
1.79k
            break;
292
3.11k
    }
293
3.11k
}
294
295
static void
296
format_uint8_masked(struct ds *s, bool *first, const char *name,
297
                    uint8_t value, uint8_t mask)
298
90.1k
{
299
90.1k
    if (mask != 0) {
300
66.4k
        if (!*first) {
301
51.0k
            ds_put_char(s, ',');
302
51.0k
        }
303
66.4k
        ds_put_format(s, "%s=", name);
304
66.4k
        if (mask == UINT8_MAX) {
305
65.3k
            ds_put_format(s, "%"PRIu8, value);
306
65.3k
        } else {
307
1.02k
            ds_put_format(s, "0x%02"PRIx8"/0x%02"PRIx8, value, mask);
308
1.02k
        }
309
66.4k
        *first = false;
310
66.4k
    }
311
90.1k
}
312
313
static void
314
format_be32_masked(struct ds *s, bool *first, const char *name,
315
                   ovs_be32 value, ovs_be32 mask)
316
95.2k
{
317
95.2k
    if (mask != htonl(0)) {
318
82.4k
        if (!*first) {
319
74.6k
            ds_put_char(s, ',');
320
74.6k
        }
321
82.4k
        ds_put_format(s, "%s=", name);
322
82.4k
        if (mask == OVS_BE32_MAX) {
323
81.1k
            ds_put_format(s, "0x%"PRIx32, ntohl(value));
324
81.1k
        } else {
325
1.32k
            ds_put_format(s, "0x%"PRIx32"/0x%08"PRIx32,
326
1.32k
                          ntohl(value), ntohl(mask));
327
1.32k
        }
328
82.4k
        *first = false;
329
82.4k
    }
330
95.2k
}
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
18.0k
{
336
18.0k
    if (!mask) {
337
0
        format_nsh_key(ds, key);
338
18.0k
    } else {
339
18.0k
        bool first = true;
340
18.0k
        uint32_t spi = nsh_path_hdr_to_spi_uint32(key->path_hdr);
341
18.0k
        uint32_t spi_mask = nsh_path_hdr_to_spi_uint32(mask->path_hdr);
342
18.0k
        if (spi_mask == (NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
343
13.1k
            spi_mask = UINT32_MAX;
344
13.1k
        }
345
18.0k
        uint8_t si = nsh_path_hdr_to_si(key->path_hdr);
346
18.0k
        uint8_t si_mask = nsh_path_hdr_to_si(mask->path_hdr);
347
348
18.0k
        format_uint8_masked(ds, &first, "flags", key->flags, mask->flags);
349
18.0k
        format_uint8_masked(ds, &first, "ttl", key->ttl, mask->ttl);
350
18.0k
        format_uint8_masked(ds, &first, "mdtype", key->mdtype, mask->mdtype);
351
18.0k
        format_uint8_masked(ds, &first, "np", key->np, mask->np);
352
18.0k
        format_be32_masked(ds, &first, "spi", htonl(spi), htonl(spi_mask));
353
18.0k
        format_uint8_masked(ds, &first, "si", si, si_mask);
354
18.0k
        format_be32_masked(ds, &first, "c1", key->context[0],
355
18.0k
                           mask->context[0]);
356
18.0k
        format_be32_masked(ds, &first, "c2", key->context[1],
357
18.0k
                           mask->context[1]);
358
18.0k
        format_be32_masked(ds, &first, "c3", key->context[2],
359
18.0k
                           mask->context[2]);
360
18.0k
        format_be32_masked(ds, &first, "c4", key->context[3],
361
18.0k
                           mask->context[3]);
362
18.0k
    }
363
18.0k
}
364
365
static void
366
format_odp_push_nsh_action(struct ds *ds,
367
                           const struct nsh_hdr *nsh_hdr)
368
1.01k
 {
369
1.01k
    size_t mdlen = nsh_hdr_len(nsh_hdr) - NSH_BASE_HDR_LEN;
370
1.01k
    uint32_t spi = ntohl(nsh_get_spi(nsh_hdr));
371
1.01k
    uint8_t si = nsh_get_si(nsh_hdr);
372
1.01k
    uint8_t flags = nsh_get_flags(nsh_hdr);
373
1.01k
    uint8_t ttl = nsh_get_ttl(nsh_hdr);
374
375
1.01k
    ds_put_cstr(ds, "push_nsh(");
376
1.01k
    ds_put_format(ds, "flags=%d", flags);
377
1.01k
    ds_put_format(ds, ",ttl=%d", ttl);
378
1.01k
    ds_put_format(ds, ",mdtype=%d", nsh_hdr->md_type);
379
1.01k
    ds_put_format(ds, ",np=%d", nsh_hdr->next_proto);
380
1.01k
    ds_put_format(ds, ",spi=0x%x", spi);
381
1.01k
    ds_put_format(ds, ",si=%d", si);
382
1.01k
    switch (nsh_hdr->md_type) {
383
614
    case NSH_M_TYPE1: {
384
614
        const struct nsh_md1_ctx *md1_ctx = &nsh_hdr->md1;
385
3.07k
        for (int i = 0; i < 4; i++) {
386
2.45k
            ds_put_format(ds, ",c%d=0x%x", i + 1,
387
2.45k
                          ntohl(get_16aligned_be32(&md1_ctx->context[i])));
388
2.45k
        }
389
614
        break;
390
0
    }
391
398
    case NSH_M_TYPE2: {
392
398
        const struct nsh_md2_tlv *md2_ctx = &nsh_hdr->md2;
393
398
        ds_put_cstr(ds, ",md2=");
394
398
        ds_put_hex(ds, md2_ctx, mdlen);
395
398
        break;
396
0
    }
397
0
    default:
398
0
        ds_put_cstr(ds, ",<error: unknown mdtype>");
399
0
        break;
400
1.01k
    }
401
1.01k
    ds_put_format(ds, ")");
402
1.01k
}
403
404
static const char *
405
slow_path_reason_to_string(uint32_t reason)
406
9.58k
{
407
9.58k
    switch ((enum slow_path_reason) reason) {
408
4.52k
#define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
409
9.58k
        SLOW_PATH_REASONS
410
9.58k
#undef SPR
411
9.58k
    }
412
413
5.05k
    return NULL;
414
9.58k
}
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
29.2k
{
432
29.2k
    return parse_flags(s, bit_to_string, ')', NULL, NULL,
433
29.2k
                       res_flags, allowed, res_mask);
434
29.2k
}
435
436
static void
437
format_odp_userspace_action(struct ds *ds, const struct nlattr *attr,
438
                            const struct hmap *portno_names)
439
3.64k
{
440
3.64k
    static const struct nl_policy ovs_userspace_policy[] = {
441
3.64k
        [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
442
3.64k
        [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
443
3.64k
                                          .optional = true },
444
3.64k
        [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
445
3.64k
                                                 .optional = true },
446
3.64k
        [OVS_USERSPACE_ATTR_ACTIONS] = { .type = NL_A_UNSPEC,
447
3.64k
                                                 .optional = true },
448
3.64k
    };
449
3.64k
    struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
450
3.64k
    const struct nlattr *userdata_attr;
451
3.64k
    const struct nlattr *tunnel_out_port_attr;
452
453
3.64k
    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
3.64k
    ds_put_format(ds, "userspace(pid=%"PRIu32,
459
3.64k
                  nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
460
461
3.64k
    userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
462
463
3.64k
    if (userdata_attr) {
464
3.64k
        const uint8_t *userdata = nl_attr_get(userdata_attr);
465
3.64k
        size_t userdata_len = nl_attr_get_size(userdata_attr);
466
3.64k
        bool userdata_unspec = true;
467
3.64k
        struct user_action_cookie cookie;
468
469
3.64k
        if (userdata_len == sizeof cookie) {
470
2.71k
            memcpy(&cookie, userdata, sizeof cookie);
471
472
2.71k
            userdata_unspec = false;
473
474
2.71k
            if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
475
195
                ds_put_format(ds, ",sFlow("
476
195
                              "vid=%"PRIu16",pcp=%d,output=%"PRIu32")",
477
195
                              vlan_tci_to_vid(cookie.sflow.vlan_tci),
478
195
                              vlan_tci_to_pcp(cookie.sflow.vlan_tci),
479
195
                              cookie.sflow.output);
480
2.52k
            } else if (cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
481
761
                ds_put_cstr(ds, ",slow_path(");
482
761
                format_flags(ds, slow_path_reason_to_string,
483
761
                             cookie.slow_path.reason, ',');
484
761
                ds_put_format(ds, ")");
485
1.76k
            } else if (cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
486
619
                ds_put_format(ds, ",flow_sample(probability=%"PRIu16
487
619
                              ",collector_set_id=%"PRIu32
488
619
                              ",obs_domain_id=%"PRIu32
489
619
                              ",obs_point_id=%"PRIu32
490
619
                              ",output_port=",
491
619
                              cookie.flow_sample.probability,
492
619
                              cookie.flow_sample.collector_set_id,
493
619
                              cookie.flow_sample.obs_domain_id,
494
619
                              cookie.flow_sample.obs_point_id);
495
619
                odp_portno_name_format(portno_names,
496
619
                                       cookie.flow_sample.output_odp_port, ds);
497
619
                if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_INGRESS) {
498
194
                    ds_put_cstr(ds, ",ingress");
499
425
                } else if (cookie.flow_sample.direction == NX_ACTION_SAMPLE_EGRESS) {
500
198
                    ds_put_cstr(ds, ",egress");
501
198
                }
502
619
                ds_put_char(ds, ')');
503
1.14k
            } else if (cookie.type == USER_ACTION_COOKIE_IPFIX) {
504
205
                ds_put_format(ds, ",ipfix(output_port=");
505
205
                odp_portno_name_format(portno_names,
506
205
                                       cookie.ipfix.output_odp_port, ds);
507
205
                ds_put_char(ds, ')');
508
936
            } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
509
196
                ds_put_format(ds, ",controller(reason=%"PRIu16
510
196
                              ",dont_send=%d"
511
196
                              ",continuation=%d"
512
196
                              ",recirc_id=%"PRIu32
513
196
                              ",rule_cookie=%#"PRIx64
514
196
                              ",controller_id=%"PRIu16
515
196
                              ",max_len=%"PRIu16,
516
196
                              cookie.controller.reason,
517
196
                              !!cookie.controller.dont_send,
518
196
                              !!cookie.controller.continuation,
519
196
                              cookie.controller.recirc_id,
520
196
                              ntohll(get_32aligned_be64(
521
196
                                         &cookie.controller.rule_cookie)),
522
196
                              cookie.controller.controller_id,
523
196
                              cookie.controller.max_len);
524
196
                ds_put_char(ds, ')');
525
740
            } else {
526
740
                userdata_unspec = true;
527
740
            }
528
2.71k
        }
529
530
3.64k
        if (userdata_unspec) {
531
1.66k
            size_t i;
532
1.66k
            ds_put_format(ds, ",userdata(");
533
1.39M
            for (i = 0; i < userdata_len; i++) {
534
1.39M
                ds_put_format(ds, "%02x", userdata[i]);
535
1.39M
            }
536
1.66k
            ds_put_char(ds, ')');
537
1.66k
        }
538
3.64k
    }
539
540
3.64k
    if (a[OVS_USERSPACE_ATTR_ACTIONS]) {
541
195
        ds_put_cstr(ds, ",actions");
542
195
    }
543
544
3.64k
    tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
545
3.64k
    if (tunnel_out_port_attr) {
546
229
        ds_put_format(ds, ",tunnel_out_port=");
547
229
        odp_portno_name_format(portno_names,
548
229
                               nl_attr_get_odp_port(tunnel_out_port_attr), ds);
549
229
    }
550
551
3.64k
    ds_put_char(ds, ')');
552
3.64k
}
553
554
static void
555
format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
556
4.34k
{
557
4.34k
    if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
558
3.65k
        ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
559
3.65k
        if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
560
599
            ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
561
599
        };
562
3.65k
        ds_put_char(ds, ',');
563
3.65k
    }
564
4.34k
    if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
565
3.36k
        ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
566
3.36k
        if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
567
232
            ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
568
232
        }
569
3.36k
        ds_put_char(ds, ',');
570
3.36k
    }
571
4.34k
    if (!(tci & htons(VLAN_CFI))) {
572
1.01k
        ds_put_cstr(ds, "cfi=0");
573
1.01k
        ds_put_char(ds, ',');
574
1.01k
    }
575
4.34k
    ds_chomp(ds, ',');
576
4.34k
}
577
578
static void
579
format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
580
1.27k
{
581
1.27k
    ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
582
1.27k
                  mpls_lse_to_label(mpls_lse),
583
1.27k
                  mpls_lse_to_tc(mpls_lse),
584
1.27k
                  mpls_lse_to_ttl(mpls_lse),
585
1.27k
                  mpls_lse_to_bos(mpls_lse));
586
1.27k
}
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
1.33k
{
592
3.21k
    for (int i = 0; i < n; i++) {
593
1.87k
        ovs_be32 key = mpls_key[i].mpls_lse;
594
595
1.87k
        if (mpls_mask == NULL) {
596
1.27k
            format_mpls_lse(ds, key);
597
1.27k
        } else {
598
606
            ovs_be32 mask = mpls_mask[i].mpls_lse;
599
600
606
            ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
601
606
                          mpls_lse_to_label(key), mpls_lse_to_label(mask),
602
606
                          mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
603
606
                          mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
604
606
                          mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
605
606
        }
606
1.87k
        ds_put_char(ds, ',');
607
1.87k
    }
608
1.33k
    ds_chomp(ds, ',');
609
1.33k
}
610
611
static void
612
format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
613
194
{
614
194
    ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
615
194
}
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
5.26k
{
947
5.26k
    struct nlattr *a[ARRAY_SIZE(ovs_nat_policy)];
948
5.26k
    size_t addr_len;
949
5.26k
    ovs_be32 ip_min, ip_max;
950
5.26k
    struct in6_addr ip6_min, ip6_max;
951
5.26k
    uint16_t proto_min, proto_max;
952
953
5.26k
    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
5.26k
    if (!(a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST])
959
484
        && (a[OVS_NAT_ATTR_IP_MIN] || a[OVS_NAT_ATTR_IP_MAX]
960
484
            || a[OVS_NAT_ATTR_PROTO_MIN] || a[OVS_NAT_ATTR_PROTO_MAX]
961
484
            || a[OVS_NAT_ATTR_PERSISTENT] || a[OVS_NAT_ATTR_PROTO_HASH]
962
484
            || 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
5.26k
    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
5.26k
    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
5.26k
    if ((!a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX])
978
5.26k
        || (!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
5.26k
    if ((a[OVS_NAT_ATTR_IP_MIN]
984
4.25k
         && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(ovs_be32) &&
985
3.75k
             nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) != sizeof(struct in6_addr)))
986
5.26k
        || (a[OVS_NAT_ATTR_IP_MIN] && a[OVS_NAT_ATTR_IP_MAX]
987
2.44k
            && (nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN])
988
2.44k
                != 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
5.26k
    addr_len = a[OVS_NAT_ATTR_IP_MIN]
994
5.26k
        ? nl_attr_get_size(a[OVS_NAT_ATTR_IP_MIN]) : 0;
995
5.26k
    ip_min = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MIN]
996
5.26k
        ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MIN]) : 0;
997
5.26k
    ip_max = addr_len == sizeof(ovs_be32) && a[OVS_NAT_ATTR_IP_MAX]
998
5.26k
        ? nl_attr_get_be32(a[OVS_NAT_ATTR_IP_MAX]) : 0;
999
5.26k
    if (addr_len == sizeof ip6_min) {
1000
3.75k
        ip6_min = a[OVS_NAT_ATTR_IP_MIN]
1001
3.75k
            ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MIN])
1002
3.75k
            : in6addr_any;
1003
3.75k
        ip6_max = a[OVS_NAT_ATTR_IP_MAX]
1004
3.75k
            ? *(struct in6_addr *)nl_attr_get(a[OVS_NAT_ATTR_IP_MAX])
1005
3.75k
            : in6addr_any;
1006
3.75k
    }
1007
5.26k
    proto_min = a[OVS_NAT_ATTR_PROTO_MIN]
1008
5.26k
        ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MIN]) : 0;
1009
5.26k
    proto_max = a[OVS_NAT_ATTR_PROTO_MAX]
1010
5.26k
        ? nl_attr_get_u16(a[OVS_NAT_ATTR_PROTO_MAX]) : 0;
1011
1012
5.26k
    if ((addr_len == sizeof(ovs_be32)
1013
5.26k
         && ip_max && ntohl(ip_min) > ntohl(ip_max))
1014
5.26k
        || (addr_len == sizeof(struct in6_addr)
1015
3.75k
            && !ipv6_mask_is_any(&ip6_max)
1016
2.14k
            && memcmp(&ip6_min, &ip6_max, sizeof ip6_min) > 0)
1017
5.26k
        || (proto_max && proto_min > proto_max)) {
1018
0
        ds_put_cstr(ds, "nat(range error)");
1019
0
        return;
1020
0
    }
1021
1022
5.26k
    ds_put_cstr(ds, "nat");
1023
5.26k
    if (a[OVS_NAT_ATTR_SRC] || a[OVS_NAT_ATTR_DST]) {
1024
4.78k
        ds_put_char(ds, '(');
1025
4.78k
        if (a[OVS_NAT_ATTR_SRC]) {
1026
2.61k
            ds_put_cstr(ds, "src");
1027
2.61k
        } else if (a[OVS_NAT_ATTR_DST]) {
1028
2.16k
            ds_put_cstr(ds, "dst");
1029
2.16k
        }
1030
1031
4.78k
        if (addr_len > 0) {
1032
4.25k
            ds_put_cstr(ds, "=");
1033
1034
4.25k
            if (addr_len == sizeof ip_min) {
1035
496
                ds_put_format(ds, IP_FMT, IP_ARGS(ip_min));
1036
1037
496
                if (ip_max && ip_max != ip_min) {
1038
297
                    ds_put_format(ds, "-"IP_FMT, IP_ARGS(ip_max));
1039
297
                }
1040
3.75k
            } else if (addr_len == sizeof ip6_min) {
1041
3.75k
                ipv6_format_addr_bracket(&ip6_min, ds, proto_min);
1042
1043
3.75k
                if (!ipv6_mask_is_any(&ip6_max) &&
1044
2.14k
                    memcmp(&ip6_max, &ip6_min, sizeof ip6_max) != 0) {
1045
2.14k
                    ds_put_char(ds, '-');
1046
2.14k
                    ipv6_format_addr_bracket(&ip6_max, ds, proto_min);
1047
2.14k
                }
1048
3.75k
            }
1049
4.25k
            if (proto_min) {
1050
1.65k
                ds_put_format(ds, ":%"PRIu16, proto_min);
1051
1052
1.65k
                if (proto_max && proto_max != proto_min) {
1053
257
                    ds_put_format(ds, "-%"PRIu16, proto_max);
1054
257
                }
1055
1.65k
            }
1056
4.25k
        }
1057
4.78k
        ds_put_char(ds, ',');
1058
4.78k
        if (a[OVS_NAT_ATTR_PERSISTENT]) {
1059
194
            ds_put_cstr(ds, "persistent,");
1060
194
        }
1061
4.78k
        if (a[OVS_NAT_ATTR_PROTO_HASH]) {
1062
889
            ds_put_cstr(ds, "hash,");
1063
889
        }
1064
4.78k
        if (a[OVS_NAT_ATTR_PROTO_RANDOM]) {
1065
314
            ds_put_cstr(ds, "random,");
1066
314
        }
1067
4.78k
        ds_chomp(ds, ',');
1068
4.78k
        ds_put_char(ds, ')');
1069
4.78k
    }
1070
5.26k
}
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
10.0k
{
1090
10.0k
    struct nlattr *a[ARRAY_SIZE(ovs_conntrack_policy)];
1091
10.0k
    const struct {
1092
10.0k
        ovs_32aligned_u128 value;
1093
10.0k
        ovs_32aligned_u128 mask;
1094
10.0k
    } *label;
1095
10.0k
    const uint32_t *mark;
1096
10.0k
    const char *helper, *timeout;
1097
10.0k
    uint16_t zone;
1098
10.0k
    bool commit, force;
1099
10.0k
    const struct nlattr *nat;
1100
1101
10.0k
    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
10.0k
    commit = a[OVS_CT_ATTR_COMMIT] ? true : false;
1107
10.0k
    force = a[OVS_CT_ATTR_FORCE_COMMIT] ? true : false;
1108
10.0k
    zone = a[OVS_CT_ATTR_ZONE] ? nl_attr_get_u16(a[OVS_CT_ATTR_ZONE]) : 0;
1109
10.0k
    mark = a[OVS_CT_ATTR_MARK] ? nl_attr_get(a[OVS_CT_ATTR_MARK]) : NULL;
1110
10.0k
    label = a[OVS_CT_ATTR_LABELS] ? nl_attr_get(a[OVS_CT_ATTR_LABELS]): NULL;
1111
10.0k
    helper = a[OVS_CT_ATTR_HELPER] ? nl_attr_get(a[OVS_CT_ATTR_HELPER]) : NULL;
1112
10.0k
    timeout = a[OVS_CT_ATTR_TIMEOUT] ?
1113
10.0k
                nl_attr_get(a[OVS_CT_ATTR_TIMEOUT]) : NULL;
1114
10.0k
    nat = a[OVS_CT_ATTR_NAT];
1115
1116
10.0k
    ds_put_format(ds, "ct");
1117
10.0k
    if (commit || force || zone || mark || label || helper || timeout || nat) {
1118
7.23k
        ds_put_cstr(ds, "(");
1119
7.23k
        if (commit) {
1120
209
            ds_put_format(ds, "commit,");
1121
209
        }
1122
7.23k
        if (force) {
1123
269
            ds_put_format(ds, "force_commit,");
1124
269
        }
1125
7.23k
        if (zone) {
1126
216
            ds_put_format(ds, "zone=%"PRIu16",", zone);
1127
216
        }
1128
7.23k
        if (mark) {
1129
287
            ds_put_format(ds, "mark=%#"PRIx32"/%#"PRIx32",", *mark,
1130
287
                          *(mark + 1));
1131
287
        }
1132
7.23k
        if (label) {
1133
949
            ds_put_format(ds, "label=");
1134
949
            format_u128(ds, &label->value, &label->mask, true);
1135
949
            ds_put_char(ds, ',');
1136
949
        }
1137
7.23k
        if (helper) {
1138
276
            ds_put_format(ds, "helper=%s,", helper);
1139
276
        }
1140
7.23k
        if (timeout) {
1141
0
            ds_put_format(ds, "timeout=%s,", timeout);
1142
0
        }
1143
7.23k
        if (nat) {
1144
5.26k
            format_odp_ct_nat(ds, nat);
1145
5.26k
        }
1146
7.23k
        ds_chomp(ds, ',');
1147
7.23k
        ds_put_cstr(ds, ")");
1148
7.23k
    }
1149
10.0k
}
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
61.2k
{
1294
61.2k
    int expected_len;
1295
61.2k
    enum ovs_action_attr type = nl_attr_type(a);
1296
61.2k
    size_t size;
1297
1298
61.2k
    expected_len = odp_action_len(nl_attr_type(a));
1299
61.2k
    if (expected_len != ATTR_LEN_VARIABLE &&
1300
31.9k
        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
61.2k
    switch (type) {
1308
196
    case OVS_ACTION_ATTR_METER:
1309
196
        ds_put_format(ds, "meter(%"PRIu32")", nl_attr_get_u32(a));
1310
196
        break;
1311
27.7k
    case OVS_ACTION_ATTR_OUTPUT:
1312
27.7k
        odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
1313
27.7k
        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
194
    case OVS_ACTION_ATTR_TRUNC: {
1318
194
        const struct ovs_action_trunc *trunc =
1319
194
                       nl_attr_get_unspec(a, sizeof *trunc);
1320
1321
194
        ds_put_format(ds, "trunc(%"PRIu32")", trunc->max_len);
1322
194
        break;
1323
0
    }
1324
281
    case OVS_ACTION_ATTR_TUNNEL_POP:
1325
281
        ds_put_cstr(ds, "tnl_pop(");
1326
281
        odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
1327
281
        ds_put_char(ds, ')');
1328
281
        break;
1329
0
    case OVS_ACTION_ATTR_TUNNEL_PUSH:
1330
0
        format_odp_tnl_push_action(ds, a, portno_names);
1331
0
        break;
1332
3.64k
    case OVS_ACTION_ATTR_USERSPACE:
1333
3.64k
        format_odp_userspace_action(ds, a, portno_names);
1334
3.64k
        break;
1335
194
    case OVS_ACTION_ATTR_RECIRC:
1336
194
        format_odp_recirc_action(ds, nl_attr_get_u32(a));
1337
194
        break;
1338
0
    case OVS_ACTION_ATTR_HASH:
1339
0
        format_odp_hash_action(ds, nl_attr_get(a));
1340
0
        break;
1341
12.2k
    case OVS_ACTION_ATTR_SET_MASKED:
1342
12.2k
        a = nl_attr_get(a);
1343
        /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1344
12.2k
        if (nl_attr_type(a) == OVS_KEY_ATTR_NSH) {
1345
0
            format_odp_set_nsh(ds, a);
1346
0
            break;
1347
0
        }
1348
12.2k
        size = nl_attr_get_size(a) / 2;
1349
12.2k
        ds_put_cstr(ds, "set(");
1350
1351
        /* Masked set action not supported for tunnel key, which is bigger. */
1352
12.2k
        if (size <= sizeof(struct ovs_key_ipv6)) {
1353
899
            struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
1354
899
                                                sizeof(struct nlattr))];
1355
899
            struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
1356
899
                                                sizeof(struct nlattr))];
1357
1358
899
            mask->nla_type = attr->nla_type = nl_attr_type(a);
1359
899
            mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
1360
899
            memcpy(attr + 1, (char *)(a + 1), size);
1361
899
            memcpy(mask + 1, (char *)(a + 1) + size, size);
1362
899
            format_odp_key_attr(attr, mask, NULL, ds, false);
1363
11.3k
        } else {
1364
11.3k
            format_odp_key_attr(a, NULL, NULL, ds, false);
1365
11.3k
        }
1366
12.2k
        ds_put_cstr(ds, ")");
1367
12.2k
        break;
1368
909
    case OVS_ACTION_ATTR_SET:
1369
909
        ds_put_cstr(ds, "set(");
1370
909
        format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
1371
909
        ds_put_cstr(ds, ")");
1372
909
        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
12.2k
    }
1380
0
    case OVS_ACTION_ATTR_POP_ETH:
1381
0
        ds_put_cstr(ds, "pop_eth");
1382
0
        break;
1383
811
    case OVS_ACTION_ATTR_PUSH_VLAN: {
1384
811
        const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
1385
811
        ds_put_cstr(ds, "push_vlan(");
1386
811
        if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
1387
273
            ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
1388
273
        }
1389
811
        format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
1390
811
        ds_put_char(ds, ')');
1391
811
        break;
1392
12.2k
    }
1393
557
    case OVS_ACTION_ATTR_POP_VLAN:
1394
557
        ds_put_cstr(ds, "pop_vlan");
1395
557
        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
12.2k
    }
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
12.2k
    }
1408
0
    case OVS_ACTION_ATTR_SAMPLE:
1409
0
        format_odp_sample_action(ds, a, portno_names);
1410
0
        break;
1411
10.0k
    case OVS_ACTION_ATTR_CT:
1412
10.0k
        format_odp_conntrack_action(ds, a);
1413
10.0k
        break;
1414
622
    case OVS_ACTION_ATTR_CT_CLEAR:
1415
622
        ds_put_cstr(ds, "ct_clear");
1416
622
        break;
1417
1.39k
    case OVS_ACTION_ATTR_CLONE:
1418
1.39k
        format_odp_clone_action(ds, a, portno_names);
1419
1.39k
        break;
1420
1.01k
    case OVS_ACTION_ATTR_PUSH_NSH: {
1421
1.01k
        uint32_t buffer[NSH_HDR_MAX_LEN / 4];
1422
1.01k
        struct nsh_hdr *nsh_hdr = ALIGNED_CAST(struct nsh_hdr *, buffer);
1423
1.01k
        nsh_reset_ver_flags_ttl_len(nsh_hdr);
1424
1.01k
        odp_nsh_hdr_from_attr(nl_attr_get(a), nsh_hdr, NSH_HDR_MAX_LEN);
1425
1.01k
        format_odp_push_nsh_action(ds, nsh_hdr);
1426
1.01k
        break;
1427
12.2k
    }
1428
1.38k
    case OVS_ACTION_ATTR_POP_NSH:
1429
1.38k
        ds_put_cstr(ds, "pop_nsh()");
1430
1.38k
        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
12.2k
    }
1443
0
    case OVS_ACTION_ATTR_DEC_TTL:
1444
0
        format_dec_ttl_action(ds, a, portno_names);
1445
0
        break;
1446
1
    case OVS_ACTION_ATTR_DROP:
1447
1
        ds_put_cstr(ds, "drop");
1448
1
        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
61.2k
    }
1458
61.2k
}
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
4.53k
{
1464
4.53k
    if (actions_len) {
1465
4.14k
        const struct nlattr *a;
1466
4.14k
        unsigned int left;
1467
1468
61.2k
        NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
1469
61.2k
            if (a != actions) {
1470
57.0k
                ds_put_char(ds, ',');
1471
57.0k
            }
1472
61.2k
            format_odp_action(ds, a, portno_names);
1473
61.2k
        }
1474
4.14k
        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
4.14k
    } else {
1487
384
        ds_put_cstr(ds, "drop");
1488
384
    }
1489
4.53k
}
1490
1491
/* Separate out parse_odp_userspace_action() function. */
1492
static int
1493
parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
1494
3.94k
{
1495
3.94k
    uint32_t pid;
1496
3.94k
    struct user_action_cookie cookie;
1497
3.94k
    struct ofpbuf buf;
1498
3.94k
    odp_port_t tunnel_out_port;
1499
3.94k
    int n = -1;
1500
3.94k
    void *user_data = NULL;
1501
3.94k
    size_t user_data_size = 0;
1502
3.94k
    bool include_actions = false;
1503
3.94k
    int res;
1504
1505
3.94k
    if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
1506
5
        return -EINVAL;
1507
5
    }
1508
1509
3.94k
    ofpbuf_init(&buf, 16);
1510
3.94k
    memset(&cookie, 0, sizeof cookie);
1511
1512
3.94k
    user_data = &cookie;
1513
3.94k
    user_data_size = sizeof cookie;
1514
3.94k
    {
1515
3.94k
        uint32_t output;
1516
3.94k
        uint32_t probability;
1517
3.94k
        uint32_t collector_set_id;
1518
3.94k
        uint32_t obs_domain_id;
1519
3.94k
        uint32_t obs_point_id;
1520
1521
        /* USER_ACTION_COOKIE_CONTROLLER. */
1522
3.94k
        uint8_t dont_send;
1523
3.94k
        uint8_t continuation;
1524
3.94k
        uint16_t reason;
1525
3.94k
        uint32_t recirc_id;
1526
3.94k
        uint64_t rule_cookie;
1527
3.94k
        uint16_t controller_id;
1528
3.94k
        uint16_t max_len;
1529
1530
3.94k
        int vid, pcp;
1531
3.94k
        int n1 = -1;
1532
3.94k
        if (ovs_scan(&s[n], ",sFlow(vid=%i,"
1533
3.94k
                     "pcp=%i,output=%"SCNi32")%n",
1534
3.94k
                     &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
3.94k
        } else if (ovs_scan(&s[n], ",slow_path(%n",
1549
3.94k
                            &n1)) {
1550
485
            n += n1;
1551
485
            cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
1552
485
            cookie.ofp_in_port = OFPP_NONE;
1553
485
            cookie.ofproto_uuid = UUID_ZERO;
1554
485
            cookie.slow_path.reason = 0;
1555
1556
485
            res = parse_odp_flags(&s[n], slow_path_reason_to_string,
1557
485
                                  &cookie.slow_path.reason,
1558
485
                                  SLOW_PATH_REASON_MASK, NULL);
1559
485
            if (res < 0 || s[n + res] != ')') {
1560
24
                goto out;
1561
24
            }
1562
461
            n += res + 1;
1563
3.45k
        } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
1564
3.45k
                            "collector_set_id=%"SCNi32","
1565
3.45k
                            "obs_domain_id=%"SCNi32","
1566
3.45k
                            "obs_point_id=%"SCNi32","
1567
3.45k
                            "output_port=%"SCNi32"%n",
1568
3.45k
                            &probability, &collector_set_id,
1569
3.45k
                            &obs_domain_id, &obs_point_id,
1570
3.45k
                            &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
3.45k
        } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
1597
3.45k
                            &output, &n1) ) {
1598
211
            n += n1;
1599
211
            cookie.type = USER_ACTION_COOKIE_IPFIX;
1600
211
            cookie.ofp_in_port = OFPP_NONE;
1601
211
            cookie.ofproto_uuid = UUID_ZERO;
1602
211
            cookie.ipfix.output_odp_port = u32_to_odp(output);
1603
3.24k
        } else if (ovs_scan(&s[n], ",controller(reason=%"SCNu16
1604
3.24k
                              ",dont_send=%"SCNu8
1605
3.24k
                              ",continuation=%"SCNu8
1606
3.24k
                              ",recirc_id=%"SCNu32
1607
3.24k
                              ",rule_cookie=%"SCNx64
1608
3.24k
                              ",controller_id=%"SCNu16
1609
3.24k
                              ",max_len=%"SCNu16")%n",
1610
3.24k
                              &reason, &dont_send, &continuation, &recirc_id,
1611
3.24k
                              &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
3.24k
       } else if (ovs_scan(&s[n], ",userdata(%n", &n1)) {
1625
2.43k
            char *end;
1626
1627
2.43k
            n += n1;
1628
2.43k
            end = ofpbuf_put_hex(&buf, &s[n], NULL);
1629
2.43k
            if (end[0] != ')') {
1630
12
                res = -EINVAL;
1631
12
                goto out;
1632
12
            }
1633
2.42k
            user_data = buf.data;
1634
2.42k
            user_data_size = buf.size;
1635
2.42k
            n = (end + 1) - s;
1636
2.42k
        }
1637
3.94k
    }
1638
1639
3.90k
    {
1640
3.90k
        int n1 = -1;
1641
3.90k
        if (ovs_scan(&s[n], ",actions%n", &n1)) {
1642
199
            n += n1;
1643
199
            include_actions = true;
1644
199
        }
1645
3.90k
    }
1646
1647
3.90k
    {
1648
3.90k
        int n1 = -1;
1649
3.90k
        if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
1650
3.90k
                     &tunnel_out_port, &n1)) {
1651
229
            res = odp_put_userspace_action(pid, user_data, user_data_size,
1652
229
                                           tunnel_out_port, include_actions,
1653
229
                                           actions, NULL);
1654
229
            if (!res) {
1655
229
                res = n + n1;
1656
229
            }
1657
229
            goto out;
1658
3.67k
        } else if (s[n] == ')') {
1659
3.64k
            res = odp_put_userspace_action(pid, user_data, user_data_size,
1660
3.64k
                                           ODPP_NONE, include_actions,
1661
3.64k
                                           actions, NULL);
1662
3.64k
            if (!res) {
1663
3.63k
                res = n + 1;
1664
3.63k
            }
1665
3.64k
            goto out;
1666
3.64k
        }
1667
3.90k
    }
1668
1669
34
    {
1670
34
        struct ovs_action_push_eth push;
1671
34
        int eth_type = 0;
1672
34
        int n1 = -1;
1673
1674
34
        if (ovs_scan(&s[n], "push_eth(src="ETH_ADDR_SCAN_FMT","
1675
34
                     "dst="ETH_ADDR_SCAN_FMT",type=%i)%n",
1676
34
                     ETH_ADDR_SCAN_ARGS(push.addresses.eth_src),
1677
34
                     ETH_ADDR_SCAN_ARGS(push.addresses.eth_dst),
1678
34
                     &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
34
    }
1687
1688
34
    if (!strncmp(&s[n], "pop_eth", 7)) {
1689
1
        nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_ETH);
1690
1
        res = 7;
1691
1
        goto out;
1692
1
    }
1693
1694
33
    res = -EINVAL;
1695
3.94k
out:
1696
3.94k
    ofpbuf_uninit(&buf);
1697
3.94k
    return res;
1698
33
}
1699
1700
static int
1701
ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
1702
10.8k
{
1703
10.8k
    struct eth_header *eth;
1704
10.8k
    struct ip_header *ip;
1705
10.8k
    struct ovs_16aligned_ip6_hdr *ip6;
1706
10.8k
    struct udp_header *udp;
1707
10.8k
    struct gre_base_hdr *greh;
1708
10.8k
    struct erspan_base_hdr *ersh;
1709
10.8k
    struct erspan_md2 *md2;
1710
10.8k
    uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, udp_csum, sid;
1711
10.8k
    ovs_be32 sip, dip;
1712
10.8k
    uint32_t tnl_type = 0, header_len = 0, ip_len = 0, erspan_idx = 0;
1713
10.8k
    void *l3, *l4;
1714
10.8k
    int n = 0;
1715
10.8k
    uint8_t hwid, dir;
1716
10.8k
    uint32_t teid;
1717
10.8k
    uint8_t gtpu_flags, gtpu_msgtype;
1718
10.8k
    uint8_t segments_left;
1719
1720
10.8k
    if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
1721
10.8k
        return -EINVAL;
1722
10.8k
    }
1723
1
    eth = (struct eth_header *) data->header;
1724
1
    l3 = (struct ip_header *) (eth + 1);
1725
1
    ip = (struct ip_header *) l3;
1726
1
    ip6 = (struct ovs_16aligned_ip6_hdr *) l3;
1727
1
    if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
1728
1
                      "eth(dst="ETH_ADDR_SCAN_FMT",",
1729
1
                      &data->header_len,
1730
1
                      &data->tnl_type,
1731
1
                      ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
1732
1
        return -EINVAL;
1733
1
    }
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
8.78k
{
2053
8.78k
    if (ovs_scan_len(s, n, "=")) {
2054
7.30k
        char ipv6_s[IPV6_SCAN_LEN + 1];
2055
7.30k
        struct in6_addr ipv6;
2056
2057
7.30k
        if (ovs_scan_len(s, n, IP_SCAN_FMT, IP_SCAN_ARGS(&p->addr_min.ip))) {
2058
909
            p->addr_len = sizeof p->addr_min.ip;
2059
909
            if (ovs_scan_len(s, n, "-")) {
2060
494
                if (!ovs_scan_len(s, n, IP_SCAN_FMT,
2061
494
                                  IP_SCAN_ARGS(&p->addr_max.ip))) {
2062
1
                    return -EINVAL;
2063
1
                }
2064
494
            }
2065
6.39k
        } else if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
2066
1.86k
                    || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
2067
6.38k
                   && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
2068
6.36k
            p->addr_len = sizeof p->addr_min.ip6;
2069
6.36k
            p->addr_min.ip6 = ipv6;
2070
6.36k
            if (ovs_scan_len(s, n, "-")) {
2071
3.06k
                if ((ovs_scan_len(s, n, IPV6_SCAN_FMT, ipv6_s)
2072
1.49k
                     || ovs_scan_len(s, n, "["IPV6_SCAN_FMT"]", ipv6_s))
2073
3.06k
                    && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
2074
3.04k
                    p->addr_max.ip6 = ipv6;
2075
3.04k
                } else {
2076
21
                    return -EINVAL;
2077
21
                }
2078
3.06k
            }
2079
6.36k
        } else {
2080
32
            return -EINVAL;
2081
32
        }
2082
7.24k
        if (ovs_scan_len(s, n, ":%"SCNu16, &p->proto_min)) {
2083
2.65k
            if (ovs_scan_len(s, n, "-")) {
2084
1.14k
                if (!ovs_scan_len(s, n, "%"SCNu16, &p->proto_max)) {
2085
1
                    return -EINVAL;
2086
1
                }
2087
1.14k
            }
2088
2.65k
        }
2089
7.24k
    }
2090
8.73k
    return 0;
2091
8.78k
}
2092
2093
static int
2094
scan_ct_nat(const char *s, struct ct_nat_params *p)
2095
7.73k
{
2096
7.73k
    int n = 0;
2097
2098
7.73k
    if (ovs_scan_len(s, &n, "nat")) {
2099
7.71k
        memset(p, 0, sizeof *p);
2100
2101
7.71k
        if (ovs_scan_len(s, &n, "(")) {
2102
6.98k
            const char *end;
2103
6.98k
            int end_n;
2104
2105
6.98k
            end = strchr(s + n, ')');
2106
6.98k
            if (!end) {
2107
0
                return -EINVAL;
2108
0
            }
2109
6.98k
            end_n = end - s;
2110
2111
18.2k
            while (n < end_n) {
2112
11.3k
                n += strspn(s + n, delimiters);
2113
11.3k
                if (ovs_scan_len(s, &n, "src")) {
2114
4.23k
                    int err = scan_ct_nat_range(s, &n, p);
2115
4.23k
                    if (err) {
2116
27
                        return err;
2117
27
                    }
2118
4.20k
                    p->snat = true;
2119
4.20k
                    continue;
2120
4.23k
                }
2121
7.15k
                if (ovs_scan_len(s, &n, "dst")) {
2122
4.55k
                    int err = scan_ct_nat_range(s, &n, p);
2123
4.55k
                    if (err) {
2124
28
                        return err;
2125
28
                    }
2126
4.52k
                    p->dnat = true;
2127
4.52k
                    continue;
2128
4.55k
                }
2129
2.59k
                if (ovs_scan_len(s, &n, "persistent")) {
2130
396
                    p->persistent = true;
2131
396
                    continue;
2132
396
                }
2133
2.20k
                if (ovs_scan_len(s, &n, "hash")) {
2134
1.40k
                    p->proto_hash = true;
2135
1.40k
                    continue;
2136
1.40k
                }
2137
801
                if (ovs_scan_len(s, &n, "random")) {
2138
766
                    p->proto_random = true;
2139
766
                    continue;
2140
766
                }
2141
35
                return -EINVAL;
2142
801
            }
2143
2144
6.89k
            if (p->snat && p->dnat) {
2145
3
                return -EINVAL;
2146
3
            }
2147
6.88k
            if ((p->addr_len != 0 &&
2148
5.47k
                 memcmp(&p->addr_max, &in6addr_any, p->addr_len) &&
2149
2.82k
                 memcmp(&p->addr_max, &p->addr_min, p->addr_len) < 0) ||
2150
6.87k
                (p->proto_max && p->proto_max < p->proto_min)) {
2151
12
                return -EINVAL;
2152
12
            }
2153
6.87k
            if (p->proto_hash && p->proto_random) {
2154
1
                return -EINVAL;
2155
1
            }
2156
6.87k
            n++;
2157
6.87k
        }
2158
7.71k
    }
2159
7.62k
    return n;
2160
7.73k
}
2161
2162
static void
2163
nl_msg_put_ct_nat(struct ct_nat_params *p, struct ofpbuf *actions)
2164
5.82k
{
2165
5.82k
    size_t start = nl_msg_start_nested(actions, OVS_CT_ATTR_NAT);
2166
2167
5.82k
    if (p->snat) {
2168
2.65k
        nl_msg_put_flag(actions, OVS_NAT_ATTR_SRC);
2169
3.17k
    } else if (p->dnat) {
2170
2.65k
        nl_msg_put_flag(actions, OVS_NAT_ATTR_DST);
2171
2.65k
    } else {
2172
520
        goto out;
2173
520
    }
2174
5.30k
    if (p->addr_len != 0) {
2175
4.77k
        nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MIN, &p->addr_min,
2176
4.77k
                          p->addr_len);
2177
4.77k
        if (memcmp(&p->addr_max, &p->addr_min, p->addr_len) > 0) {
2178
2.57k
            nl_msg_put_unspec(actions, OVS_NAT_ATTR_IP_MAX, &p->addr_max,
2179
2.57k
                              p->addr_len);
2180
2.57k
        }
2181
4.77k
        if (p->proto_min) {
2182
2.01k
            nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MIN, p->proto_min);
2183
2.01k
            if (p->proto_max && p->proto_max > p->proto_min) {
2184
319
                nl_msg_put_u16(actions, OVS_NAT_ATTR_PROTO_MAX, p->proto_max);
2185
319
            }
2186
2.01k
        }
2187
4.77k
        if (p->persistent) {
2188
194
            nl_msg_put_flag(actions, OVS_NAT_ATTR_PERSISTENT);
2189
194
        }
2190
4.77k
        if (p->proto_hash) {
2191
889
            nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_HASH);
2192
889
        }
2193
4.77k
        if (p->proto_random) {
2194
474
            nl_msg_put_flag(actions, OVS_NAT_ATTR_PROTO_RANDOM);
2195
474
        }
2196
4.77k
    }
2197
5.82k
out:
2198
5.82k
    nl_msg_end_nested(actions, start);
2199
5.82k
}
2200
2201
static int
2202
parse_conntrack_action(const char *s_, struct ofpbuf *actions)
2203
24.1k
{
2204
24.1k
    const char *s = s_;
2205
2206
24.1k
    if (ovs_scan(s, "ct")) {
2207
13.3k
        const char *helper = NULL, *timeout = NULL;
2208
13.3k
        size_t helper_len = 0, timeout_len = 0;
2209
13.3k
        bool commit = false;
2210
13.3k
        bool force_commit = false;
2211
13.3k
        uint16_t zone = 0;
2212
13.3k
        struct {
2213
13.3k
            uint32_t value;
2214
13.3k
            uint32_t mask;
2215
13.3k
        } ct_mark = { 0, 0 };
2216
13.3k
        struct {
2217
13.3k
            ovs_u128 value;
2218
13.3k
            ovs_u128 mask;
2219
13.3k
        } ct_label;
2220
13.3k
        struct ct_nat_params nat_params = {
2221
13.3k
            .snat = false,
2222
13.3k
            .dnat = false,
2223
13.3k
        };
2224
13.3k
        bool have_nat = false;
2225
13.3k
        const char *end;
2226
13.3k
        size_t start;
2227
2228
13.3k
        memset(&ct_label, 0, sizeof(ct_label));
2229
2230
13.3k
        s += 2;
2231
13.3k
        if (ovs_scan(s, "(")) {
2232
8.53k
            s++;
2233
16.1k
find_end:
2234
16.1k
            end = strchr(s, ')');
2235
16.1k
            if (!end) {
2236
139
                return -EINVAL;
2237
139
            }
2238
2239
19.5k
            while (s != end) {
2240
11.3k
                int n;
2241
2242
11.3k
                s += strspn(s, delimiters);
2243
11.3k
                if (ovs_scan(s, "commit%n", &n)) {
2244
453
                    commit = true;
2245
453
                    s += n;
2246
453
                    continue;
2247
453
                }
2248
10.8k
                if (ovs_scan(s, "force_commit%n", &n)) {
2249
329
                    force_commit = true;
2250
329
                    s += n;
2251
329
                    continue;
2252
329
                }
2253
10.5k
                if (ovs_scan(s, "zone=%"SCNu16"%n", &zone, &n)) {
2254
419
                    s += n;
2255
419
                    continue;
2256
419
                }
2257
10.1k
                if (ovs_scan(s, "mark=%"SCNx32"%n", &ct_mark.value, &n)) {
2258
744
                    s += n;
2259
744
                    n = -1;
2260
744
                    if (ovs_scan(s, "/%"SCNx32"%n", &ct_mark.mask, &n)) {
2261
304
                        s += n;
2262
440
                    } else {
2263
440
                        ct_mark.mask = UINT32_MAX;
2264
440
                    }
2265
744
                    continue;
2266
744
                }
2267
9.38k
                if (ovs_scan(s, "label=%n", &n)) {
2268
1.16k
                    int retval;
2269
2270
1.16k
                    s += n;
2271
1.16k
                    retval = scan_u128(s, &ct_label.value, &ct_label.mask);
2272
1.16k
                    if (retval == 0) {
2273
9
                        return -EINVAL;
2274
9
                    }
2275
1.15k
                    s += retval;
2276
1.15k
                    continue;
2277
1.16k
                }
2278
8.22k
                if (ovs_scan(s, "helper=%n", &n)) {
2279
492
                    s += n;
2280
492
                    helper_len = strcspn(s, delimiters_end);
2281
492
                    if (!helper_len || helper_len > 15) {
2282
28
                        return -EINVAL;
2283
28
                    }
2284
464
                    helper = s;
2285
464
                    s += helper_len;
2286
464
                    continue;
2287
492
                }
2288
7.73k
                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
7.73k
                n = scan_ct_nat(s, &nat_params);
2300
7.73k
                if (n > 0) {
2301
7.61k
                    s += n;
2302
7.61k
                    have_nat = true;
2303
2304
                    /* end points to the end of the nested, nat action.
2305
                     * find the real end. */
2306
7.61k
                    goto find_end;
2307
7.61k
                }
2308
                /* Nothing matched. */
2309
121
                return -EINVAL;
2310
7.73k
            }
2311
8.23k
            s++;
2312
8.23k
        }
2313
13.0k
        if (commit && force_commit) {
2314
1
            return -EINVAL;
2315
1
        }
2316
2317
13.0k
        start = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CT);
2318
13.0k
        if (commit) {
2319
265
            nl_msg_put_flag(actions, OVS_CT_ATTR_COMMIT);
2320
12.7k
        } else if (force_commit) {
2321
269
            nl_msg_put_flag(actions, OVS_CT_ATTR_FORCE_COMMIT);
2322
269
        }
2323
13.0k
        if (zone) {
2324
232
            nl_msg_put_u16(actions, OVS_CT_ATTR_ZONE, zone);
2325
232
        }
2326
13.0k
        if (ct_mark.mask) {
2327
287
            nl_msg_put_unspec(actions, OVS_CT_ATTR_MARK, &ct_mark,
2328
287
                              sizeof(ct_mark));
2329
287
        }
2330
13.0k
        if (!ovs_u128_is_zero(ct_label.mask)) {
2331
950
            nl_msg_put_unspec(actions, OVS_CT_ATTR_LABELS, &ct_label,
2332
950
                              sizeof ct_label);
2333
950
        }
2334
13.0k
        if (helper) {
2335
405
            nl_msg_put_string__(actions, OVS_CT_ATTR_HELPER, helper,
2336
405
                                helper_len);
2337
405
        }
2338
13.0k
        if (timeout) {
2339
0
            nl_msg_put_string__(actions, OVS_CT_ATTR_TIMEOUT, timeout,
2340
0
                                timeout_len);
2341
0
        }
2342
13.0k
        if (have_nat) {
2343
5.82k
            nl_msg_put_ct_nat(&nat_params, actions);
2344
5.82k
        }
2345
13.0k
        nl_msg_end_nested(actions, start);
2346
13.0k
    }
2347
2348
23.8k
    return s - s_;
2349
24.1k
}
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
91.8k
{
2356
91.8k
    size_t nsh_key_ofs;
2357
91.8k
    struct ovs_nsh_key_base base;
2358
2359
91.8k
    base.flags = nsh->flags;
2360
91.8k
    base.ttl = nsh->ttl;
2361
91.8k
    base.mdtype = nsh->mdtype;
2362
91.8k
    base.np = nsh->np;
2363
91.8k
    base.path_hdr = nsh->path_hdr;
2364
2365
91.8k
    nsh_key_ofs = nl_msg_start_nested(buf, OVS_KEY_ATTR_NSH);
2366
91.8k
    nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_BASE, &base, sizeof base);
2367
2368
91.8k
    if (is_mask) {
2369
45.3k
        nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context,
2370
45.3k
                          sizeof nsh->context);
2371
46.5k
    } else {
2372
46.5k
        switch (nsh->mdtype) {
2373
18.2k
        case NSH_M_TYPE1:
2374
18.2k
            nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD1, nsh->context,
2375
18.2k
                              sizeof nsh->context);
2376
18.2k
            break;
2377
2.50k
        case NSH_M_TYPE2:
2378
2.50k
            if (metadata && md_size > 0) {
2379
203
                nl_msg_put_unspec(buf, OVS_NSH_KEY_ATTR_MD2, metadata,
2380
203
                                  md_size);
2381
203
            }
2382
2.50k
            break;
2383
25.7k
        default:
2384
            /* No match support for other MD formats yet. */
2385
25.7k
            break;
2386
46.5k
        }
2387
46.5k
    }
2388
91.8k
    nl_msg_end_nested(buf, nsh_key_ofs);
2389
91.8k
}
2390
2391
2392
static int
2393
parse_odp_push_nsh_action(const char *s, struct ofpbuf *actions)
2394
1.35k
{
2395
1.35k
    int n = 0;
2396
1.35k
    int ret = 0;
2397
1.35k
    uint32_t spi = 0;
2398
1.35k
    uint8_t si = 255;
2399
1.35k
    uint32_t cd;
2400
1.35k
    struct ovs_key_nsh nsh;
2401
1.35k
    uint8_t metadata[NSH_CTX_HDRS_MAX_LEN];
2402
1.35k
    uint8_t md_size = 0;
2403
2404
1.35k
    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
1.35k
    nsh.flags = 0;
2411
1.35k
    nsh.ttl = 63;
2412
1.35k
    nsh.mdtype = NSH_M_TYPE1;
2413
1.35k
    nsh.np = NSH_P_ETHERNET;
2414
1.35k
    nsh.path_hdr = nsh_spi_si_to_path_hdr(0, 255);
2415
1.35k
    memset(nsh.context, 0, NSH_M_TYPE1_MDLEN);
2416
2417
4.82k
    for (;;) {
2418
4.82k
        n += strspn(s + n, delimiters);
2419
4.82k
        if (s[n] == ')') {
2420
1.20k
            break;
2421
1.20k
        }
2422
2423
3.62k
        if (ovs_scan_len(s, &n, "flags=%"SCNi8, &nsh.flags)) {
2424
194
            continue;
2425
194
        }
2426
3.42k
        if (ovs_scan_len(s, &n, "ttl=%"SCNi8, &nsh.ttl)) {
2427
195
            continue;
2428
195
        }
2429
3.23k
        if (ovs_scan_len(s, &n, "mdtype=%"SCNi8, &nsh.mdtype)) {
2430
803
            switch (nsh.mdtype) {
2431
197
            case NSH_M_TYPE1:
2432
197
                /* This is the default format. */;
2433
197
                break;
2434
605
            case NSH_M_TYPE2:
2435
                /* Length will be updated later. */
2436
605
                md_size = 0;
2437
605
                break;
2438
1
            default:
2439
1
                ret = -EINVAL;
2440
1
                goto out;
2441
803
            }
2442
802
            continue;
2443
803
        }
2444
2.43k
        if (ovs_scan_len(s, &n, "np=%"SCNi8, &nsh.np)) {
2445
264
            continue;
2446
264
        }
2447
2.16k
        if (ovs_scan_len(s, &n, "spi=0x%"SCNx32, &spi)) {
2448
230
            continue;
2449
230
        }
2450
1.93k
        if (ovs_scan_len(s, &n, "si=%"SCNi8, &si)) {
2451
239
            continue;
2452
239
        }
2453
1.69k
        if (nsh.mdtype == NSH_M_TYPE1) {
2454
889
            if (ovs_scan_len(s, &n, "c1=0x%"SCNx32, &cd)) {
2455
194
                nsh.context[0] = htonl(cd);
2456
194
                continue;
2457
194
            }
2458
695
            if (ovs_scan_len(s, &n, "c2=0x%"SCNx32, &cd)) {
2459
198
                nsh.context[1] = htonl(cd);
2460
198
                continue;
2461
198
            }
2462
497
            if (ovs_scan_len(s, &n, "c3=0x%"SCNx32, &cd)) {
2463
195
                nsh.context[2] = htonl(cd);
2464
195
                continue;
2465
195
            }
2466
302
            if (ovs_scan_len(s, &n, "c4=0x%"SCNx32, &cd)) {
2467
209
                nsh.context[3] = htonl(cd);
2468
209
                continue;
2469
209
            }
2470
302
        }
2471
808
        else if (nsh.mdtype == NSH_M_TYPE2) {
2472
808
            struct ofpbuf b;
2473
808
            char buf[512];
2474
808
            size_t mdlen, padding;
2475
808
            if (ovs_scan_len(s, &n, "md2=0x%511[0-9a-fA-F]", buf)
2476
758
                && n/2 <= sizeof metadata) {
2477
750
                ofpbuf_use_stub(&b, metadata, sizeof metadata);
2478
750
                ofpbuf_put_hex(&b, buf, &mdlen);
2479
                /* Pad metadata to 4 bytes. */
2480
750
                padding = PAD_SIZE(mdlen, 4);
2481
750
                if (padding > 0) {
2482
351
                    ofpbuf_put_zeros(&b, padding);
2483
351
                }
2484
750
                md_size = mdlen + padding;
2485
750
                ofpbuf_uninit(&b);
2486
750
                continue;
2487
750
            }
2488
808
        }
2489
2490
151
        ret = -EINVAL;
2491
151
        goto out;
2492
1.69k
    }
2493
1.35k
out:
2494
1.35k
    if (ret >= 0) {
2495
1.20k
        nsh.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
2496
1.20k
        size_t offset = nl_msg_start_nested(actions, OVS_ACTION_ATTR_PUSH_NSH);
2497
1.20k
        nsh_key_to_attr(actions, &nsh, metadata, md_size, false);
2498
1.20k
        nl_msg_end_nested(actions, offset);
2499
1.20k
        ret = n;
2500
1.20k
    }
2501
1.35k
    return ret;
2502
1.35k
}
2503
2504
static int
2505
parse_odp_psample_action(const char *s, struct ofpbuf *actions)
2506
4
{
2507
4
    char buf[2 * OVS_PSAMPLE_COOKIE_MAX_SIZE + 1];
2508
4
    uint8_t cookie[OVS_PSAMPLE_COOKIE_MAX_SIZE];
2509
4
    bool has_group = false;
2510
4
    size_t cookie_len = 0;
2511
4
    uint32_t group;
2512
4
    int n = 0;
2513
2514
4
    if (!ovs_scan_len(s, &n, "psample(")) {
2515
0
        return -EINVAL;
2516
0
    }
2517
2518
4
    while (s[n] != ')') {
2519
3
        n += strspn(s + n, delimiters);
2520
2521
3
        if (!has_group && ovs_scan_len(s, &n, "group=%"SCNi32, &group)) {
2522
0
            has_group = true;
2523
0
            continue;
2524
0
        }
2525
2526
3
        if (!cookie_len &&
2527
3
            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
3
        return -EINVAL;
2536
3
    }
2537
1
    n++;
2538
2539
1
    if (!has_group) {
2540
1
        return -EINVAL;
2541
1
    }
2542
2543
0
    odp_put_psample_action(actions, group, cookie_len ? cookie : NULL,
2544
0
                           cookie_len);
2545
0
    return n;
2546
1
}
2547
2548
static int
2549
parse_action_list(struct parse_odp_context *context, const char *s,
2550
                  struct ofpbuf *actions)
2551
1.82k
{
2552
1.82k
    int n = 0;
2553
2554
58.5k
    for (;;) {
2555
58.5k
        int retval;
2556
2557
58.5k
        n += strspn(s + n, delimiters);
2558
58.5k
        if (s[n] == ')') {
2559
1.53k
            break;
2560
1.53k
        }
2561
57.0k
        retval = parse_odp_action(context, s + n, actions);
2562
57.0k
        if (retval < 0) {
2563
289
            return retval;
2564
56.7k
        } else if (nl_attr_oversized(actions->size - NLA_HDRLEN)) {
2565
3
            return -E2BIG;
2566
3
        }
2567
56.7k
        n += retval;
2568
56.7k
    }
2569
2570
1.53k
    return n;
2571
1.82k
}
2572
2573
2574
static int
2575
parse_odp_action(struct parse_odp_context *context, const char *s,
2576
                 struct ofpbuf *actions)
2577
88.4k
{
2578
88.4k
    int retval;
2579
2580
88.4k
    context->depth++;
2581
2582
88.4k
    if (context->depth == MAX_ODP_NESTED) {
2583
2
        retval = -EINVAL;
2584
88.4k
    } else {
2585
88.4k
        retval = parse_odp_action__(context, s, actions);
2586
88.4k
    }
2587
2588
88.4k
    context->depth--;
2589
2590
88.4k
    return retval;
2591
88.4k
}
2592
2593
2594
static int
2595
parse_odp_action__(struct parse_odp_context *context, const char *s,
2596
                   struct ofpbuf *actions)
2597
88.4k
{
2598
88.4k
    {
2599
88.4k
        uint32_t port;
2600
88.4k
        int n;
2601
2602
88.4k
        if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
2603
36.9k
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
2604
36.9k
            return n;
2605
36.9k
        }
2606
88.4k
    }
2607
2608
51.4k
    {
2609
51.4k
        uint32_t bond_id;
2610
51.4k
        int n;
2611
2612
51.4k
        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
51.4k
    }
2617
2618
51.4k
    {
2619
51.4k
        uint32_t max_len;
2620
51.4k
        int n;
2621
2622
51.4k
        if (ovs_scan(s, "trunc(%"SCNi32")%n", &max_len, &n)) {
2623
194
            struct ovs_action_trunc *trunc;
2624
2625
194
            trunc = nl_msg_put_unspec_uninit(actions,
2626
194
                     OVS_ACTION_ATTR_TRUNC, sizeof *trunc);
2627
194
            trunc->max_len = max_len;
2628
194
            return n;
2629
194
        }
2630
51.4k
    }
2631
2632
51.2k
    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
51.2k
    {
2644
51.2k
        uint32_t recirc_id;
2645
51.2k
        int n = -1;
2646
2647
51.2k
        if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
2648
250
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
2649
250
            return n;
2650
250
        }
2651
51.2k
    }
2652
2653
51.0k
    if (!strncmp(s, "userspace(", 10)) {
2654
3.94k
        return parse_odp_userspace_action(s, actions);
2655
3.94k
    }
2656
2657
47.0k
    if (!strncmp(s, "set(", 4)) {
2658
14.8k
        size_t start_ofs;
2659
14.8k
        int retval;
2660
14.8k
        struct nlattr mask[1024 / sizeof(struct nlattr)];
2661
14.8k
        struct ofpbuf maskbuf = OFPBUF_STUB_INITIALIZER(mask);
2662
14.8k
        struct nlattr *nested, *key;
2663
14.8k
        size_t size;
2664
2665
14.8k
        start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
2666
14.8k
        retval = parse_odp_key_mask_attr(context, s + 4, actions, &maskbuf);
2667
14.8k
        if (retval < 0) {
2668
841
            ofpbuf_uninit(&maskbuf);
2669
841
            return retval;
2670
841
        }
2671
14.0k
        if (s[retval + 4] != ')') {
2672
49
            ofpbuf_uninit(&maskbuf);
2673
49
            return -EINVAL;
2674
49
        }
2675
2676
13.9k
        nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2677
13.9k
        key = nested + 1;
2678
2679
13.9k
        size = nl_attr_get_size(mask);
2680
13.9k
        if (size == nl_attr_get_size(key)) {
2681
            /* Change to masked set action if not fully masked. */
2682
13.3k
            if (!is_all_ones(mask + 1, size)) {
2683
                /* Remove padding of eariler key payload  */
2684
12.5k
                actions->size -= NLA_ALIGN(key->nla_len) - key->nla_len;
2685
2686
                /* Put mask payload right after key payload */
2687
12.5k
                key->nla_len += size;
2688
12.5k
                ofpbuf_put(actions, mask + 1, size);
2689
2690
                /* 'actions' may have been reallocated by ofpbuf_put(). */
2691
12.5k
                nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
2692
12.5k
                nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
2693
2694
12.5k
                key = nested + 1;
2695
                /* Add new padding as needed */
2696
12.5k
                ofpbuf_put_zeros(actions, NLA_ALIGN(key->nla_len) -
2697
12.5k
                                          key->nla_len);
2698
12.5k
            }
2699
13.3k
        }
2700
13.9k
        ofpbuf_uninit(&maskbuf);
2701
2702
13.9k
        nl_msg_end_nested(actions, start_ofs);
2703
13.9k
        return retval + 5;
2704
14.0k
    }
2705
2706
32.2k
    {
2707
32.2k
        struct ovs_action_push_vlan push;
2708
32.2k
        int tpid = ETH_TYPE_VLAN;
2709
32.2k
        int vid, pcp;
2710
32.2k
        int cfi = 1;
2711
32.2k
        int n = -1;
2712
2713
32.2k
        if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
2714
31.9k
            || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2715
31.9k
                        &vid, &pcp, &cfi, &n)
2716
31.5k
            || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2717
31.5k
                        &tpid, &vid, &pcp, &n)
2718
31.1k
            || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2719
31.1k
                        &tpid, &vid, &pcp, &cfi, &n)) {
2720
1.39k
            if ((vid & ~(VLAN_VID_MASK >> VLAN_VID_SHIFT)) != 0
2721
1.36k
                || (pcp & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) != 0) {
2722
75
                return -EINVAL;
2723
75
            }
2724
1.32k
            push.vlan_tpid = htons(tpid);
2725
1.32k
            push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
2726
1.32k
                                  | (pcp << VLAN_PCP_SHIFT)
2727
1.32k
                                  | (cfi ? VLAN_CFI : 0));
2728
1.32k
            nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
2729
1.32k
                              &push, sizeof push);
2730
2731
1.32k
            return n;
2732
1.39k
        }
2733
32.2k
    }
2734
2735
30.8k
    if (!strncmp(s, "pop_vlan", 8)) {
2736
607
        nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
2737
607
        return 8;
2738
607
    }
2739
2740
30.2k
    {
2741
30.2k
        unsigned long long int meter_id;
2742
30.2k
        int n = -1;
2743
2744
30.2k
        if (sscanf(s, "meter(%lli)%n", &meter_id, &n) > 0 && n > 0) {
2745
204
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_METER, meter_id);
2746
204
            return n;
2747
204
        }
2748
30.2k
    }
2749
2750
30.0k
    {
2751
30.0k
        double percentage;
2752
30.0k
        int n = -1;
2753
2754
30.0k
        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
30.0k
    }
2781
2782
30.0k
    {
2783
30.0k
        if (!strncmp(s, "clone(", 6)) {
2784
1.82k
            size_t actions_ofs;
2785
1.82k
            int n = 6;
2786
2787
1.82k
            actions_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_CLONE);
2788
1.82k
            int retval = parse_action_list(context, s + n, actions);
2789
1.82k
            if (retval < 0) {
2790
292
                return retval;
2791
292
            }
2792
1.53k
            n += retval;
2793
1.53k
            nl_msg_end_nested(actions, actions_ofs);
2794
1.53k
            return n + 1;
2795
1.82k
        }
2796
30.0k
    }
2797
2798
28.1k
    {
2799
28.1k
        if (!strncmp(s, "push_nsh(", 9)) {
2800
1.35k
            int retval = parse_odp_push_nsh_action(s, actions);
2801
1.35k
            if (retval < 0) {
2802
152
                return retval;
2803
152
            }
2804
1.20k
            return retval + 1;
2805
1.35k
        }
2806
28.1k
    }
2807
2808
26.8k
    {
2809
26.8k
        int n;
2810
26.8k
        if (ovs_scan(s, "pop_nsh()%n", &n)) {
2811
1.56k
            nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_NSH);
2812
1.56k
            return n;
2813
1.56k
        }
2814
26.8k
    }
2815
2816
25.2k
    {
2817
25.2k
        uint32_t port;
2818
25.2k
        int n;
2819
2820
25.2k
        if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
2821
409
            nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
2822
409
            return n;
2823
409
        }
2824
25.2k
    }
2825
2826
24.8k
    {
2827
24.8k
        if (!strncmp(s, "ct_clear", 8)) {
2828
678
            nl_msg_put_flag(actions, OVS_ACTION_ATTR_CT_CLEAR);
2829
678
            return 8;
2830
678
        }
2831
24.8k
    }
2832
2833
24.1k
    {
2834
24.1k
        uint16_t pkt_len;
2835
24.1k
        int n = -1;
2836
24.1k
        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
24.1k
    }
2878
2879
24.1k
    {
2880
24.1k
        int retval;
2881
2882
24.1k
        retval = parse_conntrack_action(s, actions);
2883
24.1k
        if (retval) {
2884
13.3k
            return retval;
2885
13.3k
        }
2886
24.1k
    }
2887
10.8k
    {
2888
10.8k
        struct ovs_action_add_mpls mpls;
2889
10.8k
        uint8_t ttl, tc, bos;
2890
10.8k
        uint16_t eth_type;
2891
10.8k
        uint32_t lse;
2892
10.8k
        int n = -1;
2893
2894
10.8k
        if (ovs_scan(s, "add_mpls(label=%"SCNi32",tc=%"SCNd8",ttl=%"SCNd8","
2895
10.8k
                                 "bos=%"SCNd8",eth_type=0x%"SCNx16")%n",
2896
10.8k
                     &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
10.8k
    }
2908
2909
10.8k
    if (!strncmp(s, "psample(", 8)) {
2910
4
        return parse_odp_psample_action(s, actions);
2911
4
    }
2912
2913
10.8k
    {
2914
10.8k
        struct ovs_action_push_tnl data;
2915
10.8k
        int n;
2916
2917
10.8k
        n = ovs_parse_tnl_push(s, &data);
2918
10.8k
        if (n > 0) {
2919
0
            odp_put_tnl_push_action(actions, &data);
2920
0
            return n;
2921
10.8k
        } else if (n < 0) {
2922
10.8k
            return n;
2923
10.8k
        }
2924
10.8k
    }
2925
2926
0
    return -EINVAL;
2927
10.8k
}
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
17.3k
{
2938
17.3k
    size_t old_size;
2939
2940
17.3k
    if (!strcasecmp(s, "drop")) {
2941
1
        nl_msg_put_u32(actions, OVS_ACTION_ATTR_DROP, XLATE_OK);
2942
1
        return 0;
2943
1
    }
2944
2945
17.3k
    struct parse_odp_context context = (struct parse_odp_context) {
2946
17.3k
        .port_names = port_names,
2947
17.3k
    };
2948
2949
17.3k
    old_size = actions->size;
2950
34.5k
    for (;;) {
2951
34.5k
        int retval;
2952
2953
34.5k
        s += strspn(s, delimiters);
2954
34.5k
        if (!*s) {
2955
3.13k
            return 0;
2956
3.13k
        }
2957
2958
31.4k
        retval = parse_odp_action(&context, s, actions);
2959
2960
31.4k
        if (retval >= 0 && nl_attr_oversized(actions->size - NLA_HDRLEN)) {
2961
3
            retval = -E2BIG;
2962
3
        }
2963
2964
31.4k
        if (retval < 0 || !strchr(delimiters, s[retval])) {
2965
14.2k
            actions->size = old_size;
2966
14.2k
            return -retval;
2967
14.2k
        }
2968
17.1k
        s += retval;
2969
17.1k
    }
2970
2971
0
    return 0;
2972
17.3k
}
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
1.06M
{
3043
1.06M
    if (type > max_type) {
3044
0
        return ATTR_LEN_INVALID;
3045
0
    }
3046
3047
1.06M
    return tbl[type].len;
3048
1.06M
}
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
53.5k
{
3071
53.5k
    switch (type) {
3072
32.2k
    case OVS_FRAG_TYPE_NONE:
3073
32.2k
        return "no";
3074
54
    case OVS_FRAG_TYPE_FIRST:
3075
54
        return "first";
3076
29
    case OVS_FRAG_TYPE_LATER:
3077
29
        return "later";
3078
0
    case __OVS_FRAG_TYPE_MAX:
3079
21.1k
    default:
3080
21.1k
        return "<error>";
3081
53.5k
    }
3082
53.5k
}
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
1.01k
{
3088
1.01k
    unsigned int left;
3089
1.01k
    const struct nlattr *a;
3090
1.01k
    bool unknown = false;
3091
1.01k
    uint8_t flags = 0;
3092
1.01k
    uint8_t ttl = 63;
3093
1.01k
    size_t mdlen = 0;
3094
1.01k
    bool has_md1 = false;
3095
1.01k
    bool has_md2 = false;
3096
3097
1.01k
    memset(nsh_hdr, 0, size);
3098
3099
1.82k
    NL_NESTED_FOR_EACH (a, left, attr) {
3100
1.82k
        uint16_t type = nl_attr_type(a);
3101
1.82k
        size_t len = nl_attr_get_size(a);
3102
1.82k
        int expected_len = odp_key_attr_len(ovs_nsh_key_attr_lens,
3103
1.82k
                                            OVS_NSH_KEY_ATTR_MAX, type);
3104
3105
1.82k
        if (len != expected_len && expected_len >= 0) {
3106
0
            return ODP_FIT_ERROR;
3107
0
        }
3108
3109
1.82k
        switch (type) {
3110
1.01k
        case OVS_NSH_KEY_ATTR_BASE: {
3111
1.01k
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
3112
1.01k
            nsh_hdr->next_proto = base->np;
3113
1.01k
            nsh_hdr->md_type = base->mdtype;
3114
1.01k
            put_16aligned_be32(&nsh_hdr->path_hdr, base->path_hdr);
3115
1.01k
            flags = base->flags;
3116
1.01k
            ttl = base->ttl;
3117
1.01k
            break;
3118
0
        }
3119
614
        case OVS_NSH_KEY_ATTR_MD1: {
3120
614
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
3121
614
            struct nsh_md1_ctx *md1_dst = &nsh_hdr->md1;
3122
614
            has_md1 = true;
3123
614
            mdlen = nl_attr_get_size(a);
3124
614
            if ((mdlen + NSH_BASE_HDR_LEN != NSH_M_TYPE1_LEN) ||
3125
614
                (mdlen + NSH_BASE_HDR_LEN > size)) {
3126
0
                return ODP_FIT_ERROR;
3127
0
            }
3128
614
            memcpy(md1_dst, md1, mdlen);
3129
614
            break;
3130
614
        }
3131
203
        case OVS_NSH_KEY_ATTR_MD2: {
3132
203
            struct nsh_md2_tlv *md2_dst = &nsh_hdr->md2;
3133
203
            const uint8_t *md2 = nl_attr_get(a);
3134
203
            has_md2 = true;
3135
203
            mdlen = nl_attr_get_size(a);
3136
203
            if (mdlen + NSH_BASE_HDR_LEN > size) {
3137
0
                return ODP_FIT_ERROR;
3138
0
            }
3139
203
            memcpy(md2_dst, md2, mdlen);
3140
203
            break;
3141
203
        }
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
1.82k
        }
3148
1.82k
    }
3149
3150
1.01k
    if (unknown) {
3151
0
        return ODP_FIT_TOO_MUCH;
3152
0
    }
3153
3154
1.01k
    if ((has_md1 && nsh_hdr->md_type != NSH_M_TYPE1)
3155
1.01k
        || (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
1.01k
    nsh_set_flags_ttl_len(nsh_hdr, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
3161
3162
1.01k
    return ODP_FIT_PERFECT;
3163
1.01k
}
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
2.25k
{
3176
2.25k
    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
2.25k
    } 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
2.25k
}
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
1.31k
{
3307
1.31k
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3308
1.31k
    unsigned int left;
3309
1.31k
    const struct nlattr *a;
3310
1.31k
    bool ttl = false;
3311
1.31k
    bool unknown = false;
3312
3313
12.0k
    NL_NESTED_FOR_EACH(a, left, attr) {
3314
12.0k
        uint16_t type = nl_attr_type(a);
3315
12.0k
        size_t len = nl_attr_get_size(a);
3316
12.0k
        int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
3317
12.0k
                                            OVS_TUNNEL_KEY_ATTR_MAX, type);
3318
3319
12.0k
        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
12.0k
        switch (type) {
3327
665
        case OVS_TUNNEL_KEY_ATTR_ID:
3328
665
            tun->tun_id = nl_attr_get_be64(a);
3329
665
            tun->flags |= FLOW_TNL_F_KEY;
3330
665
            break;
3331
292
        case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
3332
292
            tun->ip_src = nl_attr_get_be32(a);
3333
292
            break;
3334
499
        case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
3335
499
            tun->ip_dst = nl_attr_get_be32(a);
3336
499
            break;
3337
443
        case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
3338
443
            tun->ipv6_src = nl_attr_get_in6_addr(a);
3339
443
            break;
3340
488
        case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
3341
488
            tun->ipv6_dst = nl_attr_get_in6_addr(a);
3342
488
            break;
3343
689
        case OVS_TUNNEL_KEY_ATTR_TOS:
3344
689
            tun->ip_tos = nl_attr_get_u8(a);
3345
689
            break;
3346
1.23k
        case OVS_TUNNEL_KEY_ATTR_TTL:
3347
1.23k
            tun->ip_ttl = nl_attr_get_u8(a);
3348
1.23k
            ttl = true;
3349
1.23k
            break;
3350
493
        case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
3351
493
            tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
3352
493
            break;
3353
267
        case OVS_TUNNEL_KEY_ATTR_CSUM:
3354
267
            tun->flags |= FLOW_TNL_F_CSUM;
3355
267
            break;
3356
598
        case OVS_TUNNEL_KEY_ATTR_TP_SRC:
3357
598
            tun->tp_src = nl_attr_get_be16(a);
3358
598
            break;
3359
535
        case OVS_TUNNEL_KEY_ATTR_TP_DST:
3360
535
            tun->tp_dst = nl_attr_get_be16(a);
3361
535
            break;
3362
496
        case OVS_TUNNEL_KEY_ATTR_OAM:
3363
496
            tun->flags |= FLOW_TNL_F_OAM;
3364
496
            break;
3365
513
        case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
3366
513
            if (odp_vxlan_tun_opts_from_attr(a, &tun->gbp_id,
3367
513
                                             &tun->gbp_flags,
3368
513
                                             NULL)) {
3369
0
                odp_parse_error(&rl, errorp, "error parsing VXLAN options");
3370
0
                return ODP_FIT_ERROR;
3371
0
            }
3372
513
            break;
3373
513
        }
3374
3.12k
        case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
3375
3.12k
            tun_metadata_from_geneve_nlattr(a, is_mask, tun);
3376
3.12k
            break;
3377
1.03k
        case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: {
3378
1.03k
            const struct erspan_metadata *opts = nl_attr_get(a);
3379
3380
1.03k
            tun->erspan_ver = opts->version;
3381
1.03k
            if (tun->erspan_ver == 1) {
3382
305
                tun->erspan_idx = ntohl(opts->u.index);
3383
729
            } else if (tun->erspan_ver == 2) {
3384
534
                tun->erspan_dir = opts->u.md2.dir;
3385
534
                tun->erspan_hwid = get_hwid(&opts->u.md2);
3386
534
            } else {
3387
195
                VLOG_WARN("%s invalid erspan version\n", __func__);
3388
195
            }
3389
1.03k
            break;
3390
513
        }
3391
726
        case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS: {
3392
726
            const struct gtpu_metadata *opts = nl_attr_get(a);
3393
3394
726
            tun->gtpu_flags = opts->flags;
3395
726
            tun->gtpu_msgtype = opts->msgtype;
3396
726
            break;
3397
513
        }
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
12.0k
        }
3405
12.0k
    }
3406
3407
1.31k
    if (!ttl) {
3408
604
        odp_parse_error(&rl, errorp, "tunnel options missing TTL");
3409
604
        return ODP_FIT_ERROR;
3410
604
    }
3411
714
    if (unknown) {
3412
0
        return ODP_FIT_TOO_MUCH;
3413
0
    }
3414
714
    return ODP_FIT_PERFECT;
3415
714
}
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
670
{
3437
670
    size_t tun_key_ofs;
3438
3439
670
    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
670
    if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
3443
64
        nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
3444
64
    }
3445
670
    if (tun_key->ip_src) {
3446
79
        nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
3447
79
    }
3448
670
    if (tun_key->ip_dst) {
3449
143
        nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
3450
143
    }
3451
670
    if (ipv6_addr_is_set(&tun_key->ipv6_src)) {
3452
227
        nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_SRC, &tun_key->ipv6_src);
3453
227
    }
3454
670
    if (ipv6_addr_is_set(&tun_key->ipv6_dst)) {
3455
228
        nl_msg_put_in6_addr(a, OVS_TUNNEL_KEY_ATTR_IPV6_DST, &tun_key->ipv6_dst);
3456
228
    }
3457
670
    if (tun_key->ip_tos) {
3458
5
        nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
3459
5
    }
3460
670
    nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
3461
670
    if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
3462
1
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
3463
1
    }
3464
670
    if (tun_key->flags & FLOW_TNL_F_CSUM) {
3465
1
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
3466
1
    }
3467
670
    if (tun_key->tp_src) {
3468
20
        nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
3469
20
    }
3470
670
    if (tun_key->tp_dst) {
3471
2
        nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
3472
2
    }
3473
670
    if (tun_key->flags & FLOW_TNL_F_OAM) {
3474
1
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
3475
1
    }
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
670
    if ((!tnl_type || !strcmp(tnl_type, "vxlan")) &&
3483
670
        (tun_key->gbp_flags || tun_key->gbp_id)) {
3484
19
        size_t vxlan_opts_ofs;
3485
19
        uint32_t gbp_raw;
3486
3487
19
        vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
3488
19
        gbp_raw = odp_encode_gbp_raw(tun_key->gbp_flags, tun_key->gbp_id);
3489
19
        nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, gbp_raw);
3490
19
        nl_msg_end_nested(a, vxlan_opts_ofs);
3491
19
    }
3492
3493
670
    if (!tnl_type || !strcmp(tnl_type, "geneve")) {
3494
670
        tun_metadata_to_geneve_nlattr(tun_key, tun_flow_key, key_buf, a);
3495
670
    }
3496
3497
670
    if ((!tnl_type || !strcmp(tnl_type, "erspan") ||
3498
0
        !strcmp(tnl_type, "ip6erspan")) &&
3499
670
        (tun_key->erspan_ver == 1 || tun_key->erspan_ver == 2)) {
3500
3
        struct erspan_metadata *opts;
3501
3502
3
        opts = nl_msg_put_unspec_zero(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
3503
3
                                      sizeof *opts);
3504
3
        opts->version = tun_key->erspan_ver;
3505
3
        if (opts->version == 1) {
3506
1
            opts->u.index = htonl(tun_key->erspan_idx);
3507
2
        } else {
3508
2
            opts->u.md2.dir = tun_key->erspan_dir;
3509
2
            set_hwid(&opts->u.md2, tun_key->erspan_hwid);
3510
2
        }
3511
3
    }
3512
3513
670
    if ((!tnl_type || !strcmp(tnl_type, "gtpu")) &&
3514
670
        (tun_key->gtpu_flags && tun_key->gtpu_msgtype)) {
3515
2
        struct gtpu_metadata opts;
3516
3517
2
        opts.flags = tun_key->gtpu_flags;
3518
2
        opts.msgtype = tun_key->gtpu_msgtype;
3519
2
        nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS,
3520
2
                          &opts, sizeof(opts));
3521
2
    }
3522
670
    nl_msg_end_nested(a, tun_key_ofs);
3523
670
}
3524
3525
static bool
3526
odp_mask_is_constant__(enum ovs_key_attr attr, const void *mask, size_t size,
3527
                       int constant)
3528
168k
{
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
168k
    ovs_be16 be16 = (OVS_FORCE ovs_be16) constant;
3532
168k
    uint32_t u32 = constant;
3533
168k
    uint8_t u8 = constant;
3534
168k
    const struct in6_addr *in6 = constant ? &in6addr_exact : &in6addr_any;
3535
3536
168k
    switch (attr) {
3537
0
    case OVS_KEY_ATTR_UNSPEC:
3538
56.6k
    case OVS_KEY_ATTR_ENCAP:
3539
56.6k
    case OVS_KEY_ATTR_TUNNEL_INFO:
3540
56.6k
    case __OVS_KEY_ATTR_MAX:
3541
56.6k
    default:
3542
56.6k
        return false;
3543
3544
472
    case OVS_KEY_ATTR_PRIORITY:
3545
1.51k
    case OVS_KEY_ATTR_IN_PORT:
3546
3.49k
    case OVS_KEY_ATTR_ETHERNET:
3547
6.23k
    case OVS_KEY_ATTR_VLAN:
3548
8.65k
    case OVS_KEY_ATTR_ETHERTYPE:
3549
11.3k
    case OVS_KEY_ATTR_IPV4:
3550
13.1k
    case OVS_KEY_ATTR_TCP:
3551
14.1k
    case OVS_KEY_ATTR_UDP:
3552
17.0k
    case OVS_KEY_ATTR_ICMP:
3553
19.0k
    case OVS_KEY_ATTR_ICMPV6:
3554
38.7k
    case OVS_KEY_ATTR_ND:
3555
40.2k
    case OVS_KEY_ATTR_ND_EXTENSIONS:
3556
41.7k
    case OVS_KEY_ATTR_SKB_MARK:
3557
48.1k
    case OVS_KEY_ATTR_TUNNEL:
3558
48.4k
    case OVS_KEY_ATTR_SCTP:
3559
49.4k
    case OVS_KEY_ATTR_DP_HASH:
3560
49.8k
    case OVS_KEY_ATTR_RECIRC_ID:
3561
51.5k
    case OVS_KEY_ATTR_MPLS:
3562
52.8k
    case OVS_KEY_ATTR_CT_STATE:
3563
58.2k
    case OVS_KEY_ATTR_CT_ZONE:
3564
61.0k
    case OVS_KEY_ATTR_CT_MARK:
3565
63.9k
    case OVS_KEY_ATTR_CT_LABELS:
3566
66.0k
    case OVS_KEY_ATTR_PACKET_TYPE:
3567
89.3k
    case OVS_KEY_ATTR_NSH:
3568
89.3k
        return is_all_byte(mask, size, u8);
3569
3570
1.02k
    case OVS_KEY_ATTR_TCP_FLAGS:
3571
1.02k
        return TCP_FLAGS(*(ovs_be16 *) mask) == TCP_FLAGS(be16);
3572
3573
12.6k
    case OVS_KEY_ATTR_IPV6: {
3574
12.6k
        const struct ovs_key_ipv6 *ipv6_mask = mask;
3575
12.6k
        return ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
3576
12.6k
                == htonl(IPV6_LABEL_MASK & u32)
3577
6.21k
                && ipv6_mask->ipv6_proto == u8
3578
5.19k
                && ipv6_mask->ipv6_tclass == u8
3579
4.73k
                && ipv6_mask->ipv6_hlimit == u8
3580
4.31k
                && ipv6_mask->ipv6_frag == u8
3581
4.05k
                && ipv6_addr_equals(&ipv6_mask->ipv6_src, in6)
3582
2.72k
                && ipv6_addr_equals(&ipv6_mask->ipv6_dst, in6));
3583
66.0k
    }
3584
3585
3.30k
    case OVS_KEY_ATTR_ARP:
3586
3.30k
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_arp, arp_tha), u8);
3587
3588
3.01k
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
3589
3.01k
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4,
3590
3.01k
                                             ipv4_proto), u8);
3591
3592
2.14k
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
3593
2.14k
        return is_all_byte(mask, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6,
3594
2.14k
                                             ipv6_proto), u8);
3595
168k
    }
3596
168k
}
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
83.8k
{
3607
83.8k
    return odp_mask_is_constant__(nl_attr_type(ma),
3608
83.8k
                                  nl_attr_get(ma), nl_attr_get_size(ma), 0);
3609
83.8k
}
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
84.2k
{
3621
84.2k
    return odp_mask_is_constant__(attr, mask, size, -1);
3622
84.2k
}
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
84.2k
{
3628
84.2k
    enum ovs_key_attr attr = nl_attr_type(ma);
3629
84.2k
    return odp_mask_is_exact(attr, nl_attr_get(ma), nl_attr_get_size(ma));
3630
84.2k
}
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
29.5k
{
3648
29.5k
    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
29.5k
    return NULL;
3659
29.5k
}
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
29.5k
{
3676
29.5k
    const char *name = odp_portno_names_get(portno_names, port_no);
3677
29.5k
    if (name) {
3678
0
        ds_put_cstr(s, name);
3679
29.5k
    } else {
3680
29.5k
        ds_put_format(s, "%"PRIu32, port_no);
3681
29.5k
    }
3682
29.5k
}
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
371k
{
3690
371k
    bool mask_empty = mask && eth_addr_is_zero(*mask);
3691
3692
371k
    if (verbose || !mask_empty) {
3693
362k
        bool mask_full = !mask || eth_mask_is_exact(*mask);
3694
3695
362k
        if (mask_full) {
3696
360k
            ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
3697
360k
        } else {
3698
2.46k
            ds_put_format(ds, "%s=", name);
3699
2.46k
            eth_format_masked(key, mask, ds);
3700
2.46k
            ds_put_char(ds, ',');
3701
2.46k
        }
3702
362k
    }
3703
371k
}
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
5.97k
{
3710
5.97k
    ovs_be64 mask = mask_ ? get_32aligned_be64(mask_) : htonll(0);
3711
3712
5.97k
    if (verbose || mask) {
3713
3.14k
        bool mask_full = !mask_ || mask == OVS_BE64_MAX;
3714
3715
3.14k
        ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
3716
3.14k
        if (!mask_full) { /* Partially masked. */
3717
318
            ds_put_format(ds, "/%#"PRIx64, ntohll(mask));
3718
318
        }
3719
3.14k
        ds_put_char(ds, ',');
3720
3.14k
    }
3721
5.97k
}
3722
3723
static void
3724
format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
3725
            const ovs_be32 *mask, bool verbose)
3726
118k
{
3727
118k
    bool mask_empty = mask && !*mask;
3728
3729
118k
    if (verbose || !mask_empty) {
3730
111k
        bool mask_full = !mask || *mask == OVS_BE32_MAX;
3731
3732
111k
        ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
3733
111k
        if (!mask_full) { /* Partially masked. */
3734
312
            ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
3735
312
        }
3736
111k
        ds_put_char(ds, ',');
3737
111k
    }
3738
118k
}
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
218k
{
3746
218k
    char buf[INET6_ADDRSTRLEN];
3747
218k
    bool mask_empty = mask && ipv6_mask_is_any(mask);
3748
3749
218k
    if (verbose || !mask_empty) {
3750
204k
        bool mask_full = !mask || ipv6_mask_is_exact(mask);
3751
3752
204k
        inet_ntop(AF_INET6, key, buf, sizeof buf);
3753
204k
        ds_put_format(ds, "%s=%s", name, buf);
3754
204k
        if (!mask_full) { /* Partially masked. */
3755
2.69k
            inet_ntop(AF_INET6, mask, buf, sizeof buf);
3756
2.69k
            ds_put_format(ds, "/%s", buf);
3757
2.69k
        }
3758
204k
        ds_put_char(ds, ',');
3759
204k
    }
3760
218k
}
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
18.5k
{
3766
18.5k
    bool mask_empty = mask && !*mask;
3767
3768
18.5k
    if (verbose || !mask_empty) {
3769
13.0k
        bool mask_full = !mask
3770
637
            || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
3771
3772
13.0k
        ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
3773
13.0k
        if (!mask_full) { /* Partially masked. */
3774
297
            ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
3775
297
        }
3776
13.0k
        ds_put_char(ds, ',');
3777
13.0k
    }
3778
18.5k
}
3779
3780
static void
3781
format_u8x(struct ds *ds, const char *name, uint8_t key,
3782
           const uint8_t *mask, bool verbose)
3783
214k
{
3784
214k
    bool mask_empty = mask && !*mask;
3785
3786
214k
    if (verbose || !mask_empty) {
3787
201k
        bool mask_full = !mask || *mask == UINT8_MAX;
3788
3789
201k
        ds_put_format(ds, "%s=%#"PRIx8, name, key);
3790
201k
        if (!mask_full) { /* Partially masked. */
3791
1.75k
            ds_put_format(ds, "/%#"PRIx8, *mask);
3792
1.75k
        }
3793
201k
        ds_put_char(ds, ',');
3794
201k
    }
3795
214k
}
3796
3797
static void
3798
format_u8u(struct ds *ds, const char *name, uint8_t key,
3799
           const uint8_t *mask, bool verbose)
3800
302k
{
3801
302k
    bool mask_empty = mask && !*mask;
3802
3803
302k
    if (verbose || !mask_empty) {
3804
282k
        bool mask_full = !mask || *mask == UINT8_MAX;
3805
3806
282k
        ds_put_format(ds, "%s=%"PRIu8, name, key);
3807
282k
        if (!mask_full) { /* Partially masked. */
3808
5.03k
            ds_put_format(ds, "/%#"PRIx8, *mask);
3809
5.03k
        }
3810
282k
        ds_put_char(ds, ',');
3811
282k
    }
3812
302k
}
3813
3814
static void
3815
format_be16(struct ds *ds, const char *name, ovs_be16 key,
3816
            const ovs_be16 *mask, bool verbose)
3817
51.4k
{
3818
51.4k
    bool mask_empty = mask && !*mask;
3819
3820
51.4k
    if (verbose || !mask_empty) {
3821
45.1k
        bool mask_full = !mask || *mask == OVS_BE16_MAX;
3822
3823
45.1k
        ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
3824
45.1k
        if (!mask_full) { /* Partially masked. */
3825
2.91k
            ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3826
2.91k
        }
3827
45.1k
        ds_put_char(ds, ',');
3828
45.1k
    }
3829
51.4k
}
3830
3831
static void
3832
format_be16x(struct ds *ds, const char *name, ovs_be16 key,
3833
             const ovs_be16 *mask, bool verbose)
3834
151k
{
3835
151k
    bool mask_empty = mask && !*mask;
3836
3837
151k
    if (verbose || !mask_empty) {
3838
147k
        bool mask_full = !mask || *mask == OVS_BE16_MAX;
3839
3840
147k
        ds_put_format(ds, "%s=%#"PRIx16, name, ntohs(key));
3841
147k
        if (!mask_full) { /* Partially masked. */
3842
592
            ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
3843
592
        }
3844
147k
        ds_put_char(ds, ',');
3845
147k
    }
3846
151k
}
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
12.0k
{
3852
12.0k
    bool mask_empty = mask && !*mask;
3853
3854
12.0k
    if (verbose || !mask_empty) {
3855
9.97k
        ds_put_cstr(ds, name);
3856
9.97k
        ds_put_char(ds, '(');
3857
9.97k
        if (mask) {
3858
1.22k
            format_flags_masked(ds, NULL, flow_tun_flag_to_string, key,
3859
1.22k
                                *mask & FLOW_TNL_F_MASK, FLOW_TNL_F_MASK);
3860
8.75k
        } else { /* Fully masked. */
3861
8.75k
            format_flags(ds, flow_tun_flag_to_string, key, '|');
3862
8.75k
        }
3863
9.97k
        ds_put_cstr(ds, "),");
3864
9.97k
    }
3865
12.0k
}
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
559k
{
3871
559k
    uint16_t type = nl_attr_type(a);
3872
559k
    int expected_len;
3873
3874
559k
    if (type > max_type) {
3875
        /* Unknown attribute, can't check the length. */
3876
0
        return true;
3877
0
    }
3878
3879
559k
    expected_len = odp_key_attr_len(tbl, max_type, type);
3880
3881
559k
    if (expected_len != ATTR_LEN_VARIABLE &&
3882
542k
        expected_len != ATTR_LEN_NESTED) {
3883
3884
462k
        bool bad_key_len = nl_attr_get_size(a) != expected_len;
3885
462k
        bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
3886
3887
462k
        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
462k
    }
3908
3909
559k
    return true;
3910
559k
}
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
2.58k
{
3930
2.58k
    unsigned int left;
3931
2.58k
    const struct nlattr *a;
3932
2.58k
    struct ofpbuf ofp;
3933
3934
2.58k
    ofpbuf_init(&ofp, 100);
3935
2.58k
    NL_NESTED_FOR_EACH(a, left, attr) {
3936
2.58k
        uint16_t type = nl_attr_type(a);
3937
2.58k
        const struct nlattr *ma = NULL;
3938
3939
2.58k
        if (mask_attr) {
3940
710
            ma = nl_attr_find__(nl_attr_get(mask_attr),
3941
710
                                nl_attr_get_size(mask_attr), type);
3942
710
            if (!ma) {
3943
194
                ma = generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens,
3944
194
                                                OVS_VXLAN_EXT_MAX,
3945
194
                                                &ofp, a);
3946
194
            }
3947
710
        }
3948
3949
2.58k
        if (!check_attr_len(ds, a, ma, ovs_vxlan_ext_attr_lens,
3950
2.58k
                            OVS_VXLAN_EXT_MAX, true)) {
3951
0
            continue;
3952
0
        }
3953
3954
2.58k
        switch (type) {
3955
2.58k
        case OVS_VXLAN_EXT_GBP: {
3956
2.58k
            uint32_t key = nl_attr_get_u32(a);
3957
2.58k
            ovs_be16 id, id_mask;
3958
2.58k
            uint8_t flags, flags_mask = 0;
3959
3960
2.58k
            odp_decode_gbp_raw(key, &id, &flags);
3961
2.58k
            if (ma) {
3962
710
                uint32_t mask = nl_attr_get_u32(ma);
3963
710
                odp_decode_gbp_raw(mask, &id_mask, &flags_mask);
3964
710
            }
3965
3966
2.58k
            ds_put_cstr(ds, "gbp(");
3967
2.58k
            format_be16(ds, "id", id, ma ? &id_mask : NULL, verbose);
3968
2.58k
            format_u8x(ds, "flags", flags, ma ? &flags_mask : NULL, verbose);
3969
2.58k
            ds_chomp(ds, ',');
3970
2.58k
            ds_put_cstr(ds, "),");
3971
2.58k
            break;
3972
0
        }
3973
3974
0
        default:
3975
0
            format_unknown_key(ds, a, ma);
3976
2.58k
        }
3977
2.58k
        ofpbuf_clear(&ofp);
3978
2.58k
    }
3979
3980
2.58k
    ds_chomp(ds, ',');
3981
2.58k
    ofpbuf_uninit(&ofp);
3982
2.58k
}
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
3.47k
{
3989
3.47k
    const struct erspan_metadata *opts, *mask;
3990
3.47k
    uint8_t ver, ver_ma, dir, dir_ma, hwid, hwid_ma;
3991
3992
3.47k
    opts = nl_attr_get(attr);
3993
3.47k
    mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
3994
3995
3.47k
    ver = (uint8_t)opts->version;
3996
3.47k
    if (mask) {
3997
1.23k
        ver_ma = (uint8_t)mask->version;
3998
1.23k
    }
3999
4000
3.47k
    format_u8u(ds, "ver", ver, mask ? &ver_ma : NULL, verbose);
4001
4002
3.47k
    if (opts->version == 1) {
4003
675
        if (mask) {
4004
383
            ds_put_format(ds, "idx=%#"PRIx32"/%#"PRIx32",",
4005
383
                          ntohl(opts->u.index),
4006
383
                          ntohl(mask->u.index));
4007
383
        } else {
4008
292
            ds_put_format(ds, "idx=%#"PRIx32",", ntohl(opts->u.index));
4009
292
        }
4010
2.79k
    } else if (opts->version == 2) {
4011
1.81k
        dir = opts->u.md2.dir;
4012
1.81k
        hwid = opts->u.md2.hwid;
4013
1.81k
        if (mask) {
4014
652
            dir_ma = mask->u.md2.dir;
4015
652
            hwid_ma = mask->u.md2.hwid;
4016
652
        }
4017
4018
1.81k
        format_u8u(ds, "dir", dir, mask ? &dir_ma : NULL, verbose);
4019
1.81k
        format_u8x(ds, "hwid", hwid, mask ? &hwid_ma : NULL, verbose);
4020
1.81k
    }
4021
3.47k
    ds_chomp(ds, ',');
4022
3.47k
}
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
1.41k
{
4029
1.41k
    const struct gtpu_metadata *opts, *mask;
4030
4031
1.41k
    opts = nl_attr_get(attr);
4032
1.41k
    mask = mask_attr ? nl_attr_get(mask_attr) : NULL;
4033
4034
1.41k
    format_u8x(ds, "flags", opts->flags, mask ? &mask->flags : NULL, verbose);
4035
1.41k
    format_u8u(ds, "msgtype", opts->msgtype, mask ? &mask->msgtype : NULL,
4036
1.41k
               verbose);
4037
1.41k
    ds_chomp(ds, ',');
4038
1.41k
}
4039
4040
1.31M
#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
8.51k
{
4047
153k
    while (opts_len > 0) {
4048
146k
        unsigned int len;
4049
146k
        uint8_t data_len, data_len_mask;
4050
4051
146k
        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
146k
        data_len = opt->length * 4;
4058
146k
        if (mask) {
4059
4.98k
            if (mask->length == 0x1f) {
4060
1.47k
                data_len_mask = UINT8_MAX;
4061
3.50k
            } else {
4062
3.50k
                data_len_mask = mask->length;
4063
3.50k
            }
4064
4.98k
        }
4065
146k
        len = sizeof *opt + data_len;
4066
146k
        if (len > opts_len) {
4067
1.53k
            ds_put_format(ds, "opt len %u greater than remaining %u",
4068
1.53k
                          len, opts_len);
4069
1.53k
            return;
4070
1.53k
        }
4071
4072
145k
        ds_put_char(ds, '{');
4073
145k
        format_be16x(ds, "class", opt->opt_class, MASK(mask, opt_class),
4074
145k
                    verbose);
4075
145k
        format_u8x(ds, "type", opt->type, MASK(mask, type), verbose);
4076
145k
        format_u8u(ds, "len", data_len, mask ? &data_len_mask : NULL, verbose);
4077
145k
        if (data_len &&
4078
7.48k
            (verbose || !mask || !is_all_zeros(mask + 1, data_len))) {
4079
7.27k
            ds_put_hex(ds, opt + 1, data_len);
4080
7.27k
            if (mask && !is_all_ones(mask + 1, data_len)) {
4081
388
                ds_put_char(ds, '/');
4082
388
                ds_put_hex(ds, mask + 1, data_len);
4083
388
            }
4084
137k
        } else {
4085
137k
            ds_chomp(ds, ',');
4086
137k
        }
4087
145k
        ds_put_char(ds, '}');
4088
4089
145k
        opt += len / sizeof(*opt);
4090
145k
        if (mask) {
4091
4.98k
            mask += len / sizeof(*opt);
4092
4.98k
        }
4093
145k
        opts_len -= len;
4094
145k
    };
4095
6.97k
}
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
9.91k
{
4102
9.91k
    int opts_len = nl_attr_get_size(attr);
4103
9.91k
    const struct geneve_opt *opt = nl_attr_get(attr);
4104
9.91k
    const struct geneve_opt *mask = mask_attr ?
4105
9.91k
                                    nl_attr_get(mask_attr) : NULL;
4106
4107
9.91k
    if (mask && nl_attr_get_size(attr) != nl_attr_get_size(mask_attr)) {
4108
1.39k
        ds_put_format(ds, "value len %"PRIuSIZE" different from mask len %"PRIuSIZE,
4109
1.39k
                      nl_attr_get_size(attr), nl_attr_get_size(mask_attr));
4110
1.39k
        return;
4111
1.39k
    }
4112
4113
8.51k
    format_geneve_opts(opt, mask, opts_len, ds, verbose);
4114
8.51k
}
4115
4116
static void
4117
format_odp_nsh_attr(const struct nlattr *attr, const struct nlattr *mask_attr,
4118
                    struct ds *ds)
4119
21.1k
{
4120
21.1k
    unsigned int left;
4121
21.1k
    const struct nlattr *a;
4122
21.1k
    struct ovs_key_nsh nsh;
4123
21.1k
    struct ovs_key_nsh nsh_mask;
4124
4125
21.1k
    memset(&nsh, 0, sizeof nsh);
4126
21.1k
    memset(&nsh_mask, 0xff, sizeof nsh_mask);
4127
4128
29.8k
    NL_NESTED_FOR_EACH (a, left, attr) {
4129
29.8k
        enum ovs_nsh_key_attr type = nl_attr_type(a);
4130
29.8k
        const struct nlattr *ma = NULL;
4131
4132
29.8k
        if (mask_attr) {
4133
25.3k
            ma = nl_attr_find__(nl_attr_get(mask_attr),
4134
25.3k
                                nl_attr_get_size(mask_attr), type);
4135
25.3k
        }
4136
4137
29.8k
        if (!check_attr_len(ds, a, ma, ovs_nsh_key_attr_lens,
4138
29.8k
                            OVS_NSH_KEY_ATTR_MAX, true)) {
4139
0
            continue;
4140
0
        }
4141
4142
29.8k
        switch (type) {
4143
0
        case OVS_NSH_KEY_ATTR_UNSPEC:
4144
0
            break;
4145
21.1k
        case OVS_NSH_KEY_ATTR_BASE: {
4146
21.1k
            const struct ovs_nsh_key_base *base = nl_attr_get(a);
4147
21.1k
            const struct ovs_nsh_key_base *base_mask
4148
21.1k
                = ma ? nl_attr_get(ma) : NULL;
4149
21.1k
            nsh.flags = base->flags;
4150
21.1k
            nsh.ttl = base->ttl;
4151
21.1k
            nsh.mdtype = base->mdtype;
4152
21.1k
            nsh.np = base->np;
4153
21.1k
            nsh.path_hdr = base->path_hdr;
4154
21.1k
            if (base_mask) {
4155
5.26k
                nsh_mask.flags = base_mask->flags;
4156
5.26k
                nsh_mask.ttl = base_mask->ttl;
4157
5.26k
                nsh_mask.mdtype = base_mask->mdtype;
4158
5.26k
                nsh_mask.np = base_mask->np;
4159
5.26k
                nsh_mask.path_hdr = base_mask->path_hdr;
4160
5.26k
            }
4161
21.1k
            break;
4162
0
        }
4163
8.65k
        case OVS_NSH_KEY_ATTR_MD1: {
4164
8.65k
            const struct ovs_nsh_key_md1 *md1 = nl_attr_get(a);
4165
8.65k
            const struct ovs_nsh_key_md1 *md1_mask
4166
8.65k
                = ma ? nl_attr_get(ma) : NULL;
4167
8.65k
            memcpy(nsh.context, md1->context, sizeof md1->context);
4168
8.65k
            if (md1_mask) {
4169
2.31k
                memcpy(nsh_mask.context, md1_mask->context,
4170
2.31k
                       sizeof md1_mask->context);
4171
2.31k
            }
4172
8.65k
            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
29.8k
        }
4180
29.8k
    }
4181
4182
21.1k
    if (mask_attr) {
4183
18.0k
        format_nsh_key_mask(ds, &nsh, &nsh_mask);
4184
18.0k
    } else {
4185
3.11k
        format_nsh_key(ds, &nsh);
4186
3.11k
    }
4187
21.1k
}
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
12.0k
{
4193
12.0k
    unsigned int left;
4194
12.0k
    const struct nlattr *a;
4195
12.0k
    uint16_t flags = 0;
4196
12.0k
    uint16_t mask_flags = 0;
4197
12.0k
    struct ofpbuf ofp;
4198
4199
12.0k
    ofpbuf_init(&ofp, 100);
4200
42.4k
    NL_NESTED_FOR_EACH(a, left, attr) {
4201
42.4k
        enum ovs_tunnel_key_attr type = nl_attr_type(a);
4202
42.4k
        const struct nlattr *ma = NULL;
4203
4204
42.4k
        if (mask_attr) {
4205
14.9k
            ma = nl_attr_find__(nl_attr_get(mask_attr),
4206
14.9k
                                nl_attr_get_size(mask_attr), type);
4207
14.9k
            if (!ma) {
4208
1.60k
                ma = generate_all_wildcard_mask(ovs_tun_key_attr_lens,
4209
1.60k
                                                OVS_TUNNEL_KEY_ATTR_MAX,
4210
1.60k
                                                &ofp, a);
4211
1.60k
            }
4212
14.9k
        }
4213
4214
42.4k
        if (!check_attr_len(ds, a, ma, ovs_tun_key_attr_lens,
4215
42.4k
                            OVS_TUNNEL_KEY_ATTR_MAX, true)) {
4216
0
            continue;
4217
0
        }
4218
4219
42.4k
        switch (type) {
4220
5.97k
        case OVS_TUNNEL_KEY_ATTR_ID:
4221
5.97k
            format_be64(ds, "tun_id", nl_attr_get_be64(a),
4222
5.97k
                        ma ? nl_attr_get(ma) : NULL, verbose);
4223
5.97k
            flags |= FLOW_TNL_F_KEY;
4224
5.97k
            if (ma) {
4225
1.41k
                mask_flags |= FLOW_TNL_F_KEY;
4226
1.41k
            }
4227
5.97k
            break;
4228
698
        case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
4229
698
            format_ipv4(ds, "src", nl_attr_get_be32(a),
4230
698
                        ma ? nl_attr_get(ma) : NULL, verbose);
4231
698
            break;
4232
3.76k
        case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
4233
3.76k
            format_ipv4(ds, "dst", nl_attr_get_be32(a),
4234
3.76k
                        ma ? nl_attr_get(ma) : NULL, verbose);
4235
3.76k
            break;
4236
1.70k
        case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
4237
1.70k
            struct in6_addr ipv6_src;
4238
1.70k
            ipv6_src = nl_attr_get_in6_addr(a);
4239
1.70k
            format_in6_addr(ds, "ipv6_src", &ipv6_src,
4240
1.70k
                            ma ? nl_attr_get(ma) : NULL, verbose);
4241
1.70k
            break;
4242
0
        }
4243
1.18k
        case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
4244
1.18k
            struct in6_addr ipv6_dst;
4245
1.18k
            ipv6_dst = nl_attr_get_in6_addr(a);
4246
1.18k
            format_in6_addr(ds, "ipv6_dst", &ipv6_dst,
4247
1.18k
                            ma ? nl_attr_get(ma) : NULL, verbose);
4248
1.18k
            break;
4249
0
        }
4250
2.61k
        case OVS_TUNNEL_KEY_ATTR_TOS:
4251
2.61k
            format_u8x(ds, "tos", nl_attr_get_u8(a),
4252
2.61k
                       ma ? nl_attr_get(ma) : NULL, verbose);
4253
2.61k
            break;
4254
3.61k
        case OVS_TUNNEL_KEY_ATTR_TTL:
4255
3.61k
            format_u8u(ds, "ttl", nl_attr_get_u8(a),
4256
3.61k
                       ma ? nl_attr_get(ma) : NULL, verbose);
4257
3.61k
            break;
4258
855
        case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
4259
855
            flags |= FLOW_TNL_F_DONT_FRAGMENT;
4260
855
            break;
4261
622
        case OVS_TUNNEL_KEY_ATTR_CSUM:
4262
622
            flags |= FLOW_TNL_F_CSUM;
4263
622
            break;
4264
1.76k
        case OVS_TUNNEL_KEY_ATTR_TP_SRC:
4265
1.76k
            format_be16(ds, "tp_src", nl_attr_get_be16(a),
4266
1.76k
                        ma ? nl_attr_get(ma) : NULL, verbose);
4267
1.76k
            break;
4268
1.37k
        case OVS_TUNNEL_KEY_ATTR_TP_DST:
4269
1.37k
            format_be16(ds, "tp_dst", nl_attr_get_be16(a),
4270
1.37k
                        ma ? nl_attr_get(ma) : NULL, verbose);
4271
1.37k
            break;
4272
873
        case OVS_TUNNEL_KEY_ATTR_OAM:
4273
873
            flags |= FLOW_TNL_F_OAM;
4274
873
            break;
4275
2.58k
        case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
4276
2.58k
            ds_put_cstr(ds, "vxlan(");
4277
2.58k
            format_odp_tun_vxlan_opt(a, ma, ds, verbose);
4278
2.58k
            ds_put_cstr(ds, "),");
4279
2.58k
            break;
4280
9.91k
        case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
4281
9.91k
            ds_put_cstr(ds, "geneve(");
4282
9.91k
            format_odp_tun_geneve(a, ma, ds, verbose);
4283
9.91k
            ds_put_cstr(ds, "),");
4284
9.91k
            break;
4285
0
        case OVS_TUNNEL_KEY_ATTR_PAD:
4286
0
            break;
4287
3.47k
        case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
4288
3.47k
            ds_put_cstr(ds, "erspan(");
4289
3.47k
            format_odp_tun_erspan_opt(a, ma, ds, verbose);
4290
3.47k
            ds_put_cstr(ds, "),");
4291
3.47k
            break;
4292
1.41k
        case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS:
4293
1.41k
            ds_put_cstr(ds, "gtpu(");
4294
1.41k
            format_odp_tun_gtpu_opt(a, ma, ds, verbose);
4295
1.41k
            ds_put_cstr(ds, "),");
4296
1.41k
            break;
4297
0
        case __OVS_TUNNEL_KEY_ATTR_MAX:
4298
0
        default:
4299
0
            format_unknown_key(ds, a, ma);
4300
42.4k
        }
4301
42.4k
        ofpbuf_clear(&ofp);
4302
42.4k
    }
4303
4304
    /* Flags can have a valid mask even if the attribute is not set, so
4305
     * we need to collect these separately. */
4306
12.0k
    if (mask_attr) {
4307
16.1k
        NL_NESTED_FOR_EACH(a, left, mask_attr) {
4308
16.1k
            switch (nl_attr_type(a)) {
4309
1.24k
            case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
4310
1.24k
                mask_flags |= FLOW_TNL_F_DONT_FRAGMENT;
4311
1.24k
                break;
4312
1.24k
            case OVS_TUNNEL_KEY_ATTR_CSUM:
4313
1.24k
                mask_flags |= FLOW_TNL_F_CSUM;
4314
1.24k
                break;
4315
1.24k
            case OVS_TUNNEL_KEY_ATTR_OAM:
4316
1.24k
                mask_flags |= FLOW_TNL_F_OAM;
4317
1.24k
                break;
4318
16.1k
            }
4319
16.1k
        }
4320
3.30k
    }
4321
4322
12.0k
    format_tun_flags(ds, "flags", flags, mask_attr ? &mask_flags : NULL,
4323
12.0k
                     verbose);
4324
12.0k
    ds_chomp(ds, ',');
4325
12.0k
    ofpbuf_uninit(&ofp);
4326
12.0k
}
4327
4328
static const char *
4329
odp_ct_state_to_string(uint32_t flag)
4330
38.0k
{
4331
38.0k
    switch (flag) {
4332
1.93k
    case OVS_CS_F_REPLY_DIR:
4333
1.93k
        return "rpl";
4334
1.74k
    case OVS_CS_F_TRACKED:
4335
1.74k
        return "trk";
4336
2.38k
    case OVS_CS_F_NEW:
4337
2.38k
        return "new";
4338
2.35k
    case OVS_CS_F_ESTABLISHED:
4339
2.35k
        return "est";
4340
1.86k
    case OVS_CS_F_RELATED:
4341
1.86k
        return "rel";
4342
1.78k
    case OVS_CS_F_INVALID:
4343
1.78k
        return "inv";
4344
1.55k
    case OVS_CS_F_SRC_NAT:
4345
1.55k
        return "snat";
4346
1.16k
    case OVS_CS_F_DST_NAT:
4347
1.16k
        return "dnat";
4348
23.2k
    default:
4349
23.2k
        return NULL;
4350
38.0k
    }
4351
38.0k
}
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
60.6k
{
4357
60.6k
    bool mask_empty = mask && !*mask;
4358
60.6k
    bool mask_full = !mask || *mask == UINT8_MAX;
4359
4360
    /* ODP frag is an enumeration field; partial masks are not meaningful. */
4361
60.6k
    if (!mask_empty && !mask_full) {
4362
0
        ds_put_format(ds, "error: partial mask not supported for frag (%#"
4363
0
                      PRIx8"),", *mask);
4364
60.6k
    } else if (!mask_empty) {
4365
53.5k
        ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
4366
53.5k
    }
4367
60.6k
}
4368
4369
static bool
4370
mask_empty(const struct nlattr *ma)
4371
4.97k
{
4372
4.97k
    const void *mask;
4373
4.97k
    size_t n;
4374
4375
4.97k
    if (!ma) {
4376
403
        return true;
4377
403
    }
4378
4.57k
    mask = nl_attr_get(ma);
4379
4.57k
    n = nl_attr_get_size(ma);
4380
4381
4.57k
    return is_all_zeros(mask, n);
4382
4.97k
}
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
466k
{
4391
466k
    enum ovs_key_attr attr = nl_attr_type(a);
4392
466k
    char namebuf[OVS_KEY_ATTR_BUFSIZE];
4393
466k
    bool is_exact;
4394
4395
466k
    is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
4396
4397
466k
    ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
4398
4399
466k
    ds_put_char(ds, '(');
4400
466k
    switch (attr) {
4401
43.6k
    case OVS_KEY_ATTR_ENCAP:
4402
43.6k
        if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
4403
17.9k
            odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
4404
17.9k
                            nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
4405
17.9k
                            verbose, skip_no_mask);
4406
25.7k
        } else if (nl_attr_get_size(a)) {
4407
13.8k
            odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
4408
13.8k
                            ds, verbose, skip_no_mask);
4409
13.8k
        }
4410
43.6k
        break;
4411
4412
4.99k
    case OVS_KEY_ATTR_PRIORITY:
4413
14.1k
    case OVS_KEY_ATTR_SKB_MARK:
4414
17.4k
    case OVS_KEY_ATTR_DP_HASH:
4415
20.6k
    case OVS_KEY_ATTR_RECIRC_ID:
4416
20.6k
        ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4417
20.6k
        if (!is_exact) {
4418
487
            ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4419
487
        }
4420
20.6k
        break;
4421
4422
4.54k
    case OVS_KEY_ATTR_CT_MARK:
4423
4.54k
        if (verbose || !mask_empty(ma)) {
4424
4.34k
            ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4425
4.34k
            if (!is_exact) {
4426
520
                ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4427
520
            }
4428
4.34k
        }
4429
4.54k
        break;
4430
4431
5.08k
    case OVS_KEY_ATTR_CT_STATE:
4432
5.08k
        if (verbose) {
4433
3.03k
                ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
4434
3.03k
                if (!is_exact) {
4435
0
                    ds_put_format(ds, "/%#"PRIx32,
4436
0
                                  mask_empty(ma) ? 0 : nl_attr_get_u32(ma));
4437
0
                }
4438
3.03k
        } else if (!is_exact) {
4439
484
            format_flags_masked(ds, NULL, odp_ct_state_to_string,
4440
484
                                nl_attr_get_u32(a),
4441
484
                                mask_empty(ma) ? 0 : nl_attr_get_u32(ma),
4442
484
                                UINT32_MAX);
4443
1.56k
        } else {
4444
1.56k
            format_flags(ds, odp_ct_state_to_string, nl_attr_get_u32(a), '|');
4445
1.56k
        }
4446
5.08k
        break;
4447
4448
5.86k
    case OVS_KEY_ATTR_CT_ZONE:
4449
5.86k
        if (verbose || !mask_empty(ma)) {
4450
5.65k
            ds_put_format(ds, "%#"PRIx16, nl_attr_get_u16(a));
4451
5.65k
            if (!is_exact) {
4452
481
                ds_put_format(ds, "/%#"PRIx16, nl_attr_get_u16(ma));
4453
481
            }
4454
5.65k
        }
4455
5.86k
        break;
4456
4457
17.7k
    case OVS_KEY_ATTR_CT_LABELS: {
4458
17.7k
        const ovs_32aligned_u128 *value = nl_attr_get(a);
4459
17.7k
        const ovs_32aligned_u128 *mask = ma ? nl_attr_get(ma) : NULL;
4460
4461
17.7k
        format_u128(ds, value, mask, verbose);
4462
17.7k
        break;
4463
17.4k
    }
4464
4465
4.66k
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: {
4466
4.66k
        const struct ovs_key_ct_tuple_ipv4 *key = nl_attr_get(a);
4467
4.66k
        const struct ovs_key_ct_tuple_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4468
4469
4.66k
        format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4470
4.66k
        format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4471
4.66k
        format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4472
4.66k
                   verbose);
4473
4.66k
        format_be16(ds, "tp_src", key->src_port, MASK(mask, src_port),
4474
4.66k
                    verbose);
4475
4.66k
        format_be16(ds, "tp_dst", key->dst_port, MASK(mask, dst_port),
4476
4.66k
                    verbose);
4477
4.66k
        ds_chomp(ds, ',');
4478
4.66k
        break;
4479
17.4k
    }
4480
4481
3.96k
    case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: {
4482
3.96k
        const struct ovs_key_ct_tuple_ipv6 *key = nl_attr_get(a);
4483
3.96k
        const struct ovs_key_ct_tuple_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4484
4485
3.96k
        format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4486
3.96k
                        verbose);
4487
3.96k
        format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4488
3.96k
                        verbose);
4489
3.96k
        format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4490
3.96k
                   verbose);
4491
3.96k
        format_be16(ds, "src_port", key->src_port, MASK(mask, src_port),
4492
3.96k
                    verbose);
4493
3.96k
        format_be16(ds, "dst_port", key->dst_port, MASK(mask, dst_port),
4494
3.96k
                    verbose);
4495
3.96k
        ds_chomp(ds, ',');
4496
3.96k
        break;
4497
17.4k
    }
4498
4499
12.0k
    case OVS_KEY_ATTR_TUNNEL:
4500
12.0k
        format_odp_tun_attr(a, ma, ds, verbose);
4501
12.0k
        break;
4502
4503
770
    case OVS_KEY_ATTR_IN_PORT:
4504
770
        if (is_exact) {
4505
502
            odp_portno_name_format(portno_names, nl_attr_get_odp_port(a), ds);
4506
502
        } else {
4507
268
            ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
4508
268
            if (!is_exact) {
4509
268
                ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
4510
268
            }
4511
268
        }
4512
770
        break;
4513
4514
6.82k
    case OVS_KEY_ATTR_PACKET_TYPE: {
4515
6.82k
        ovs_be32 value = nl_attr_get_be32(a);
4516
6.82k
        ovs_be32 mask = ma ? nl_attr_get_be32(ma) : OVS_BE32_MAX;
4517
4518
6.82k
        ovs_be16 ns = htons(pt_ns(value));
4519
6.82k
        ovs_be16 ns_mask = htons(pt_ns(mask));
4520
6.82k
        format_be16(ds, "ns", ns, &ns_mask, verbose);
4521
4522
6.82k
        ovs_be16 ns_type = pt_ns_type_be(value);
4523
6.82k
        ovs_be16 ns_type_mask = pt_ns_type_be(mask);
4524
6.82k
        format_be16x(ds, "id", ns_type, &ns_type_mask, verbose);
4525
4526
6.82k
        ds_chomp(ds, ',');
4527
6.82k
        break;
4528
17.4k
    }
4529
4530
4.91k
    case OVS_KEY_ATTR_ETHERNET: {
4531
4.91k
        const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
4532
4.91k
        const struct ovs_key_ethernet *key = nl_attr_get(a);
4533
4534
4.91k
        format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
4535
4.91k
        format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
4536
4.91k
        ds_chomp(ds, ',');
4537
4.91k
        break;
4538
17.4k
    }
4539
3.53k
    case OVS_KEY_ATTR_VLAN:
4540
3.53k
        format_vlan_tci(ds, nl_attr_get_be16(a),
4541
3.53k
                        ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
4542
3.53k
        break;
4543
4544
1.56k
    case OVS_KEY_ATTR_MPLS: {
4545
1.56k
        const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
4546
1.56k
        const struct ovs_key_mpls *mpls_mask = NULL;
4547
1.56k
        size_t size = nl_attr_get_size(a);
4548
4549
1.56k
        if (!size || size % sizeof *mpls_key) {
4550
17
            ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
4551
17
            return;
4552
17
        }
4553
1.54k
        if (!is_exact) {
4554
678
            mpls_mask = nl_attr_get(ma);
4555
678
            if (size != nl_attr_get_size(ma)) {
4556
210
                ds_put_format(ds, "(key length %"PRIuSIZE" != "
4557
210
                              "mask length %"PRIuSIZE")",
4558
210
                              size, nl_attr_get_size(ma));
4559
210
                return;
4560
210
            }
4561
678
        }
4562
1.33k
        format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
4563
1.33k
        break;
4564
1.54k
    }
4565
4.36k
    case OVS_KEY_ATTR_ETHERTYPE:
4566
4.36k
        ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
4567
4.36k
        if (!is_exact) {
4568
473
            ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
4569
473
        }
4570
4.36k
        break;
4571
4572
42.0k
    case OVS_KEY_ATTR_IPV4: {
4573
42.0k
        const struct ovs_key_ipv4 *key = nl_attr_get(a);
4574
42.0k
        const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
4575
4576
42.0k
        format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
4577
42.0k
        format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
4578
42.0k
        format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
4579
42.0k
                      verbose);
4580
42.0k
        format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
4581
42.0k
        format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
4582
42.0k
        format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
4583
42.0k
                    verbose);
4584
42.0k
        ds_chomp(ds, ',');
4585
42.0k
        break;
4586
1.54k
    }
4587
18.5k
    case OVS_KEY_ATTR_IPV6: {
4588
18.5k
        const struct ovs_key_ipv6 *key = nl_attr_get(a);
4589
18.5k
        const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
4590
4591
18.5k
        format_in6_addr(ds, "src", &key->ipv6_src, MASK(mask, ipv6_src),
4592
18.5k
                        verbose);
4593
18.5k
        format_in6_addr(ds, "dst", &key->ipv6_dst, MASK(mask, ipv6_dst),
4594
18.5k
                        verbose);
4595
18.5k
        format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
4596
18.5k
                          verbose);
4597
18.5k
        format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
4598
18.5k
                   verbose);
4599
18.5k
        format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
4600
18.5k
                   verbose);
4601
18.5k
        format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
4602
18.5k
                   verbose);
4603
18.5k
        format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
4604
18.5k
                    verbose);
4605
18.5k
        ds_chomp(ds, ',');
4606
18.5k
        break;
4607
1.54k
    }
4608
        /* These have the same structure and format. */
4609
1.48k
    case OVS_KEY_ATTR_TCP:
4610
3.26k
    case OVS_KEY_ATTR_UDP:
4611
5.59k
    case OVS_KEY_ATTR_SCTP: {
4612
5.59k
        const struct ovs_key_tcp *key = nl_attr_get(a);
4613
5.59k
        const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
4614
4615
5.59k
        format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
4616
5.59k
        format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
4617
5.59k
        ds_chomp(ds, ',');
4618
5.59k
        break;
4619
3.26k
    }
4620
46.7k
    case OVS_KEY_ATTR_TCP_FLAGS:
4621
46.7k
        if (!is_exact) {
4622
243
            format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
4623
243
                                ntohs(nl_attr_get_be16(a)),
4624
243
                                TCP_FLAGS(nl_attr_get_be16(ma)),
4625
243
                                TCP_FLAGS(OVS_BE16_MAX));
4626
46.5k
        } else {
4627
46.5k
            format_flags(ds, packet_tcp_flag_to_string,
4628
46.5k
                         ntohs(nl_attr_get_be16(a)), '|');
4629
46.5k
        }
4630
46.7k
        break;
4631
4632
4.11k
    case OVS_KEY_ATTR_ICMP: {
4633
4.11k
        const struct ovs_key_icmp *key = nl_attr_get(a);
4634
4.11k
        const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
4635
4636
4.11k
        format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
4637
4.11k
        format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
4638
4.11k
        ds_chomp(ds, ',');
4639
4.11k
        break;
4640
3.26k
    }
4641
1.70k
    case OVS_KEY_ATTR_ICMPV6: {
4642
1.70k
        const struct ovs_key_icmpv6 *key = nl_attr_get(a);
4643
1.70k
        const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
4644
4645
1.70k
        format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
4646
1.70k
                   verbose);
4647
1.70k
        format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
4648
1.70k
                   verbose);
4649
1.70k
        ds_chomp(ds, ',');
4650
1.70k
        break;
4651
3.26k
    }
4652
10.4k
    case OVS_KEY_ATTR_ARP: {
4653
10.4k
        const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
4654
10.4k
        const struct ovs_key_arp *key = nl_attr_get(a);
4655
4656
10.4k
        format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
4657
10.4k
        format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
4658
10.4k
        format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
4659
10.4k
        format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
4660
10.4k
        format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
4661
10.4k
        ds_chomp(ds, ',');
4662
10.4k
        break;
4663
3.26k
    }
4664
170k
    case OVS_KEY_ATTR_ND: {
4665
170k
        const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
4666
170k
        const struct ovs_key_nd *key = nl_attr_get(a);
4667
4668
170k
        format_in6_addr(ds, "target", &key->nd_target, MASK(mask, nd_target),
4669
170k
                        verbose);
4670
170k
        format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
4671
170k
        format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
4672
4673
170k
        ds_chomp(ds, ',');
4674
170k
        break;
4675
3.26k
    }
4676
5.10k
    case OVS_KEY_ATTR_ND_EXTENSIONS: {
4677
5.10k
        const struct ovs_key_nd_extensions *mask = ma ? nl_attr_get(ma) : NULL;
4678
5.10k
        const struct ovs_key_nd_extensions *key = nl_attr_get(a);
4679
4680
5.10k
        bool first = true;
4681
5.10k
        format_be32_masked(ds, &first, "nd_reserved", key->nd_reserved,
4682
5.10k
                           OVS_BE32_MAX);
4683
5.10k
        ds_put_char(ds, ',');
4684
4685
5.10k
        format_u8u(ds, "nd_options_type", key->nd_options_type,
4686
5.10k
                   MASK(mask, nd_options_type), verbose);
4687
4688
5.10k
        ds_chomp(ds, ',');
4689
5.10k
        break;
4690
3.26k
    }
4691
21.1k
    case OVS_KEY_ATTR_NSH: {
4692
21.1k
        format_odp_nsh_attr(a, ma, ds);
4693
21.1k
        break;
4694
3.26k
    }
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
466k
    }
4706
465k
    ds_put_char(ds, ')');
4707
465k
}
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
13.1k
{
4714
13.1k
    if (check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4715
13.1k
                        OVS_KEY_ATTR_MAX, false)) {
4716
13.1k
        format_odp_key_attr__(a, ma, portno_names, ds, verbose, false);
4717
13.1k
    }
4718
13.1k
}
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
17.1k
{
4724
17.1k
    const struct nlattr *a;
4725
17.1k
    unsigned int left;
4726
17.1k
    int type = nl_attr_type(key);
4727
17.1k
    int size = nl_attr_get_size(key);
4728
4729
17.1k
    if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
4730
1.60k
        nl_msg_put_unspec_zero(ofp, type, size);
4731
15.5k
    } else {
4732
15.5k
        size_t nested_mask;
4733
4734
15.5k
        if (tbl[type].next) {
4735
13.0k
            const struct attr_len_tbl *entry = &tbl[type];
4736
13.0k
            tbl = entry->next;
4737
13.0k
            max = entry->next_max;
4738
13.0k
        }
4739
4740
15.5k
        nested_mask = nl_msg_start_nested(ofp, type);
4741
15.5k
        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
15.5k
        nl_msg_end_nested(ofp, nested_mask);
4745
15.5k
    }
4746
4747
17.1k
    return ofp->base;
4748
17.1k
}
4749
4750
static void
4751
format_u128(struct ds *ds, const ovs_32aligned_u128 *key,
4752
            const ovs_32aligned_u128 *mask, bool verbose)
4753
18.7k
{
4754
18.7k
    if (verbose || (mask && !ovs_u128_is_zero(get_32aligned_u128(mask)))) {
4755
16.4k
        ovs_be128 value = hton128(get_32aligned_u128(key));
4756
16.4k
        ds_put_hex(ds, &value, sizeof value);
4757
16.4k
        if (mask && !(ovs_u128_is_ones(get_32aligned_u128(mask)))) {
4758
1.15k
            value = hton128(get_32aligned_u128(mask));
4759
1.15k
            ds_put_char(ds, '/');
4760
1.15k
            ds_put_hex(ds, &value, sizeof value);
4761
1.15k
        }
4762
16.4k
    }
4763
18.7k
}
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
17.1k
{
4774
17.1k
    char *s = CONST_CAST(char *, s_);
4775
17.1k
    ovs_be128 be_value;
4776
17.1k
    ovs_be128 be_mask;
4777
4778
17.1k
    if (!parse_int_string(s, (uint8_t *)&be_value, sizeof be_value, &s)) {
4779
17.0k
        *value = ntoh128(be_value);
4780
4781
17.0k
        if (mask) {
4782
17.0k
            int n;
4783
4784
17.0k
            if (ovs_scan(s, "/%n", &n)) {
4785
2.98k
                int error;
4786
4787
2.98k
                s += n;
4788
2.98k
                error = parse_int_string(s, (uint8_t *)&be_mask,
4789
2.98k
                                         sizeof be_mask, &s);
4790
2.98k
                if (error) {
4791
9
                    return 0;
4792
9
                }
4793
2.97k
                *mask = ntoh128(be_mask);
4794
14.1k
            } else {
4795
14.1k
                *mask = OVS_U128_MAX;
4796
14.1k
            }
4797
17.0k
        }
4798
17.0k
        return s - s_;
4799
17.0k
    }
4800
4801
19
    return 0;
4802
17.1k
}
4803
4804
int
4805
odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
4806
138k
{
4807
138k
    const char *s = s_;
4808
4809
138k
    if (ovs_scan(s, "ufid:")) {
4810
362
        s += 5;
4811
4812
362
        if (!uuid_from_string_prefix((struct uuid *)ufid, s)) {
4813
102
            return -EINVAL;
4814
102
        }
4815
260
        s += UUID_LEN;
4816
4817
260
        return s - s_;
4818
362
    }
4819
4820
137k
    return 0;
4821
138k
}
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
39.5k
{
4841
39.5k
    if (key_len) {
4842
39.5k
        const struct nlattr *a;
4843
39.5k
        unsigned int left;
4844
39.5k
        bool has_ethtype_key = false;
4845
39.5k
        bool has_packet_type_key = false;
4846
39.5k
        struct ofpbuf ofp;
4847
39.5k
        bool first_field = true;
4848
4849
39.5k
        ofpbuf_init(&ofp, 100);
4850
471k
        NL_ATTR_FOR_EACH (a, left, key, key_len) {
4851
471k
            int attr_type = nl_attr_type(a);
4852
471k
            const struct nlattr *ma = (mask && mask_len
4853
471k
                                       ? nl_attr_find__(mask, mask_len,
4854
101k
                                                        attr_type)
4855
471k
                                       : NULL);
4856
471k
            if (!check_attr_len(ds, a, ma, ovs_flow_key_attr_lens,
4857
471k
                                OVS_KEY_ATTR_MAX, false)
4858
471k
                || (skip_no_mask && !ma)) {
4859
0
                continue;
4860
0
            }
4861
4862
471k
            bool is_nested_attr;
4863
471k
            bool is_wildcard = false;
4864
4865
471k
            if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
4866
4.36k
                has_ethtype_key = true;
4867
466k
            } else if (attr_type == OVS_KEY_ATTR_PACKET_TYPE) {
4868
7.08k
                has_packet_type_key = true;
4869
7.08k
            }
4870
4871
471k
            is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
4872
471k
                                              OVS_KEY_ATTR_MAX, attr_type) ==
4873
471k
                             ATTR_LEN_NESTED;
4874
4875
471k
            if (mask && mask_len) {
4876
101k
                ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
4877
101k
                is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
4878
101k
            }
4879
4880
471k
            if (verbose || !is_wildcard  || is_nested_attr) {
4881
452k
                if (is_wildcard && !ma) {
4882
15.3k
                    ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
4883
15.3k
                                                    OVS_KEY_ATTR_MAX,
4884
15.3k
                                                    &ofp, a);
4885
15.3k
                }
4886
452k
                if (!first_field) {
4887
415k
                    ds_put_char(ds, ',');
4888
415k
                }
4889
452k
                format_odp_key_attr__(a, ma, portno_names, ds, verbose,
4890
452k
                                      skip_no_mask);
4891
452k
                first_field = false;
4892
452k
            } else if (attr_type == OVS_KEY_ATTR_ETHERNET
4893
1.62k
                       && !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
1.43k
                if (!first_field) {
4917
931
                    ds_put_char(ds, ',');
4918
931
                }
4919
1.43k
                ds_put_cstr(ds, "eth()");
4920
16.8k
            } 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
261
                has_packet_type_key = false;
4925
261
            }
4926
471k
            ofpbuf_clear(&ofp);
4927
471k
        }
4928
39.5k
        ofpbuf_uninit(&ofp);
4929
4930
39.5k
        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
39.5k
        if (!has_ethtype_key && mask) {
4943
22.3k
            const struct nlattr *ma = nl_attr_find__(mask, mask_len,
4944
22.3k
                                                     OVS_KEY_ATTR_ETHERTYPE);
4945
22.3k
            if (ma) {
4946
10.4k
                ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
4947
10.4k
                              ntohs(nl_attr_get_be16(ma)));
4948
10.4k
            }
4949
22.3k
        }
4950
39.5k
    } else {
4951
1
        ds_put_cstr(ds, "<empty>");
4952
1
    }
4953
39.5k
}
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
2.75k
{
4961
2.75k
    odp_flow_format(key, key_len, NULL, 0, NULL, ds, true, false);
4962
2.75k
}
4963
4964
static bool
4965
ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
4966
22.2k
{
4967
22.2k
    if (!strcasecmp(s, "no")) {
4968
21.6k
        *type = OVS_FRAG_TYPE_NONE;
4969
21.6k
    } else if (!strcasecmp(s, "first")) {
4970
248
        *type = OVS_FRAG_TYPE_FIRST;
4971
342
    } else if (!strcasecmp(s, "later")) {
4972
209
        *type = OVS_FRAG_TYPE_LATER;
4973
209
    } else {
4974
133
        return false;
4975
133
    }
4976
22.0k
    return true;
4977
22.2k
}
4978
4979
/* Parsing. */
4980
4981
static int
4982
scan_eth(const char *s, struct eth_addr *key, struct eth_addr *mask)
4983
2.18k
{
4984
2.18k
    int n;
4985
4986
2.18k
    if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n",
4987
2.18k
                 ETH_ADDR_SCAN_ARGS(*key), &n)) {
4988
2.10k
        int len = n;
4989
4990
2.10k
        if (mask) {
4991
2.10k
            if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
4992
2.10k
                         ETH_ADDR_SCAN_ARGS(*mask), &n)) {
4993
455
                len += n;
4994
1.64k
            } else {
4995
1.64k
                memset(mask, 0xff, sizeof *mask);
4996
1.64k
            }
4997
2.10k
        }
4998
2.10k
        return len;
4999
2.10k
    }
5000
89
    return 0;
5001
2.18k
}
5002
5003
static int
5004
scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
5005
6.55k
{
5006
6.55k
    int n;
5007
5008
6.55k
    if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
5009
6.48k
        int len = n;
5010
5011
6.48k
        if (mask) {
5012
6.48k
            if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
5013
6.48k
                         IP_SCAN_ARGS(mask), &n)) {
5014
815
                len += n;
5015
5.66k
            } else {
5016
5.66k
                *mask = OVS_BE32_MAX;
5017
5.66k
            }
5018
6.48k
        }
5019
6.48k
        return len;
5020
6.48k
    }
5021
75
    return 0;
5022
6.55k
}
5023
5024
static int
5025
scan_in6_addr(const char *s, struct in6_addr *key, struct in6_addr *mask)
5026
6.65k
{
5027
6.65k
    int n;
5028
6.65k
    char ipv6_s[IPV6_SCAN_LEN + 1];
5029
5030
6.65k
    if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
5031
6.59k
        && inet_pton(AF_INET6, ipv6_s, key) == 1) {
5032
6.57k
        int len = n;
5033
5034
6.57k
        if (mask) {
5035
6.57k
            if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
5036
1.82k
                && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
5037
1.81k
                len += n;
5038
4.76k
            } else {
5039
4.76k
                memset(mask, 0xff, sizeof *mask);
5040
4.76k
            }
5041
6.57k
        }
5042
6.57k
        return len;
5043
6.57k
    }
5044
81
    return 0;
5045
6.65k
}
5046
5047
static int
5048
scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
5049
746
{
5050
746
    int key_, mask_;
5051
746
    int n;
5052
5053
746
    if (ovs_scan(s, "%i%n", &key_, &n)
5054
736
        && (key_ & ~IPV6_LABEL_MASK) == 0) {
5055
685
        int len = n;
5056
5057
685
        *key = htonl(key_);
5058
685
        if (mask) {
5059
685
            if (ovs_scan(s + len, "/%i%n", &mask_, &n)
5060
371
                && (mask_ & ~IPV6_LABEL_MASK) == 0) {
5061
346
                len += n;
5062
346
                *mask = htonl(mask_);
5063
346
            } else {
5064
339
                *mask = htonl(IPV6_LABEL_MASK);
5065
339
            }
5066
685
        }
5067
685
        return len;
5068
685
    }
5069
61
    return 0;
5070
746
}
5071
5072
static int
5073
scan_u8(const char *s, uint8_t *key, uint8_t *mask)
5074
101k
{
5075
101k
    int n;
5076
5077
101k
    if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
5078
101k
        int len = n;
5079
5080
101k
        if (mask) {
5081
101k
            if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
5082
34.2k
                len += n;
5083
67.0k
            } else {
5084
67.0k
                *mask = UINT8_MAX;
5085
67.0k
            }
5086
101k
        }
5087
101k
        return len;
5088
101k
    }
5089
202
    return 0;
5090
101k
}
5091
5092
static int
5093
scan_u16(const char *s, uint16_t *key, uint16_t *mask)
5094
6.46k
{
5095
6.46k
    int n;
5096
5097
6.46k
    if (ovs_scan(s, "%"SCNi16"%n", key, &n)) {
5098
6.46k
        int len = n;
5099
5100
6.46k
        if (mask) {
5101
6.46k
            if (ovs_scan(s + len, "/%"SCNi16"%n", mask, &n)) {
5102
622
                len += n;
5103
5.84k
            } else {
5104
5.84k
                *mask = UINT16_MAX;
5105
5.84k
            }
5106
6.46k
        }
5107
6.46k
        return len;
5108
6.46k
    }
5109
3
    return 0;
5110
6.46k
}
5111
5112
static int
5113
scan_u32(const char *s, uint32_t *key, uint32_t *mask)
5114
17.1k
{
5115
17.1k
    int n;
5116
5117
17.1k
    if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
5118
17.1k
        int len = n;
5119
5120
17.1k
        if (mask) {
5121
16.6k
            if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
5122
8.78k
                len += n;
5123
8.78k
            } else {
5124
7.86k
                *mask = UINT32_MAX;
5125
7.86k
            }
5126
16.6k
        }
5127
17.1k
        return len;
5128
17.1k
    }
5129
28
    return 0;
5130
17.1k
}
5131
5132
static int
5133
scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
5134
25.0k
{
5135
25.0k
    uint16_t key_, mask_;
5136
25.0k
    int n;
5137
5138
25.0k
    if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
5139
24.9k
        int len = n;
5140
5141
24.9k
        *key = htons(key_);
5142
24.9k
        if (mask) {
5143
24.9k
            if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
5144
3.50k
                len += n;
5145
3.50k
                *mask = htons(mask_);
5146
21.4k
            } else {
5147
21.4k
                *mask = OVS_BE16_MAX;
5148
21.4k
            }
5149
24.9k
        }
5150
24.9k
        return len;
5151
24.9k
    }
5152
147
    return 0;
5153
25.0k
}
5154
5155
static int
5156
scan_be32(const char *s, ovs_be32 *key, ovs_be32 *mask)
5157
25.1k
{
5158
25.1k
    uint32_t key_, mask_;
5159
25.1k
    int n;
5160
5161
25.1k
    if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
5162
25.0k
        int len = n;
5163
5164
25.0k
        *key = htonl(key_);
5165
25.0k
        if (mask) {
5166
25.0k
            if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
5167
5.75k
                len += n;
5168
5.75k
                *mask = htonl(mask_);
5169
19.3k
            } else {
5170
19.3k
                *mask = OVS_BE32_MAX;
5171
19.3k
            }
5172
25.0k
        }
5173
25.0k
        return len;
5174
25.0k
    }
5175
50
    return 0;
5176
25.1k
}
5177
5178
static int
5179
scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
5180
36.1k
{
5181
36.1k
    uint64_t key_, mask_;
5182
36.1k
    int n;
5183
5184
36.1k
    if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
5185
36.1k
        int len = n;
5186
5187
36.1k
        *key = htonll(key_);
5188
36.1k
        if (mask) {
5189
36.1k
            if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
5190
6.16k
                len += n;
5191
6.16k
                *mask = htonll(mask_);
5192
29.9k
            } else {
5193
29.9k
                *mask = OVS_BE64_MAX;
5194
29.9k
            }
5195
36.1k
        }
5196
36.1k
        return len;
5197
36.1k
    }
5198
4
    return 0;
5199
36.1k
}
5200
5201
static int
5202
scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
5203
3.61k
{
5204
3.61k
    uint32_t flags, fmask;
5205
3.61k
    int n;
5206
5207
3.61k
    n = parse_odp_flags(s, flow_tun_flag_to_string, &flags,
5208
3.61k
                        FLOW_TNL_F_MASK, mask ? &fmask : NULL);
5209
3.61k
    if (n >= 0 && s[n] == ')') {
5210
3.53k
        *key = flags;
5211
3.53k
        if (mask) {
5212
3.53k
            *mask = fmask;
5213
3.53k
        }
5214
3.53k
        return n + 1;
5215
3.53k
    }
5216
89
    return 0;
5217
3.61k
}
5218
5219
static int
5220
scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
5221
25.1k
{
5222
25.1k
    uint32_t flags, fmask;
5223
25.1k
    int n;
5224
5225
25.1k
    n = parse_odp_flags(s, packet_tcp_flag_to_string, &flags,
5226
25.1k
                        TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
5227
25.1k
    if (n >= 0) {
5228
24.9k
        *key = htons(flags);
5229
24.9k
        if (mask) {
5230
24.9k
            *mask = htons(fmask);
5231
24.9k
        }
5232
24.9k
        return n;
5233
24.9k
    }
5234
139
    return 0;
5235
25.1k
}
5236
5237
static uint32_t
5238
ovs_to_odp_ct_state(uint8_t state)
5239
5.56k
{
5240
5.56k
    uint32_t odp = 0;
5241
5242
5.56k
#define CS_STATE(ENUM, INDEX, NAME)             \
5243
44.5k
    if (state & CS_##ENUM) {                    \
5244
22.6k
        odp |= OVS_CS_F_##ENUM;                 \
5245
22.6k
    }
5246
5.56k
    CS_STATES
5247
5.56k
#undef CS_STATE
5248
5249
5.56k
    return odp;
5250
5.56k
}
5251
5252
static uint8_t
5253
odp_to_ovs_ct_state(uint32_t flags)
5254
54
{
5255
54
    uint32_t state = 0;
5256
5257
54
#define CS_STATE(ENUM, INDEX, NAME) \
5258
432
    if (flags & OVS_CS_F_##ENUM) {  \
5259
116
        state |= CS_##ENUM;         \
5260
116
    }
5261
54
    CS_STATES
5262
54
#undef CS_STATE
5263
5264
54
    return state;
5265
54
}
5266
5267
static int
5268
scan_ct_state(const char *s, uint32_t *key, uint32_t *mask)
5269
2.81k
{
5270
2.81k
    uint32_t flags, fmask;
5271
2.81k
    int n;
5272
5273
2.81k
    n = parse_flags(s, odp_ct_state_to_string, ')', NULL, NULL, &flags,
5274
2.81k
                    ovs_to_odp_ct_state(CS_SUPPORTED_MASK),
5275
2.81k
                    mask ? &fmask : NULL);
5276
5277
2.81k
    if (n >= 0) {
5278
2.37k
        *key = flags;
5279
2.37k
        if (mask) {
5280
2.37k
            *mask = fmask;
5281
2.37k
        }
5282
2.37k
        return n;
5283
2.37k
    }
5284
444
    return 0;
5285
2.81k
}
5286
5287
static int
5288
scan_frag(const char *s, uint8_t *key, uint8_t *mask)
5289
22.2k
{
5290
22.2k
    int n;
5291
22.2k
    char frag[8];
5292
22.2k
    enum ovs_frag_type frag_type;
5293
5294
22.2k
    if (ovs_scan(s, "%7[a-z]%n", frag, &n)
5295
22.2k
        && ovs_frag_type_from_string(frag, &frag_type)) {
5296
22.0k
        int len = n;
5297
5298
22.0k
        *key = frag_type;
5299
22.0k
        if (mask) {
5300
22.0k
            *mask = UINT8_MAX;
5301
22.0k
        }
5302
22.0k
        return len;
5303
22.0k
    }
5304
158
    return 0;
5305
22.2k
}
5306
5307
static int
5308
scan_port(const char *s, uint32_t *key, uint32_t *mask,
5309
          const struct simap *port_names)
5310
1.44k
{
5311
1.44k
    int n;
5312
5313
1.44k
    if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
5314
1.43k
        int len = n;
5315
5316
1.43k
        if (mask) {
5317
1.43k
            if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
5318
432
                len += n;
5319
1.00k
            } else {
5320
1.00k
                *mask = UINT32_MAX;
5321
1.00k
            }
5322
1.43k
        }
5323
1.43k
        return len;
5324
1.43k
    } 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
10
    return 0;
5340
1.44k
}
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
4.11k
{
5350
4.11k
    const uint16_t mask = ((1U << bits) - 1) << offset;
5351
5352
4.11k
    if (value >> bits) {
5353
38
        return false;
5354
38
    }
5355
5356
4.07k
    *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
5357
4.07k
    return true;
5358
4.11k
}
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
3.37k
{
5364
3.37k
    uint16_t key_, mask_;
5365
3.37k
    int n;
5366
5367
3.37k
    if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
5368
3.34k
        int len = n;
5369
5370
3.34k
        if (set_be16_bf(key, bits, offset, key_)) {
5371
3.32k
            if (mask) {
5372
3.32k
                if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
5373
766
                    len += n;
5374
5375
766
                    if (!set_be16_bf(mask, bits, offset, mask_)) {
5376
21
                        return 0;
5377
21
                    }
5378
2.56k
                } else {
5379
2.56k
                    *mask |= htons(((1U << bits) - 1) << offset);
5380
2.56k
                }
5381
3.32k
            }
5382
3.30k
            return len;
5383
3.32k
        }
5384
3.34k
    }
5385
43
    return 0;
5386
3.37k
}
5387
5388
static int
5389
scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
5390
1.54k
{
5391
1.54k
    return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
5392
1.54k
}
5393
5394
static int
5395
scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
5396
621
{
5397
621
    return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
5398
621
}
5399
5400
static int
5401
scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
5402
1.20k
{
5403
1.20k
    return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
5404
1.20k
}
5405
5406
/* For MPLS. */
5407
static bool
5408
set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
5409
4.84k
{
5410
4.84k
    const uint32_t mask = ((1U << bits) - 1) << offset;
5411
5412
4.84k
    if (value >> bits) {
5413
86
        return false;
5414
86
    }
5415
5416
4.75k
    *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
5417
4.75k
    return true;
5418
4.84k
}
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
4.72k
{
5424
4.72k
    uint32_t key_, mask_;
5425
4.72k
    int n;
5426
5427
4.72k
    if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
5428
4.60k
        int len = n;
5429
5430
4.60k
        if (set_be32_bf(key, bits, offset, key_)) {
5431
4.55k
            if (mask) {
5432
4.55k
                if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
5433
237
                    len += n;
5434
5435
237
                    if (!set_be32_bf(mask, bits, offset, mask_)) {
5436
37
                        return 0;
5437
37
                    }
5438
4.31k
                } else {
5439
4.31k
                    *mask |= htonl(((1U << bits) - 1) << offset);
5440
4.31k
                }
5441
4.55k
            }
5442
4.51k
            return len;
5443
4.55k
        }
5444
4.60k
    }
5445
175
    return 0;
5446
4.72k
}
5447
5448
static int
5449
scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
5450
759
{
5451
759
    return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
5452
759
}
5453
5454
static int
5455
scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
5456
1.29k
{
5457
1.29k
    return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
5458
1.29k
}
5459
5460
static int
5461
scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
5462
1.84k
{
5463
1.84k
    return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
5464
1.84k
}
5465
5466
static int
5467
scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
5468
834
{
5469
834
    return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
5470
834
}
5471
5472
static int
5473
scan_vxlan_gbp(const char *s, uint32_t *key, uint32_t *mask)
5474
4.42k
{
5475
4.42k
    const char *s_base = s;
5476
4.42k
    ovs_be16 id = 0, id_mask = 0;
5477
4.42k
    uint8_t flags = 0, flags_mask = 0;
5478
4.42k
    int len;
5479
5480
4.42k
    if (!strncmp(s, "id=", 3)) {
5481
3.45k
        s += 3;
5482
3.45k
        len = scan_be16(s, &id, mask ? &id_mask : NULL);
5483
3.45k
        if (len == 0) {
5484
3
            return 0;
5485
3
        }
5486
3.44k
        s += len;
5487
3.44k
    }
5488
5489
4.42k
    if (s[0] == ',') {
5490
681
        s++;
5491
681
    }
5492
4.42k
    if (!strncmp(s, "flags=", 6)) {
5493
612
        s += 6;
5494
612
        len = scan_u8(s, &flags, mask ? &flags_mask : NULL);
5495
612
        if (len == 0) {
5496
3
            return 0;
5497
3
        }
5498
609
        s += len;
5499
609
    }
5500
5501
4.42k
    if (!strncmp(s, "))", 2)) {
5502
4.14k
        s += 2;
5503
5504
4.14k
        *key = (flags << 16) | ntohs(id);
5505
4.14k
        if (mask) {
5506
4.14k
            *mask = (flags_mask << 16) | ntohs(id_mask);
5507
4.14k
        }
5508
5509
4.14k
        return s - s_base;
5510
4.14k
    }
5511
5512
277
    return 0;
5513
4.42k
}
5514
5515
static int
5516
scan_gtpu_metadata(const char *s,
5517
                   struct gtpu_metadata *key,
5518
                   struct gtpu_metadata *mask)
5519
3.49k
{
5520
3.49k
    const char *s_base = s;
5521
3.49k
    uint8_t flags = 0, flags_ma = 0;
5522
3.49k
    uint8_t msgtype = 0, msgtype_ma = 0;
5523
3.49k
    int len;
5524
5525
3.49k
    if (!strncmp(s, "flags=", 6)) {
5526
773
        s += 6;
5527
773
        len = scan_u8(s, &flags, mask ? &flags_ma : NULL);
5528
773
        if (len == 0) {
5529
3
            return 0;
5530
3
        }
5531
770
        s += len;
5532
770
    }
5533
5534
3.49k
    if (s[0] == ',') {
5535
547
        s++;
5536
547
    }
5537
5538
3.49k
    if (!strncmp(s, "msgtype=", 8)) {
5539
698
        s += 8;
5540
698
        len = scan_u8(s, &msgtype, mask ? &msgtype_ma : NULL);
5541
698
        if (len == 0) {
5542
3
            return 0;
5543
3
        }
5544
695
        s += len;
5545
695
    }
5546
5547
3.49k
    if (!strncmp(s, ")", 1)) {
5548
2.09k
        s += 1;
5549
2.09k
        key->flags = flags;
5550
2.09k
        key->msgtype = msgtype;
5551
2.09k
        if (mask) {
5552
2.09k
            mask->flags = flags_ma;
5553
2.09k
            mask->msgtype = msgtype_ma;
5554
2.09k
        }
5555
2.09k
    }
5556
3.49k
    return s - s_base;
5557
3.49k
}
5558
5559
static int
5560
scan_erspan_metadata(const char *s,
5561
                     struct erspan_metadata *key,
5562
                     struct erspan_metadata *mask)
5563
6.51k
{
5564
6.51k
    const char *s_base = s;
5565
6.51k
    uint32_t idx = 0, idx_mask = 0;
5566
6.51k
    uint8_t ver = 0, dir = 0, hwid = 0;
5567
6.51k
    uint8_t ver_mask = 0, dir_mask = 0, hwid_mask = 0;
5568
6.51k
    int len;
5569
5570
6.51k
    if (!strncmp(s, "ver=", 4)) {
5571
6.42k
        s += 4;
5572
6.42k
        len = scan_u8(s, &ver, mask ? &ver_mask : NULL);
5573
6.42k
        if (len == 0) {
5574
3
            return 0;
5575
3
        }
5576
6.41k
        s += len;
5577
6.41k
    }
5578
5579
6.51k
    if (s[0] == ',') {
5580
554
        s++;
5581
554
    }
5582
5583
6.51k
    if (ver == 1) {
5584
2.36k
        if (!strncmp(s, "idx=", 4)) {
5585
647
            s += 4;
5586
647
            len = scan_u32(s, &idx, mask ? &idx_mask : NULL);
5587
647
            if (len == 0) {
5588
3
                return 0;
5589
3
            }
5590
644
            s += len;
5591
644
        }
5592
5593
2.36k
        if (!strncmp(s, ")", 1)) {
5594
1.56k
            s += 1;
5595
1.56k
            key->version = ver;
5596
1.56k
            key->u.index = htonl(idx);
5597
1.56k
            if (mask) {
5598
1.56k
                mask->u.index = htonl(idx_mask);
5599
1.56k
            }
5600
1.56k
        }
5601
2.36k
        return s - s_base;
5602
5603
4.14k
    } else if (ver == 2) {
5604
4.03k
        if (!strncmp(s, "dir=", 4)) {
5605
397
            s += 4;
5606
397
            len = scan_u8(s, &dir, mask ? &dir_mask : NULL);
5607
397
            if (len == 0) {
5608
3
                return 0;
5609
3
            }
5610
394
            s += len;
5611
394
        }
5612
4.02k
        if (s[0] == ',') {
5613
321
            s++;
5614
321
        }
5615
4.02k
        if (!strncmp(s, "hwid=", 5)) {
5616
897
            s += 5;
5617
897
            len = scan_u8(s, &hwid, mask ? &hwid_mask : NULL);
5618
897
            if (len == 0) {
5619
3
                return 0;
5620
3
            }
5621
894
            s += len;
5622
894
        }
5623
5624
4.02k
        if (!strncmp(s, ")", 1)) {
5625
3.13k
            s += 1;
5626
3.13k
            key->version = ver;
5627
3.13k
            key->u.md2.hwid = hwid;
5628
3.13k
            key->u.md2.dir = dir;
5629
3.13k
            if (mask) {
5630
3.13k
                mask->u.md2.hwid = hwid_mask;
5631
3.13k
                mask->u.md2.dir = dir_mask;
5632
3.13k
            }
5633
3.13k
        }
5634
4.02k
        return s - s_base;
5635
4.02k
    }
5636
5637
115
    return 0;
5638
6.51k
}
5639
5640
static int
5641
scan_geneve(const char *s, struct geneve_scan *key, struct geneve_scan *mask)
5642
14.4k
{
5643
14.4k
    const char *s_base = s;
5644
14.4k
    struct geneve_opt *opt = key->d;
5645
14.4k
    struct geneve_opt *opt_mask = mask ? mask->d : NULL;
5646
14.4k
    int len_remain = sizeof key->d;
5647
14.4k
    int len;
5648
5649
130k
    while (s[0] == '{' && len_remain >= sizeof *opt) {
5650
116k
        int data_len = 0;
5651
5652
116k
        s++;
5653
116k
        len_remain -= sizeof *opt;
5654
5655
116k
        if (!strncmp(s, "class=", 6)) {
5656
488
            s += 6;
5657
488
            len = scan_be16(s, &opt->opt_class,
5658
488
                            mask ? &opt_mask->opt_class : NULL);
5659
488
            if (len == 0) {
5660
6
                return 0;
5661
6
            }
5662
482
            s += len;
5663
116k
        } else if (mask) {
5664
116k
            memset(&opt_mask->opt_class, 0, sizeof opt_mask->opt_class);
5665
116k
        }
5666
5667
116k
        if (s[0] == ',') {
5668
459
            s++;
5669
459
        }
5670
116k
        if (!strncmp(s, "type=", 5)) {
5671
537
            s += 5;
5672
537
            len = scan_u8(s, &opt->type, mask ? &opt_mask->type : NULL);
5673
537
            if (len == 0) {
5674
10
                return 0;
5675
10
            }
5676
527
            s += len;
5677
116k
        } else if (mask) {
5678
116k
            memset(&opt_mask->type, 0, sizeof opt_mask->type);
5679
116k
        }
5680
5681
116k
        if (s[0] == ',') {
5682
265
            s++;
5683
265
        }
5684
116k
        if (!strncmp(s, "len=", 4)) {
5685
14.2k
            uint8_t opt_len, opt_len_mask;
5686
14.2k
            s += 4;
5687
14.2k
            len = scan_u8(s, &opt_len, mask ? &opt_len_mask : NULL);
5688
14.2k
            if (len == 0) {
5689
11
                return 0;
5690
11
            }
5691
14.1k
            s += len;
5692
5693
14.1k
            if (opt_len > 124 || opt_len % 4 || opt_len > len_remain) {
5694
18
                return 0;
5695
18
            }
5696
14.1k
            opt->length = opt_len / 4;
5697
14.1k
            if (mask) {
5698
14.1k
                opt_mask->length = opt_len_mask;
5699
14.1k
            }
5700
14.1k
            data_len = opt_len;
5701
102k
        } else if (mask) {
5702
102k
            memset(&opt_mask->type, 0, sizeof opt_mask->type);
5703
102k
        }
5704
5705
116k
        if (s[0] == ',') {
5706
345
            s++;
5707
345
            if (parse_int_string(s, (uint8_t *)(opt + 1),
5708
345
                                 data_len, (char **)&s)) {
5709
5
                return 0;
5710
5
            }
5711
345
        }
5712
116k
        if (mask) {
5713
116k
            if (s[0] == '/') {
5714
3.35k
                s++;
5715
3.35k
                if (parse_int_string(s, (uint8_t *)(opt_mask + 1),
5716
3.35k
                                     data_len, (char **)&s)) {
5717
205
                    return 0;
5718
205
                }
5719
3.35k
            }
5720
116k
            opt_mask->r1 = 0;
5721
116k
            opt_mask->r2 = 0;
5722
116k
            opt_mask->r3 = 0;
5723
116k
        }
5724
5725
116k
        if (s[0] == '}') {
5726
115k
            s++;
5727
115k
            opt += 1 + data_len / 4;
5728
115k
            if (mask) {
5729
115k
                opt_mask += 1 + data_len / 4;
5730
115k
            }
5731
115k
            len_remain -= data_len;
5732
115k
        } else {
5733
432
            return 0;
5734
432
        }
5735
116k
    }
5736
5737
13.7k
    if (s[0] == ')') {
5738
13.6k
        len = sizeof key->d - len_remain;
5739
5740
13.6k
        s++;
5741
13.6k
        key->len = len;
5742
13.6k
        if (mask) {
5743
13.6k
            mask->len = len;
5744
13.6k
        }
5745
13.6k
        return s - s_base;
5746
13.6k
    }
5747
5748
41
    return 0;
5749
13.7k
}
5750
5751
static void
5752
tun_flags_to_attr(struct ofpbuf *a, const void *data_)
5753
7.06k
{
5754
7.06k
    const uint16_t *flags = data_;
5755
5756
7.06k
    if (*flags & FLOW_TNL_F_DONT_FRAGMENT) {
5757
5.41k
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
5758
5.41k
    }
5759
7.06k
    if (*flags & FLOW_TNL_F_CSUM) {
5760
4.06k
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
5761
4.06k
    }
5762
7.06k
    if (*flags & FLOW_TNL_F_OAM) {
5763
4.58k
        nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
5764
4.58k
    }
5765
7.06k
}
5766
5767
static void
5768
vxlan_gbp_to_attr(struct ofpbuf *a, const void *data_)
5769
8.29k
{
5770
8.29k
    const uint32_t *gbp = data_;
5771
5772
8.29k
    if (*gbp) {
5773
7.56k
        size_t vxlan_opts_ofs;
5774
5775
7.56k
        vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
5776
7.56k
        nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP, *gbp);
5777
7.56k
        nl_msg_end_nested(a, vxlan_opts_ofs);
5778
7.56k
    }
5779
8.29k
}
5780
5781
static void
5782
geneve_to_attr(struct ofpbuf *a, const void *data_)
5783
27.3k
{
5784
27.3k
    const struct geneve_scan *geneve = data_;
5785
5786
27.3k
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, geneve->d,
5787
27.3k
                      geneve->len);
5788
27.3k
}
5789
5790
static void
5791
erspan_to_attr(struct ofpbuf *a, const void *data_)
5792
12.7k
{
5793
12.7k
    const struct erspan_metadata *md = data_;
5794
5795
12.7k
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, md,
5796
12.7k
                      sizeof *md);
5797
12.7k
}
5798
5799
static void
5800
gtpu_to_attr(struct ofpbuf *a, const void *data_)
5801
6.54k
{
5802
6.54k
    const struct gtpu_metadata *md = data_;
5803
5804
6.54k
    nl_msg_put_unspec(a, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS, md,
5805
6.54k
                      sizeof *md);
5806
6.54k
}
5807
5808
#define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC)                      \
5809
885k
    {                                                             \
5810
885k
        unsigned long call_fn = (unsigned long)FUNC;              \
5811
885k
        if (call_fn) {                                            \
5812
62.0k
            typedef void (*fn)(struct ofpbuf *, const void *);    \
5813
62.0k
            fn func = FUNC;                                       \
5814
62.0k
            func(BUF, &(DATA));                                   \
5815
823k
        } else {                                                  \
5816
823k
            nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5817
823k
        }                                                         \
5818
885k
    }
5819
5820
#define SCAN_IF(NAME)                           \
5821
9.81M
    if (strncmp(s, NAME, strlen(NAME)) == 0) {  \
5822
352k
        const char *start = s;                  \
5823
352k
        int len;                                \
5824
352k
                                                \
5825
352k
        s += strlen(NAME)
5826
5827
/* Usually no special initialization is needed. */
5828
#define SCAN_BEGIN(NAME, TYPE)                  \
5829
8.22M
    SCAN_IF(NAME);                              \
5830
324k
        TYPE skey, smask;                       \
5831
324k
        memset(&skey, 0, sizeof skey);          \
5832
324k
        memset(&smask, 0, sizeof smask);        \
5833
339k
        do {                                    \
5834
339k
            len = 0;
5835
5836
/* Init as fully-masked as mask will not be scanned. */
5837
#define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE)     \
5838
443k
    SCAN_IF(NAME);                              \
5839
502
        TYPE skey, smask;                       \
5840
502
        memset(&skey, 0, sizeof skey);          \
5841
502
        memset(&smask, 0xff, sizeof smask);     \
5842
502
        do {                                    \
5843
502
            len = 0;
5844
5845
/* VLAN needs special initialization. */
5846
#define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT)  \
5847
377k
    SCAN_IF(NAME);                                        \
5848
4.68k
        TYPE skey = KEY_INIT;                       \
5849
4.68k
        TYPE smask = MASK_INIT;                     \
5850
5.45k
        do {                                        \
5851
5.45k
            len = 0;
5852
5853
/* Scan unnamed entry as 'TYPE' */
5854
#define SCAN_TYPE(TYPE, KEY, MASK)              \
5855
256k
    len = scan_##TYPE(s, KEY, MASK);            \
5856
256k
    if (len == 0) {                             \
5857
3.10k
        return -EINVAL;                         \
5858
3.10k
    }                                           \
5859
256k
    s += len
5860
5861
/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5862
#define SCAN_FIELD(NAME, TYPE, FIELD)                                   \
5863
949k
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
5864
60.6k
        s += strlen(NAME);                                              \
5865
60.6k
        SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL);       \
5866
59.9k
        continue;                                                       \
5867
60.6k
    }
5868
5869
#define SCAN_FINISH()                           \
5870
403k
        } while (*s++ == ',' && len != 0);      \
5871
281k
        if (s[-1] != ')') {                     \
5872
7.66k
            return -EINVAL;                     \
5873
7.66k
        }
5874
5875
#define SCAN_FINISH_SINGLE()                    \
5876
70.8k
        } while (false);                        \
5877
71.4k
        if (*s++ != ')') {                      \
5878
576
            return -EINVAL;                     \
5879
576
        }
5880
5881
/* Beginning of nested attribute. */
5882
#define SCAN_BEGIN_NESTED(NAME, ATTR)                      \
5883
402k
    SCAN_IF(NAME);                                         \
5884
17.8k
        size_t key_offset, mask_offset = 0;                \
5885
17.8k
        key_offset = nl_msg_start_nested(key, ATTR);       \
5886
17.8k
        if (mask) {                                        \
5887
17.8k
            mask_offset = nl_msg_start_nested(mask, ATTR); \
5888
17.8k
        }                                                  \
5889
125k
        do {                                               \
5890
125k
            len = 0;
5891
5892
#define SCAN_END_NESTED()                                                     \
5893
16.3k
        SCAN_FINISH();                                                        \
5894
14.4k
        if (nl_attr_oversized(key->size - key_offset - NLA_HDRLEN)) {         \
5895
8
            return -E2BIG;                                                    \
5896
8
        }                                                                     \
5897
14.4k
        nl_msg_end_nested(key, key_offset);                                   \
5898
14.4k
        if (mask) {                                                           \
5899
14.4k
            nl_msg_end_nested(mask, mask_offset);                             \
5900
14.4k
        }                                                                     \
5901
14.4k
        return s - start;                                                     \
5902
14.4k
    }
5903
5904
#define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC)  \
5905
824k
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                \
5906
121k
        TYPE skey, smask;                                     \
5907
121k
        memset(&skey, 0, sizeof skey);                        \
5908
121k
        memset(&smask, 0xff, sizeof smask);                   \
5909
121k
        s += strlen(NAME);                                    \
5910
121k
        SCAN_TYPE(SCAN_AS, &skey, &smask);                    \
5911
119k
        SCAN_PUT(ATTR, FUNC);                                 \
5912
119k
        continue;                                             \
5913
121k
    }
5914
5915
#define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR)  \
5916
712k
        SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5917
5918
#define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC)  \
5919
111k
        SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5920
5921
#define SCAN_PUT(ATTR, FUNC)                            \
5922
442k
        SCAN_PUT_ATTR(key, ATTR, skey, FUNC);           \
5923
442k
        if (mask)                                       \
5924
442k
            SCAN_PUT_ATTR(mask, ATTR, smask, FUNC);     \
5925
5926
#define SCAN_END(ATTR)                                  \
5927
257k
        SCAN_FINISH();                                  \
5928
252k
        SCAN_PUT(ATTR, NULL);                           \
5929
252k
        return s - start;                               \
5930
257k
    }
5931
5932
#define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT)       \
5933
368k
    SCAN_IF(NAME);                              \
5934
4.66k
        TYPE skey[CNT], smask[CNT];             \
5935
4.66k
        memset(&skey, 0, sizeof skey);          \
5936
4.66k
        memset(&smask, 0, sizeof smask);        \
5937
4.66k
        int idx = 0, cnt = CNT;                 \
5938
4.66k
        uint64_t fields = 0;                    \
5939
7.10k
        do {                                    \
5940
7.10k
            int field = 0;                      \
5941
7.10k
            len = 0;
5942
5943
/* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5944
#define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD)                             \
5945
21.6k
    if (strncmp(s, NAME, strlen(NAME)) == 0) {                          \
5946
4.74k
        if (fields & (1UL << field)) {                                  \
5947
2.15k
            fields = 0;                                                 \
5948
2.15k
            if (++idx == cnt) {                                         \
5949
12
                break;                                                  \
5950
12
            }                                                           \
5951
2.15k
        }                                                               \
5952
4.74k
        s += strlen(NAME);                                              \
5953
4.72k
        SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5954
4.51k
        fields |= 1UL << field;                                         \
5955
4.51k
        continue;                                                       \
5956
4.72k
    }                                                                   \
5957
21.6k
    field++;
5958
5959
#define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT)                    \
5960
8.16k
    nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5961
5962
#define SCAN_PUT_ARRAY(ATTR, CNT)                        \
5963
4.08k
    SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT);       \
5964
4.08k
    if (mask) {                                      \
5965
4.08k
        SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5966
4.08k
    }
5967
5968
#define SCAN_END_ARRAY(ATTR)             \
5969
4.45k
        SCAN_FINISH();                   \
5970
4.08k
        if (idx == cnt) {                \
5971
0
            return -EINVAL;              \
5972
0
        }                                \
5973
4.08k
        SCAN_PUT_ARRAY(ATTR, idx + 1);   \
5974
4.08k
        return s - start;                \
5975
4.08k
    }
5976
5977
#define SCAN_END_SINGLE(ATTR)                           \
5978
70.8k
        SCAN_FINISH_SINGLE();                           \
5979
70.2k
        SCAN_PUT(ATTR, NULL);                           \
5980
70.2k
        return s - start;                               \
5981
70.8k
    }
5982
5983
#define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR)       \
5984
3.76M
    SCAN_BEGIN(NAME, TYPE) {                         \
5985
139k
        SCAN_TYPE(SCAN_AS, &skey, &smask);           \
5986
69.5k
    } SCAN_END_SINGLE(ATTR)
5987
5988
#define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5989
443k
    SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) {                   \
5990
1.00k
        SCAN_TYPE(SCAN_AS, &skey, NULL);                    \
5991
502
    } SCAN_END_SINGLE(ATTR)
5992
5993
/* scan_port needs one extra argument. */
5994
#define SCAN_SINGLE_PORT(NAME, TYPE, ATTR)  \
5995
384k
    SCAN_BEGIN(NAME, TYPE) {                            \
5996
2.89k
        len = scan_port(s, &skey, &smask,               \
5997
2.89k
                        context->port_names);           \
5998
2.89k
        if (len == 0) {                                 \
5999
10
            return -EINVAL;                             \
6000
10
        }                                               \
6001
2.89k
        s += len;                                       \
6002
1.44k
    } 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
99.9k
{
6008
99.9k
    if (strncmp(s, "nsh(", 4) == 0) {
6009
46.2k
        const char *start = s;
6010
46.2k
        int len;
6011
46.2k
        struct ovs_key_nsh skey, smask;
6012
46.2k
        uint32_t spi = 0, spi_mask = 0;
6013
46.2k
        uint8_t si = 0, si_mask = 0;
6014
6015
46.2k
        s += 4;
6016
6017
46.2k
        memset(&skey, 0, sizeof skey);
6018
46.2k
        memset(&smask, 0, sizeof smask);
6019
49.4k
        do {
6020
49.4k
            len = 0;
6021
6022
49.4k
            if (strncmp(s, "flags=", 6) == 0) {
6023
554
                s += 6;
6024
554
                len = scan_u8(s, &skey.flags, mask ? &smask.flags : NULL);
6025
554
                if (len == 0) {
6026
6
                    return -EINVAL;
6027
6
                }
6028
548
                s += len;
6029
548
                continue;
6030
554
            }
6031
6032
48.8k
            if (strncmp(s, "mdtype=", 7) == 0) {
6033
20.6k
                s += 7;
6034
20.6k
                len = scan_u8(s, &skey.mdtype, mask ? &smask.mdtype : NULL);
6035
20.6k
                if (len == 0) {
6036
8
                    return -EINVAL;
6037
8
                }
6038
20.6k
                s += len;
6039
20.6k
                continue;
6040
20.6k
            }
6041
6042
28.2k
            if (strncmp(s, "np=", 3) == 0) {
6043
2.00k
                s += 3;
6044
2.00k
                len = scan_u8(s, &skey.np, mask ? &smask.np : NULL);
6045
2.00k
                if (len == 0) {
6046
10
                    return -EINVAL;
6047
10
                }
6048
1.99k
                s += len;
6049
1.99k
                continue;
6050
2.00k
            }
6051
6052
26.2k
            if (strncmp(s, "spi=", 4) == 0) {
6053
1.45k
                s += 4;
6054
1.45k
                len = scan_u32(s, &spi, mask ? &spi_mask : NULL);
6055
1.45k
                if (len == 0) {
6056
10
                    return -EINVAL;
6057
10
                }
6058
1.44k
                s += len;
6059
1.44k
                continue;
6060
1.45k
            }
6061
6062
24.8k
            if (strncmp(s, "si=", 3) == 0) {
6063
1.20k
                s += 3;
6064
1.20k
                len = scan_u8(s, &si, mask ? &si_mask : NULL);
6065
1.20k
                if (len == 0) {
6066
13
                    return -EINVAL;
6067
13
                }
6068
1.19k
                s += len;
6069
1.19k
                continue;
6070
1.20k
            }
6071
6072
23.6k
            if (strncmp(s, "c1=", 3) == 0) {
6073
10.8k
                s += 3;
6074
10.8k
                len = scan_be32(s, &skey.context[0],
6075
10.8k
                                mask ? &smask.context[0] : NULL);
6076
10.8k
                if (len == 0) {
6077
8
                    return -EINVAL;
6078
8
                }
6079
10.7k
                s += len;
6080
10.7k
                continue;
6081
10.8k
            }
6082
6083
12.8k
            if (strncmp(s, "c2=", 3) == 0) {
6084
7.61k
                s += 3;
6085
7.61k
                len = scan_be32(s, &skey.context[1],
6086
7.61k
                                mask ? &smask.context[1] : NULL);
6087
7.61k
                if (len == 0) {
6088
14
                    return -EINVAL;
6089
14
                }
6090
7.60k
                s += len;
6091
7.60k
                continue;
6092
7.61k
            }
6093
6094
5.19k
            if (strncmp(s, "c3=", 3) == 0) {
6095
2.95k
                s += 3;
6096
2.95k
                len = scan_be32(s, &skey.context[2],
6097
2.95k
                                mask ? &smask.context[2] : NULL);
6098
2.95k
                if (len == 0) {
6099
10
                    return -EINVAL;
6100
10
                }
6101
2.94k
                s += len;
6102
2.94k
                continue;
6103
2.95k
            }
6104
6105
2.23k
            if (strncmp(s, "c4=", 3) == 0) {
6106
1.15k
                s += 3;
6107
1.15k
                len = scan_be32(s, &skey.context[3],
6108
1.15k
                                mask ? &smask.context[3] : NULL);
6109
1.15k
                if (len == 0) {
6110
10
                    return -EINVAL;
6111
10
                }
6112
1.14k
                s += len;
6113
1.14k
                continue;
6114
1.15k
            }
6115
49.3k
        } while (*s++ == ',' && len != 0);
6116
46.1k
        if (s[-1] != ')') {
6117
793
            return -EINVAL;
6118
793
        }
6119
6120
45.3k
        skey.path_hdr = nsh_spi_si_to_path_hdr(spi, si);
6121
45.3k
        smask.path_hdr = nsh_spi_si_to_path_hdr(spi_mask, si_mask);
6122
6123
45.3k
        nsh_key_to_attr(key, &skey, NULL, 0, false);
6124
45.3k
        if (mask) {
6125
45.3k
            nsh_key_to_attr(mask, &smask, NULL, 0, true);
6126
45.3k
        }
6127
45.3k
        return s - start;
6128
46.1k
    }
6129
53.7k
    return 0;
6130
99.9k
}
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
452k
{
6136
452k
    int retval;
6137
6138
452k
    context->depth++;
6139
6140
452k
    if (context->depth == MAX_ODP_NESTED) {
6141
4
        retval = -EINVAL;
6142
452k
    } else {
6143
452k
        retval = parse_odp_key_mask_attr__(context, s, key, mask);
6144
452k
    }
6145
6146
452k
    context->depth--;
6147
6148
452k
    return retval;
6149
452k
}
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
452k
{
6155
452k
    SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
6156
450k
    SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
6157
443k
    SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
6158
442k
                             OVS_KEY_ATTR_RECIRC_ID);
6159
442k
    SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
6160
6161
441k
    SCAN_SINGLE("ct_state(", uint32_t, ct_state, OVS_KEY_ATTR_CT_STATE);
6162
438k
    SCAN_SINGLE("ct_zone(", uint16_t, u16, OVS_KEY_ATTR_CT_ZONE);
6163
432k
    SCAN_SINGLE("ct_mark(", uint32_t, u32, OVS_KEY_ATTR_CT_MARK);
6164
428k
    SCAN_SINGLE("ct_label(", ovs_u128, u128, OVS_KEY_ATTR_CT_LABELS);
6165
6166
412k
    SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4) {
6167
12.8k
        SCAN_FIELD("src=", ipv4, ipv4_src);
6168
6.94k
        SCAN_FIELD("dst=", ipv4, ipv4_dst);
6169
6.39k
        SCAN_FIELD("proto=", u8, ipv4_proto);
6170
5.04k
        SCAN_FIELD("tp_src=", be16, src_port);
6171
4.64k
        SCAN_FIELD("tp_dst=", be16, dst_port);
6172
406k
    } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
6173
6174
406k
    SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6) {
6175
10.5k
        SCAN_FIELD("src=", in6_addr, ipv6_src);
6176
5.60k
        SCAN_FIELD("dst=", in6_addr, ipv6_dst);
6177
5.11k
        SCAN_FIELD("proto=", u8, ipv6_proto);
6178
4.14k
        SCAN_FIELD("tp_src=", be16, src_port);
6179
3.73k
        SCAN_FIELD("tp_dst=", be16, dst_port);
6180
402k
    } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
6181
6182
402k
    SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL) {
6183
143k
        SCAN_FIELD_NESTED("tun_id=", ovs_be64, be64, OVS_TUNNEL_KEY_ATTR_ID);
6184
89.4k
        SCAN_FIELD_NESTED("src=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_SRC);
6185
88.0k
        SCAN_FIELD_NESTED("dst=", ovs_be32, ipv4, OVS_TUNNEL_KEY_ATTR_IPV4_DST);
6186
85.0k
        SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_SRC);
6187
83.4k
        SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr, in6_addr, OVS_TUNNEL_KEY_ATTR_IPV6_DST);
6188
82.0k
        SCAN_FIELD_NESTED("tos=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TOS);
6189
78.4k
        SCAN_FIELD_NESTED("ttl=", uint8_t, u8, OVS_TUNNEL_KEY_ATTR_TTL);
6190
41.6k
        SCAN_FIELD_NESTED("tp_src=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_SRC);
6191
38.9k
        SCAN_FIELD_NESTED("tp_dst=", ovs_be16, be16, OVS_TUNNEL_KEY_ATTR_TP_DST);
6192
36.7k
        SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata, erspan_metadata,
6193
30.1k
                               erspan_to_attr);
6194
30.1k
        SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp, vxlan_gbp_to_attr);
6195
25.7k
        SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan, geneve,
6196
11.3k
                               geneve_to_attr);
6197
11.3k
        SCAN_FIELD_NESTED_FUNC("gtpu(", struct gtpu_metadata, gtpu_metadata,
6198
7.85k
                               gtpu_to_attr);
6199
7.85k
        SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags, tun_flags_to_attr);
6200
384k
    } SCAN_END_NESTED();
6201
6202
384k
    SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
6203
6204
383k
    SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
6205
11.3k
        SCAN_FIELD("src=", eth, eth_src);
6206
5.59k
        SCAN_FIELD("dst=", eth, eth_dst);
6207
377k
    } SCAN_END(OVS_KEY_ATTR_ETHERNET);
6208
6209
377k
    SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
6210
10.1k
                    { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
6211
10.1k
        SCAN_FIELD("vid=", vid, tci);
6212
3.90k
        SCAN_FIELD("pcp=", pcp, tci);
6213
3.28k
        SCAN_FIELD("cfi=", cfi, tci);
6214
372k
    } SCAN_END(OVS_KEY_ATTR_VLAN);
6215
6216
372k
    SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
6217
6218
368k
    SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls, FLOW_MAX_MPLS_LABELS) {
6219
11.7k
        SCAN_FIELD_ARRAY("label=", mpls_label, mpls_lse);
6220
6.33k
        SCAN_FIELD_ARRAY("tc=", mpls_tc, mpls_lse);
6221
5.04k
        SCAN_FIELD_ARRAY("ttl=", mpls_ttl, mpls_lse);
6222
3.19k
        SCAN_FIELD_ARRAY("bos=", mpls_bos, mpls_lse);
6223
363k
    } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS);
6224
6225
363k
    SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
6226
51.2k
        SCAN_FIELD("src=", ipv4, ipv4_src);
6227
26.3k
        SCAN_FIELD("dst=", ipv4, ipv4_dst);
6228
26.0k
        SCAN_FIELD("proto=", u8, ipv4_proto);
6229
24.3k
        SCAN_FIELD("tos=", u8, ipv4_tos);
6230
23.9k
        SCAN_FIELD("ttl=", u8, ipv4_ttl);
6231
22.9k
        SCAN_FIELD("frag=", frag, ipv4_frag);
6232
339k
    } SCAN_END(OVS_KEY_ATTR_IPV4);
6233
6234
339k
    SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
6235
49.5k
        SCAN_FIELD("src=", in6_addr, ipv6_src);
6236
24.6k
        SCAN_FIELD("dst=", in6_addr, ipv6_dst);
6237
23.5k
        SCAN_FIELD("label=", ipv6_label, ipv6_label);
6238
22.8k
        SCAN_FIELD("proto=", u8, ipv6_proto);
6239
20.7k
        SCAN_FIELD("tclass=", u8, ipv6_tclass);
6240
20.4k
        SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
6241
20.0k
        SCAN_FIELD("frag=", frag, ipv6_frag);
6242
315k
    } SCAN_END(OVS_KEY_ATTR_IPV6);
6243
6244
315k
    SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
6245
13.6k
        SCAN_FIELD("src=", be16, tcp_src);
6246
6.54k
        SCAN_FIELD("dst=", be16, tcp_dst);
6247
308k
    } SCAN_END(OVS_KEY_ATTR_TCP);
6248
6249
308k
    SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
6250
6251
283k
    SCAN_BEGIN("udp(", struct ovs_key_udp) {
6252
7.40k
        SCAN_FIELD("src=", be16, udp_src);
6253
3.69k
        SCAN_FIELD("dst=", be16, udp_dst);
6254
280k
    } SCAN_END(OVS_KEY_ATTR_UDP);
6255
6256
280k
    SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
6257
6.18k
        SCAN_FIELD("src=", be16, sctp_src);
6258
3.00k
        SCAN_FIELD("dst=", be16, sctp_dst);
6259
277k
    } SCAN_END(OVS_KEY_ATTR_SCTP);
6260
6261
277k
    SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
6262
13.5k
        SCAN_FIELD("type=", u8, icmp_type);
6263
5.43k
        SCAN_FIELD("code=", u8, icmp_code);
6264
271k
    } SCAN_END(OVS_KEY_ATTR_ICMP);
6265
6266
271k
    SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
6267
6.44k
        SCAN_FIELD("type=", u8, icmpv6_type);
6268
2.43k
        SCAN_FIELD("code=", u8, icmpv6_code);
6269
267k
    } SCAN_END(OVS_KEY_ATTR_ICMPV6);
6270
6271
267k
    SCAN_BEGIN("arp(", struct ovs_key_arp) {
6272
20.8k
        SCAN_FIELD("sip=", ipv4, arp_sip);
6273
10.6k
        SCAN_FIELD("tip=", ipv4, arp_tip);
6274
10.3k
        SCAN_FIELD("op=", be16, arp_op);
6275
9.78k
        SCAN_FIELD("sha=", eth, arp_sha);
6276
9.52k
        SCAN_FIELD("tha=", eth, arp_tha);
6277
258k
    } SCAN_END(OVS_KEY_ATTR_ARP);
6278
6279
296k
    SCAN_BEGIN("nd(", struct ovs_key_nd) {
6280
296k
        SCAN_FIELD("target=", in6_addr, nd_target);
6281
148k
        SCAN_FIELD("sll=", eth, nd_sll);
6282
147k
        SCAN_FIELD("tll=", eth, nd_tll);
6283
147k
    } SCAN_END(OVS_KEY_ATTR_ND);
6284
6285
110k
    SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions) {
6286
10.5k
        SCAN_FIELD("nd_reserved=", be32, nd_reserved);
6287
2.81k
        SCAN_FIELD("nd_options_type=", u8, nd_options_type);
6288
105k
    } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS);
6289
6290
105k
    struct packet_type {
6291
105k
        ovs_be16 ns;
6292
105k
        ovs_be16 id;
6293
105k
    };
6294
105k
    SCAN_BEGIN("packet_type(", struct packet_type) {
6295
11.7k
        SCAN_FIELD("ns=", be16, ns);
6296
5.57k
        SCAN_FIELD("id=", be16, id);
6297
99.9k
    } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE);
6298
6299
    /* nsh is nested, it needs special process */
6300
99.9k
    int ret = parse_odp_nsh_key_mask_attr(s, key, mask);
6301
99.9k
    if (ret < 0) {
6302
882
        return ret;
6303
99.0k
    } else {
6304
99.0k
        s += ret;
6305
99.0k
    }
6306
6307
    /* Encap open-coded. */
6308
99.0k
    if (!strncmp(s, "encap(", 6)) {
6309
85.9k
        const char *start = s;
6310
85.9k
        size_t encap, encap_mask = 0;
6311
6312
85.9k
        encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
6313
85.9k
        if (mask) {
6314
85.9k
            encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
6315
85.9k
        }
6316
6317
85.9k
        s += 6;
6318
384k
        for (;;) {
6319
384k
            int retval;
6320
6321
384k
            s += strspn(s, delimiters);
6322
384k
            if (!*s) {
6323
65
                return -EINVAL;
6324
384k
            } else if (*s == ')') {
6325
85.0k
                break;
6326
85.0k
            }
6327
6328
299k
            retval = parse_odp_key_mask_attr(context, s, key, mask);
6329
299k
            if (retval < 0) {
6330
840
                return retval;
6331
840
            }
6332
6333
298k
            if (nl_attr_oversized(key->size - encap - NLA_HDRLEN)) {
6334
5
                return -E2BIG;
6335
5
            }
6336
298k
            s += retval;
6337
298k
        }
6338
85.0k
        s++;
6339
6340
85.0k
        nl_msg_end_nested(key, encap);
6341
85.0k
        if (mask) {
6342
85.0k
            nl_msg_end_nested(mask, encap_mask);
6343
85.0k
        }
6344
6345
85.0k
        return s - start;
6346
85.9k
    }
6347
6348
13.1k
    return -EINVAL;
6349
99.0k
}
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
34.7k
{
6370
34.7k
    if (errorp) {
6371
0
        *errorp = NULL;
6372
0
    }
6373
6374
34.7k
    const size_t old_size = key->size;
6375
34.7k
    struct parse_odp_context context = (struct parse_odp_context) {
6376
34.7k
        .port_names = port_names,
6377
34.7k
    };
6378
148k
    for (;;) {
6379
148k
        int retval;
6380
6381
148k
        s += strspn(s, delimiters);
6382
148k
        if (!*s) {
6383
10.0k
            return 0;
6384
10.0k
        }
6385
6386
        /* Skip UFID. */
6387
138k
        ovs_u128 ufid;
6388
138k
        retval = odp_ufid_from_string(s, &ufid);
6389
138k
        if (retval < 0) {
6390
102
            if (errorp) {
6391
0
                *errorp = xasprintf("syntax error at %s", s);
6392
0
            }
6393
102
            key->size = old_size;
6394
102
            return -retval;
6395
137k
        } else if (retval > 0) {
6396
260
            s += retval;
6397
260
            s += s[0] == ' ' ? 1 : 0;
6398
260
        }
6399
6400
137k
        retval = parse_odp_key_mask_attr(&context, s, key, mask);
6401
6402
137k
        if (retval >= 0) {
6403
113k
            if (nl_attr_oversized(key->size - NLA_HDRLEN)) {
6404
4
                retval = -E2BIG;
6405
113k
            } else if (mask && nl_attr_oversized(mask->size - NLA_HDRLEN)) {
6406
4
                retval = -E2BIG;
6407
4
            }
6408
113k
        }
6409
6410
137k
        if (retval < 0) {
6411
24.6k
            if (errorp) {
6412
0
                *errorp = xasprintf("syntax error at %s", s);
6413
0
            }
6414
24.6k
            key->size = old_size;
6415
24.6k
            return -retval;
6416
24.6k
        }
6417
113k
        s += retval;
6418
113k
    }
6419
6420
0
    return 0;
6421
34.7k
}
6422
6423
static uint8_t
6424
ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
6425
789
{
6426
789
    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
789
    return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
6435
789
        : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
6436
9
        : OVS_FRAG_TYPE_FIRST;
6437
789
}
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
2.75k
{
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
2.75k
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
6472
6473
2.75k
    struct ovs_key_ethernet *eth_key;
6474
2.75k
    size_t encap[FLOW_MAX_VLAN_HEADERS] = {0};
6475
2.75k
    size_t max_vlans;
6476
2.75k
    const struct flow *flow = parms->flow;
6477
2.75k
    const struct flow *mask = parms->mask;
6478
2.75k
    const struct flow *data = export_mask ? mask : flow;
6479
6480
2.75k
    if (parms->support.recirc) {
6481
2.75k
        nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
6482
2.75k
        nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
6483
2.75k
    }
6484
6485
2.75k
    nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
6486
6487
2.75k
    if (flow_tnl_dst_is_set(&flow->tunnel) ||
6488
2.37k
        flow_tnl_src_is_set(&flow->tunnel) || export_mask) {
6489
670
        tun_key_to_attr(buf, &data->tunnel, &parms->flow->tunnel,
6490
670
                        parms->key_buf, NULL);
6491
670
    }
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
2.75k
    if (export_mask || flow->in_port.odp_port != ODPP_NONE) {
6496
59
        nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, data->in_port.odp_port);
6497
59
    }
6498
6499
2.75k
    nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
6500
6501
2.75k
    if (parms->support.ct_state) {
6502
2.75k
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_STATE,
6503
2.75k
                       ovs_to_odp_ct_state(data->ct_state));
6504
2.75k
    }
6505
2.75k
    if (parms->support.ct_zone) {
6506
2.75k
        nl_msg_put_u16(buf, OVS_KEY_ATTR_CT_ZONE, data->ct_zone);
6507
2.75k
    }
6508
2.75k
    if (parms->support.ct_mark) {
6509
2.75k
        nl_msg_put_u32(buf, OVS_KEY_ATTR_CT_MARK, data->ct_mark);
6510
2.75k
    }
6511
2.75k
    if (parms->support.ct_label) {
6512
2.75k
        nl_msg_put_unspec(buf, OVS_KEY_ATTR_CT_LABELS, &data->ct_label,
6513
2.75k
                          sizeof(data->ct_label));
6514
2.75k
    }
6515
2.75k
    if (flow->ct_nw_proto) {
6516
19
        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
19
        } 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
19
    }
6542
6543
2.75k
    nl_msg_put_be32(buf, OVS_KEY_ATTR_PACKET_TYPE, data->packet_type);
6544
6545
2.75k
    if (OVS_UNLIKELY(parms->probe)) {
6546
0
        max_vlans = FLOW_MAX_VLAN_HEADERS;
6547
2.75k
    } else {
6548
2.75k
        max_vlans = MIN(parms->support.max_vlan_headers, flow_vlan_limit);
6549
2.75k
    }
6550
6551
    /* Conditionally add L2 attributes for Ethernet packets */
6552
2.75k
    if (flow->packet_type == htonl(PT_ETH)) {
6553
2.36k
        eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
6554
2.36k
                                           sizeof *eth_key);
6555
2.36k
        get_ethernet_key(data, eth_key);
6556
6557
2.38k
        for (int encaps = 0; encaps < max_vlans; encaps++) {
6558
2.37k
            ovs_be16 tpid = flow->vlans[encaps].tpid;
6559
6560
2.37k
            if (flow->vlans[encaps].tci == htons(0)) {
6561
2.36k
                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
2.36k
                } else {
6565
2.36k
                    break;
6566
2.36k
                }
6567
2.36k
            }
6568
6569
18
            if (export_mask) {
6570
0
                nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6571
18
            } else {
6572
18
                nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, tpid);
6573
18
            }
6574
18
            nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlans[encaps].tci);
6575
18
            encap[encaps] = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
6576
18
            if (flow->vlans[encaps].tci == htons(0)) {
6577
0
                goto unencap;
6578
0
            }
6579
18
        }
6580
2.36k
    }
6581
6582
2.75k
    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
1.72k
        if (export_mask) {
6595
0
            nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
6596
0
        }
6597
1.72k
        goto unencap;
6598
1.72k
    }
6599
6600
1.02k
    nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
6601
6602
1.02k
    if (eth_type_vlan(flow->dl_type)) {
6603
1
        goto unencap;
6604
1
    }
6605
6606
1.02k
    if (flow->dl_type == htons(ETH_TYPE_IP)) {
6607
89
        struct ovs_key_ipv4 *ipv4_key;
6608
6609
89
        ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
6610
89
                                            sizeof *ipv4_key);
6611
89
        get_ipv4_key(data, ipv4_key, export_mask);
6612
939
    } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
6613
700
        struct ovs_key_ipv6 *ipv6_key;
6614
6615
700
        ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
6616
700
                                            sizeof *ipv6_key);
6617
700
        get_ipv6_key(data, ipv6_key, export_mask);
6618
700
    } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
6619
231
               flow->dl_type == htons(ETH_TYPE_RARP)) {
6620
12
        struct ovs_key_arp *arp_key;
6621
6622
12
        arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
6623
12
                                           sizeof *arp_key);
6624
12
        get_arp_key(data, arp_key);
6625
227
    } else if (eth_type_mpls(flow->dl_type)) {
6626
43
        struct ovs_key_mpls *mpls_key;
6627
43
        int i, n;
6628
6629
43
        n = flow_count_mpls_labels(flow, NULL);
6630
43
        if (export_mask) {
6631
0
            n = MIN(n, parms->support.max_mpls_depth);
6632
0
        }
6633
43
        mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
6634
43
                                            n * sizeof *mpls_key);
6635
76
        for (i = 0; i < n; i++) {
6636
33
            mpls_key[i].mpls_lse = data->mpls_lse[i];
6637
33
        }
6638
184
    } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
6639
1
        nsh_key_to_attr(buf, &data->nsh, NULL, 0, export_mask);
6640
1
    }
6641
6642
1.02k
    if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
6643
684
        if (flow->nw_proto == IPPROTO_TCP) {
6644
11
            union ovs_key_tp *tcp_key;
6645
6646
11
            tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
6647
11
                                               sizeof *tcp_key);
6648
11
            get_tp_key(data, tcp_key);
6649
11
            if (data->tcp_flags || (mask && mask->tcp_flags)) {
6650
2
                nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
6651
2
            }
6652
673
        } else if (flow->nw_proto == IPPROTO_UDP) {
6653
3
            union ovs_key_tp *udp_key;
6654
6655
3
            udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
6656
3
                                               sizeof *udp_key);
6657
3
            get_tp_key(data, udp_key);
6658
670
        } else if (flow->nw_proto == IPPROTO_SCTP) {
6659
3
            union ovs_key_tp *sctp_key;
6660
6661
3
            sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
6662
3
                                               sizeof *sctp_key);
6663
3
            get_tp_key(data, sctp_key);
6664
667
        } else if (flow->dl_type == htons(ETH_TYPE_IP)
6665
60
                && flow->nw_proto == IPPROTO_ICMP) {
6666
7
            struct ovs_key_icmp *icmp_key;
6667
6668
7
            icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
6669
7
                                                sizeof *icmp_key);
6670
7
            icmp_key->icmp_type = ntohs(data->tp_src);
6671
7
            icmp_key->icmp_code = ntohs(data->tp_dst);
6672
660
        } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
6673
607
                && flow->nw_proto == IPPROTO_ICMPV6) {
6674
22
            struct ovs_key_icmpv6 *icmpv6_key;
6675
6676
22
            icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
6677
22
                                                  sizeof *icmpv6_key);
6678
22
            icmpv6_key->icmpv6_type = ntohs(data->tp_src);
6679
22
            icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
6680
6681
22
            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
4
                && (!export_mask || data->tp_src == htons(0xff))) {
6686
4
                struct ovs_key_nd *nd_key;
6687
4
                nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
6688
4
                                                    sizeof *nd_key);
6689
4
                nd_key->nd_target = data->nd_target;
6690
4
                nd_key->nd_sll = data->arp_sha;
6691
4
                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
4
                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
4
            }
6709
22
        }
6710
684
    }
6711
6712
2.75k
unencap:
6713
8.25k
    for (int encaps = max_vlans - 1; encaps >= 0; encaps--) {
6714
5.50k
        if (encap[encaps]) {
6715
18
            nl_msg_end_nested(buf, encap[encaps]);
6716
18
        }
6717
5.50k
    }
6718
2.75k
}
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
2.75k
{
6728
2.75k
    odp_flow_key_from_flow__(parms, false, buf);
6729
2.75k
}
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
450
{
6939
450
    struct ds s;
6940
450
    int i;
6941
6942
450
    if (VLOG_DROP_DBG(rl)) {
6943
450
        return;
6944
450
    }
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
787
{
6969
787
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
6970
6971
787
    if (is_mask) {
6972
0
        return odp_frag ? FLOW_NW_FRAG_MASK : 0;
6973
0
    }
6974
6975
787
    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
787
    return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
6981
787
        : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
6982
9
        :  FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
6983
787
}
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
5.05k
{
7002
5.05k
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7003
5.05k
    const struct nlattr *nla;
7004
5.05k
    uint64_t present_attrs;
7005
5.05k
    size_t left;
7006
7007
5.05k
    BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
7008
5.05k
    present_attrs = 0;
7009
5.05k
    *out_of_range_attrp = 0;
7010
7.54k
    NL_ATTR_FOR_EACH (nla, left, key, key_len) {
7011
7.54k
        uint16_t type = nl_attr_type(nla);
7012
7.54k
        size_t len = nl_attr_get_size(nla);
7013
7.54k
        int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
7014
7.54k
                                            OVS_KEY_ATTR_MAX, type);
7015
7016
7.54k
        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
7.54k
        if (type > OVS_KEY_ATTR_MAX) {
7028
0
            *out_of_range_attrp = type;
7029
7.54k
        } else {
7030
7.54k
            if (present_attrs & (UINT64_C(1) << type)) {
7031
1.62k
                char namebuf[OVS_KEY_ATTR_BUFSIZE];
7032
7033
1.62k
                odp_parse_error(&rl, errorp,
7034
1.62k
                                "duplicate %s attribute in flow key",
7035
1.62k
                                ovs_key_attr_to_string(type, namebuf,
7036
1.62k
                                                       sizeof namebuf));
7037
1.62k
                return false;
7038
1.62k
            }
7039
7040
5.91k
            present_attrs |= UINT64_C(1) << type;
7041
5.91k
            attrs[type] = nla;
7042
5.91k
        }
7043
7.54k
    }
7044
3.43k
    if (left) {
7045
0
        odp_parse_error(&rl, errorp, "trailing garbage in flow key");
7046
0
        return false;
7047
0
    }
7048
7049
3.43k
    *present_attrsp = present_attrs;
7050
3.43k
    return true;
7051
3.43k
}
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
2.76k
{
7058
2.76k
    uint64_t missing_attrs;
7059
2.76k
    uint64_t extra_attrs;
7060
7061
2.76k
    missing_attrs = expected_attrs & ~present_attrs;
7062
2.76k
    if (missing_attrs) {
7063
57
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7064
57
        log_odp_key_attributes(&rl, "expected but not present",
7065
57
                               missing_attrs, 0, key, key_len);
7066
57
        return ODP_FIT_TOO_LITTLE;
7067
57
    }
7068
7069
2.70k
    extra_attrs = present_attrs & ~expected_attrs;
7070
2.70k
    if (extra_attrs || out_of_range_attr) {
7071
393
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
7072
393
        log_odp_key_attributes(&rl, "present but not expected",
7073
393
                               extra_attrs, out_of_range_attr, key, key_len);
7074
393
        return ODP_FIT_TOO_MUCH;
7075
393
    }
7076
7077
2.31k
    return ODP_FIT_PERFECT;
7078
2.70k
}
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
2.82k
{
7097
2.82k
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7098
2.82k
    bool is_mask = flow != src_flow;
7099
7100
2.82k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
7101
304
        flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
7102
304
        if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
7103
17
            odp_parse_error(&rl, errorp,
7104
17
                            "invalid Ethertype %"PRIu16" in flow key",
7105
17
                            ntohs(flow->dl_type));
7106
17
            return false;
7107
17
        }
7108
287
        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
287
        *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
7116
2.52k
    } else {
7117
2.52k
        if (!is_mask) {
7118
            /* Default ethertype for well-known L3 packets. */
7119
2.52k
            if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
7120
88
                flow->dl_type = htons(ETH_TYPE_IP);
7121
2.43k
            } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
7122
697
                flow->dl_type = htons(ETH_TYPE_IPV6);
7123
1.73k
            } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7124
43
                flow->dl_type = htons(ETH_TYPE_MPLS);
7125
1.69k
            } else {
7126
1.69k
                flow->dl_type = htons(FLOW_DL_TYPE_NONE);
7127
1.69k
            }
7128
2.52k
        } 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
2.52k
    }
7138
2.81k
    return true;
7139
2.82k
}
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
2.74k
{
7165
2.74k
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7166
2.74k
    bool is_mask = src_flow != flow;
7167
2.74k
    const void *check_start = NULL;
7168
2.74k
    size_t check_len = 0;
7169
2.74k
    enum ovs_key_attr expected_bit = 0xff;
7170
7171
2.74k
    if (eth_type_mpls(src_flow->dl_type)) {
7172
44
        if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7173
44
            *expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
7174
44
        }
7175
44
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
7176
43
            size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
7177
43
            const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
7178
43
            int n = size / sizeof(ovs_be32);
7179
43
            int i;
7180
7181
43
            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
43
            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
96
            for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
7195
53
                flow->mpls_lse[i] = mpls_lse[i];
7196
53
            }
7197
43
            if (n > FLOW_MAX_MPLS_LABELS) {
7198
0
                return ODP_FIT_TOO_MUCH;
7199
0
            }
7200
7201
43
            if (!is_mask) {
7202
                /* BOS may be set only in the innermost label. */
7203
52
                for (i = 0; i < n - 1; i++) {
7204
10
                    if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
7205
1
                        odp_parse_error(&rl, errorp,
7206
1
                                        "MPLS BOS set in non-innermost label");
7207
1
                        return ODP_FIT_ERROR;
7208
1
                    }
7209
10
                }
7210
7211
                /* BOS must be set in the innermost label. */
7212
42
                if (n < FLOW_MAX_MPLS_LABELS
7213
40
                    && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
7214
38
                    return ODP_FIT_TOO_LITTLE;
7215
38
                }
7216
42
            }
7217
43
        }
7218
7219
5
        goto done;
7220
2.69k
    } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
7221
89
        if (!is_mask) {
7222
89
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
7223
89
        }
7224
89
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
7225
88
            const struct ovs_key_ipv4 *ipv4_key;
7226
7227
88
            ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
7228
88
            put_ipv4_key(ipv4_key, flow, is_mask);
7229
88
            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
88
            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
88
        }
7241
2.61k
    } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
7242
700
        if (!is_mask) {
7243
700
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
7244
700
        }
7245
700
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
7246
699
            const struct ovs_key_ipv6 *ipv6_key;
7247
7248
699
            ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
7249
699
            put_ipv6_key(ipv6_key, flow, is_mask);
7250
699
            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
699
            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
699
        }
7261
1.91k
    } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
7262
1.89k
               src_flow->dl_type == htons(ETH_TYPE_RARP)) {
7263
19
        if (!is_mask) {
7264
19
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
7265
19
        }
7266
19
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
7267
15
            const struct ovs_key_arp *arp_key;
7268
7269
15
            arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
7270
15
            if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
7271
7
                odp_parse_error(&rl, errorp,
7272
7
                                "unsupported ARP opcode %"PRIu16" in flow "
7273
7
                                "key", ntohs(arp_key->arp_op));
7274
7
                return ODP_FIT_ERROR;
7275
7
            }
7276
8
            put_arp_key(arp_key, flow);
7277
8
            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
8
        }
7283
1.89k
    } else if (src_flow->dl_type == htons(ETH_TYPE_NSH)) {
7284
1
        if (!is_mask) {
7285
1
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_NSH;
7286
1
        }
7287
1
        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
1.89k
    } else {
7300
1.89k
        goto done;
7301
1.89k
    }
7302
802
    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
802
    expected_bit = OVS_KEY_ATTR_UNSPEC;
7316
802
    if (src_flow->nw_proto == IPPROTO_TCP
7317
15
        && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7318
4
            src_flow->dl_type == htons(ETH_TYPE_IPV6))
7319
13
        && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7320
12
        if (!is_mask) {
7321
12
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
7322
12
        }
7323
12
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
7324
1
            const union ovs_key_tp *tcp_key;
7325
7326
1
            tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
7327
1
            put_tp_key(tcp_key, flow);
7328
1
            expected_bit = OVS_KEY_ATTR_TCP;
7329
1
        }
7330
12
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
7331
2
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
7332
2
            flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
7333
2
        }
7334
790
    } else if (src_flow->nw_proto == IPPROTO_UDP
7335
6
               && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7336
5
                   src_flow->dl_type == htons(ETH_TYPE_IPV6))
7337
4
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7338
3
        if (!is_mask) {
7339
3
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
7340
3
        }
7341
3
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
7342
1
            const union ovs_key_tp *udp_key;
7343
7344
1
            udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
7345
1
            put_tp_key(udp_key, flow);
7346
1
            expected_bit = OVS_KEY_ATTR_UDP;
7347
1
        }
7348
787
    } else if (src_flow->nw_proto == IPPROTO_SCTP
7349
5
               && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
7350
4
                   src_flow->dl_type == htons(ETH_TYPE_IPV6))
7351
4
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7352
3
        if (!is_mask) {
7353
3
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
7354
3
        }
7355
3
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
7356
1
            const union ovs_key_tp *sctp_key;
7357
7358
1
            sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
7359
1
            put_tp_key(sctp_key, flow);
7360
1
            expected_bit = OVS_KEY_ATTR_SCTP;
7361
1
        }
7362
784
    } else if (src_flow->nw_proto == IPPROTO_ICMP
7363
13
               && src_flow->dl_type == htons(ETH_TYPE_IP)
7364
8
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7365
7
        if (!is_mask) {
7366
7
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
7367
7
        }
7368
7
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
7369
1
            const struct ovs_key_icmp *icmp_key;
7370
7371
1
            icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
7372
1
            flow->tp_src = htons(icmp_key->icmp_type);
7373
1
            flow->tp_dst = htons(icmp_key->icmp_code);
7374
1
            expected_bit = OVS_KEY_ATTR_ICMP;
7375
1
        }
7376
777
    } else if (src_flow->nw_proto == IPPROTO_ICMPV6
7377
87
               && src_flow->dl_type == htons(ETH_TYPE_IPV6)
7378
85
               && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7379
84
        if (!is_mask) {
7380
84
            *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
7381
84
        }
7382
84
        if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
7383
82
            const struct ovs_key_icmpv6 *icmpv6_key;
7384
7385
82
            icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
7386
82
            flow->tp_src = htons(icmpv6_key->icmpv6_type);
7387
82
            flow->tp_dst = htons(icmpv6_key->icmpv6_code);
7388
82
            expected_bit = OVS_KEY_ATTR_ICMPV6;
7389
82
            if (is_nd(src_flow, NULL)) {
7390
4
                if (!is_mask) {
7391
4
                    *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
7392
4
                }
7393
4
                if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
7394
1
                    const struct ovs_key_nd *nd_key;
7395
7396
1
                    nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
7397
1
                    flow->nd_target = nd_key->nd_target;
7398
1
                    flow->arp_sha = nd_key->nd_sll;
7399
1
                    flow->arp_tha = nd_key->nd_tll;
7400
1
                    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
1
                }
7418
4
                if (present_attrs &
7419
4
                    (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS)) {
7420
1
                    const struct ovs_key_nd_extensions *nd_ext_key;
7421
1
                    if (!is_mask) {
7422
1
                        *expected_attrs |=
7423
1
                                UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS;
7424
1
                    }
7425
7426
1
                    nd_ext_key =
7427
1
                        nl_attr_get(attrs[OVS_KEY_ATTR_ND_EXTENSIONS]);
7428
1
                    flow->igmp_group_ip4 = nd_ext_key->nd_reserved;
7429
1
                    flow->tcp_flags = htons(nd_ext_key->nd_options_type);
7430
7431
1
                    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
1
                }
7447
4
            }
7448
82
        }
7449
84
    }
7450
802
    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
2.69k
done:
7461
2.69k
    return need_check ? check_expectations(present_attrs, out_of_range_attr,
7462
2.65k
                            *expected_attrs, key, key_len) : ODP_FIT_PERFECT;
7463
802
}
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
67
{
7474
67
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7475
67
    bool is_mask = src_flow != flow;
7476
7477
67
    const struct nlattr *encap;
7478
67
    enum odp_key_fitness encap_fitness;
7479
67
    enum odp_key_fitness fitness = ODP_FIT_ERROR;
7480
67
    int vlan_limit;
7481
67
    int encaps = 0;
7482
7483
67
    vlan_limit = ignore_vlan_limit ? FLOW_MAX_VLAN_HEADERS : flow_vlan_limit;
7484
111
    while (encaps < vlan_limit &&
7485
106
           (is_mask
7486
106
            ? (src_flow->vlans[encaps].tci & htons(VLAN_CFI)) != 0
7487
106
            : eth_type_vlan(flow->dl_type))) {
7488
7489
78
        encap = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
7490
78
                 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
7491
7492
        /* Calculate fitness of outer attributes. */
7493
78
        if (!is_mask) {
7494
78
            expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
7495
78
                              (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
7496
78
        } 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
78
        fitness = check_expectations(present_attrs, out_of_range_attr,
7505
78
                                     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
78
        flow->vlans[encaps].tpid = flow->dl_type;
7510
78
        flow->dl_type = htons(0);
7511
78
        flow->vlans[encaps].tci =
7512
78
                        (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
7513
78
                        ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
7514
78
                        : htons(0));
7515
78
        if (!is_mask) {
7516
78
            if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) ||
7517
72
                !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
7518
23
                return ODP_FIT_TOO_LITTLE;
7519
55
            } else if (flow->vlans[encaps].tci == htons(0)) {
7520
                /* Corner case for a truncated 802.1Q header. */
7521
3
                if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
7522
1
                    return ODP_FIT_TOO_MUCH;
7523
1
                }
7524
2
                return fitness;
7525
52
            } else if (!(flow->vlans[encaps].tci & htons(VLAN_CFI))) {
7526
1
                odp_parse_error(
7527
1
                    &rl, errorp, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
7528
1
                    "but CFI bit is not set", ntohs(flow->vlans[encaps].tci));
7529
1
                return ODP_FIT_ERROR;
7530
1
            }
7531
78
        } 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
51
        if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
7539
51
                                attrs, &present_attrs, &out_of_range_attr,
7540
51
                                errorp)) {
7541
2
            return ODP_FIT_ERROR;
7542
2
        }
7543
49
        expected_attrs = 0;
7544
7545
        /* For OVS to be backward compatible with newer datapath
7546
         * implementations, we should ignore out of range attributes. */
7547
49
        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
49
        if (!parse_ethertype(attrs, present_attrs, &expected_attrs,
7554
49
                             flow, src_flow, errorp)) {
7555
3
            return ODP_FIT_ERROR;
7556
3
        }
7557
46
        encap_fitness = parse_l2_5_onward(attrs, present_attrs,
7558
46
                                          out_of_range_attr,
7559
46
                                          &expected_attrs,
7560
46
                                          flow, key, key_len,
7561
46
                                          src_flow, false, errorp);
7562
46
        if (encap_fitness != ODP_FIT_PERFECT) {
7563
2
            return encap_fitness;
7564
2
        }
7565
44
        encaps++;
7566
44
    }
7567
7568
33
    return check_expectations(present_attrs, out_of_range_attr,
7569
33
                              expected_attrs, key, key_len);
7570
67
}
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
5.00k
{
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
5.00k
    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 43);
7581
7582
5.00k
    enum odp_key_fitness fitness = ODP_FIT_ERROR;
7583
5.00k
    if (errorp) {
7584
0
        *errorp = NULL;
7585
0
    }
7586
7587
5.00k
    const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
7588
5.00k
    uint64_t expected_attrs;
7589
5.00k
    uint64_t present_attrs;
7590
5.00k
    int out_of_range_attr;
7591
5.00k
    bool is_mask = src_flow != flow;
7592
7593
5.00k
    memset(flow, 0, sizeof *flow);
7594
7595
    /* Parse attributes. */
7596
5.00k
    if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
7597
5.00k
                            &out_of_range_attr, errorp)) {
7598
1.62k
        goto exit;
7599
1.62k
    }
7600
3.38k
    expected_attrs = 0;
7601
7602
    /* For OVS to be backward compatible with newer datapath implementations,
7603
     * we should ignore out of range attributes. */
7604
3.38k
    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
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
7612
12
        flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
7613
12
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
7614
3.37k
    } 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
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
7620
7
        flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
7621
7
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
7622
7
    }
7623
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
7624
5
        flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
7625
5
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
7626
5
    }
7627
7628
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
7629
6
        flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
7630
6
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
7631
6
    }
7632
7633
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE)) {
7634
54
        uint32_t odp_state = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_STATE]);
7635
7636
54
        flow->ct_state = odp_to_ovs_ct_state(odp_state);
7637
54
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE;
7638
54
    }
7639
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE)) {
7640
9
        flow->ct_zone = nl_attr_get_u16(attrs[OVS_KEY_ATTR_CT_ZONE]);
7641
9
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE;
7642
9
    }
7643
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK)) {
7644
8
        flow->ct_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_CT_MARK]);
7645
8
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK;
7646
8
    }
7647
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS)) {
7648
261
        flow->ct_label = nl_attr_get_u128(attrs[OVS_KEY_ATTR_CT_LABELS]);
7649
261
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS;
7650
261
    }
7651
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
7652
74
        const struct ovs_key_ct_tuple_ipv4 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
7653
74
        flow->ct_nw_src = ct->ipv4_src;
7654
74
        flow->ct_nw_dst = ct->ipv4_dst;
7655
74
        flow->ct_nw_proto = ct->ipv4_proto;
7656
74
        flow->ct_tp_src = ct->src_port;
7657
74
        flow->ct_tp_dst = ct->dst_port;
7658
74
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
7659
74
    }
7660
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
7661
49
        const struct ovs_key_ct_tuple_ipv6 *ct = nl_attr_get(attrs[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
7662
7663
49
        flow->ct_ipv6_src = ct->ipv6_src;
7664
49
        flow->ct_ipv6_dst = ct->ipv6_dst;
7665
49
        flow->ct_nw_proto = ct->ipv6_proto;
7666
49
        flow->ct_tp_src = ct->src_port;
7667
49
        flow->ct_tp_dst = ct->dst_port;
7668
49
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
7669
49
    }
7670
7671
3.38k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
7672
1.31k
        enum odp_key_fitness res;
7673
7674
1.31k
        res = odp_tun_key_from_attr__(attrs[OVS_KEY_ATTR_TUNNEL], is_mask,
7675
1.31k
                                      &flow->tunnel, errorp);
7676
1.31k
        if (res == ODP_FIT_ERROR) {
7677
604
            goto exit;
7678
714
        } else if (res == ODP_FIT_PERFECT) {
7679
714
            expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
7680
714
        }
7681
1.31k
    }
7682
7683
2.77k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
7684
60
        flow->in_port.odp_port
7685
60
            = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
7686
60
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
7687
2.71k
    } else if (!is_mask) {
7688
2.71k
        flow->in_port.odp_port = ODPP_NONE;
7689
2.71k
    }
7690
7691
2.77k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE)) {
7692
206
        flow->packet_type
7693
206
            = nl_attr_get_be32(attrs[OVS_KEY_ATTR_PACKET_TYPE]);
7694
206
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE;
7695
206
        if (pt_ns(src_flow->packet_type) == OFPHTN_ETHERTYPE) {
7696
36
            flow->dl_type = pt_ns_type_be(flow->packet_type);
7697
36
        }
7698
2.57k
    } else if (!is_mask) {
7699
2.57k
        flow->packet_type = htonl(PT_ETH);
7700
2.57k
    }
7701
7702
    /* Check for Ethernet header. */
7703
2.77k
    if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
7704
56
        const struct ovs_key_ethernet *eth_key;
7705
7706
56
        eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
7707
56
        put_ethernet_key(eth_key, flow);
7708
56
        if (!is_mask) {
7709
56
            flow->packet_type = htonl(PT_ETH);
7710
56
        }
7711
56
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
7712
56
    }
7713
2.72k
    else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
7714
217
        ovs_be16 ethertype = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
7715
217
        if (!is_mask) {
7716
217
            flow->packet_type = PACKET_TYPE_BE(OFPHTN_ETHERTYPE,
7717
217
                                               ntohs(ethertype));
7718
217
        }
7719
217
        expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
7720
217
    }
7721
7722
    /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
7723
2.77k
    if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
7724
2.77k
                         src_flow, errorp)) {
7725
14
        goto exit;
7726
14
    }
7727
7728
2.76k
    if (is_mask
7729
2.76k
        ? (src_flow->vlans[0].tci & htons(VLAN_CFI)) != 0
7730
2.76k
        : eth_type_vlan(src_flow->dl_type)) {
7731
67
        fitness = parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
7732
67
                                     expected_attrs, flow, key, key_len,
7733
67
                                     src_flow, errorp, ignore_vlan_limit);
7734
2.69k
    } else {
7735
2.69k
        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
2.69k
        fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
7747
2.69k
                                    &expected_attrs, flow, key, key_len,
7748
2.69k
                                    src_flow, true, errorp);
7749
2.69k
    }
7750
7751
5.00k
exit:;
7752
5.00k
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
7753
5.00k
    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
5.00k
    return fitness;
7774
2.76k
}
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
5.00k
{
7799
5.00k
    return odp_flow_key_to_flow__(key, key_len, flow, flow, errorp, false);
7800
5.00k
}
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
3.87k
{
7933
3.87k
    size_t userdata_ofs;
7934
3.87k
    size_t offset;
7935
7936
3.87k
    offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
7937
3.87k
    nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
7938
3.87k
    if (userdata) {
7939
3.87k
        if (nl_attr_oversized(userdata_size)) {
7940
4
            return -E2BIG;
7941
4
        }
7942
3.86k
        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
3.86k
        memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
7955
3.86k
                                      MAX(8, userdata_size)),
7956
3.86k
               userdata, userdata_size);
7957
3.86k
    } else {
7958
0
        userdata_ofs = 0;
7959
0
    }
7960
3.86k
    if (tunnel_out_port != ODPP_NONE) {
7961
229
        nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
7962
229
                            tunnel_out_port);
7963
229
    }
7964
3.86k
    if (include_actions) {
7965
197
        nl_msg_put_flag(odp_actions, OVS_USERSPACE_ATTR_ACTIONS);
7966
197
    }
7967
3.86k
    if (nl_attr_oversized(odp_actions->size - offset - NLA_HDRLEN)) {
7968
3
        return -E2BIG;
7969
3
    }
7970
3.86k
    nl_msg_end_nested(odp_actions, offset);
7971
7972
3.86k
    if (odp_actions_ofs) {
7973
0
        *odp_actions_ofs = userdata_ofs;
7974
0
    }
7975
7976
3.86k
    return 0;
7977
3.86k
}
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
2.36k
{
8190
2.36k
    eth->eth_src = flow->dl_src;
8191
2.36k
    eth->eth_dst = flow->dl_dst;
8192
2.36k
}
8193
8194
static void
8195
put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
8196
56
{
8197
56
    flow->dl_src = eth->eth_src;
8198
56
    flow->dl_dst = eth->eth_dst;
8199
56
}
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
89
{
8336
89
    ipv4->ipv4_src = flow->nw_src;
8337
89
    ipv4->ipv4_dst = flow->nw_dst;
8338
89
    ipv4->ipv4_proto = flow->nw_proto;
8339
89
    ipv4->ipv4_tos = flow->nw_tos;
8340
89
    ipv4->ipv4_ttl = flow->nw_ttl;
8341
89
    ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
8342
89
}
8343
8344
static void
8345
put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
8346
88
{
8347
88
    flow->nw_src = ipv4->ipv4_src;
8348
88
    flow->nw_dst = ipv4->ipv4_dst;
8349
88
    flow->nw_proto = ipv4->ipv4_proto;
8350
88
    flow->nw_tos = ipv4->ipv4_tos;
8351
88
    flow->nw_ttl = ipv4->ipv4_ttl;
8352
88
    flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
8353
88
}
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
700
{
8391
700
    ipv6->ipv6_src = flow->ipv6_src;
8392
700
    ipv6->ipv6_dst = flow->ipv6_dst;
8393
700
    ipv6->ipv6_label = flow->ipv6_label;
8394
700
    ipv6->ipv6_proto = flow->nw_proto;
8395
700
    ipv6->ipv6_tclass = flow->nw_tos;
8396
700
    ipv6->ipv6_hlimit = flow->nw_ttl;
8397
700
    ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
8398
700
}
8399
8400
static void
8401
put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
8402
699
{
8403
699
    flow->ipv6_src = ipv6->ipv6_src;
8404
699
    flow->ipv6_dst = ipv6->ipv6_dst;
8405
699
    flow->ipv6_label = ipv6->ipv6_label;
8406
699
    flow->nw_proto = ipv6->ipv6_proto;
8407
699
    flow->nw_tos = ipv6->ipv6_tclass;
8408
699
    flow->nw_ttl = ipv6->ipv6_hlimit;
8409
699
    flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
8410
699
}
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
12
{
8449
    /* ARP key has padding, clear it. */
8450
12
    memset(arp, 0, sizeof *arp);
8451
8452
12
    arp->arp_sip = flow->nw_src;
8453
12
    arp->arp_tip = flow->nw_dst;
8454
12
    arp->arp_op = flow->nw_proto == UINT8_MAX ?
8455
12
                  OVS_BE16_MAX : htons(flow->nw_proto);
8456
12
    arp->arp_sha = flow->arp_sha;
8457
12
    arp->arp_tha = flow->arp_tha;
8458
12
}
8459
8460
static void
8461
put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
8462
8
{
8463
8
    flow->nw_src = arp->arp_sip;
8464
8
    flow->nw_dst = arp->arp_tip;
8465
8
    flow->nw_proto = ntohs(arp->arp_op);
8466
8
    flow->arp_sha = arp->arp_sha;
8467
8
    flow->arp_tha = arp->arp_tha;
8468
8
}
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
17
{
8762
17
    tp->tcp.tcp_src = flow->tp_src;
8763
17
    tp->tcp.tcp_dst = flow->tp_dst;
8764
17
}
8765
8766
static void
8767
put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
8768
3
{
8769
3
    flow->tp_src = tp->tcp.tcp_src;
8770
3
    flow->tp_dst = tp->tcp.tcp_dst;
8771
3
}
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
513
{
9021
513
    static const struct nl_policy vxlan_opts_policy[] = {
9022
513
        [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
9023
513
    };
9024
513
    struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
9025
9026
513
    if (!nl_parse_nested(tun_attr, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
9027
0
        return EINVAL;
9028
0
    }
9029
9030
513
    if (ext[OVS_VXLAN_EXT_GBP]) {
9031
513
        uint32_t gbp_raw = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
9032
9033
513
        odp_decode_gbp_raw(gbp_raw, id, flags);
9034
513
    }
9035
9036
513
    if (id_present) {
9037
0
        *id_present = !!ext[OVS_VXLAN_EXT_GBP];
9038
0
    }
9039
9040
513
    return 0;
9041
513
}