Coverage Report

Created: 2026-04-12 06:26

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