Coverage Report

Created: 2025-07-11 06:12

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