]> git.proxmox.com Git - ovs.git/blob - lib/odp-util.c
odp-util: Correctly generate wildcards when formating nested attributes.
[ovs.git] / lib / odp-util.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 <arpa/inet.h>
19 #include "odp-util.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <math.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "byte-order.h"
29 #include "coverage.h"
30 #include "dpif.h"
31 #include "dynamic-string.h"
32 #include "flow.h"
33 #include "netlink.h"
34 #include "ofpbuf.h"
35 #include "packets.h"
36 #include "simap.h"
37 #include "timeval.h"
38 #include "unaligned.h"
39 #include "util.h"
40 #include "openvswitch/vlog.h"
41
42 VLOG_DEFINE_THIS_MODULE(odp_util);
43
44 /* The interface between userspace and kernel uses an "OVS_*" prefix.
45 * Since this is fairly non-specific for the OVS userspace components,
46 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
47 * interactions with the datapath.
48 */
49
50 /* The set of characters that may separate one action or one key attribute
51 * from another. */
52 static const char *delimiters = ", \t\r\n";
53
54 static const char *hex_chars = "0123456789abcdefABCDEF";
55
56 struct attr_len_tbl {
57 int len;
58 const struct attr_len_tbl *next;
59 int next_max;
60 };
61 #define ATTR_LEN_INVALID -1
62 #define ATTR_LEN_VARIABLE -2
63 #define ATTR_LEN_NESTED -3
64
65 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
66 struct ofpbuf *, struct ofpbuf *);
67 static void format_odp_key_attr(const struct nlattr *a,
68 const struct nlattr *ma,
69 const struct hmap *portno_names, struct ds *ds,
70 bool verbose);
71
72 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
73 * 'type':
74 *
75 * - For an action whose argument has a fixed length, returned that
76 * nonnegative length in bytes.
77 *
78 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
79 *
80 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
81 static int
82 odp_action_len(uint16_t type)
83 {
84 if (type > OVS_ACTION_ATTR_MAX) {
85 return -1;
86 }
87
88 switch ((enum ovs_action_attr) type) {
89 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
90 case OVS_ACTION_ATTR_TUNNEL_PUSH: return ATTR_LEN_VARIABLE;
91 case OVS_ACTION_ATTR_TUNNEL_POP: return sizeof(uint32_t);
92 case OVS_ACTION_ATTR_USERSPACE: return ATTR_LEN_VARIABLE;
93 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
94 case OVS_ACTION_ATTR_POP_VLAN: return 0;
95 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
96 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
97 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
98 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
99 case OVS_ACTION_ATTR_SET: return ATTR_LEN_VARIABLE;
100 case OVS_ACTION_ATTR_SET_MASKED: return ATTR_LEN_VARIABLE;
101 case OVS_ACTION_ATTR_SAMPLE: return ATTR_LEN_VARIABLE;
102
103 case OVS_ACTION_ATTR_UNSPEC:
104 case __OVS_ACTION_ATTR_MAX:
105 return ATTR_LEN_INVALID;
106 }
107
108 return ATTR_LEN_INVALID;
109 }
110
111 /* Returns a string form of 'attr'. The return value is either a statically
112 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
113 * should be at least OVS_KEY_ATTR_BUFSIZE. */
114 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
115 static const char *
116 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
117 {
118 switch (attr) {
119 case OVS_KEY_ATTR_UNSPEC: return "unspec";
120 case OVS_KEY_ATTR_ENCAP: return "encap";
121 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
122 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
123 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
124 case OVS_KEY_ATTR_IN_PORT: return "in_port";
125 case OVS_KEY_ATTR_ETHERNET: return "eth";
126 case OVS_KEY_ATTR_VLAN: return "vlan";
127 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
128 case OVS_KEY_ATTR_IPV4: return "ipv4";
129 case OVS_KEY_ATTR_IPV6: return "ipv6";
130 case OVS_KEY_ATTR_TCP: return "tcp";
131 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
132 case OVS_KEY_ATTR_UDP: return "udp";
133 case OVS_KEY_ATTR_SCTP: return "sctp";
134 case OVS_KEY_ATTR_ICMP: return "icmp";
135 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
136 case OVS_KEY_ATTR_ARP: return "arp";
137 case OVS_KEY_ATTR_ND: return "nd";
138 case OVS_KEY_ATTR_MPLS: return "mpls";
139 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
140 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
141
142 case __OVS_KEY_ATTR_MAX:
143 default:
144 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
145 return namebuf;
146 }
147 }
148
149 static void
150 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
151 {
152 size_t len = nl_attr_get_size(a);
153
154 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
155 if (len) {
156 const uint8_t *unspec;
157 unsigned int i;
158
159 unspec = nl_attr_get(a);
160 for (i = 0; i < len; i++) {
161 ds_put_char(ds, i ? ' ': '(');
162 ds_put_format(ds, "%02x", unspec[i]);
163 }
164 ds_put_char(ds, ')');
165 }
166 }
167
168 static void
169 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
170 {
171 static const struct nl_policy ovs_sample_policy[] = {
172 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
173 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
174 };
175 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
176 double percentage;
177 const struct nlattr *nla_acts;
178 int len;
179
180 ds_put_cstr(ds, "sample");
181
182 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
183 ds_put_cstr(ds, "(error)");
184 return;
185 }
186
187 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
188 UINT32_MAX;
189
190 ds_put_format(ds, "(sample=%.1f%%,", percentage);
191
192 ds_put_cstr(ds, "actions(");
193 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
194 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
195 format_odp_actions(ds, nla_acts, len);
196 ds_put_format(ds, "))");
197 }
198
199 static const char *
200 slow_path_reason_to_string(uint32_t reason)
201 {
202 switch ((enum slow_path_reason) reason) {
203 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
204 SLOW_PATH_REASONS
205 #undef SPR
206 }
207
208 return NULL;
209 }
210
211 const char *
212 slow_path_reason_to_explanation(enum slow_path_reason reason)
213 {
214 switch (reason) {
215 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
216 SLOW_PATH_REASONS
217 #undef SPR
218 }
219
220 return "<unknown>";
221 }
222
223 static int
224 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
225 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
226 {
227 uint32_t result = 0;
228 int n;
229
230 /* Parse masked flags in numeric format? */
231 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
232 res_flags, res_mask, &n) && n > 0) {
233 if (*res_flags & ~allowed || *res_mask & ~allowed) {
234 return -EINVAL;
235 }
236 return n;
237 }
238
239 n = 0;
240
241 if (res_mask && (*s == '+' || *s == '-')) {
242 uint32_t flags = 0, mask = 0;
243
244 /* Parse masked flags. */
245 while (s[0] != ')') {
246 bool set;
247 uint32_t bit;
248 int name_len;
249
250 if (s[0] == '+') {
251 set = true;
252 } else if (s[0] == '-') {
253 set = false;
254 } else {
255 return -EINVAL;
256 }
257 s++;
258 n++;
259
260 name_len = strcspn(s, "+-)");
261
262 for (bit = 1; bit; bit <<= 1) {
263 const char *fname = bit_to_string(bit);
264 size_t len;
265
266 if (!fname) {
267 continue;
268 }
269
270 len = strlen(fname);
271 if (len != name_len) {
272 continue;
273 }
274 if (!strncmp(s, fname, len)) {
275 if (mask & bit) {
276 /* bit already set. */
277 return -EINVAL;
278 }
279 if (!(bit & allowed)) {
280 return -EINVAL;
281 }
282 if (set) {
283 flags |= bit;
284 }
285 mask |= bit;
286 break;
287 }
288 }
289
290 if (!bit) {
291 return -EINVAL; /* Unknown flag name */
292 }
293 s += name_len;
294 n += name_len;
295 }
296
297 *res_flags = flags;
298 *res_mask = mask;
299 return n;
300 }
301
302 /* Parse unmasked flags. If a flag is present, it is set, otherwise
303 * it is not set. */
304 while (s[n] != ')') {
305 unsigned long long int flags;
306 uint32_t bit;
307 int n0;
308
309 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
310 if (flags & ~allowed) {
311 return -EINVAL;
312 }
313 n += n0 + (s[n + n0] == ',');
314 result |= flags;
315 continue;
316 }
317
318 for (bit = 1; bit; bit <<= 1) {
319 const char *name = bit_to_string(bit);
320 size_t len;
321
322 if (!name) {
323 continue;
324 }
325
326 len = strlen(name);
327 if (!strncmp(s + n, name, len) &&
328 (s[n + len] == ',' || s[n + len] == ')')) {
329 if (!(bit & allowed)) {
330 return -EINVAL;
331 }
332 result |= bit;
333 n += len + (s[n + len] == ',');
334 break;
335 }
336 }
337
338 if (!bit) {
339 return -EINVAL;
340 }
341 }
342
343 *res_flags = result;
344 if (res_mask) {
345 *res_mask = UINT32_MAX;
346 }
347 return n;
348 }
349
350 static void
351 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
352 {
353 static const struct nl_policy ovs_userspace_policy[] = {
354 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
355 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
356 .optional = true },
357 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
358 .optional = true },
359 };
360 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
361 const struct nlattr *userdata_attr;
362 const struct nlattr *tunnel_out_port_attr;
363
364 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
365 ds_put_cstr(ds, "userspace(error)");
366 return;
367 }
368
369 ds_put_format(ds, "userspace(pid=%"PRIu32,
370 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
371
372 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
373
374 if (userdata_attr) {
375 const uint8_t *userdata = nl_attr_get(userdata_attr);
376 size_t userdata_len = nl_attr_get_size(userdata_attr);
377 bool userdata_unspec = true;
378 union user_action_cookie cookie;
379
380 if (userdata_len >= sizeof cookie.type
381 && userdata_len <= sizeof cookie) {
382
383 memset(&cookie, 0, sizeof cookie);
384 memcpy(&cookie, userdata, userdata_len);
385
386 userdata_unspec = false;
387
388 if (userdata_len == sizeof cookie.sflow
389 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
390 ds_put_format(ds, ",sFlow("
391 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
392 vlan_tci_to_vid(cookie.sflow.vlan_tci),
393 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
394 cookie.sflow.output);
395 } else if (userdata_len == sizeof cookie.slow_path
396 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
397 ds_put_cstr(ds, ",slow_path(");
398 format_flags(ds, slow_path_reason_to_string,
399 cookie.slow_path.reason, ',');
400 ds_put_format(ds, ")");
401 } else if (userdata_len == sizeof cookie.flow_sample
402 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
403 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
404 ",collector_set_id=%"PRIu32
405 ",obs_domain_id=%"PRIu32
406 ",obs_point_id=%"PRIu32")",
407 cookie.flow_sample.probability,
408 cookie.flow_sample.collector_set_id,
409 cookie.flow_sample.obs_domain_id,
410 cookie.flow_sample.obs_point_id);
411 } else if (userdata_len >= sizeof cookie.ipfix
412 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
413 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
414 cookie.ipfix.output_odp_port);
415 } else {
416 userdata_unspec = true;
417 }
418 }
419
420 if (userdata_unspec) {
421 size_t i;
422 ds_put_format(ds, ",userdata(");
423 for (i = 0; i < userdata_len; i++) {
424 ds_put_format(ds, "%02x", userdata[i]);
425 }
426 ds_put_char(ds, ')');
427 }
428 }
429
430 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
431 if (tunnel_out_port_attr) {
432 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
433 nl_attr_get_u32(tunnel_out_port_attr));
434 }
435
436 ds_put_char(ds, ')');
437 }
438
439 static void
440 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
441 {
442 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
443 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
444 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
445 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
446 };
447 ds_put_char(ds, ',');
448 }
449 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
450 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
451 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
452 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
453 }
454 ds_put_char(ds, ',');
455 }
456 if (!(tci & htons(VLAN_CFI))) {
457 ds_put_cstr(ds, "cfi=0");
458 ds_put_char(ds, ',');
459 }
460 ds_chomp(ds, ',');
461 }
462
463 static void
464 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
465 {
466 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
467 mpls_lse_to_label(mpls_lse),
468 mpls_lse_to_tc(mpls_lse),
469 mpls_lse_to_ttl(mpls_lse),
470 mpls_lse_to_bos(mpls_lse));
471 }
472
473 static void
474 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
475 const struct ovs_key_mpls *mpls_mask, int n)
476 {
477 if (n == 1) {
478 ovs_be32 key = mpls_key->mpls_lse;
479
480 if (mpls_mask == NULL) {
481 format_mpls_lse(ds, key);
482 } else {
483 ovs_be32 mask = mpls_mask->mpls_lse;
484
485 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
486 mpls_lse_to_label(key), mpls_lse_to_label(mask),
487 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
488 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
489 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
490 }
491 } else {
492 int i;
493
494 for (i = 0; i < n; i++) {
495 ds_put_format(ds, "lse%d=%#"PRIx32,
496 i, ntohl(mpls_key[i].mpls_lse));
497 if (mpls_mask) {
498 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
499 }
500 ds_put_char(ds, ',');
501 }
502 ds_chomp(ds, ',');
503 }
504 }
505
506 static void
507 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
508 {
509 ds_put_format(ds, "recirc(%#"PRIx32")", recirc_id);
510 }
511
512 static void
513 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
514 {
515 ds_put_format(ds, "hash(");
516
517 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
518 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
519 } else {
520 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
521 hash_act->hash_alg);
522 }
523 ds_put_format(ds, ")");
524 }
525
526 static const void *
527 format_udp_tnl_push_header(struct ds *ds, const struct ip_header *ip)
528 {
529 const struct udp_header *udp;
530
531 udp = (const struct udp_header *) (ip + 1);
532 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16",csum=0x%"PRIx16"),",
533 ntohs(udp->udp_src), ntohs(udp->udp_dst),
534 ntohs(udp->udp_csum));
535
536 return udp + 1;
537 }
538
539 static void
540 format_odp_tnl_push_header(struct ds *ds, struct ovs_action_push_tnl *data)
541 {
542 const struct eth_header *eth;
543 const struct ip_header *ip;
544 const void *l3;
545
546 eth = (const struct eth_header *)data->header;
547
548 l3 = eth + 1;
549 ip = (const struct ip_header *)l3;
550
551 /* Ethernet */
552 ds_put_format(ds, "header(size=%"PRIu8",type=%"PRIu8",eth(dst=",
553 data->header_len, data->tnl_type);
554 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_dst));
555 ds_put_format(ds, ",src=");
556 ds_put_format(ds, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth->eth_src));
557 ds_put_format(ds, ",dl_type=0x%04"PRIx16"),", ntohs(eth->eth_type));
558
559 /* IPv4 */
560 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
561 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=0x%"PRIx16"),",
562 IP_ARGS(get_16aligned_be32(&ip->ip_src)),
563 IP_ARGS(get_16aligned_be32(&ip->ip_dst)),
564 ip->ip_proto, ip->ip_tos,
565 ip->ip_ttl,
566 ip->ip_frag_off);
567
568 if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) {
569 const struct vxlanhdr *vxh;
570
571 vxh = format_udp_tnl_push_header(ds, ip);
572
573 ds_put_format(ds, "vxlan(flags=0x%"PRIx32",vni=0x%"PRIx32")",
574 ntohl(get_16aligned_be32(&vxh->vx_flags)),
575 ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8);
576 } else if (data->tnl_type == OVS_VPORT_TYPE_GENEVE) {
577 const struct genevehdr *gnh;
578
579 gnh = format_udp_tnl_push_header(ds, ip);
580
581 ds_put_format(ds, "geneve(%svni=0x%"PRIx32")",
582 gnh->oam ? "oam," : "",
583 ntohl(get_16aligned_be32(&gnh->vni)) >> 8);
584 } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) {
585 const struct gre_base_hdr *greh;
586 ovs_16aligned_be32 *options;
587 void *l4;
588
589 l4 = ((uint8_t *)l3 + sizeof(struct ip_header));
590 greh = (const struct gre_base_hdr *) l4;
591
592 ds_put_format(ds, "gre((flags=0x%"PRIx16",proto=0x%"PRIx16")",
593 ntohs(greh->flags), ntohs(greh->protocol));
594 options = (ovs_16aligned_be32 *)(greh + 1);
595 if (greh->flags & htons(GRE_CSUM)) {
596 ds_put_format(ds, ",csum=0x%"PRIx16, ntohs(*((ovs_be16 *)options)));
597 options++;
598 }
599 if (greh->flags & htons(GRE_KEY)) {
600 ds_put_format(ds, ",key=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
601 options++;
602 }
603 if (greh->flags & htons(GRE_SEQ)) {
604 ds_put_format(ds, ",seq=0x%"PRIx32, ntohl(get_16aligned_be32(options)));
605 options++;
606 }
607 ds_put_format(ds, ")");
608 }
609 ds_put_format(ds, ")");
610 }
611
612 static void
613 format_odp_tnl_push_action(struct ds *ds, const struct nlattr *attr)
614 {
615 struct ovs_action_push_tnl *data;
616
617 data = (struct ovs_action_push_tnl *) nl_attr_get(attr);
618
619 ds_put_format(ds, "tnl_push(tnl_port(%"PRIu32"),", data->tnl_port);
620 format_odp_tnl_push_header(ds, data);
621 ds_put_format(ds, ",out_port(%"PRIu32"))", data->out_port);
622 }
623
624 static void
625 format_odp_action(struct ds *ds, const struct nlattr *a)
626 {
627 int expected_len;
628 enum ovs_action_attr type = nl_attr_type(a);
629 const struct ovs_action_push_vlan *vlan;
630 size_t size;
631
632 expected_len = odp_action_len(nl_attr_type(a));
633 if (expected_len != ATTR_LEN_VARIABLE &&
634 nl_attr_get_size(a) != expected_len) {
635 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
636 nl_attr_get_size(a), expected_len);
637 format_generic_odp_action(ds, a);
638 return;
639 }
640
641 switch (type) {
642 case OVS_ACTION_ATTR_OUTPUT:
643 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
644 break;
645 case OVS_ACTION_ATTR_TUNNEL_POP:
646 ds_put_format(ds, "tnl_pop(%"PRIu32")", nl_attr_get_u32(a));
647 break;
648 case OVS_ACTION_ATTR_TUNNEL_PUSH:
649 format_odp_tnl_push_action(ds, a);
650 break;
651 case OVS_ACTION_ATTR_USERSPACE:
652 format_odp_userspace_action(ds, a);
653 break;
654 case OVS_ACTION_ATTR_RECIRC:
655 format_odp_recirc_action(ds, nl_attr_get_u32(a));
656 break;
657 case OVS_ACTION_ATTR_HASH:
658 format_odp_hash_action(ds, nl_attr_get(a));
659 break;
660 case OVS_ACTION_ATTR_SET_MASKED:
661 a = nl_attr_get(a);
662 size = nl_attr_get_size(a) / 2;
663 ds_put_cstr(ds, "set(");
664
665 /* Masked set action not supported for tunnel key, which is bigger. */
666 if (size <= sizeof(struct ovs_key_ipv6)) {
667 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
668 sizeof(struct nlattr))];
669 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
670 sizeof(struct nlattr))];
671
672 mask->nla_type = attr->nla_type = nl_attr_type(a);
673 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
674 memcpy(attr + 1, (char *)(a + 1), size);
675 memcpy(mask + 1, (char *)(a + 1) + size, size);
676 format_odp_key_attr(attr, mask, NULL, ds, false);
677 } else {
678 format_odp_key_attr(a, NULL, NULL, ds, false);
679 }
680 ds_put_cstr(ds, ")");
681 break;
682 case OVS_ACTION_ATTR_SET:
683 ds_put_cstr(ds, "set(");
684 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
685 ds_put_cstr(ds, ")");
686 break;
687 case OVS_ACTION_ATTR_PUSH_VLAN:
688 vlan = nl_attr_get(a);
689 ds_put_cstr(ds, "push_vlan(");
690 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
691 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
692 }
693 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
694 ds_put_char(ds, ')');
695 break;
696 case OVS_ACTION_ATTR_POP_VLAN:
697 ds_put_cstr(ds, "pop_vlan");
698 break;
699 case OVS_ACTION_ATTR_PUSH_MPLS: {
700 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
701 ds_put_cstr(ds, "push_mpls(");
702 format_mpls_lse(ds, mpls->mpls_lse);
703 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
704 break;
705 }
706 case OVS_ACTION_ATTR_POP_MPLS: {
707 ovs_be16 ethertype = nl_attr_get_be16(a);
708 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
709 break;
710 }
711 case OVS_ACTION_ATTR_SAMPLE:
712 format_odp_sample_action(ds, a);
713 break;
714 case OVS_ACTION_ATTR_UNSPEC:
715 case __OVS_ACTION_ATTR_MAX:
716 default:
717 format_generic_odp_action(ds, a);
718 break;
719 }
720 }
721
722 void
723 format_odp_actions(struct ds *ds, const struct nlattr *actions,
724 size_t actions_len)
725 {
726 if (actions_len) {
727 const struct nlattr *a;
728 unsigned int left;
729
730 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
731 if (a != actions) {
732 ds_put_char(ds, ',');
733 }
734 format_odp_action(ds, a);
735 }
736 if (left) {
737 int i;
738
739 if (left == actions_len) {
740 ds_put_cstr(ds, "<empty>");
741 }
742 ds_put_format(ds, ",***%u leftover bytes*** (", left);
743 for (i = 0; i < left; i++) {
744 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
745 }
746 ds_put_char(ds, ')');
747 }
748 } else {
749 ds_put_cstr(ds, "drop");
750 }
751 }
752
753 /* Separate out parse_odp_userspace_action() function. */
754 static int
755 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
756 {
757 uint32_t pid;
758 union user_action_cookie cookie;
759 struct ofpbuf buf;
760 odp_port_t tunnel_out_port;
761 int n = -1;
762 void *user_data = NULL;
763 size_t user_data_size = 0;
764
765 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
766 return -EINVAL;
767 }
768
769 {
770 uint32_t output;
771 uint32_t probability;
772 uint32_t collector_set_id;
773 uint32_t obs_domain_id;
774 uint32_t obs_point_id;
775 int vid, pcp;
776 int n1 = -1;
777 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
778 "pcp=%i,output=%"SCNi32")%n",
779 &vid, &pcp, &output, &n1)) {
780 uint16_t tci;
781
782 n += n1;
783 tci = vid | (pcp << VLAN_PCP_SHIFT);
784 if (tci) {
785 tci |= VLAN_CFI;
786 }
787
788 cookie.type = USER_ACTION_COOKIE_SFLOW;
789 cookie.sflow.vlan_tci = htons(tci);
790 cookie.sflow.output = output;
791 user_data = &cookie;
792 user_data_size = sizeof cookie.sflow;
793 } else if (ovs_scan(&s[n], ",slow_path(%n",
794 &n1)) {
795 int res;
796
797 n += n1;
798 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
799 cookie.slow_path.unused = 0;
800 cookie.slow_path.reason = 0;
801
802 res = parse_flags(&s[n], slow_path_reason_to_string,
803 &cookie.slow_path.reason,
804 SLOW_PATH_REASON_MASK, NULL);
805 if (res < 0 || s[n + res] != ')') {
806 return res;
807 }
808 n += res + 1;
809
810 user_data = &cookie;
811 user_data_size = sizeof cookie.slow_path;
812 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
813 "collector_set_id=%"SCNi32","
814 "obs_domain_id=%"SCNi32","
815 "obs_point_id=%"SCNi32")%n",
816 &probability, &collector_set_id,
817 &obs_domain_id, &obs_point_id, &n1)) {
818 n += n1;
819
820 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
821 cookie.flow_sample.probability = probability;
822 cookie.flow_sample.collector_set_id = collector_set_id;
823 cookie.flow_sample.obs_domain_id = obs_domain_id;
824 cookie.flow_sample.obs_point_id = obs_point_id;
825 user_data = &cookie;
826 user_data_size = sizeof cookie.flow_sample;
827 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
828 &output, &n1) ) {
829 n += n1;
830 cookie.type = USER_ACTION_COOKIE_IPFIX;
831 cookie.ipfix.output_odp_port = u32_to_odp(output);
832 user_data = &cookie;
833 user_data_size = sizeof cookie.ipfix;
834 } else if (ovs_scan(&s[n], ",userdata(%n",
835 &n1)) {
836 char *end;
837
838 n += n1;
839 ofpbuf_init(&buf, 16);
840 end = ofpbuf_put_hex(&buf, &s[n], NULL);
841 if (end[0] != ')') {
842 return -EINVAL;
843 }
844 user_data = buf.data;
845 user_data_size = buf.size;
846 n = (end + 1) - s;
847 }
848 }
849
850 {
851 int n1 = -1;
852 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
853 &tunnel_out_port, &n1)) {
854 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
855 return n + n1;
856 } else if (s[n] == ')') {
857 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
858 return n + 1;
859 }
860 }
861
862 return -EINVAL;
863 }
864
865 static int
866 ovs_parse_tnl_push(const char *s, struct ovs_action_push_tnl *data)
867 {
868 struct eth_header *eth;
869 struct ip_header *ip;
870 struct udp_header *udp;
871 struct gre_base_hdr *greh;
872 uint16_t gre_proto, gre_flags, dl_type, udp_src, udp_dst, csum;
873 ovs_be32 sip, dip;
874 uint32_t tnl_type = 0, header_len = 0;
875 void *l3, *l4;
876 int n = 0;
877
878 if (!ovs_scan_len(s, &n, "tnl_push(tnl_port(%"SCNi32"),", &data->tnl_port)) {
879 return -EINVAL;
880 }
881 eth = (struct eth_header *) data->header;
882 l3 = (data->header + sizeof *eth);
883 l4 = ((uint8_t *) l3 + sizeof (struct ip_header));
884 ip = (struct ip_header *) l3;
885 if (!ovs_scan_len(s, &n, "header(size=%"SCNi32",type=%"SCNi32","
886 "eth(dst="ETH_ADDR_SCAN_FMT",",
887 &data->header_len,
888 &data->tnl_type,
889 ETH_ADDR_SCAN_ARGS(eth->eth_dst))) {
890 return -EINVAL;
891 }
892
893 if (!ovs_scan_len(s, &n, "src="ETH_ADDR_SCAN_FMT",",
894 ETH_ADDR_SCAN_ARGS(eth->eth_src))) {
895 return -EINVAL;
896 }
897 if (!ovs_scan_len(s, &n, "dl_type=0x%"SCNx16"),", &dl_type)) {
898 return -EINVAL;
899 }
900 eth->eth_type = htons(dl_type);
901
902 /* IPv4 */
903 if (!ovs_scan_len(s, &n, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT",proto=%"SCNi8
904 ",tos=%"SCNi8",ttl=%"SCNi8",frag=0x%"SCNx16"),",
905 IP_SCAN_ARGS(&sip),
906 IP_SCAN_ARGS(&dip),
907 &ip->ip_proto, &ip->ip_tos,
908 &ip->ip_ttl, &ip->ip_frag_off)) {
909 return -EINVAL;
910 }
911 put_16aligned_be32(&ip->ip_src, sip);
912 put_16aligned_be32(&ip->ip_dst, dip);
913
914 /* Tunnel header */
915 udp = (struct udp_header *) l4;
916 greh = (struct gre_base_hdr *) l4;
917 if (ovs_scan_len(s, &n, "udp(src=%"SCNi16",dst=%"SCNi16",csum=0x%"SCNx16"),",
918 &udp_src, &udp_dst, &csum)) {
919 uint32_t vx_flags, vni;
920
921 udp->udp_src = htons(udp_src);
922 udp->udp_dst = htons(udp_dst);
923 udp->udp_len = 0;
924 udp->udp_csum = htons(csum);
925
926 if (ovs_scan_len(s, &n, "vxlan(flags=0x%"SCNx32",vni=0x%"SCNx32"))",
927 &vx_flags, &vni)) {
928 struct vxlanhdr *vxh = (struct vxlanhdr *) (udp + 1);
929
930 put_16aligned_be32(&vxh->vx_flags, htonl(vx_flags));
931 put_16aligned_be32(&vxh->vx_vni, htonl(vni << 8));
932 tnl_type = OVS_VPORT_TYPE_VXLAN;
933 header_len = sizeof *eth + sizeof *ip +
934 sizeof *udp + sizeof *vxh;
935 } else if (ovs_scan_len(s, &n, "geneve(")) {
936 struct genevehdr *gnh = (struct genevehdr *) (udp + 1);
937
938 memset(gnh, 0, sizeof *gnh);
939 if (ovs_scan_len(s, &n, "oam,")) {
940 gnh->oam = 1;
941 }
942 if (!ovs_scan_len(s, &n, "vni=0x%"SCNx32"))", &vni)) {
943 return -EINVAL;
944 }
945 gnh->proto_type = htons(ETH_TYPE_TEB);
946 put_16aligned_be32(&gnh->vni, htonl(vni << 8));
947 tnl_type = OVS_VPORT_TYPE_GENEVE;
948 header_len = sizeof *eth + sizeof *ip +
949 sizeof *udp + sizeof *gnh;
950 } else {
951 return -EINVAL;
952 }
953 } else if (ovs_scan_len(s, &n, "gre((flags=0x%"SCNx16",proto=0x%"SCNx16")",
954 &gre_flags, &gre_proto)){
955
956 tnl_type = OVS_VPORT_TYPE_GRE;
957 greh->flags = htons(gre_flags);
958 greh->protocol = htons(gre_proto);
959 ovs_16aligned_be32 *options = (ovs_16aligned_be32 *) (greh + 1);
960
961 if (greh->flags & htons(GRE_CSUM)) {
962 if (!ovs_scan_len(s, &n, ",csum=0x%"SCNx16, &csum)) {
963 return -EINVAL;
964 }
965
966 memset(options, 0, sizeof *options);
967 *((ovs_be16 *)options) = htons(csum);
968 options++;
969 }
970 if (greh->flags & htons(GRE_KEY)) {
971 uint32_t key;
972
973 if (!ovs_scan_len(s, &n, ",key=0x%"SCNx32, &key)) {
974 return -EINVAL;
975 }
976
977 put_16aligned_be32(options, htonl(key));
978 options++;
979 }
980 if (greh->flags & htons(GRE_SEQ)) {
981 uint32_t seq;
982
983 if (!ovs_scan_len(s, &n, ",seq=0x%"SCNx32, &seq)) {
984 return -EINVAL;
985 }
986 put_16aligned_be32(options, htonl(seq));
987 options++;
988 }
989
990 if (!ovs_scan_len(s, &n, "))")) {
991 return -EINVAL;
992 }
993
994 header_len = sizeof *eth + sizeof *ip +
995 ((uint8_t *) options - (uint8_t *) greh);
996 } else {
997 return -EINVAL;
998 }
999
1000 /* check tunnel meta data. */
1001 if (data->tnl_type != tnl_type) {
1002 return -EINVAL;
1003 }
1004 if (data->header_len != header_len) {
1005 return -EINVAL;
1006 }
1007
1008 /* Out port */
1009 if (!ovs_scan_len(s, &n, ",out_port(%"SCNi32"))", &data->out_port)) {
1010 return -EINVAL;
1011 }
1012
1013 return n;
1014 }
1015
1016 static int
1017 parse_odp_action(const char *s, const struct simap *port_names,
1018 struct ofpbuf *actions)
1019 {
1020 {
1021 uint32_t port;
1022 int n;
1023
1024 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
1025 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
1026 return n;
1027 }
1028 }
1029
1030 if (port_names) {
1031 int len = strcspn(s, delimiters);
1032 struct simap_node *node;
1033
1034 node = simap_find_len(port_names, s, len);
1035 if (node) {
1036 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
1037 return len;
1038 }
1039 }
1040
1041 {
1042 uint32_t recirc_id;
1043 int n = -1;
1044
1045 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
1046 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
1047 return n;
1048 }
1049 }
1050
1051 if (!strncmp(s, "userspace(", 10)) {
1052 return parse_odp_userspace_action(s, actions);
1053 }
1054
1055 if (!strncmp(s, "set(", 4)) {
1056 size_t start_ofs;
1057 int retval;
1058 struct nlattr mask[128 / sizeof(struct nlattr)];
1059 struct ofpbuf maskbuf;
1060 struct nlattr *nested, *key;
1061 size_t size;
1062
1063 /* 'mask' is big enough to hold any key. */
1064 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
1065
1066 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
1067 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
1068 if (retval < 0) {
1069 return retval;
1070 }
1071 if (s[retval + 4] != ')') {
1072 return -EINVAL;
1073 }
1074
1075 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1076 key = nested + 1;
1077
1078 size = nl_attr_get_size(mask);
1079 if (size == nl_attr_get_size(key)) {
1080 /* Change to masked set action if not fully masked. */
1081 if (!is_all_ones(mask + 1, size)) {
1082 key->nla_len += size;
1083 ofpbuf_put(actions, mask + 1, size);
1084 /* 'actions' may have been reallocated by ofpbuf_put(). */
1085 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
1086 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
1087 }
1088 }
1089
1090 nl_msg_end_nested(actions, start_ofs);
1091 return retval + 5;
1092 }
1093
1094 {
1095 struct ovs_action_push_vlan push;
1096 int tpid = ETH_TYPE_VLAN;
1097 int vid, pcp;
1098 int cfi = 1;
1099 int n = -1;
1100
1101 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
1102 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1103 &vid, &pcp, &cfi, &n)
1104 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1105 &tpid, &vid, &pcp, &n)
1106 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1107 &tpid, &vid, &pcp, &cfi, &n)) {
1108 push.vlan_tpid = htons(tpid);
1109 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
1110 | (pcp << VLAN_PCP_SHIFT)
1111 | (cfi ? VLAN_CFI : 0));
1112 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
1113 &push, sizeof push);
1114
1115 return n;
1116 }
1117 }
1118
1119 if (!strncmp(s, "pop_vlan", 8)) {
1120 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
1121 return 8;
1122 }
1123
1124 {
1125 double percentage;
1126 int n = -1;
1127
1128 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
1129 && percentage >= 0. && percentage <= 100.0) {
1130 size_t sample_ofs, actions_ofs;
1131 double probability;
1132
1133 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
1134 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
1135 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
1136 (probability <= 0 ? 0
1137 : probability >= UINT32_MAX ? UINT32_MAX
1138 : probability));
1139
1140 actions_ofs = nl_msg_start_nested(actions,
1141 OVS_SAMPLE_ATTR_ACTIONS);
1142 for (;;) {
1143 int retval;
1144
1145 n += strspn(s + n, delimiters);
1146 if (s[n] == ')') {
1147 break;
1148 }
1149
1150 retval = parse_odp_action(s + n, port_names, actions);
1151 if (retval < 0) {
1152 return retval;
1153 }
1154 n += retval;
1155 }
1156 nl_msg_end_nested(actions, actions_ofs);
1157 nl_msg_end_nested(actions, sample_ofs);
1158
1159 return s[n + 1] == ')' ? n + 2 : -EINVAL;
1160 }
1161 }
1162
1163 {
1164 uint32_t port;
1165 int n;
1166
1167 if (ovs_scan(s, "tnl_pop(%"SCNi32")%n", &port, &n)) {
1168 nl_msg_put_u32(actions, OVS_ACTION_ATTR_TUNNEL_POP, port);
1169 return n;
1170 }
1171 }
1172
1173 {
1174 struct ovs_action_push_tnl data;
1175 int n;
1176
1177 n = ovs_parse_tnl_push(s, &data);
1178 if (n > 0) {
1179 odp_put_tnl_push_action(actions, &data);
1180 return n;
1181 } else if (n < 0) {
1182 return n;
1183 }
1184 }
1185 return -EINVAL;
1186 }
1187
1188 /* Parses the string representation of datapath actions, in the format output
1189 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1190 * value. On success, the ODP actions are appended to 'actions' as a series of
1191 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1192 * way, 'actions''s data might be reallocated. */
1193 int
1194 odp_actions_from_string(const char *s, const struct simap *port_names,
1195 struct ofpbuf *actions)
1196 {
1197 size_t old_size;
1198
1199 if (!strcasecmp(s, "drop")) {
1200 return 0;
1201 }
1202
1203 old_size = actions->size;
1204 for (;;) {
1205 int retval;
1206
1207 s += strspn(s, delimiters);
1208 if (!*s) {
1209 return 0;
1210 }
1211
1212 retval = parse_odp_action(s, port_names, actions);
1213 if (retval < 0 || !strchr(delimiters, s[retval])) {
1214 actions->size = old_size;
1215 return -retval;
1216 }
1217 s += retval;
1218 }
1219
1220 return 0;
1221 }
1222 \f
1223 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens[OVS_VXLAN_EXT_MAX + 1] = {
1224 [OVS_VXLAN_EXT_GBP] = { .len = 4 },
1225 };
1226
1227 static const struct attr_len_tbl ovs_tun_key_attr_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1228 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = 8 },
1229 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = 4 },
1230 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = 4 },
1231 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
1232 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
1233 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
1234 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
1235 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = 2 },
1236 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = 2 },
1237 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
1238 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = ATTR_LEN_VARIABLE },
1239 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = ATTR_LEN_NESTED,
1240 .next = ovs_vxlan_ext_attr_lens ,
1241 .next_max = OVS_VXLAN_EXT_MAX},
1242 };
1243
1244 static const struct attr_len_tbl ovs_flow_key_attr_lens[OVS_KEY_ATTR_MAX + 1] = {
1245 [OVS_KEY_ATTR_ENCAP] = { .len = ATTR_LEN_NESTED },
1246 [OVS_KEY_ATTR_PRIORITY] = { .len = 4 },
1247 [OVS_KEY_ATTR_SKB_MARK] = { .len = 4 },
1248 [OVS_KEY_ATTR_DP_HASH] = { .len = 4 },
1249 [OVS_KEY_ATTR_RECIRC_ID] = { .len = 4 },
1250 [OVS_KEY_ATTR_TUNNEL] = { .len = ATTR_LEN_NESTED,
1251 .next = ovs_tun_key_attr_lens,
1252 .next_max = OVS_TUNNEL_KEY_ATTR_MAX },
1253 [OVS_KEY_ATTR_IN_PORT] = { .len = 4 },
1254 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
1255 [OVS_KEY_ATTR_VLAN] = { .len = 2 },
1256 [OVS_KEY_ATTR_ETHERTYPE] = { .len = 2 },
1257 [OVS_KEY_ATTR_MPLS] = { .len = ATTR_LEN_VARIABLE },
1258 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
1259 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
1260 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
1261 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = 2 },
1262 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
1263 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
1264 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
1265 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
1266 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
1267 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
1268 };
1269
1270 /* Returns the correct length of the payload for a flow key attribute of the
1271 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1272 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1273 * payload is a nested type. */
1274 static int
1275 odp_key_attr_len(const struct attr_len_tbl tbl[], int max_len, uint16_t type)
1276 {
1277 if (type > max_len) {
1278 return ATTR_LEN_INVALID;
1279 }
1280
1281 return tbl[type].len;
1282 }
1283
1284 static void
1285 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
1286 {
1287 size_t len = nl_attr_get_size(a);
1288 if (len) {
1289 const uint8_t *unspec;
1290 unsigned int i;
1291
1292 unspec = nl_attr_get(a);
1293 for (i = 0; i < len; i++) {
1294 if (i) {
1295 ds_put_char(ds, ' ');
1296 }
1297 ds_put_format(ds, "%02x", unspec[i]);
1298 }
1299 }
1300 }
1301
1302 static const char *
1303 ovs_frag_type_to_string(enum ovs_frag_type type)
1304 {
1305 switch (type) {
1306 case OVS_FRAG_TYPE_NONE:
1307 return "no";
1308 case OVS_FRAG_TYPE_FIRST:
1309 return "first";
1310 case OVS_FRAG_TYPE_LATER:
1311 return "later";
1312 case __OVS_FRAG_TYPE_MAX:
1313 default:
1314 return "<error>";
1315 }
1316 }
1317
1318 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1319 static int
1320 parse_geneve_opts(const struct nlattr *attr)
1321 {
1322 int opts_len = nl_attr_get_size(attr);
1323 const struct geneve_opt *opt = nl_attr_get(attr);
1324
1325 while (opts_len > 0) {
1326 int len;
1327
1328 if (opts_len < sizeof(*opt)) {
1329 return -EINVAL;
1330 }
1331
1332 len = sizeof(*opt) + opt->length * 4;
1333 if (len > opts_len) {
1334 return -EINVAL;
1335 }
1336
1337 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1338 default:
1339 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1340 return -EINVAL;
1341 }
1342 };
1343
1344 opt = opt + len / sizeof(*opt);
1345 opts_len -= len;
1346 };
1347
1348 return 0;
1349 }
1350
1351 enum odp_key_fitness
1352 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1353 {
1354 unsigned int left;
1355 const struct nlattr *a;
1356 bool ttl = false;
1357 bool unknown = false;
1358
1359 NL_NESTED_FOR_EACH(a, left, attr) {
1360 uint16_t type = nl_attr_type(a);
1361 size_t len = nl_attr_get_size(a);
1362 int expected_len = odp_key_attr_len(ovs_tun_key_attr_lens,
1363 OVS_TUNNEL_ATTR_MAX, type);
1364
1365 if (len != expected_len && expected_len >= 0) {
1366 return ODP_FIT_ERROR;
1367 }
1368
1369 switch (type) {
1370 case OVS_TUNNEL_KEY_ATTR_ID:
1371 tun->tun_id = nl_attr_get_be64(a);
1372 tun->flags |= FLOW_TNL_F_KEY;
1373 break;
1374 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1375 tun->ip_src = nl_attr_get_be32(a);
1376 break;
1377 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1378 tun->ip_dst = nl_attr_get_be32(a);
1379 break;
1380 case OVS_TUNNEL_KEY_ATTR_TOS:
1381 tun->ip_tos = nl_attr_get_u8(a);
1382 break;
1383 case OVS_TUNNEL_KEY_ATTR_TTL:
1384 tun->ip_ttl = nl_attr_get_u8(a);
1385 ttl = true;
1386 break;
1387 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1388 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1389 break;
1390 case OVS_TUNNEL_KEY_ATTR_CSUM:
1391 tun->flags |= FLOW_TNL_F_CSUM;
1392 break;
1393 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1394 tun->tp_src = nl_attr_get_be16(a);
1395 break;
1396 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1397 tun->tp_dst = nl_attr_get_be16(a);
1398 break;
1399 case OVS_TUNNEL_KEY_ATTR_OAM:
1400 tun->flags |= FLOW_TNL_F_OAM;
1401 break;
1402 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: {
1403 static const struct nl_policy vxlan_opts_policy[] = {
1404 [OVS_VXLAN_EXT_GBP] = { .type = NL_A_U32 },
1405 };
1406 struct nlattr *ext[ARRAY_SIZE(vxlan_opts_policy)];
1407
1408 if (!nl_parse_nested(a, vxlan_opts_policy, ext, ARRAY_SIZE(ext))) {
1409 return ODP_FIT_ERROR;
1410 }
1411
1412 if (ext[OVS_VXLAN_EXT_GBP]) {
1413 uint32_t gbp = nl_attr_get_u32(ext[OVS_VXLAN_EXT_GBP]);
1414
1415 tun->gbp_id = htons(gbp & 0xFFFF);
1416 tun->gbp_flags = (gbp >> 16) & 0xFF;
1417 }
1418
1419 break;
1420 }
1421 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1422 if (parse_geneve_opts(a)) {
1423 return ODP_FIT_ERROR;
1424 }
1425 /* It is necessary to reproduce options exactly (including order)
1426 * so it's easiest to just echo them back. */
1427 unknown = true;
1428 break;
1429 }
1430 default:
1431 /* Allow this to show up as unexpected, if there are unknown
1432 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1433 unknown = true;
1434 break;
1435 }
1436 }
1437
1438 if (!ttl) {
1439 return ODP_FIT_ERROR;
1440 }
1441 if (unknown) {
1442 return ODP_FIT_TOO_MUCH;
1443 }
1444 return ODP_FIT_PERFECT;
1445 }
1446
1447 static void
1448 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1449 {
1450 size_t tun_key_ofs;
1451
1452 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1453
1454 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1455 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1456 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1457 }
1458 if (tun_key->ip_src) {
1459 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1460 }
1461 if (tun_key->ip_dst) {
1462 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1463 }
1464 if (tun_key->ip_tos) {
1465 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1466 }
1467 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1468 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1469 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1470 }
1471 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1472 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1473 }
1474 if (tun_key->tp_src) {
1475 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1476 }
1477 if (tun_key->tp_dst) {
1478 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1479 }
1480 if (tun_key->flags & FLOW_TNL_F_OAM) {
1481 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1482 }
1483 if (tun_key->gbp_flags || tun_key->gbp_id) {
1484 size_t vxlan_opts_ofs;
1485
1486 vxlan_opts_ofs = nl_msg_start_nested(a, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
1487 nl_msg_put_u32(a, OVS_VXLAN_EXT_GBP,
1488 (tun_key->gbp_flags << 16) | ntohs(tun_key->gbp_id));
1489 nl_msg_end_nested(a, vxlan_opts_ofs);
1490 }
1491
1492 nl_msg_end_nested(a, tun_key_ofs);
1493 }
1494
1495 static bool
1496 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1497 {
1498 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1499 }
1500
1501 static bool
1502 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1503 {
1504 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1505 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1506 }
1507 if (attr == OVS_KEY_ATTR_IPV6) {
1508 const struct ovs_key_ipv6 *ipv6_mask = mask;
1509
1510 return
1511 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1512 == htonl(IPV6_LABEL_MASK))
1513 && ipv6_mask->ipv6_proto == UINT8_MAX
1514 && ipv6_mask->ipv6_tclass == UINT8_MAX
1515 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1516 && ipv6_mask->ipv6_frag == UINT8_MAX
1517 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1518 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1519 }
1520 if (attr == OVS_KEY_ATTR_TUNNEL) {
1521 const struct flow_tnl *tun_mask = mask;
1522
1523 return tun_mask->flags == FLOW_TNL_F_MASK
1524 && tun_mask->tun_id == OVS_BE64_MAX
1525 && tun_mask->ip_src == OVS_BE32_MAX
1526 && tun_mask->ip_dst == OVS_BE32_MAX
1527 && tun_mask->ip_tos == UINT8_MAX
1528 && tun_mask->ip_ttl == UINT8_MAX
1529 && tun_mask->tp_src == OVS_BE16_MAX
1530 && tun_mask->tp_dst == OVS_BE16_MAX
1531 && tun_mask->gbp_id == OVS_BE16_MAX
1532 && tun_mask->gbp_flags == UINT8_MAX;
1533 }
1534
1535 if (attr == OVS_KEY_ATTR_ARP) {
1536 /* ARP key has padding, ignore it. */
1537 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1538 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1539 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1540 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1541 }
1542
1543 return is_all_ones(mask, size);
1544 }
1545
1546 static bool
1547 odp_mask_attr_is_exact(const struct nlattr *ma)
1548 {
1549 struct flow_tnl tun_mask;
1550 enum ovs_key_attr attr = nl_attr_type(ma);
1551 const void *mask;
1552 size_t size;
1553
1554 if (attr == OVS_KEY_ATTR_TUNNEL) {
1555 memset(&tun_mask, 0, sizeof tun_mask);
1556 odp_tun_key_from_attr(ma, &tun_mask);
1557 mask = &tun_mask;
1558 size = sizeof tun_mask;
1559 } else {
1560 mask = nl_attr_get(ma);
1561 size = nl_attr_get_size(ma);
1562 }
1563
1564 return odp_mask_is_exact(attr, mask, size);
1565 }
1566
1567 void
1568 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1569 char *port_name)
1570 {
1571 struct odp_portno_names *odp_portno_names;
1572
1573 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1574 odp_portno_names->port_no = port_no;
1575 odp_portno_names->name = xstrdup(port_name);
1576 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1577 hash_odp_port(port_no));
1578 }
1579
1580 static char *
1581 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1582 {
1583 struct odp_portno_names *odp_portno_names;
1584
1585 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1586 hash_odp_port(port_no), portno_names) {
1587 if (odp_portno_names->port_no == port_no) {
1588 return odp_portno_names->name;
1589 }
1590 }
1591 return NULL;
1592 }
1593
1594 void
1595 odp_portno_names_destroy(struct hmap *portno_names)
1596 {
1597 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1598 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1599 hmap_node, portno_names) {
1600 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1601 free(odp_portno_names->name);
1602 free(odp_portno_names);
1603 }
1604 }
1605
1606 /* Format helpers. */
1607
1608 static void
1609 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1610 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1611 {
1612 bool mask_empty = mask && eth_addr_is_zero(*mask);
1613
1614 if (verbose || !mask_empty) {
1615 bool mask_full = !mask || eth_mask_is_exact(*mask);
1616
1617 if (mask_full) {
1618 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1619 } else {
1620 ds_put_format(ds, "%s=", name);
1621 eth_format_masked(key, *mask, ds);
1622 ds_put_char(ds, ',');
1623 }
1624 }
1625 }
1626
1627 static void
1628 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1629 const ovs_be64 *mask, bool verbose)
1630 {
1631 bool mask_empty = mask && !*mask;
1632
1633 if (verbose || !mask_empty) {
1634 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1635
1636 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1637 if (!mask_full) { /* Partially masked. */
1638 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1639 }
1640 ds_put_char(ds, ',');
1641 }
1642 }
1643
1644 static void
1645 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1646 const ovs_be32 *mask, bool verbose)
1647 {
1648 bool mask_empty = mask && !*mask;
1649
1650 if (verbose || !mask_empty) {
1651 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1652
1653 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1654 if (!mask_full) { /* Partially masked. */
1655 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1656 }
1657 ds_put_char(ds, ',');
1658 }
1659 }
1660
1661 static void
1662 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1663 const ovs_be32 (*mask_)[4], bool verbose)
1664 {
1665 char buf[INET6_ADDRSTRLEN];
1666 const struct in6_addr *key = (const struct in6_addr *)key_;
1667 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1668 : NULL;
1669 bool mask_empty = mask && ipv6_mask_is_any(mask);
1670
1671 if (verbose || !mask_empty) {
1672 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1673
1674 inet_ntop(AF_INET6, key, buf, sizeof buf);
1675 ds_put_format(ds, "%s=%s", name, buf);
1676 if (!mask_full) { /* Partially masked. */
1677 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1678 ds_put_format(ds, "/%s", buf);
1679 }
1680 ds_put_char(ds, ',');
1681 }
1682 }
1683
1684 static void
1685 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1686 const ovs_be32 *mask, bool verbose)
1687 {
1688 bool mask_empty = mask && !*mask;
1689
1690 if (verbose || !mask_empty) {
1691 bool mask_full = !mask
1692 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1693
1694 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1695 if (!mask_full) { /* Partially masked. */
1696 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1697 }
1698 ds_put_char(ds, ',');
1699 }
1700 }
1701
1702 static void
1703 format_u8x(struct ds *ds, const char *name, uint8_t key,
1704 const uint8_t *mask, bool verbose)
1705 {
1706 bool mask_empty = mask && !*mask;
1707
1708 if (verbose || !mask_empty) {
1709 bool mask_full = !mask || *mask == UINT8_MAX;
1710
1711 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1712 if (!mask_full) { /* Partially masked. */
1713 ds_put_format(ds, "/%#"PRIx8, *mask);
1714 }
1715 ds_put_char(ds, ',');
1716 }
1717 }
1718
1719 static void
1720 format_u8u(struct ds *ds, const char *name, uint8_t key,
1721 const uint8_t *mask, bool verbose)
1722 {
1723 bool mask_empty = mask && !*mask;
1724
1725 if (verbose || !mask_empty) {
1726 bool mask_full = !mask || *mask == UINT8_MAX;
1727
1728 ds_put_format(ds, "%s=%"PRIu8, name, key);
1729 if (!mask_full) { /* Partially masked. */
1730 ds_put_format(ds, "/%#"PRIx8, *mask);
1731 }
1732 ds_put_char(ds, ',');
1733 }
1734 }
1735
1736 static void
1737 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1738 const ovs_be16 *mask, bool verbose)
1739 {
1740 bool mask_empty = mask && !*mask;
1741
1742 if (verbose || !mask_empty) {
1743 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1744
1745 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1746 if (!mask_full) { /* Partially masked. */
1747 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1748 }
1749 ds_put_char(ds, ',');
1750 }
1751 }
1752
1753 static void
1754 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1755 const uint16_t *mask, bool verbose)
1756 {
1757 bool mask_empty = mask && !*mask;
1758
1759 if (verbose || !mask_empty) {
1760 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1761
1762 ds_put_cstr(ds, name);
1763 ds_put_char(ds, '(');
1764 if (!mask_full) { /* Partially masked. */
1765 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1766 } else { /* Fully masked. */
1767 format_flags(ds, flow_tun_flag_to_string, key, ',');
1768 }
1769 ds_put_cstr(ds, "),");
1770 }
1771 }
1772
1773 static void
1774 format_frag(struct ds *ds, const char *name, uint8_t key,
1775 const uint8_t *mask, bool verbose)
1776 {
1777 bool mask_empty = mask && !*mask;
1778
1779 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1780 if (verbose || !mask_empty) {
1781 bool mask_full = !mask || *mask == UINT8_MAX;
1782
1783 if (!mask_full) { /* Partially masked. */
1784 ds_put_format(ds, "error: partial mask not supported for frag (%#"
1785 PRIx8"),", *mask);
1786 } else {
1787 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1788 }
1789 }
1790 }
1791
1792 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1793
1794 static void
1795 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1796 const struct hmap *portno_names, struct ds *ds,
1797 bool verbose)
1798 {
1799 enum ovs_key_attr attr = nl_attr_type(a);
1800 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1801 int expected_len;
1802 bool is_exact;
1803
1804 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1805
1806 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1807
1808 {
1809 expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
1810 OVS_KEY_ATTR_MAX, nl_attr_type(a));
1811 if (expected_len != ATTR_LEN_VARIABLE &&
1812 expected_len != ATTR_LEN_NESTED) {
1813 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1814 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1815
1816 if (bad_key_len || bad_mask_len) {
1817 if (bad_key_len) {
1818 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1819 nl_attr_get_size(a), expected_len);
1820 }
1821 format_generic_odp_key(a, ds);
1822 if (ma) {
1823 ds_put_char(ds, '/');
1824 if (bad_mask_len) {
1825 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1826 nl_attr_get_size(ma), expected_len);
1827 }
1828 format_generic_odp_key(ma, ds);
1829 }
1830 ds_put_char(ds, ')');
1831 return;
1832 }
1833 }
1834 }
1835
1836 ds_put_char(ds, '(');
1837 switch (attr) {
1838 case OVS_KEY_ATTR_ENCAP:
1839 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1840 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1841 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1842 verbose);
1843 } else if (nl_attr_get_size(a)) {
1844 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1845 ds, verbose);
1846 }
1847 break;
1848
1849 case OVS_KEY_ATTR_PRIORITY:
1850 case OVS_KEY_ATTR_SKB_MARK:
1851 case OVS_KEY_ATTR_DP_HASH:
1852 case OVS_KEY_ATTR_RECIRC_ID:
1853 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1854 if (!is_exact) {
1855 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1856 }
1857 break;
1858
1859 case OVS_KEY_ATTR_TUNNEL: {
1860 struct flow_tnl key, mask_;
1861 struct flow_tnl *mask = ma ? &mask_ : NULL;
1862
1863 if (mask) {
1864 memset(mask, 0, sizeof *mask);
1865 odp_tun_key_from_attr(ma, mask);
1866 }
1867 memset(&key, 0, sizeof key);
1868 if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1869 ds_put_format(ds, "error");
1870 return;
1871 }
1872 format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1873 format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1874 format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1875 format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1876 format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1877 format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1878 format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1879 format_be16(ds, "gbp_id", key.gbp_id, MASK(mask, gbp_id), verbose);
1880 format_u8x(ds, "gbp_flags", key.gbp_flags, MASK(mask, gbp_flags), verbose);
1881 format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1882 ds_chomp(ds, ',');
1883 break;
1884 }
1885 case OVS_KEY_ATTR_IN_PORT:
1886 if (portno_names && verbose && is_exact) {
1887 char *name = odp_portno_names_get(portno_names,
1888 u32_to_odp(nl_attr_get_u32(a)));
1889 if (name) {
1890 ds_put_format(ds, "%s", name);
1891 } else {
1892 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1893 }
1894 } else {
1895 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1896 if (!is_exact) {
1897 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1898 }
1899 }
1900 break;
1901
1902 case OVS_KEY_ATTR_ETHERNET: {
1903 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1904 const struct ovs_key_ethernet *key = nl_attr_get(a);
1905
1906 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1907 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1908 ds_chomp(ds, ',');
1909 break;
1910 }
1911 case OVS_KEY_ATTR_VLAN:
1912 format_vlan_tci(ds, nl_attr_get_be16(a),
1913 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1914 break;
1915
1916 case OVS_KEY_ATTR_MPLS: {
1917 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1918 const struct ovs_key_mpls *mpls_mask = NULL;
1919 size_t size = nl_attr_get_size(a);
1920
1921 if (!size || size % sizeof *mpls_key) {
1922 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1923 return;
1924 }
1925 if (!is_exact) {
1926 mpls_mask = nl_attr_get(ma);
1927 if (size != nl_attr_get_size(ma)) {
1928 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1929 "mask length %"PRIuSIZE")",
1930 size, nl_attr_get_size(ma));
1931 return;
1932 }
1933 }
1934 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1935 break;
1936 }
1937 case OVS_KEY_ATTR_ETHERTYPE:
1938 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1939 if (!is_exact) {
1940 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1941 }
1942 break;
1943
1944 case OVS_KEY_ATTR_IPV4: {
1945 const struct ovs_key_ipv4 *key = nl_attr_get(a);
1946 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1947
1948 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1949 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1950 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1951 verbose);
1952 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1953 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1954 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1955 verbose);
1956 ds_chomp(ds, ',');
1957 break;
1958 }
1959 case OVS_KEY_ATTR_IPV6: {
1960 const struct ovs_key_ipv6 *key = nl_attr_get(a);
1961 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1962
1963 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1964 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1965 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1966 verbose);
1967 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1968 verbose);
1969 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1970 verbose);
1971 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1972 verbose);
1973 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1974 verbose);
1975 ds_chomp(ds, ',');
1976 break;
1977 }
1978 /* These have the same structure and format. */
1979 case OVS_KEY_ATTR_TCP:
1980 case OVS_KEY_ATTR_UDP:
1981 case OVS_KEY_ATTR_SCTP: {
1982 const struct ovs_key_tcp *key = nl_attr_get(a);
1983 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1984
1985 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1986 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1987 ds_chomp(ds, ',');
1988 break;
1989 }
1990 case OVS_KEY_ATTR_TCP_FLAGS:
1991 if (!is_exact) {
1992 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1993 ntohs(nl_attr_get_be16(a)),
1994 ntohs(nl_attr_get_be16(ma)));
1995 } else {
1996 format_flags(ds, packet_tcp_flag_to_string,
1997 ntohs(nl_attr_get_be16(a)), ',');
1998 }
1999 break;
2000
2001 case OVS_KEY_ATTR_ICMP: {
2002 const struct ovs_key_icmp *key = nl_attr_get(a);
2003 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
2004
2005 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
2006 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
2007 ds_chomp(ds, ',');
2008 break;
2009 }
2010 case OVS_KEY_ATTR_ICMPV6: {
2011 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
2012 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
2013
2014 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
2015 verbose);
2016 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
2017 verbose);
2018 ds_chomp(ds, ',');
2019 break;
2020 }
2021 case OVS_KEY_ATTR_ARP: {
2022 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
2023 const struct ovs_key_arp *key = nl_attr_get(a);
2024
2025 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
2026 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
2027 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
2028 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
2029 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
2030 ds_chomp(ds, ',');
2031 break;
2032 }
2033 case OVS_KEY_ATTR_ND: {
2034 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
2035 const struct ovs_key_nd *key = nl_attr_get(a);
2036
2037 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
2038 verbose);
2039 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
2040 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
2041
2042 ds_chomp(ds, ',');
2043 break;
2044 }
2045 case OVS_KEY_ATTR_UNSPEC:
2046 case __OVS_KEY_ATTR_MAX:
2047 default:
2048 format_generic_odp_key(a, ds);
2049 if (!is_exact) {
2050 ds_put_char(ds, '/');
2051 format_generic_odp_key(ma, ds);
2052 }
2053 break;
2054 }
2055 ds_put_char(ds, ')');
2056 }
2057
2058 static struct nlattr *
2059 generate_all_wildcard_mask(const struct attr_len_tbl tbl[], int max,
2060 struct ofpbuf *ofp, const struct nlattr *key)
2061 {
2062 const struct nlattr *a;
2063 unsigned int left;
2064 int type = nl_attr_type(key);
2065 int size = nl_attr_get_size(key);
2066
2067 if (odp_key_attr_len(tbl, max, type) != ATTR_LEN_NESTED) {
2068 nl_msg_put_unspec_zero(ofp, type, size);
2069 } else {
2070 size_t nested_mask;
2071
2072 if (tbl[type].next) {
2073 tbl = tbl[type].next;
2074 max = tbl[type].next_max;
2075 }
2076
2077 nested_mask = nl_msg_start_nested(ofp, type);
2078 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
2079 generate_all_wildcard_mask(tbl, max, ofp, nl_attr_get(a));
2080 }
2081 nl_msg_end_nested(ofp, nested_mask);
2082 }
2083
2084 return ofp->base;
2085 }
2086
2087 int
2088 odp_ufid_from_string(const char *s_, ovs_u128 *ufid)
2089 {
2090 const char *s = s_;
2091
2092 if (ovs_scan(s, "ufid:")) {
2093 size_t n;
2094
2095 s += 5;
2096 if (ovs_scan(s, "0x")) {
2097 s += 2;
2098 }
2099
2100 n = strspn(s, hex_chars);
2101 if (n != 32) {
2102 return -EINVAL;
2103 }
2104
2105 if (!ovs_scan(s, "%16"SCNx64"%16"SCNx64, &ufid->u64.hi,
2106 &ufid->u64.lo)) {
2107 return -EINVAL;
2108 }
2109 s += n;
2110 s += strspn(s, delimiters);
2111
2112 return s - s_;
2113 }
2114
2115 return 0;
2116 }
2117
2118 void
2119 odp_format_ufid(const ovs_u128 *ufid, struct ds *ds)
2120 {
2121 ds_put_format(ds, "ufid:%016"PRIx64"%016"PRIx64, ufid->u64.hi,
2122 ufid->u64.lo);
2123 }
2124
2125 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2126 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
2127 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
2128 * non-null and 'verbose' is true, translates odp port number to its name. */
2129 void
2130 odp_flow_format(const struct nlattr *key, size_t key_len,
2131 const struct nlattr *mask, size_t mask_len,
2132 const struct hmap *portno_names, struct ds *ds, bool verbose)
2133 {
2134 if (key_len) {
2135 const struct nlattr *a;
2136 unsigned int left;
2137 bool has_ethtype_key = false;
2138 const struct nlattr *ma = NULL;
2139 struct ofpbuf ofp;
2140 bool first_field = true;
2141
2142 ofpbuf_init(&ofp, 100);
2143 NL_ATTR_FOR_EACH (a, left, key, key_len) {
2144 bool is_nested_attr;
2145 bool is_wildcard = false;
2146 int attr_type = nl_attr_type(a);
2147
2148 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
2149 has_ethtype_key = true;
2150 }
2151
2152 is_nested_attr = odp_key_attr_len(ovs_flow_key_attr_lens,
2153 OVS_KEY_ATTR_MAX, attr_type) ==
2154 ATTR_LEN_NESTED;
2155
2156 if (mask && mask_len) {
2157 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
2158 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
2159 }
2160
2161 if (verbose || !is_wildcard || is_nested_attr) {
2162 if (is_wildcard && !ma) {
2163 ma = generate_all_wildcard_mask(ovs_flow_key_attr_lens,
2164 OVS_KEY_ATTR_MAX,
2165 &ofp, a);
2166 }
2167 if (!first_field) {
2168 ds_put_char(ds, ',');
2169 }
2170 format_odp_key_attr(a, ma, portno_names, ds, verbose);
2171 first_field = false;
2172 }
2173 ofpbuf_clear(&ofp);
2174 }
2175 ofpbuf_uninit(&ofp);
2176
2177 if (left) {
2178 int i;
2179
2180 if (left == key_len) {
2181 ds_put_cstr(ds, "<empty>");
2182 }
2183 ds_put_format(ds, ",***%u leftover bytes*** (", left);
2184 for (i = 0; i < left; i++) {
2185 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
2186 }
2187 ds_put_char(ds, ')');
2188 }
2189 if (!has_ethtype_key) {
2190 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
2191 if (ma) {
2192 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
2193 ntohs(nl_attr_get_be16(ma)));
2194 }
2195 }
2196 } else {
2197 ds_put_cstr(ds, "<empty>");
2198 }
2199 }
2200
2201 /* Appends to 'ds' a string representation of the 'key_len' bytes of
2202 * OVS_KEY_ATTR_* attributes in 'key'. */
2203 void
2204 odp_flow_key_format(const struct nlattr *key,
2205 size_t key_len, struct ds *ds)
2206 {
2207 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
2208 }
2209
2210 static bool
2211 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
2212 {
2213 if (!strcasecmp(s, "no")) {
2214 *type = OVS_FRAG_TYPE_NONE;
2215 } else if (!strcasecmp(s, "first")) {
2216 *type = OVS_FRAG_TYPE_FIRST;
2217 } else if (!strcasecmp(s, "later")) {
2218 *type = OVS_FRAG_TYPE_LATER;
2219 } else {
2220 return false;
2221 }
2222 return true;
2223 }
2224
2225 /* Parsing. */
2226
2227 static int
2228 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
2229 uint8_t (*mask)[ETH_ADDR_LEN])
2230 {
2231 int n;
2232
2233 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
2234 int len = n;
2235
2236 if (mask) {
2237 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
2238 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
2239 len += n;
2240 } else {
2241 memset(mask, 0xff, sizeof *mask);
2242 }
2243 }
2244 return len;
2245 }
2246 return 0;
2247 }
2248
2249 static int
2250 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
2251 {
2252 int n;
2253
2254 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
2255 int len = n;
2256
2257 if (mask) {
2258 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
2259 IP_SCAN_ARGS(mask), &n)) {
2260 len += n;
2261 } else {
2262 *mask = OVS_BE32_MAX;
2263 }
2264 }
2265 return len;
2266 }
2267 return 0;
2268 }
2269
2270 static int
2271 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
2272 {
2273 int n;
2274 char ipv6_s[IPV6_SCAN_LEN + 1];
2275
2276 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
2277 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
2278 int len = n;
2279
2280 if (mask) {
2281 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
2282 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
2283 len += n;
2284 } else {
2285 memset(mask, 0xff, sizeof *mask);
2286 }
2287 }
2288 return len;
2289 }
2290 return 0;
2291 }
2292
2293 static int
2294 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2295 {
2296 int key_, mask_;
2297 int n;
2298
2299 if (ovs_scan(s, "%i%n", &key_, &n)
2300 && (key_ & ~IPV6_LABEL_MASK) == 0) {
2301 int len = n;
2302
2303 *key = htonl(key_);
2304 if (mask) {
2305 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
2306 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
2307 len += n;
2308 *mask = htonl(mask_);
2309 } else {
2310 *mask = htonl(IPV6_LABEL_MASK);
2311 }
2312 }
2313 return len;
2314 }
2315 return 0;
2316 }
2317
2318 static int
2319 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
2320 {
2321 int n;
2322
2323 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
2324 int len = n;
2325
2326 if (mask) {
2327 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
2328 len += n;
2329 } else {
2330 *mask = UINT8_MAX;
2331 }
2332 }
2333 return len;
2334 }
2335 return 0;
2336 }
2337
2338 static int
2339 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
2340 {
2341 int n;
2342
2343 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2344 int len = n;
2345
2346 if (mask) {
2347 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2348 len += n;
2349 } else {
2350 *mask = UINT32_MAX;
2351 }
2352 }
2353 return len;
2354 }
2355 return 0;
2356 }
2357
2358 static int
2359 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
2360 {
2361 uint16_t key_, mask_;
2362 int n;
2363
2364 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2365 int len = n;
2366
2367 *key = htons(key_);
2368 if (mask) {
2369 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2370 len += n;
2371 *mask = htons(mask_);
2372 } else {
2373 *mask = OVS_BE16_MAX;
2374 }
2375 }
2376 return len;
2377 }
2378 return 0;
2379 }
2380
2381 static int
2382 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2383 {
2384 uint64_t key_, mask_;
2385 int n;
2386
2387 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2388 int len = n;
2389
2390 *key = htonll(key_);
2391 if (mask) {
2392 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2393 len += n;
2394 *mask = htonll(mask_);
2395 } else {
2396 *mask = OVS_BE64_MAX;
2397 }
2398 }
2399 return len;
2400 }
2401 return 0;
2402 }
2403
2404 static int
2405 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2406 {
2407 uint32_t flags, fmask;
2408 int n;
2409
2410 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2411 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2412 if (n >= 0 && s[n] == ')') {
2413 *key = flags;
2414 if (mask) {
2415 *mask = fmask;
2416 }
2417 return n + 1;
2418 }
2419 return 0;
2420 }
2421
2422 static int
2423 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2424 {
2425 uint32_t flags, fmask;
2426 int n;
2427
2428 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2429 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2430 if (n >= 0) {
2431 *key = htons(flags);
2432 if (mask) {
2433 *mask = htons(fmask);
2434 }
2435 return n;
2436 }
2437 return 0;
2438 }
2439
2440 static int
2441 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2442 {
2443 int n;
2444 char frag[8];
2445 enum ovs_frag_type frag_type;
2446
2447 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2448 && ovs_frag_type_from_string(frag, &frag_type)) {
2449 int len = n;
2450
2451 *key = frag_type;
2452 if (mask) {
2453 *mask = UINT8_MAX;
2454 }
2455 return len;
2456 }
2457 return 0;
2458 }
2459
2460 static int
2461 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2462 const struct simap *port_names)
2463 {
2464 int n;
2465
2466 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2467 int len = n;
2468
2469 if (mask) {
2470 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2471 len += n;
2472 } else {
2473 *mask = UINT32_MAX;
2474 }
2475 }
2476 return len;
2477 } else if (port_names) {
2478 const struct simap_node *node;
2479 int len;
2480
2481 len = strcspn(s, ")");
2482 node = simap_find_len(port_names, s, len);
2483 if (node) {
2484 *key = node->data;
2485
2486 if (mask) {
2487 *mask = UINT32_MAX;
2488 }
2489 return len;
2490 }
2491 }
2492 return 0;
2493 }
2494
2495 /* Helper for vlan parsing. */
2496 struct ovs_key_vlan__ {
2497 ovs_be16 tci;
2498 };
2499
2500 static bool
2501 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2502 {
2503 const uint16_t mask = ((1U << bits) - 1) << offset;
2504
2505 if (value >> bits) {
2506 return false;
2507 }
2508
2509 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2510 return true;
2511 }
2512
2513 static int
2514 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2515 uint8_t offset)
2516 {
2517 uint16_t key_, mask_;
2518 int n;
2519
2520 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2521 int len = n;
2522
2523 if (set_be16_bf(key, bits, offset, key_)) {
2524 if (mask) {
2525 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2526 len += n;
2527
2528 if (!set_be16_bf(mask, bits, offset, mask_)) {
2529 return 0;
2530 }
2531 } else {
2532 *mask |= htons(((1U << bits) - 1) << offset);
2533 }
2534 }
2535 return len;
2536 }
2537 }
2538 return 0;
2539 }
2540
2541 static int
2542 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2543 {
2544 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2545 }
2546
2547 static int
2548 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2549 {
2550 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2551 }
2552
2553 static int
2554 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2555 {
2556 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2557 }
2558
2559 /* For MPLS. */
2560 static bool
2561 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2562 {
2563 const uint32_t mask = ((1U << bits) - 1) << offset;
2564
2565 if (value >> bits) {
2566 return false;
2567 }
2568
2569 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2570 return true;
2571 }
2572
2573 static int
2574 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2575 uint8_t offset)
2576 {
2577 uint32_t key_, mask_;
2578 int n;
2579
2580 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2581 int len = n;
2582
2583 if (set_be32_bf(key, bits, offset, key_)) {
2584 if (mask) {
2585 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2586 len += n;
2587
2588 if (!set_be32_bf(mask, bits, offset, mask_)) {
2589 return 0;
2590 }
2591 } else {
2592 *mask |= htonl(((1U << bits) - 1) << offset);
2593 }
2594 }
2595 return len;
2596 }
2597 }
2598 return 0;
2599 }
2600
2601 static int
2602 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2603 {
2604 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2605 }
2606
2607 static int
2608 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2609 {
2610 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2611 }
2612
2613 static int
2614 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2615 {
2616 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2617 }
2618
2619 static int
2620 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2621 {
2622 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2623 }
2624
2625 /* ATTR is compile-time constant, so only the case with correct data type
2626 * will be used. However, the compiler complains about the data type for
2627 * the other cases, so we must cast to make the compiler silent. */
2628 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2629 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2630 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2631 } else { \
2632 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2633 }
2634
2635 #define SCAN_IF(NAME) \
2636 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2637 const char *start = s; \
2638 int len; \
2639 \
2640 s += strlen(NAME)
2641
2642 /* Usually no special initialization is needed. */
2643 #define SCAN_BEGIN(NAME, TYPE) \
2644 SCAN_IF(NAME); \
2645 TYPE skey, smask; \
2646 memset(&skey, 0, sizeof skey); \
2647 memset(&smask, 0, sizeof smask); \
2648 do { \
2649 len = 0;
2650
2651 /* Init as fully-masked as mask will not be scanned. */
2652 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
2653 SCAN_IF(NAME); \
2654 TYPE skey, smask; \
2655 memset(&skey, 0, sizeof skey); \
2656 memset(&smask, 0xff, sizeof smask); \
2657 do { \
2658 len = 0;
2659
2660 /* VLAN needs special initialization. */
2661 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2662 SCAN_IF(NAME); \
2663 TYPE skey = KEY_INIT; \
2664 TYPE smask = MASK_INIT; \
2665 do { \
2666 len = 0;
2667
2668 /* Scan unnamed entry as 'TYPE' */
2669 #define SCAN_TYPE(TYPE, KEY, MASK) \
2670 len = scan_##TYPE(s, KEY, MASK); \
2671 if (len == 0) { \
2672 return -EINVAL; \
2673 } \
2674 s += len
2675
2676 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2677 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2678 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2679 s += strlen(NAME); \
2680 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2681 continue; \
2682 }
2683
2684 #define SCAN_FINISH() \
2685 } while (*s++ == ',' && len != 0); \
2686 if (s[-1] != ')') { \
2687 return -EINVAL; \
2688 }
2689
2690 #define SCAN_FINISH_SINGLE() \
2691 } while (false); \
2692 if (*s++ != ')') { \
2693 return -EINVAL; \
2694 }
2695
2696 #define SCAN_PUT(ATTR) \
2697 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2698 SCAN_PUT_ATTR(key, ATTR, skey); \
2699 if (mask) { \
2700 SCAN_PUT_ATTR(mask, ATTR, smask); \
2701 } \
2702 }
2703
2704 #define SCAN_END(ATTR) \
2705 SCAN_FINISH(); \
2706 SCAN_PUT(ATTR); \
2707 return s - start; \
2708 }
2709
2710 #define SCAN_END_SINGLE(ATTR) \
2711 SCAN_FINISH_SINGLE(); \
2712 SCAN_PUT(ATTR); \
2713 return s - start; \
2714 }
2715
2716 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2717 SCAN_BEGIN(NAME, TYPE) { \
2718 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2719 } SCAN_END_SINGLE(ATTR)
2720
2721 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
2722 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
2723 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2724 } SCAN_END_SINGLE(ATTR)
2725
2726 /* scan_port needs one extra argument. */
2727 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2728 SCAN_BEGIN(NAME, TYPE) { \
2729 len = scan_port(s, &skey, &smask, port_names); \
2730 if (len == 0) { \
2731 return -EINVAL; \
2732 } \
2733 s += len; \
2734 } SCAN_END_SINGLE(ATTR)
2735
2736 static int
2737 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2738 struct ofpbuf *key, struct ofpbuf *mask)
2739 {
2740 if (!strncmp(s, "ufid:", 5)) {
2741 const char *start = s;
2742
2743 /* Skip UFID. */
2744 s += 5;
2745 s += strspn(s, hex_chars);
2746 s += strspn(s, delimiters);
2747
2748 return s - start;
2749 }
2750
2751 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2752 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2753 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32,
2754 OVS_KEY_ATTR_RECIRC_ID);
2755 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2756
2757 SCAN_BEGIN("tunnel(", struct flow_tnl) {
2758 SCAN_FIELD("tun_id=", be64, tun_id);
2759 SCAN_FIELD("src=", ipv4, ip_src);
2760 SCAN_FIELD("dst=", ipv4, ip_dst);
2761 SCAN_FIELD("tos=", u8, ip_tos);
2762 SCAN_FIELD("ttl=", u8, ip_ttl);
2763 SCAN_FIELD("tp_src=", be16, tp_src);
2764 SCAN_FIELD("tp_dst=", be16, tp_dst);
2765 SCAN_FIELD("gbp_id=", be16, gbp_id);
2766 SCAN_FIELD("gbp_flags=", u8, gbp_flags);
2767 SCAN_FIELD("flags(", tun_flags, flags);
2768 } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2769
2770 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2771
2772 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2773 SCAN_FIELD("src=", eth, eth_src);
2774 SCAN_FIELD("dst=", eth, eth_dst);
2775 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2776
2777 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2778 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2779 SCAN_FIELD("vid=", vid, tci);
2780 SCAN_FIELD("pcp=", pcp, tci);
2781 SCAN_FIELD("cfi=", cfi, tci);
2782 } SCAN_END(OVS_KEY_ATTR_VLAN);
2783
2784 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2785
2786 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2787 SCAN_FIELD("label=", mpls_label, mpls_lse);
2788 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2789 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2790 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2791 } SCAN_END(OVS_KEY_ATTR_MPLS);
2792
2793 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2794 SCAN_FIELD("src=", ipv4, ipv4_src);
2795 SCAN_FIELD("dst=", ipv4, ipv4_dst);
2796 SCAN_FIELD("proto=", u8, ipv4_proto);
2797 SCAN_FIELD("tos=", u8, ipv4_tos);
2798 SCAN_FIELD("ttl=", u8, ipv4_ttl);
2799 SCAN_FIELD("frag=", frag, ipv4_frag);
2800 } SCAN_END(OVS_KEY_ATTR_IPV4);
2801
2802 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2803 SCAN_FIELD("src=", ipv6, ipv6_src);
2804 SCAN_FIELD("dst=", ipv6, ipv6_dst);
2805 SCAN_FIELD("label=", ipv6_label, ipv6_label);
2806 SCAN_FIELD("proto=", u8, ipv6_proto);
2807 SCAN_FIELD("tclass=", u8, ipv6_tclass);
2808 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2809 SCAN_FIELD("frag=", frag, ipv6_frag);
2810 } SCAN_END(OVS_KEY_ATTR_IPV6);
2811
2812 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2813 SCAN_FIELD("src=", be16, tcp_src);
2814 SCAN_FIELD("dst=", be16, tcp_dst);
2815 } SCAN_END(OVS_KEY_ATTR_TCP);
2816
2817 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2818
2819 SCAN_BEGIN("udp(", struct ovs_key_udp) {
2820 SCAN_FIELD("src=", be16, udp_src);
2821 SCAN_FIELD("dst=", be16, udp_dst);
2822 } SCAN_END(OVS_KEY_ATTR_UDP);
2823
2824 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2825 SCAN_FIELD("src=", be16, sctp_src);
2826 SCAN_FIELD("dst=", be16, sctp_dst);
2827 } SCAN_END(OVS_KEY_ATTR_SCTP);
2828
2829 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2830 SCAN_FIELD("type=", u8, icmp_type);
2831 SCAN_FIELD("code=", u8, icmp_code);
2832 } SCAN_END(OVS_KEY_ATTR_ICMP);
2833
2834 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2835 SCAN_FIELD("type=", u8, icmpv6_type);
2836 SCAN_FIELD("code=", u8, icmpv6_code);
2837 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2838
2839 SCAN_BEGIN("arp(", struct ovs_key_arp) {
2840 SCAN_FIELD("sip=", ipv4, arp_sip);
2841 SCAN_FIELD("tip=", ipv4, arp_tip);
2842 SCAN_FIELD("op=", be16, arp_op);
2843 SCAN_FIELD("sha=", eth, arp_sha);
2844 SCAN_FIELD("tha=", eth, arp_tha);
2845 } SCAN_END(OVS_KEY_ATTR_ARP);
2846
2847 SCAN_BEGIN("nd(", struct ovs_key_nd) {
2848 SCAN_FIELD("target=", ipv6, nd_target);
2849 SCAN_FIELD("sll=", eth, nd_sll);
2850 SCAN_FIELD("tll=", eth, nd_tll);
2851 } SCAN_END(OVS_KEY_ATTR_ND);
2852
2853 /* Encap open-coded. */
2854 if (!strncmp(s, "encap(", 6)) {
2855 const char *start = s;
2856 size_t encap, encap_mask = 0;
2857
2858 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2859 if (mask) {
2860 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2861 }
2862
2863 s += 6;
2864 for (;;) {
2865 int retval;
2866
2867 s += strspn(s, delimiters);
2868 if (!*s) {
2869 return -EINVAL;
2870 } else if (*s == ')') {
2871 break;
2872 }
2873
2874 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2875 if (retval < 0) {
2876 return retval;
2877 }
2878 s += retval;
2879 }
2880 s++;
2881
2882 nl_msg_end_nested(key, encap);
2883 if (mask) {
2884 nl_msg_end_nested(mask, encap_mask);
2885 }
2886
2887 return s - start;
2888 }
2889
2890 return -EINVAL;
2891 }
2892
2893 /* Parses the string representation of a datapath flow key, in the
2894 * format output by odp_flow_key_format(). Returns 0 if successful,
2895 * otherwise a positive errno value. On success, the flow key is
2896 * appended to 'key' as a series of Netlink attributes. On failure, no
2897 * data is appended to 'key'. Either way, 'key''s data might be
2898 * reallocated.
2899 *
2900 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2901 * to a port number. (Port names may be used instead of port numbers in
2902 * in_port.)
2903 *
2904 * On success, the attributes appended to 'key' are individually syntactically
2905 * valid, but they may not be valid as a sequence. 'key' might, for example,
2906 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2907 int
2908 odp_flow_from_string(const char *s, const struct simap *port_names,
2909 struct ofpbuf *key, struct ofpbuf *mask)
2910 {
2911 const size_t old_size = key->size;
2912 for (;;) {
2913 int retval;
2914
2915 s += strspn(s, delimiters);
2916 if (!*s) {
2917 return 0;
2918 }
2919
2920 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2921 if (retval < 0) {
2922 key->size = old_size;
2923 return -retval;
2924 }
2925 s += retval;
2926 }
2927
2928 return 0;
2929 }
2930
2931 static uint8_t
2932 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
2933 {
2934 if (is_mask) {
2935 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
2936 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
2937 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
2938 * must use a zero mask for the netlink frag field, and all ones mask
2939 * otherwise. */
2940 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
2941 }
2942 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
2943 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
2944 : OVS_FRAG_TYPE_FIRST;
2945 }
2946
2947 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
2948 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
2949 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
2950 bool is_mask);
2951 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
2952 bool is_mask);
2953 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
2954 bool is_mask);
2955 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
2956 bool is_mask);
2957 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
2958 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
2959 static void get_nd_key(const struct flow *, struct ovs_key_nd *);
2960 static void put_nd_key(const struct ovs_key_nd *, struct flow *);
2961
2962 /* These share the same layout. */
2963 union ovs_key_tp {
2964 struct ovs_key_tcp tcp;
2965 struct ovs_key_udp udp;
2966 struct ovs_key_sctp sctp;
2967 };
2968
2969 static void get_tp_key(const struct flow *, union ovs_key_tp *);
2970 static void put_tp_key(const union ovs_key_tp *, struct flow *);
2971
2972 static void
2973 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2974 const struct flow *mask, odp_port_t odp_in_port,
2975 size_t max_mpls_depth, bool recirc, bool export_mask)
2976 {
2977 struct ovs_key_ethernet *eth_key;
2978 size_t encap;
2979 const struct flow *data = export_mask ? mask : flow;
2980
2981 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2982
2983 if (flow->tunnel.ip_dst || export_mask) {
2984 tun_key_to_attr(buf, &data->tunnel);
2985 }
2986
2987 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2988
2989 if (recirc) {
2990 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2991 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2992 }
2993
2994 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2995 * is not the magical value "ODPP_NONE". */
2996 if (export_mask || odp_in_port != ODPP_NONE) {
2997 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2998 }
2999
3000 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
3001 sizeof *eth_key);
3002 get_ethernet_key(data, eth_key);
3003
3004 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
3005 if (export_mask) {
3006 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3007 } else {
3008 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
3009 }
3010 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
3011 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
3012 if (flow->vlan_tci == htons(0)) {
3013 goto unencap;
3014 }
3015 } else {
3016 encap = 0;
3017 }
3018
3019 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3020 /* For backwards compatibility with kernels that don't support
3021 * wildcarding, the following convention is used to encode the
3022 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
3023 *
3024 * key mask matches
3025 * -------- -------- -------
3026 * >0x5ff 0xffff Specified Ethernet II Ethertype.
3027 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
3028 * <none> 0xffff Any non-Ethernet II frame (except valid
3029 * 802.3 SNAP packet with valid eth_type).
3030 */
3031 if (export_mask) {
3032 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
3033 }
3034 goto unencap;
3035 }
3036
3037 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
3038
3039 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3040 struct ovs_key_ipv4 *ipv4_key;
3041
3042 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
3043 sizeof *ipv4_key);
3044 get_ipv4_key(data, ipv4_key, export_mask);
3045 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
3046 struct ovs_key_ipv6 *ipv6_key;
3047
3048 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
3049 sizeof *ipv6_key);
3050 get_ipv6_key(data, ipv6_key, export_mask);
3051 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3052 flow->dl_type == htons(ETH_TYPE_RARP)) {
3053 struct ovs_key_arp *arp_key;
3054
3055 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
3056 sizeof *arp_key);
3057 get_arp_key(data, arp_key);
3058 } else if (eth_type_mpls(flow->dl_type)) {
3059 struct ovs_key_mpls *mpls_key;
3060 int i, n;
3061
3062 n = flow_count_mpls_labels(flow, NULL);
3063 n = MIN(n, max_mpls_depth);
3064 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
3065 n * sizeof *mpls_key);
3066 for (i = 0; i < n; i++) {
3067 mpls_key[i].mpls_lse = data->mpls_lse[i];
3068 }
3069 }
3070
3071 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3072 if (flow->nw_proto == IPPROTO_TCP) {
3073 union ovs_key_tp *tcp_key;
3074
3075 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
3076 sizeof *tcp_key);
3077 get_tp_key(data, tcp_key);
3078 if (data->tcp_flags) {
3079 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
3080 }
3081 } else if (flow->nw_proto == IPPROTO_UDP) {
3082 union ovs_key_tp *udp_key;
3083
3084 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
3085 sizeof *udp_key);
3086 get_tp_key(data, udp_key);
3087 } else if (flow->nw_proto == IPPROTO_SCTP) {
3088 union ovs_key_tp *sctp_key;
3089
3090 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
3091 sizeof *sctp_key);
3092 get_tp_key(data, sctp_key);
3093 } else if (flow->dl_type == htons(ETH_TYPE_IP)
3094 && flow->nw_proto == IPPROTO_ICMP) {
3095 struct ovs_key_icmp *icmp_key;
3096
3097 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
3098 sizeof *icmp_key);
3099 icmp_key->icmp_type = ntohs(data->tp_src);
3100 icmp_key->icmp_code = ntohs(data->tp_dst);
3101 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
3102 && flow->nw_proto == IPPROTO_ICMPV6) {
3103 struct ovs_key_icmpv6 *icmpv6_key;
3104
3105 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
3106 sizeof *icmpv6_key);
3107 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
3108 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
3109
3110 if (flow->tp_dst == htons(0)
3111 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
3112 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
3113 && (!export_mask || (data->tp_src == htons(0xffff)
3114 && data->tp_dst == htons(0xffff)))) {
3115
3116 struct ovs_key_nd *nd_key;
3117
3118 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
3119 sizeof *nd_key);
3120 memcpy(nd_key->nd_target, &data->nd_target,
3121 sizeof nd_key->nd_target);
3122 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
3123 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
3124 }
3125 }
3126 }
3127
3128 unencap:
3129 if (encap) {
3130 nl_msg_end_nested(buf, encap);
3131 }
3132 }
3133
3134 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
3135 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
3136 * number rather than a datapath port number). Instead, if 'odp_in_port'
3137 * is anything other than ODPP_NONE, it is included in 'buf' as the input
3138 * port.
3139 *
3140 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3141 * capable of being expanded to allow for that much space.
3142 *
3143 * 'recirc' indicates support for recirculation fields. If this is true, then
3144 * these fields will always be serialised. */
3145 void
3146 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
3147 const struct flow *mask, odp_port_t odp_in_port,
3148 bool recirc)
3149 {
3150 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
3151 false);
3152 }
3153
3154 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
3155 * 'buf'. 'flow' is used as a template to determine how to interpret
3156 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
3157 * it doesn't indicate whether the other fields should be interpreted as
3158 * ARP, IPv4, IPv6, etc.
3159 *
3160 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
3161 * capable of being expanded to allow for that much space.
3162 *
3163 * 'recirc' indicates support for recirculation fields. If this is true, then
3164 * these fields will always be serialised. */
3165 void
3166 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
3167 const struct flow *flow, uint32_t odp_in_port_mask,
3168 size_t max_mpls_depth, bool recirc)
3169 {
3170 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
3171 max_mpls_depth, recirc, true);
3172 }
3173
3174 /* Generate ODP flow key from the given packet metadata */
3175 void
3176 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
3177 {
3178 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
3179
3180 if (md->tunnel.ip_dst) {
3181 tun_key_to_attr(buf, &md->tunnel);
3182 }
3183
3184 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
3185
3186 /* Add an ingress port attribute if 'odp_in_port' is not the magical
3187 * value "ODPP_NONE". */
3188 if (md->in_port.odp_port != ODPP_NONE) {
3189 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
3190 }
3191 }
3192
3193 /* Generate packet metadata from the given ODP flow key. */
3194 void
3195 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
3196 struct pkt_metadata *md)
3197 {
3198 const struct nlattr *nla;
3199 size_t left;
3200 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
3201 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
3202 1u << OVS_KEY_ATTR_IN_PORT;
3203
3204 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3205
3206 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3207 uint16_t type = nl_attr_type(nla);
3208 size_t len = nl_attr_get_size(nla);
3209 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3210 OVS_KEY_ATTR_MAX, type);
3211
3212 if (len != expected_len && expected_len >= 0) {
3213 continue;
3214 }
3215
3216 switch (type) {
3217 case OVS_KEY_ATTR_RECIRC_ID:
3218 md->recirc_id = nl_attr_get_u32(nla);
3219 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
3220 break;
3221 case OVS_KEY_ATTR_DP_HASH:
3222 md->dp_hash = nl_attr_get_u32(nla);
3223 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
3224 break;
3225 case OVS_KEY_ATTR_PRIORITY:
3226 md->skb_priority = nl_attr_get_u32(nla);
3227 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
3228 break;
3229 case OVS_KEY_ATTR_SKB_MARK:
3230 md->pkt_mark = nl_attr_get_u32(nla);
3231 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
3232 break;
3233 case OVS_KEY_ATTR_TUNNEL: {
3234 enum odp_key_fitness res;
3235
3236 res = odp_tun_key_from_attr(nla, &md->tunnel);
3237 if (res == ODP_FIT_ERROR) {
3238 memset(&md->tunnel, 0, sizeof md->tunnel);
3239 } else if (res == ODP_FIT_PERFECT) {
3240 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
3241 }
3242 break;
3243 }
3244 case OVS_KEY_ATTR_IN_PORT:
3245 md->in_port.odp_port = nl_attr_get_odp_port(nla);
3246 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
3247 break;
3248 default:
3249 break;
3250 }
3251
3252 if (!wanted_attrs) {
3253 return; /* Have everything. */
3254 }
3255 }
3256 }
3257
3258 uint32_t
3259 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
3260 {
3261 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
3262 return hash_words(ALIGNED_CAST(const uint32_t *, key),
3263 key_len / sizeof(uint32_t), 0);
3264 }
3265
3266 static void
3267 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
3268 uint64_t attrs, int out_of_range_attr,
3269 const struct nlattr *key, size_t key_len)
3270 {
3271 struct ds s;
3272 int i;
3273
3274 if (VLOG_DROP_DBG(rl)) {
3275 return;
3276 }
3277
3278 ds_init(&s);
3279 for (i = 0; i < 64; i++) {
3280 if (attrs & (UINT64_C(1) << i)) {
3281 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3282
3283 ds_put_format(&s, " %s",
3284 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
3285 }
3286 }
3287 if (out_of_range_attr) {
3288 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
3289 }
3290
3291 ds_put_cstr(&s, ": ");
3292 odp_flow_key_format(key, key_len, &s);
3293
3294 VLOG_DBG("%s:%s", title, ds_cstr(&s));
3295 ds_destroy(&s);
3296 }
3297
3298 static uint8_t
3299 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
3300 {
3301 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3302
3303 if (is_mask) {
3304 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
3305 }
3306
3307 if (odp_frag > OVS_FRAG_TYPE_LATER) {
3308 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
3309 return 0xff; /* Error. */
3310 }
3311
3312 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
3313 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
3314 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
3315 }
3316
3317 static bool
3318 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3319 const struct nlattr *attrs[], uint64_t *present_attrsp,
3320 int *out_of_range_attrp)
3321 {
3322 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3323 const struct nlattr *nla;
3324 uint64_t present_attrs;
3325 size_t left;
3326
3327 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3328 present_attrs = 0;
3329 *out_of_range_attrp = 0;
3330 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3331 uint16_t type = nl_attr_type(nla);
3332 size_t len = nl_attr_get_size(nla);
3333 int expected_len = odp_key_attr_len(ovs_flow_key_attr_lens,
3334 OVS_KEY_ATTR_MAX, type);
3335
3336 if (len != expected_len && expected_len >= 0) {
3337 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3338
3339 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3340 "length %d", ovs_key_attr_to_string(type, namebuf,
3341 sizeof namebuf),
3342 len, expected_len);
3343 return false;
3344 }
3345
3346 if (type > OVS_KEY_ATTR_MAX) {
3347 *out_of_range_attrp = type;
3348 } else {
3349 if (present_attrs & (UINT64_C(1) << type)) {
3350 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3351
3352 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3353 ovs_key_attr_to_string(type,
3354 namebuf, sizeof namebuf));
3355 return false;
3356 }
3357
3358 present_attrs |= UINT64_C(1) << type;
3359 attrs[type] = nla;
3360 }
3361 }
3362 if (left) {
3363 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3364 return false;
3365 }
3366
3367 *present_attrsp = present_attrs;
3368 return true;
3369 }
3370
3371 static enum odp_key_fitness
3372 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3373 uint64_t expected_attrs,
3374 const struct nlattr *key, size_t key_len)
3375 {
3376 uint64_t missing_attrs;
3377 uint64_t extra_attrs;
3378
3379 missing_attrs = expected_attrs & ~present_attrs;
3380 if (missing_attrs) {
3381 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3382 log_odp_key_attributes(&rl, "expected but not present",
3383 missing_attrs, 0, key, key_len);
3384 return ODP_FIT_TOO_LITTLE;
3385 }
3386
3387 extra_attrs = present_attrs & ~expected_attrs;
3388 if (extra_attrs || out_of_range_attr) {
3389 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3390 log_odp_key_attributes(&rl, "present but not expected",
3391 extra_attrs, out_of_range_attr, key, key_len);
3392 return ODP_FIT_TOO_MUCH;
3393 }
3394
3395 return ODP_FIT_PERFECT;
3396 }
3397
3398 static bool
3399 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3400 uint64_t present_attrs, uint64_t *expected_attrs,
3401 struct flow *flow, const struct flow *src_flow)
3402 {
3403 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3404 bool is_mask = flow != src_flow;
3405
3406 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3407 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3408 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3409 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3410 ntohs(flow->dl_type));
3411 return false;
3412 }
3413 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3414 flow->dl_type != htons(0xffff)) {
3415 return false;
3416 }
3417 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3418 } else {
3419 if (!is_mask) {
3420 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3421 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3422 /* See comments in odp_flow_key_from_flow__(). */
3423 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3424 return false;
3425 }
3426 }
3427 return true;
3428 }
3429
3430 static enum odp_key_fitness
3431 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3432 uint64_t present_attrs, int out_of_range_attr,
3433 uint64_t expected_attrs, struct flow *flow,
3434 const struct nlattr *key, size_t key_len,
3435 const struct flow *src_flow)
3436 {
3437 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3438 bool is_mask = src_flow != flow;
3439 const void *check_start = NULL;
3440 size_t check_len = 0;
3441 enum ovs_key_attr expected_bit = 0xff;
3442
3443 if (eth_type_mpls(src_flow->dl_type)) {
3444 if (!is_mask || present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3445 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3446 }
3447 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3448 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3449 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3450 int n = size / sizeof(ovs_be32);
3451 int i;
3452
3453 if (!size || size % sizeof(ovs_be32)) {
3454 return ODP_FIT_ERROR;
3455 }
3456 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3457 return ODP_FIT_ERROR;
3458 }
3459
3460 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3461 flow->mpls_lse[i] = mpls_lse[i];
3462 }
3463 if (n > FLOW_MAX_MPLS_LABELS) {
3464 return ODP_FIT_TOO_MUCH;
3465 }
3466
3467 if (!is_mask) {
3468 /* BOS may be set only in the innermost label. */
3469 for (i = 0; i < n - 1; i++) {
3470 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3471 return ODP_FIT_ERROR;
3472 }
3473 }
3474
3475 /* BOS must be set in the innermost label. */
3476 if (n < FLOW_MAX_MPLS_LABELS
3477 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3478 return ODP_FIT_TOO_LITTLE;
3479 }
3480 }
3481 }
3482
3483 goto done;
3484 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3485 if (!is_mask) {
3486 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3487 }
3488 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3489 const struct ovs_key_ipv4 *ipv4_key;
3490
3491 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3492 put_ipv4_key(ipv4_key, flow, is_mask);
3493 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3494 return ODP_FIT_ERROR;
3495 }
3496 if (is_mask) {
3497 check_start = ipv4_key;
3498 check_len = sizeof *ipv4_key;
3499 expected_bit = OVS_KEY_ATTR_IPV4;
3500 }
3501 }
3502 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3503 if (!is_mask) {
3504 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3505 }
3506 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3507 const struct ovs_key_ipv6 *ipv6_key;
3508
3509 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3510 put_ipv6_key(ipv6_key, flow, is_mask);
3511 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3512 return ODP_FIT_ERROR;
3513 }
3514 if (is_mask) {
3515 check_start = ipv6_key;
3516 check_len = sizeof *ipv6_key;
3517 expected_bit = OVS_KEY_ATTR_IPV6;
3518 }
3519 }
3520 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3521 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3522 if (!is_mask) {
3523 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3524 }
3525 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3526 const struct ovs_key_arp *arp_key;
3527
3528 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3529 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3530 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3531 "key", ntohs(arp_key->arp_op));
3532 return ODP_FIT_ERROR;
3533 }
3534 put_arp_key(arp_key, flow);
3535 if (is_mask) {
3536 check_start = arp_key;
3537 check_len = sizeof *arp_key;
3538 expected_bit = OVS_KEY_ATTR_ARP;
3539 }
3540 }
3541 } else {
3542 goto done;
3543 }
3544 if (check_len > 0) { /* Happens only when 'is_mask'. */
3545 if (!is_all_zeros(check_start, check_len) &&
3546 flow->dl_type != htons(0xffff)) {
3547 return ODP_FIT_ERROR;
3548 } else {
3549 expected_attrs |= UINT64_C(1) << expected_bit;
3550 }
3551 }
3552
3553 expected_bit = OVS_KEY_ATTR_UNSPEC;
3554 if (src_flow->nw_proto == IPPROTO_TCP
3555 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3556 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3557 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3558 if (!is_mask) {
3559 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3560 }
3561 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3562 const union ovs_key_tp *tcp_key;
3563
3564 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3565 put_tp_key(tcp_key, flow);
3566 expected_bit = OVS_KEY_ATTR_TCP;
3567 }
3568 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3569 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3570 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3571 }
3572 } else if (src_flow->nw_proto == IPPROTO_UDP
3573 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3574 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3575 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3576 if (!is_mask) {
3577 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3578 }
3579 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3580 const union ovs_key_tp *udp_key;
3581
3582 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3583 put_tp_key(udp_key, flow);
3584 expected_bit = OVS_KEY_ATTR_UDP;
3585 }
3586 } else if (src_flow->nw_proto == IPPROTO_SCTP
3587 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3588 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3589 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3590 if (!is_mask) {
3591 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3592 }
3593 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3594 const union ovs_key_tp *sctp_key;
3595
3596 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3597 put_tp_key(sctp_key, flow);
3598 expected_bit = OVS_KEY_ATTR_SCTP;
3599 }
3600 } else if (src_flow->nw_proto == IPPROTO_ICMP
3601 && src_flow->dl_type == htons(ETH_TYPE_IP)
3602 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3603 if (!is_mask) {
3604 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3605 }
3606 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3607 const struct ovs_key_icmp *icmp_key;
3608
3609 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3610 flow->tp_src = htons(icmp_key->icmp_type);
3611 flow->tp_dst = htons(icmp_key->icmp_code);
3612 expected_bit = OVS_KEY_ATTR_ICMP;
3613 }
3614 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3615 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3616 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3617 if (!is_mask) {
3618 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3619 }
3620 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3621 const struct ovs_key_icmpv6 *icmpv6_key;
3622
3623 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3624 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3625 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3626 expected_bit = OVS_KEY_ATTR_ICMPV6;
3627 if (src_flow->tp_dst == htons(0) &&
3628 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3629 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3630 if (!is_mask) {
3631 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3632 }
3633 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3634 const struct ovs_key_nd *nd_key;
3635
3636 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3637 memcpy(&flow->nd_target, nd_key->nd_target,
3638 sizeof flow->nd_target);
3639 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3640 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3641 if (is_mask) {
3642 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3643 (flow->tp_src != htons(0xffff) ||
3644 flow->tp_dst != htons(0xffff))) {
3645 return ODP_FIT_ERROR;
3646 } else {
3647 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3648 }
3649 }
3650 }
3651 }
3652 }
3653 }
3654 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3655 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3656 return ODP_FIT_ERROR;
3657 } else {
3658 expected_attrs |= UINT64_C(1) << expected_bit;
3659 }
3660 }
3661
3662 done:
3663 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3664 key, key_len);
3665 }
3666
3667 /* Parse 802.1Q header then encapsulated L3 attributes. */
3668 static enum odp_key_fitness
3669 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3670 uint64_t present_attrs, int out_of_range_attr,
3671 uint64_t expected_attrs, struct flow *flow,
3672 const struct nlattr *key, size_t key_len,
3673 const struct flow *src_flow)
3674 {
3675 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3676 bool is_mask = src_flow != flow;
3677
3678 const struct nlattr *encap
3679 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3680 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3681 enum odp_key_fitness encap_fitness;
3682 enum odp_key_fitness fitness;
3683
3684 /* Calculate fitness of outer attributes. */
3685 if (!is_mask) {
3686 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3687 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3688 } else {
3689 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3690 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3691 }
3692 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3693 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3694 }
3695 }
3696 fitness = check_expectations(present_attrs, out_of_range_attr,
3697 expected_attrs, key, key_len);
3698
3699 /* Set vlan_tci.
3700 * Remove the TPID from dl_type since it's not the real Ethertype. */
3701 flow->dl_type = htons(0);
3702 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3703 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3704 : htons(0));
3705 if (!is_mask) {
3706 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3707 return ODP_FIT_TOO_LITTLE;
3708 } else if (flow->vlan_tci == htons(0)) {
3709 /* Corner case for a truncated 802.1Q header. */
3710 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3711 return ODP_FIT_TOO_MUCH;
3712 }
3713 return fitness;
3714 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3715 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3716 "but CFI bit is not set", ntohs(flow->vlan_tci));
3717 return ODP_FIT_ERROR;
3718 }
3719 } else {
3720 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3721 return fitness;
3722 }
3723 }
3724
3725 /* Now parse the encapsulated attributes. */
3726 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3727 attrs, &present_attrs, &out_of_range_attr)) {
3728 return ODP_FIT_ERROR;
3729 }
3730 expected_attrs = 0;
3731
3732 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3733 return ODP_FIT_ERROR;
3734 }
3735 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3736 expected_attrs, flow, key, key_len,
3737 src_flow);
3738
3739 /* The overall fitness is the worse of the outer and inner attributes. */
3740 return MAX(fitness, encap_fitness);
3741 }
3742
3743 static enum odp_key_fitness
3744 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3745 struct flow *flow, const struct flow *src_flow)
3746 {
3747 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3748 uint64_t expected_attrs;
3749 uint64_t present_attrs;
3750 int out_of_range_attr;
3751 bool is_mask = src_flow != flow;
3752
3753 memset(flow, 0, sizeof *flow);
3754
3755 /* Parse attributes. */
3756 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3757 &out_of_range_attr)) {
3758 return ODP_FIT_ERROR;
3759 }
3760 expected_attrs = 0;
3761
3762 /* Metadata. */
3763 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3764 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3765 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3766 } else if (is_mask) {
3767 /* Always exact match recirc_id if it is not specified. */
3768 flow->recirc_id = UINT32_MAX;
3769 }
3770
3771 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3772 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3773 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3774 }
3775 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3776 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3777 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3778 }
3779
3780 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3781 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3782 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3783 }
3784
3785 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3786 enum odp_key_fitness res;
3787
3788 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3789 if (res == ODP_FIT_ERROR) {
3790 return ODP_FIT_ERROR;
3791 } else if (res == ODP_FIT_PERFECT) {
3792 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3793 }
3794 }
3795
3796 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3797 flow->in_port.odp_port
3798 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3799 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3800 } else if (!is_mask) {
3801 flow->in_port.odp_port = ODPP_NONE;
3802 }
3803
3804 /* Ethernet header. */
3805 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3806 const struct ovs_key_ethernet *eth_key;
3807
3808 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3809 put_ethernet_key(eth_key, flow);
3810 if (is_mask) {
3811 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3812 }
3813 }
3814 if (!is_mask) {
3815 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3816 }
3817
3818 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3819 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3820 src_flow)) {
3821 return ODP_FIT_ERROR;
3822 }
3823
3824 if (is_mask
3825 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3826 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3827 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3828 expected_attrs, flow, key, key_len, src_flow);
3829 }
3830 if (is_mask) {
3831 flow->vlan_tci = htons(0xffff);
3832 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3833 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3834 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3835 }
3836 }
3837 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3838 expected_attrs, flow, key, key_len, src_flow);
3839 }
3840
3841 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3842 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3843 * 'key' fits our expectations for what a flow key should contain.
3844 *
3845 * The 'in_port' will be the datapath's understanding of the port. The
3846 * caller will need to translate with odp_port_to_ofp_port() if the
3847 * OpenFlow port is needed.
3848 *
3849 * This function doesn't take the packet itself as an argument because none of
3850 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3851 * it is always possible to infer which additional attribute(s) should appear
3852 * by looking at the attributes for lower-level protocols, e.g. if the network
3853 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3854 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3855 * must be absent. */
3856 enum odp_key_fitness
3857 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3858 struct flow *flow)
3859 {
3860 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3861 }
3862
3863 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3864 * structure in 'mask'. 'flow' must be a previously translated flow
3865 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3866 * 'key' fits our expectations for what a flow key should contain. */
3867 enum odp_key_fitness
3868 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3869 struct flow *mask, const struct flow *flow)
3870 {
3871 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3872 }
3873
3874 /* Returns 'fitness' as a string, for use in debug messages. */
3875 const char *
3876 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3877 {
3878 switch (fitness) {
3879 case ODP_FIT_PERFECT:
3880 return "OK";
3881 case ODP_FIT_TOO_MUCH:
3882 return "too_much";
3883 case ODP_FIT_TOO_LITTLE:
3884 return "too_little";
3885 case ODP_FIT_ERROR:
3886 return "error";
3887 default:
3888 return "<unknown>";
3889 }
3890 }
3891
3892 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3893 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3894 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3895 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3896 * null, then the return value is not meaningful.) */
3897 size_t
3898 odp_put_userspace_action(uint32_t pid,
3899 const void *userdata, size_t userdata_size,
3900 odp_port_t tunnel_out_port,
3901 struct ofpbuf *odp_actions)
3902 {
3903 size_t userdata_ofs;
3904 size_t offset;
3905
3906 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3907 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3908 if (userdata) {
3909 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3910
3911 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3912 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3913 * long:
3914 *
3915 * - The kernel rejected shorter userdata with -ERANGE.
3916 *
3917 * - The kernel silently dropped userdata beyond the first 8 bytes.
3918 *
3919 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3920 * separately disable features that required more than 8 bytes.) */
3921 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3922 MAX(8, userdata_size)),
3923 userdata, userdata_size);
3924 } else {
3925 userdata_ofs = 0;
3926 }
3927 if (tunnel_out_port != ODPP_NONE) {
3928 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3929 tunnel_out_port);
3930 }
3931 nl_msg_end_nested(odp_actions, offset);
3932
3933 return userdata_ofs;
3934 }
3935
3936 void
3937 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3938 struct ofpbuf *odp_actions)
3939 {
3940 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3941 tun_key_to_attr(odp_actions, tunnel);
3942 nl_msg_end_nested(odp_actions, offset);
3943 }
3944
3945 void
3946 odp_put_tnl_push_action(struct ofpbuf *odp_actions,
3947 struct ovs_action_push_tnl *data)
3948 {
3949 int size = offsetof(struct ovs_action_push_tnl, header);
3950
3951 size += data->header_len;
3952 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_TUNNEL_PUSH, data, size);
3953 }
3954
3955 \f
3956 /* The commit_odp_actions() function and its helpers. */
3957
3958 static void
3959 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3960 const void *key, size_t key_size)
3961 {
3962 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3963 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3964 nl_msg_end_nested(odp_actions, offset);
3965 }
3966
3967 /* Masked set actions have a mask following the data within the netlink
3968 * attribute. The unmasked bits in the data will be cleared as the data
3969 * is copied to the action. */
3970 void
3971 commit_masked_set_action(struct ofpbuf *odp_actions,
3972 enum ovs_key_attr key_type,
3973 const void *key_, const void *mask_, size_t key_size)
3974 {
3975 size_t offset = nl_msg_start_nested(odp_actions,
3976 OVS_ACTION_ATTR_SET_MASKED);
3977 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3978 const char *key = key_, *mask = mask_;
3979
3980 memcpy(data + key_size, mask, key_size);
3981 /* Clear unmasked bits while copying. */
3982 while (key_size--) {
3983 *data++ = *key++ & *mask++;
3984 }
3985 nl_msg_end_nested(odp_actions, offset);
3986 }
3987
3988 /* If any of the flow key data that ODP actions can modify are different in
3989 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3990 * 'odp_actions' that change the flow tunneling information in key from
3991 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3992 * same way. In other words, operates the same as commit_odp_actions(), but
3993 * only on tunneling information. */
3994 void
3995 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3996 struct ofpbuf *odp_actions)
3997 {
3998 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3999 if (flow->tunnel.ip_dst) {
4000 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
4001 return;
4002 }
4003 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
4004 odp_put_tunnel_action(&base->tunnel, odp_actions);
4005 }
4006 }
4007
4008 static bool
4009 commit(enum ovs_key_attr attr, bool use_masked_set,
4010 const void *key, void *base, void *mask, size_t size,
4011 struct ofpbuf *odp_actions)
4012 {
4013 if (memcmp(key, base, size)) {
4014 bool fully_masked = odp_mask_is_exact(attr, mask, size);
4015
4016 if (use_masked_set && !fully_masked) {
4017 commit_masked_set_action(odp_actions, attr, key, mask, size);
4018 } else {
4019 if (!fully_masked) {
4020 memset(mask, 0xff, size);
4021 }
4022 commit_set_action(odp_actions, attr, key, size);
4023 }
4024 memcpy(base, key, size);
4025 return true;
4026 } else {
4027 /* Mask bits are set when we have either read or set the corresponding
4028 * values. Masked bits will be exact-matched, no need to set them
4029 * if the value did not actually change. */
4030 return false;
4031 }
4032 }
4033
4034 static void
4035 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
4036 {
4037 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
4038 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
4039 }
4040
4041 static void
4042 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
4043 {
4044 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
4045 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
4046 }
4047
4048 static void
4049 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
4050 struct ofpbuf *odp_actions,
4051 struct flow_wildcards *wc,
4052 bool use_masked)
4053 {
4054 struct ovs_key_ethernet key, base, mask;
4055
4056 get_ethernet_key(flow, &key);
4057 get_ethernet_key(base_flow, &base);
4058 get_ethernet_key(&wc->masks, &mask);
4059
4060 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
4061 &key, &base, &mask, sizeof key, odp_actions)) {
4062 put_ethernet_key(&base, base_flow);
4063 put_ethernet_key(&mask, &wc->masks);
4064 }
4065 }
4066
4067 static void
4068 pop_vlan(struct flow *base,
4069 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4070 {
4071 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
4072
4073 if (base->vlan_tci & htons(VLAN_CFI)) {
4074 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
4075 base->vlan_tci = 0;
4076 }
4077 }
4078
4079 static void
4080 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
4081 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4082 {
4083 if (base->vlan_tci == vlan_tci) {
4084 return;
4085 }
4086
4087 pop_vlan(base, odp_actions, wc);
4088 if (vlan_tci & htons(VLAN_CFI)) {
4089 struct ovs_action_push_vlan vlan;
4090
4091 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
4092 vlan.vlan_tci = vlan_tci;
4093 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
4094 &vlan, sizeof vlan);
4095 }
4096 base->vlan_tci = vlan_tci;
4097 }
4098
4099 /* Wildcarding already done at action translation time. */
4100 static void
4101 commit_mpls_action(const struct flow *flow, struct flow *base,
4102 struct ofpbuf *odp_actions)
4103 {
4104 int base_n = flow_count_mpls_labels(base, NULL);
4105 int flow_n = flow_count_mpls_labels(flow, NULL);
4106 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
4107 NULL);
4108
4109 while (base_n > common_n) {
4110 if (base_n - 1 == common_n && flow_n > common_n) {
4111 /* If there is only one more LSE in base than there are common
4112 * between base and flow; and flow has at least one more LSE than
4113 * is common then the topmost LSE of base may be updated using
4114 * set */
4115 struct ovs_key_mpls mpls_key;
4116
4117 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
4118 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
4119 &mpls_key, sizeof mpls_key);
4120 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
4121 common_n++;
4122 } else {
4123 /* Otherwise, if there more LSEs in base than are common between
4124 * base and flow then pop the topmost one. */
4125 ovs_be16 dl_type;
4126 bool popped;
4127
4128 /* If all the LSEs are to be popped and this is not the outermost
4129 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
4130 * POP_MPLS action instead of flow->dl_type.
4131 *
4132 * This is because the POP_MPLS action requires its ethertype
4133 * argument to be an MPLS ethernet type but in this case
4134 * flow->dl_type will be a non-MPLS ethernet type.
4135 *
4136 * When the final POP_MPLS action occurs it use flow->dl_type and
4137 * the and the resulting packet will have the desired dl_type. */
4138 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
4139 dl_type = htons(ETH_TYPE_MPLS);
4140 } else {
4141 dl_type = flow->dl_type;
4142 }
4143 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
4144 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
4145 ovs_assert(popped);
4146 base_n--;
4147 }
4148 }
4149
4150 /* If, after the above popping and setting, there are more LSEs in flow
4151 * than base then some LSEs need to be pushed. */
4152 while (base_n < flow_n) {
4153 struct ovs_action_push_mpls *mpls;
4154
4155 mpls = nl_msg_put_unspec_zero(odp_actions,
4156 OVS_ACTION_ATTR_PUSH_MPLS,
4157 sizeof *mpls);
4158 mpls->mpls_ethertype = flow->dl_type;
4159 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
4160 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
4161 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
4162 base_n++;
4163 }
4164 }
4165
4166 static void
4167 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
4168 {
4169 ipv4->ipv4_src = flow->nw_src;
4170 ipv4->ipv4_dst = flow->nw_dst;
4171 ipv4->ipv4_proto = flow->nw_proto;
4172 ipv4->ipv4_tos = flow->nw_tos;
4173 ipv4->ipv4_ttl = flow->nw_ttl;
4174 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4175 }
4176
4177 static void
4178 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
4179 {
4180 flow->nw_src = ipv4->ipv4_src;
4181 flow->nw_dst = ipv4->ipv4_dst;
4182 flow->nw_proto = ipv4->ipv4_proto;
4183 flow->nw_tos = ipv4->ipv4_tos;
4184 flow->nw_ttl = ipv4->ipv4_ttl;
4185 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
4186 }
4187
4188 static void
4189 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
4190 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4191 bool use_masked)
4192 {
4193 struct ovs_key_ipv4 key, mask, base;
4194
4195 /* Check that nw_proto and nw_frag remain unchanged. */
4196 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4197 flow->nw_frag == base_flow->nw_frag);
4198
4199 get_ipv4_key(flow, &key, false);
4200 get_ipv4_key(base_flow, &base, false);
4201 get_ipv4_key(&wc->masks, &mask, true);
4202 mask.ipv4_proto = 0; /* Not writeable. */
4203 mask.ipv4_frag = 0; /* Not writable. */
4204
4205 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
4206 odp_actions)) {
4207 put_ipv4_key(&base, base_flow, false);
4208 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
4209 put_ipv4_key(&mask, &wc->masks, true);
4210 }
4211 }
4212 }
4213
4214 static void
4215 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
4216 {
4217 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
4218 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
4219 ipv6->ipv6_label = flow->ipv6_label;
4220 ipv6->ipv6_proto = flow->nw_proto;
4221 ipv6->ipv6_tclass = flow->nw_tos;
4222 ipv6->ipv6_hlimit = flow->nw_ttl;
4223 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
4224 }
4225
4226 static void
4227 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
4228 {
4229 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
4230 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
4231 flow->ipv6_label = ipv6->ipv6_label;
4232 flow->nw_proto = ipv6->ipv6_proto;
4233 flow->nw_tos = ipv6->ipv6_tclass;
4234 flow->nw_ttl = ipv6->ipv6_hlimit;
4235 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
4236 }
4237
4238 static void
4239 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
4240 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4241 bool use_masked)
4242 {
4243 struct ovs_key_ipv6 key, mask, base;
4244
4245 /* Check that nw_proto and nw_frag remain unchanged. */
4246 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
4247 flow->nw_frag == base_flow->nw_frag);
4248
4249 get_ipv6_key(flow, &key, false);
4250 get_ipv6_key(base_flow, &base, false);
4251 get_ipv6_key(&wc->masks, &mask, true);
4252 mask.ipv6_proto = 0; /* Not writeable. */
4253 mask.ipv6_frag = 0; /* Not writable. */
4254
4255 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
4256 odp_actions)) {
4257 put_ipv6_key(&base, base_flow, false);
4258 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
4259 put_ipv6_key(&mask, &wc->masks, true);
4260 }
4261 }
4262 }
4263
4264 static void
4265 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
4266 {
4267 /* ARP key has padding, clear it. */
4268 memset(arp, 0, sizeof *arp);
4269
4270 arp->arp_sip = flow->nw_src;
4271 arp->arp_tip = flow->nw_dst;
4272 arp->arp_op = htons(flow->nw_proto);
4273 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
4274 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
4275 }
4276
4277 static void
4278 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
4279 {
4280 flow->nw_src = arp->arp_sip;
4281 flow->nw_dst = arp->arp_tip;
4282 flow->nw_proto = ntohs(arp->arp_op);
4283 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
4284 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
4285 }
4286
4287 static enum slow_path_reason
4288 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
4289 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4290 {
4291 struct ovs_key_arp key, mask, base;
4292
4293 get_arp_key(flow, &key);
4294 get_arp_key(base_flow, &base);
4295 get_arp_key(&wc->masks, &mask);
4296
4297 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
4298 odp_actions)) {
4299 put_arp_key(&base, base_flow);
4300 put_arp_key(&mask, &wc->masks);
4301 return SLOW_ACTION;
4302 }
4303 return 0;
4304 }
4305
4306 static void
4307 get_nd_key(const struct flow *flow, struct ovs_key_nd *nd)
4308 {
4309 memcpy(nd->nd_target, &flow->nd_target, sizeof flow->nd_target);
4310 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4311 memcpy(nd->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
4312 memcpy(nd->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
4313 }
4314
4315 static void
4316 put_nd_key(const struct ovs_key_nd *nd, struct flow *flow)
4317 {
4318 memcpy(&flow->nd_target, &flow->nd_target, sizeof flow->nd_target);
4319 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
4320 memcpy(flow->arp_sha, nd->nd_sll, ETH_ADDR_LEN);
4321 memcpy(flow->arp_tha, nd->nd_tll, ETH_ADDR_LEN);
4322 }
4323
4324 static enum slow_path_reason
4325 commit_set_nd_action(const struct flow *flow, struct flow *base_flow,
4326 struct ofpbuf *odp_actions,
4327 struct flow_wildcards *wc, bool use_masked)
4328 {
4329 struct ovs_key_nd key, mask, base;
4330
4331 get_nd_key(flow, &key);
4332 get_nd_key(base_flow, &base);
4333 get_nd_key(&wc->masks, &mask);
4334
4335 if (commit(OVS_KEY_ATTR_ND, use_masked, &key, &base, &mask, sizeof key,
4336 odp_actions)) {
4337 put_nd_key(&base, base_flow);
4338 put_nd_key(&mask, &wc->masks);
4339 return SLOW_ACTION;
4340 }
4341
4342 return 0;
4343 }
4344
4345 static enum slow_path_reason
4346 commit_set_nw_action(const struct flow *flow, struct flow *base,
4347 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4348 bool use_masked)
4349 {
4350 /* Check if 'flow' really has an L3 header. */
4351 if (!flow->nw_proto) {
4352 return 0;
4353 }
4354
4355 switch (ntohs(base->dl_type)) {
4356 case ETH_TYPE_IP:
4357 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
4358 break;
4359
4360 case ETH_TYPE_IPV6:
4361 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
4362 return commit_set_nd_action(flow, base, odp_actions, wc, use_masked);
4363
4364 case ETH_TYPE_ARP:
4365 return commit_set_arp_action(flow, base, odp_actions, wc);
4366 }
4367
4368 return 0;
4369 }
4370
4371 /* TCP, UDP, and SCTP keys have the same layout. */
4372 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
4373 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
4374
4375 static void
4376 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
4377 {
4378 tp->tcp.tcp_src = flow->tp_src;
4379 tp->tcp.tcp_dst = flow->tp_dst;
4380 }
4381
4382 static void
4383 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
4384 {
4385 flow->tp_src = tp->tcp.tcp_src;
4386 flow->tp_dst = tp->tcp.tcp_dst;
4387 }
4388
4389 static void
4390 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
4391 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4392 bool use_masked)
4393 {
4394 enum ovs_key_attr key_type;
4395 union ovs_key_tp key, mask, base;
4396
4397 /* Check if 'flow' really has an L3 header. */
4398 if (!flow->nw_proto) {
4399 return;
4400 }
4401
4402 if (!is_ip_any(base_flow)) {
4403 return;
4404 }
4405
4406 if (flow->nw_proto == IPPROTO_TCP) {
4407 key_type = OVS_KEY_ATTR_TCP;
4408 } else if (flow->nw_proto == IPPROTO_UDP) {
4409 key_type = OVS_KEY_ATTR_UDP;
4410 } else if (flow->nw_proto == IPPROTO_SCTP) {
4411 key_type = OVS_KEY_ATTR_SCTP;
4412 } else {
4413 return;
4414 }
4415
4416 get_tp_key(flow, &key);
4417 get_tp_key(base_flow, &base);
4418 get_tp_key(&wc->masks, &mask);
4419
4420 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
4421 odp_actions)) {
4422 put_tp_key(&base, base_flow);
4423 put_tp_key(&mask, &wc->masks);
4424 }
4425 }
4426
4427 static void
4428 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
4429 struct ofpbuf *odp_actions,
4430 struct flow_wildcards *wc,
4431 bool use_masked)
4432 {
4433 uint32_t key, mask, base;
4434
4435 key = flow->skb_priority;
4436 base = base_flow->skb_priority;
4437 mask = wc->masks.skb_priority;
4438
4439 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
4440 sizeof key, odp_actions)) {
4441 base_flow->skb_priority = base;
4442 wc->masks.skb_priority = mask;
4443 }
4444 }
4445
4446 static void
4447 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4448 struct ofpbuf *odp_actions,
4449 struct flow_wildcards *wc,
4450 bool use_masked)
4451 {
4452 uint32_t key, mask, base;
4453
4454 key = flow->pkt_mark;
4455 base = base_flow->pkt_mark;
4456 mask = wc->masks.pkt_mark;
4457
4458 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4459 sizeof key, odp_actions)) {
4460 base_flow->pkt_mark = base;
4461 wc->masks.pkt_mark = mask;
4462 }
4463 }
4464
4465 /* If any of the flow key data that ODP actions can modify are different in
4466 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4467 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4468 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4469 * in addition to this function if needed. Sets fields in 'wc' that are
4470 * used as part of the action.
4471 *
4472 * Returns a reason to force processing the flow's packets into the userspace
4473 * slow path, if there is one, otherwise 0. */
4474 enum slow_path_reason
4475 commit_odp_actions(const struct flow *flow, struct flow *base,
4476 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4477 bool use_masked)
4478 {
4479 enum slow_path_reason slow;
4480
4481 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4482 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4483 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4484 commit_mpls_action(flow, base, odp_actions);
4485 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4486 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4487 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);
4488
4489 return slow;
4490 }