Coverage Report

Created: 2026-12-01 06:58

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