Coverage Report

Created: 2023-03-26 07:41

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