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