2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
30 #include "dynamic-string.h"
40 VLOG_DEFINE_THIS_MODULE(odp_util);
42 /* The interface between userspace and kernel uses an "OVS_*" prefix.
43 * Since this is fairly non-specific for the OVS userspace components,
44 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
45 * interactions with the datapath.
48 /* The set of characters that may separate one action or one key attribute
50 static const char *delimiters = ", \t\r\n";
52 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
53 struct ofpbuf *, struct ofpbuf *);
54 static void format_odp_key_attr(const struct nlattr *a,
55 const struct nlattr *ma,
56 const struct hmap *portno_names, struct ds *ds,
59 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
62 * - For an action whose argument has a fixed length, returned that
63 * nonnegative length in bytes.
65 * - For an action with a variable-length argument, returns -2.
67 * - For an invalid 'type', returns -1. */
69 odp_action_len(uint16_t type)
71 if (type > OVS_ACTION_ATTR_MAX) {
75 switch ((enum ovs_action_attr) type) {
76 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
77 case OVS_ACTION_ATTR_USERSPACE: return -2;
78 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
79 case OVS_ACTION_ATTR_POP_VLAN: return 0;
80 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
81 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
82 case OVS_ACTION_ATTR_RECIRC: return sizeof(uint32_t);
83 case OVS_ACTION_ATTR_HASH: return sizeof(struct ovs_action_hash);
84 case OVS_ACTION_ATTR_SET: return -2;
85 case OVS_ACTION_ATTR_SET_MASKED: return -2;
86 case OVS_ACTION_ATTR_SAMPLE: return -2;
88 case OVS_ACTION_ATTR_UNSPEC:
89 case __OVS_ACTION_ATTR_MAX:
96 /* Returns a string form of 'attr'. The return value is either a statically
97 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
98 * should be at least OVS_KEY_ATTR_BUFSIZE. */
99 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
101 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
104 case OVS_KEY_ATTR_UNSPEC: return "unspec";
105 case OVS_KEY_ATTR_ENCAP: return "encap";
106 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
107 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
108 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
109 case OVS_KEY_ATTR_IN_PORT: return "in_port";
110 case OVS_KEY_ATTR_ETHERNET: return "eth";
111 case OVS_KEY_ATTR_VLAN: return "vlan";
112 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
113 case OVS_KEY_ATTR_IPV4: return "ipv4";
114 case OVS_KEY_ATTR_IPV6: return "ipv6";
115 case OVS_KEY_ATTR_TCP: return "tcp";
116 case OVS_KEY_ATTR_TCP_FLAGS: return "tcp_flags";
117 case OVS_KEY_ATTR_UDP: return "udp";
118 case OVS_KEY_ATTR_SCTP: return "sctp";
119 case OVS_KEY_ATTR_ICMP: return "icmp";
120 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
121 case OVS_KEY_ATTR_ARP: return "arp";
122 case OVS_KEY_ATTR_ND: return "nd";
123 case OVS_KEY_ATTR_MPLS: return "mpls";
124 case OVS_KEY_ATTR_DP_HASH: return "dp_hash";
125 case OVS_KEY_ATTR_RECIRC_ID: return "recirc_id";
127 case __OVS_KEY_ATTR_MAX:
129 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
135 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
137 size_t len = nl_attr_get_size(a);
139 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
141 const uint8_t *unspec;
144 unspec = nl_attr_get(a);
145 for (i = 0; i < len; i++) {
146 ds_put_char(ds, i ? ' ': '(');
147 ds_put_format(ds, "%02x", unspec[i]);
149 ds_put_char(ds, ')');
154 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
156 static const struct nl_policy ovs_sample_policy[] = {
157 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
158 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
160 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
162 const struct nlattr *nla_acts;
165 ds_put_cstr(ds, "sample");
167 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
168 ds_put_cstr(ds, "(error)");
172 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
175 ds_put_format(ds, "(sample=%.1f%%,", percentage);
177 ds_put_cstr(ds, "actions(");
178 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
179 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
180 format_odp_actions(ds, nla_acts, len);
181 ds_put_format(ds, "))");
185 slow_path_reason_to_string(uint32_t reason)
187 switch ((enum slow_path_reason) reason) {
188 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
197 slow_path_reason_to_explanation(enum slow_path_reason reason)
200 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
209 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
210 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
215 /* Parse masked flags in numeric format? */
216 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
217 res_flags, res_mask, &n) && n > 0) {
218 if (*res_flags & ~allowed || *res_mask & ~allowed) {
226 if (res_mask && (*s == '+' || *s == '-')) {
227 uint32_t flags = 0, mask = 0;
229 /* Parse masked flags. */
230 while (s[n] != ')') {
237 } else if (s[n] == '-') {
244 name_len = strcspn(s + n, "+-)");
246 for (bit = 1; bit; bit <<= 1) {
247 const char *fname = bit_to_string(bit);
255 if (len != name_len) {
258 if (!strncmp(s + n, fname, len)) {
260 /* bit already set. */
263 if (!(bit & allowed)) {
275 return -EINVAL; /* Unknown flag name */
285 /* Parse unmasked flags. If a flag is present, it is set, otherwise
287 while (s[n] != ')') {
288 unsigned long long int flags;
292 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
293 if (flags & ~allowed) {
296 n += n0 + (s[n + n0] == ',');
301 for (bit = 1; bit; bit <<= 1) {
302 const char *name = bit_to_string(bit);
310 if (!strncmp(s + n, name, len) &&
311 (s[n + len] == ',' || s[n + len] == ')')) {
312 if (!(bit & allowed)) {
316 n += len + (s[n + len] == ',');
328 *res_mask = UINT32_MAX;
334 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
336 static const struct nl_policy ovs_userspace_policy[] = {
337 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
338 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
340 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
343 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
344 const struct nlattr *userdata_attr;
345 const struct nlattr *tunnel_out_port_attr;
347 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
348 ds_put_cstr(ds, "userspace(error)");
352 ds_put_format(ds, "userspace(pid=%"PRIu32,
353 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
355 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
358 const uint8_t *userdata = nl_attr_get(userdata_attr);
359 size_t userdata_len = nl_attr_get_size(userdata_attr);
360 bool userdata_unspec = true;
361 union user_action_cookie cookie;
363 if (userdata_len >= sizeof cookie.type
364 && userdata_len <= sizeof cookie) {
366 memset(&cookie, 0, sizeof cookie);
367 memcpy(&cookie, userdata, userdata_len);
369 userdata_unspec = false;
371 if (userdata_len == sizeof cookie.sflow
372 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
373 ds_put_format(ds, ",sFlow("
374 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
375 vlan_tci_to_vid(cookie.sflow.vlan_tci),
376 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
377 cookie.sflow.output);
378 } else if (userdata_len == sizeof cookie.slow_path
379 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
380 ds_put_cstr(ds, ",slow_path(");
381 format_flags(ds, slow_path_reason_to_string,
382 cookie.slow_path.reason, ',');
383 ds_put_format(ds, ")");
384 } else if (userdata_len == sizeof cookie.flow_sample
385 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
386 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
387 ",collector_set_id=%"PRIu32
388 ",obs_domain_id=%"PRIu32
389 ",obs_point_id=%"PRIu32")",
390 cookie.flow_sample.probability,
391 cookie.flow_sample.collector_set_id,
392 cookie.flow_sample.obs_domain_id,
393 cookie.flow_sample.obs_point_id);
394 } else if (userdata_len >= sizeof cookie.ipfix
395 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
396 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
397 cookie.ipfix.output_odp_port);
399 userdata_unspec = true;
403 if (userdata_unspec) {
405 ds_put_format(ds, ",userdata(");
406 for (i = 0; i < userdata_len; i++) {
407 ds_put_format(ds, "%02x", userdata[i]);
409 ds_put_char(ds, ')');
413 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
414 if (tunnel_out_port_attr) {
415 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
416 nl_attr_get_u32(tunnel_out_port_attr));
419 ds_put_char(ds, ')');
423 format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
425 if (verbose || vlan_tci_to_vid(tci) || vlan_tci_to_vid(mask)) {
426 ds_put_format(ds, "vid=%"PRIu16, vlan_tci_to_vid(tci));
427 if (vlan_tci_to_vid(mask) != VLAN_VID_MASK) { /* Partially masked. */
428 ds_put_format(ds, "/0x%"PRIx16, vlan_tci_to_vid(mask));
430 ds_put_char(ds, ',');
432 if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
433 ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
434 if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
435 ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
437 ds_put_char(ds, ',');
439 if (!(tci & htons(VLAN_CFI))) {
440 ds_put_cstr(ds, "cfi=0");
441 ds_put_char(ds, ',');
447 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
449 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
450 mpls_lse_to_label(mpls_lse),
451 mpls_lse_to_tc(mpls_lse),
452 mpls_lse_to_ttl(mpls_lse),
453 mpls_lse_to_bos(mpls_lse));
457 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
458 const struct ovs_key_mpls *mpls_mask, int n)
461 ovs_be32 key = mpls_key->mpls_lse;
463 if (mpls_mask == NULL) {
464 format_mpls_lse(ds, key);
466 ovs_be32 mask = mpls_mask->mpls_lse;
468 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
469 mpls_lse_to_label(key), mpls_lse_to_label(mask),
470 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
471 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
472 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
477 for (i = 0; i < n; i++) {
478 ds_put_format(ds, "lse%d=%#"PRIx32,
479 i, ntohl(mpls_key[i].mpls_lse));
481 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
483 ds_put_char(ds, ',');
490 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
492 ds_put_format(ds, "recirc(%"PRIu32")", recirc_id);
496 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
498 ds_put_format(ds, "hash(");
500 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
501 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
503 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
506 ds_put_format(ds, ")");
510 format_odp_action(struct ds *ds, const struct nlattr *a)
513 enum ovs_action_attr type = nl_attr_type(a);
514 const struct ovs_action_push_vlan *vlan;
517 expected_len = odp_action_len(nl_attr_type(a));
518 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
519 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
520 nl_attr_get_size(a), expected_len);
521 format_generic_odp_action(ds, a);
526 case OVS_ACTION_ATTR_OUTPUT:
527 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
529 case OVS_ACTION_ATTR_USERSPACE:
530 format_odp_userspace_action(ds, a);
532 case OVS_ACTION_ATTR_RECIRC:
533 format_odp_recirc_action(ds, nl_attr_get_u32(a));
535 case OVS_ACTION_ATTR_HASH:
536 format_odp_hash_action(ds, nl_attr_get(a));
538 case OVS_ACTION_ATTR_SET_MASKED:
540 size = nl_attr_get_size(a) / 2;
541 ds_put_cstr(ds, "set(");
543 /* Masked set action not supported for tunnel key, which is bigger. */
544 if (size <= sizeof(struct ovs_key_ipv6)) {
545 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
546 sizeof(struct nlattr))];
547 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
548 sizeof(struct nlattr))];
550 mask->nla_type = attr->nla_type = nl_attr_type(a);
551 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
552 memcpy(attr + 1, (char *)(a + 1), size);
553 memcpy(mask + 1, (char *)(a + 1) + size, size);
554 format_odp_key_attr(attr, mask, NULL, ds, false);
556 format_odp_key_attr(a, NULL, NULL, ds, false);
558 ds_put_cstr(ds, ")");
560 case OVS_ACTION_ATTR_SET:
561 ds_put_cstr(ds, "set(");
562 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
563 ds_put_cstr(ds, ")");
565 case OVS_ACTION_ATTR_PUSH_VLAN:
566 vlan = nl_attr_get(a);
567 ds_put_cstr(ds, "push_vlan(");
568 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
569 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
571 format_vlan_tci(ds, vlan->vlan_tci, OVS_BE16_MAX, false);
572 ds_put_char(ds, ')');
574 case OVS_ACTION_ATTR_POP_VLAN:
575 ds_put_cstr(ds, "pop_vlan");
577 case OVS_ACTION_ATTR_PUSH_MPLS: {
578 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
579 ds_put_cstr(ds, "push_mpls(");
580 format_mpls_lse(ds, mpls->mpls_lse);
581 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
584 case OVS_ACTION_ATTR_POP_MPLS: {
585 ovs_be16 ethertype = nl_attr_get_be16(a);
586 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
589 case OVS_ACTION_ATTR_SAMPLE:
590 format_odp_sample_action(ds, a);
592 case OVS_ACTION_ATTR_UNSPEC:
593 case __OVS_ACTION_ATTR_MAX:
595 format_generic_odp_action(ds, a);
601 format_odp_actions(struct ds *ds, const struct nlattr *actions,
605 const struct nlattr *a;
608 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
610 ds_put_char(ds, ',');
612 format_odp_action(ds, a);
617 if (left == actions_len) {
618 ds_put_cstr(ds, "<empty>");
620 ds_put_format(ds, ",***%u leftover bytes*** (", left);
621 for (i = 0; i < left; i++) {
622 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
624 ds_put_char(ds, ')');
627 ds_put_cstr(ds, "drop");
631 /* Separate out parse_odp_userspace_action() function. */
633 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
636 union user_action_cookie cookie;
638 odp_port_t tunnel_out_port;
640 void *user_data = NULL;
641 size_t user_data_size = 0;
643 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
649 uint32_t probability;
650 uint32_t collector_set_id;
651 uint32_t obs_domain_id;
652 uint32_t obs_point_id;
655 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
656 "pcp=%i,output=%"SCNi32")%n",
657 &vid, &pcp, &output, &n1)) {
661 tci = vid | (pcp << VLAN_PCP_SHIFT);
666 cookie.type = USER_ACTION_COOKIE_SFLOW;
667 cookie.sflow.vlan_tci = htons(tci);
668 cookie.sflow.output = output;
670 user_data_size = sizeof cookie.sflow;
671 } else if (ovs_scan(&s[n], ",slow_path(%n",
676 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
677 cookie.slow_path.unused = 0;
678 cookie.slow_path.reason = 0;
680 res = parse_flags(&s[n], slow_path_reason_to_string,
681 &cookie.slow_path.reason,
682 SLOW_PATH_REASON_MASK, NULL);
683 if (res < 0 || s[n + res] != ')') {
689 user_data_size = sizeof cookie.slow_path;
690 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
691 "collector_set_id=%"SCNi32","
692 "obs_domain_id=%"SCNi32","
693 "obs_point_id=%"SCNi32")%n",
694 &probability, &collector_set_id,
695 &obs_domain_id, &obs_point_id, &n1)) {
698 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
699 cookie.flow_sample.probability = probability;
700 cookie.flow_sample.collector_set_id = collector_set_id;
701 cookie.flow_sample.obs_domain_id = obs_domain_id;
702 cookie.flow_sample.obs_point_id = obs_point_id;
704 user_data_size = sizeof cookie.flow_sample;
705 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
708 cookie.type = USER_ACTION_COOKIE_IPFIX;
709 cookie.ipfix.output_odp_port = u32_to_odp(output);
711 user_data_size = sizeof cookie.ipfix;
712 } else if (ovs_scan(&s[n], ",userdata(%n",
717 ofpbuf_init(&buf, 16);
718 end = ofpbuf_put_hex(&buf, &s[n], NULL);
722 user_data = ofpbuf_data(&buf);
723 user_data_size = ofpbuf_size(&buf);
730 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
731 &tunnel_out_port, &n1)) {
732 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
734 } else if (s[n] == ')') {
735 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
744 parse_odp_action(const char *s, const struct simap *port_names,
745 struct ofpbuf *actions)
751 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
752 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
758 int len = strcspn(s, delimiters);
759 struct simap_node *node;
761 node = simap_find_len(port_names, s, len);
763 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
772 if (ovs_scan(s, "recirc(%"PRIu32")%n", &recirc_id, &n)) {
773 nl_msg_put_u32(actions, OVS_ACTION_ATTR_RECIRC, recirc_id);
778 if (!strncmp(s, "userspace(", 10)) {
779 return parse_odp_userspace_action(s, actions);
782 if (!strncmp(s, "set(", 4)) {
785 struct nlattr mask[128 / sizeof(struct nlattr)];
786 struct ofpbuf maskbuf;
787 struct nlattr *nested, *key;
790 /* 'mask' is big enough to hold any key. */
791 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
793 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
794 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
798 if (s[retval + 4] != ')') {
802 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
805 size = nl_attr_get_size(mask);
806 if (size == nl_attr_get_size(key)) {
807 /* Change to masked set action if not fully masked. */
808 if (!is_all_ones(mask + 1, size)) {
809 key->nla_len += size;
810 ofpbuf_put(actions, mask + 1, size);
811 /* 'actions' may have been reallocated by ofpbuf_put(). */
812 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
813 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
817 nl_msg_end_nested(actions, start_ofs);
822 struct ovs_action_push_vlan push;
823 int tpid = ETH_TYPE_VLAN;
828 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
829 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
830 &vid, &pcp, &cfi, &n)
831 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
832 &tpid, &vid, &pcp, &n)
833 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
834 &tpid, &vid, &pcp, &cfi, &n)) {
835 push.vlan_tpid = htons(tpid);
836 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
837 | (pcp << VLAN_PCP_SHIFT)
838 | (cfi ? VLAN_CFI : 0));
839 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
846 if (!strncmp(s, "pop_vlan", 8)) {
847 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
855 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
856 && percentage >= 0. && percentage <= 100.0) {
857 size_t sample_ofs, actions_ofs;
860 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
861 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
862 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
863 (probability <= 0 ? 0
864 : probability >= UINT32_MAX ? UINT32_MAX
867 actions_ofs = nl_msg_start_nested(actions,
868 OVS_SAMPLE_ATTR_ACTIONS);
872 n += strspn(s + n, delimiters);
877 retval = parse_odp_action(s + n, port_names, actions);
883 nl_msg_end_nested(actions, actions_ofs);
884 nl_msg_end_nested(actions, sample_ofs);
886 return s[n + 1] == ')' ? n + 2 : -EINVAL;
893 /* Parses the string representation of datapath actions, in the format output
894 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
895 * value. On success, the ODP actions are appended to 'actions' as a series of
896 * Netlink attributes. On failure, no data is appended to 'actions'. Either
897 * way, 'actions''s data might be reallocated. */
899 odp_actions_from_string(const char *s, const struct simap *port_names,
900 struct ofpbuf *actions)
904 if (!strcasecmp(s, "drop")) {
908 old_size = ofpbuf_size(actions);
912 s += strspn(s, delimiters);
917 retval = parse_odp_action(s, port_names, actions);
918 if (retval < 0 || !strchr(delimiters, s[retval])) {
919 ofpbuf_set_size(actions, old_size);
928 /* Returns the correct length of the payload for a flow key attribute of the
929 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
930 * is variable length. */
932 odp_flow_key_attr_len(uint16_t type)
934 if (type > OVS_KEY_ATTR_MAX) {
938 switch ((enum ovs_key_attr) type) {
939 case OVS_KEY_ATTR_ENCAP: return -2;
940 case OVS_KEY_ATTR_PRIORITY: return 4;
941 case OVS_KEY_ATTR_SKB_MARK: return 4;
942 case OVS_KEY_ATTR_DP_HASH: return 4;
943 case OVS_KEY_ATTR_RECIRC_ID: return 4;
944 case OVS_KEY_ATTR_TUNNEL: return -2;
945 case OVS_KEY_ATTR_IN_PORT: return 4;
946 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
947 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
948 case OVS_KEY_ATTR_ETHERTYPE: return 2;
949 case OVS_KEY_ATTR_MPLS: return -2;
950 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
951 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
952 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
953 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
954 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
955 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
956 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
957 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
958 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
959 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
961 case OVS_KEY_ATTR_UNSPEC:
962 case __OVS_KEY_ATTR_MAX:
970 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
972 size_t len = nl_attr_get_size(a);
974 const uint8_t *unspec;
977 unspec = nl_attr_get(a);
978 for (i = 0; i < len; i++) {
980 ds_put_char(ds, ' ');
982 ds_put_format(ds, "%02x", unspec[i]);
988 ovs_frag_type_to_string(enum ovs_frag_type type)
991 case OVS_FRAG_TYPE_NONE:
993 case OVS_FRAG_TYPE_FIRST:
995 case OVS_FRAG_TYPE_LATER:
997 case __OVS_FRAG_TYPE_MAX:
1004 tunnel_key_attr_len(int type)
1007 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
1008 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
1009 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
1010 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
1011 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
1012 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
1013 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
1014 case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
1015 case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
1016 case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
1017 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
1018 case __OVS_TUNNEL_KEY_ATTR_MAX:
1024 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1026 parse_geneve_opts(const struct nlattr *attr)
1028 int opts_len = nl_attr_get_size(attr);
1029 const struct geneve_opt *opt = nl_attr_get(attr);
1031 while (opts_len > 0) {
1034 if (opts_len < sizeof(*opt)) {
1038 len = sizeof(*opt) + opt->length * 4;
1039 if (len > opts_len) {
1043 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1045 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1050 opt = opt + len / sizeof(*opt);
1057 enum odp_key_fitness
1058 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1061 const struct nlattr *a;
1063 bool unknown = false;
1065 NL_NESTED_FOR_EACH(a, left, attr) {
1066 uint16_t type = nl_attr_type(a);
1067 size_t len = nl_attr_get_size(a);
1068 int expected_len = tunnel_key_attr_len(type);
1070 if (len != expected_len && expected_len >= 0) {
1071 return ODP_FIT_ERROR;
1075 case OVS_TUNNEL_KEY_ATTR_ID:
1076 tun->tun_id = nl_attr_get_be64(a);
1077 tun->flags |= FLOW_TNL_F_KEY;
1079 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1080 tun->ip_src = nl_attr_get_be32(a);
1082 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1083 tun->ip_dst = nl_attr_get_be32(a);
1085 case OVS_TUNNEL_KEY_ATTR_TOS:
1086 tun->ip_tos = nl_attr_get_u8(a);
1088 case OVS_TUNNEL_KEY_ATTR_TTL:
1089 tun->ip_ttl = nl_attr_get_u8(a);
1092 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1093 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1095 case OVS_TUNNEL_KEY_ATTR_CSUM:
1096 tun->flags |= FLOW_TNL_F_CSUM;
1098 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1099 tun->tp_src = nl_attr_get_be16(a);
1101 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1102 tun->tp_dst = nl_attr_get_be16(a);
1104 case OVS_TUNNEL_KEY_ATTR_OAM:
1105 tun->flags |= FLOW_TNL_F_OAM;
1107 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1108 if (parse_geneve_opts(a)) {
1109 return ODP_FIT_ERROR;
1111 /* It is necessary to reproduce options exactly (including order)
1112 * so it's easiest to just echo them back. */
1117 /* Allow this to show up as unexpected, if there are unknown
1118 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1125 return ODP_FIT_ERROR;
1128 return ODP_FIT_TOO_MUCH;
1130 return ODP_FIT_PERFECT;
1134 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1138 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1140 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1141 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1142 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1144 if (tun_key->ip_src) {
1145 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1147 if (tun_key->ip_dst) {
1148 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1150 if (tun_key->ip_tos) {
1151 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1153 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1154 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1155 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1157 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1158 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1160 if (tun_key->tp_src) {
1161 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1163 if (tun_key->tp_dst) {
1164 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1166 if (tun_key->flags & FLOW_TNL_F_OAM) {
1167 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1170 nl_msg_end_nested(a, tun_key_ofs);
1174 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1176 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1180 odp_mask_is_exact(enum ovs_key_attr attr, const void *mask, size_t size)
1182 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1183 return TCP_FLAGS(*(ovs_be16 *)mask) == TCP_FLAGS(OVS_BE16_MAX);
1185 if (attr == OVS_KEY_ATTR_IPV6) {
1186 const struct ovs_key_ipv6 *ipv6_mask = mask;
1189 ((ipv6_mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1190 == htonl(IPV6_LABEL_MASK))
1191 && ipv6_mask->ipv6_proto == UINT8_MAX
1192 && ipv6_mask->ipv6_tclass == UINT8_MAX
1193 && ipv6_mask->ipv6_hlimit == UINT8_MAX
1194 && ipv6_mask->ipv6_frag == UINT8_MAX
1195 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_src)
1196 && ipv6_mask_is_exact((const struct in6_addr *)ipv6_mask->ipv6_dst);
1198 if (attr == OVS_KEY_ATTR_TUNNEL) {
1199 const struct flow_tnl *tun_mask = mask;
1201 return tun_mask->flags == FLOW_TNL_F_MASK
1202 && tun_mask->tun_id == OVS_BE64_MAX
1203 && tun_mask->ip_src == OVS_BE32_MAX
1204 && tun_mask->ip_dst == OVS_BE32_MAX
1205 && tun_mask->ip_tos == UINT8_MAX
1206 && tun_mask->ip_ttl == UINT8_MAX
1207 && tun_mask->tp_src == OVS_BE16_MAX
1208 && tun_mask->tp_dst == OVS_BE16_MAX;
1211 if (attr == OVS_KEY_ATTR_ARP) {
1212 /* ARP key has padding, ignore it. */
1213 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp) == 24);
1214 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp, arp_tha) == 10 + 6);
1215 size = offsetof(struct ovs_key_arp, arp_tha) + ETH_ADDR_LEN;
1216 ovs_assert(((uint16_t *)mask)[size/2] == 0);
1219 return is_all_ones(mask, size);
1223 odp_mask_attr_is_exact(const struct nlattr *ma)
1225 struct flow_tnl tun_mask;
1226 enum ovs_key_attr attr = nl_attr_type(ma);
1230 if (attr == OVS_KEY_ATTR_TUNNEL) {
1231 memset(&tun_mask, 0, sizeof tun_mask);
1232 odp_tun_key_from_attr(ma, &tun_mask);
1234 size = sizeof tun_mask;
1236 mask = nl_attr_get(ma);
1237 size = nl_attr_get_size(ma);
1240 return odp_mask_is_exact(attr, mask, size);
1244 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1247 struct odp_portno_names *odp_portno_names;
1249 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1250 odp_portno_names->port_no = port_no;
1251 odp_portno_names->name = xstrdup(port_name);
1252 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1253 hash_odp_port(port_no));
1257 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1259 struct odp_portno_names *odp_portno_names;
1261 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1262 hash_odp_port(port_no), portno_names) {
1263 if (odp_portno_names->port_no == port_no) {
1264 return odp_portno_names->name;
1271 odp_portno_names_destroy(struct hmap *portno_names)
1273 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1274 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1275 hmap_node, portno_names) {
1276 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1277 free(odp_portno_names->name);
1278 free(odp_portno_names);
1282 /* Format helpers. */
1285 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1286 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1288 bool mask_empty = mask && eth_addr_is_zero(*mask);
1290 if (verbose || !mask_empty) {
1291 bool mask_full = !mask || eth_mask_is_exact(*mask);
1294 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1296 ds_put_format(ds, "%s=", name);
1297 eth_format_masked(key, *mask, ds);
1298 ds_put_char(ds, ',');
1304 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1305 const ovs_be64 *mask, bool verbose)
1307 bool mask_empty = mask && !*mask;
1309 if (verbose || !mask_empty) {
1310 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1312 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1313 if (!mask_full) { /* Partially masked. */
1314 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1316 ds_put_char(ds, ',');
1321 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1322 const ovs_be32 *mask, bool verbose)
1324 bool mask_empty = mask && !*mask;
1326 if (verbose || !mask_empty) {
1327 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1329 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1330 if (!mask_full) { /* Partially masked. */
1331 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1333 ds_put_char(ds, ',');
1338 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1339 const ovs_be32 (*mask_)[4], bool verbose)
1341 char buf[INET6_ADDRSTRLEN];
1342 const struct in6_addr *key = (const struct in6_addr *)key_;
1343 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1345 bool mask_empty = mask && ipv6_mask_is_any(mask);
1347 if (verbose || !mask_empty) {
1348 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1350 inet_ntop(AF_INET6, key, buf, sizeof buf);
1351 ds_put_format(ds, "%s=%s", name, buf);
1352 if (!mask_full) { /* Partially masked. */
1353 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1354 ds_put_format(ds, "/%s", buf);
1356 ds_put_char(ds, ',');
1361 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1362 const ovs_be32 *mask, bool verbose)
1364 bool mask_empty = mask && !*mask;
1366 if (verbose || !mask_empty) {
1367 bool mask_full = !mask
1368 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1370 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1371 if (!mask_full) { /* Partially masked. */
1372 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1374 ds_put_char(ds, ',');
1379 format_u8x(struct ds *ds, const char *name, uint8_t key,
1380 const uint8_t *mask, bool verbose)
1382 bool mask_empty = mask && !*mask;
1384 if (verbose || !mask_empty) {
1385 bool mask_full = !mask || *mask == UINT8_MAX;
1387 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1388 if (!mask_full) { /* Partially masked. */
1389 ds_put_format(ds, "/%#"PRIx8, *mask);
1391 ds_put_char(ds, ',');
1396 format_u8u(struct ds *ds, const char *name, uint8_t key,
1397 const uint8_t *mask, bool verbose)
1399 bool mask_empty = mask && !*mask;
1401 if (verbose || !mask_empty) {
1402 bool mask_full = !mask || *mask == UINT8_MAX;
1404 ds_put_format(ds, "%s=%"PRIu8, name, key);
1405 if (!mask_full) { /* Partially masked. */
1406 ds_put_format(ds, "/%#"PRIx8, *mask);
1408 ds_put_char(ds, ',');
1413 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1414 const ovs_be16 *mask, bool verbose)
1416 bool mask_empty = mask && !*mask;
1418 if (verbose || !mask_empty) {
1419 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1421 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1422 if (!mask_full) { /* Partially masked. */
1423 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1425 ds_put_char(ds, ',');
1430 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1431 const uint16_t *mask, bool verbose)
1433 bool mask_empty = mask && !*mask;
1435 if (verbose || !mask_empty) {
1436 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1438 ds_put_cstr(ds, name);
1439 ds_put_char(ds, '(');
1440 if (!mask_full) { /* Partially masked. */
1441 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1442 } else { /* Fully masked. */
1443 format_flags(ds, flow_tun_flag_to_string, key, ',');
1445 ds_put_cstr(ds, "),");
1450 format_frag(struct ds *ds, const char *name, uint8_t key,
1451 const uint8_t *mask, bool verbose)
1453 bool mask_empty = mask && !*mask;
1455 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1456 if (verbose || !mask_empty) {
1457 bool mask_full = !mask || *mask == UINT8_MAX;
1459 if (!mask_full) { /* Partially masked. */
1460 ds_put_format(ds, "error: partial mask not supported for frag (%#"
1463 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1468 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1471 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1472 const struct hmap *portno_names, struct ds *ds,
1475 enum ovs_key_attr attr = nl_attr_type(a);
1476 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1480 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1482 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1485 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1486 if (expected_len != -2) {
1487 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1488 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1490 if (bad_key_len || bad_mask_len) {
1492 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1493 nl_attr_get_size(a), expected_len);
1495 format_generic_odp_key(a, ds);
1497 ds_put_char(ds, '/');
1499 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1500 nl_attr_get_size(ma), expected_len);
1502 format_generic_odp_key(ma, ds);
1504 ds_put_char(ds, ')');
1510 ds_put_char(ds, '(');
1512 case OVS_KEY_ATTR_ENCAP:
1513 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1514 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1515 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1517 } else if (nl_attr_get_size(a)) {
1518 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1523 case OVS_KEY_ATTR_PRIORITY:
1524 case OVS_KEY_ATTR_SKB_MARK:
1525 case OVS_KEY_ATTR_DP_HASH:
1526 case OVS_KEY_ATTR_RECIRC_ID:
1527 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1529 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1533 case OVS_KEY_ATTR_TUNNEL: {
1534 struct flow_tnl key, mask_;
1535 struct flow_tnl *mask = ma ? &mask_ : NULL;
1538 memset(mask, 0, sizeof *mask);
1539 odp_tun_key_from_attr(ma, mask);
1541 memset(&key, 0, sizeof key);
1542 if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1543 ds_put_format(ds, "error");
1546 format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1547 format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1548 format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1549 format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1550 format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1551 format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1552 format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1553 format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1557 case OVS_KEY_ATTR_IN_PORT:
1558 if (portno_names && verbose && is_exact) {
1559 char *name = odp_portno_names_get(portno_names,
1560 u32_to_odp(nl_attr_get_u32(a)));
1562 ds_put_format(ds, "%s", name);
1564 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1567 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1569 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1574 case OVS_KEY_ATTR_ETHERNET: {
1575 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1576 const struct ovs_key_ethernet *key = nl_attr_get(a);
1578 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1579 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1583 case OVS_KEY_ATTR_VLAN:
1584 format_vlan_tci(ds, nl_attr_get_be16(a),
1585 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1588 case OVS_KEY_ATTR_MPLS: {
1589 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1590 const struct ovs_key_mpls *mpls_mask = NULL;
1591 size_t size = nl_attr_get_size(a);
1593 if (!size || size % sizeof *mpls_key) {
1594 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1598 mpls_mask = nl_attr_get(ma);
1599 if (size != nl_attr_get_size(ma)) {
1600 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1601 "mask length %"PRIuSIZE")",
1602 size, nl_attr_get_size(ma));
1606 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1609 case OVS_KEY_ATTR_ETHERTYPE:
1610 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1612 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1616 case OVS_KEY_ATTR_IPV4: {
1617 const struct ovs_key_ipv4 *key = nl_attr_get(a);
1618 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1620 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1621 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1622 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1624 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1625 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1626 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1631 case OVS_KEY_ATTR_IPV6: {
1632 const struct ovs_key_ipv6 *key = nl_attr_get(a);
1633 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1635 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1636 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1637 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1639 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1641 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1643 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1645 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1650 /* These have the same structure and format. */
1651 case OVS_KEY_ATTR_TCP:
1652 case OVS_KEY_ATTR_UDP:
1653 case OVS_KEY_ATTR_SCTP: {
1654 const struct ovs_key_tcp *key = nl_attr_get(a);
1655 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1657 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1658 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1662 case OVS_KEY_ATTR_TCP_FLAGS:
1664 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1665 ntohs(nl_attr_get_be16(a)),
1666 ntohs(nl_attr_get_be16(ma)));
1668 format_flags(ds, packet_tcp_flag_to_string,
1669 ntohs(nl_attr_get_be16(a)), ',');
1673 case OVS_KEY_ATTR_ICMP: {
1674 const struct ovs_key_icmp *key = nl_attr_get(a);
1675 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
1677 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
1678 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
1682 case OVS_KEY_ATTR_ICMPV6: {
1683 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
1684 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
1686 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
1688 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
1693 case OVS_KEY_ATTR_ARP: {
1694 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
1695 const struct ovs_key_arp *key = nl_attr_get(a);
1697 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
1698 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
1699 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
1700 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
1701 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
1705 case OVS_KEY_ATTR_ND: {
1706 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
1707 const struct ovs_key_nd *key = nl_attr_get(a);
1709 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
1711 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
1712 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
1717 case OVS_KEY_ATTR_UNSPEC:
1718 case __OVS_KEY_ATTR_MAX:
1720 format_generic_odp_key(a, ds);
1722 ds_put_char(ds, '/');
1723 format_generic_odp_key(ma, ds);
1727 ds_put_char(ds, ')');
1730 static struct nlattr *
1731 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1733 const struct nlattr *a;
1735 int type = nl_attr_type(key);
1736 int size = nl_attr_get_size(key);
1738 if (odp_flow_key_attr_len(type) >=0) {
1739 nl_msg_put_unspec_zero(ofp, type, size);
1743 nested_mask = nl_msg_start_nested(ofp, type);
1744 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1745 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1747 nl_msg_end_nested(ofp, nested_mask);
1750 return ofpbuf_base(ofp);
1753 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1754 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1755 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
1756 * non-null and 'verbose' is true, translates odp port number to its name. */
1758 odp_flow_format(const struct nlattr *key, size_t key_len,
1759 const struct nlattr *mask, size_t mask_len,
1760 const struct hmap *portno_names, struct ds *ds, bool verbose)
1763 const struct nlattr *a;
1765 bool has_ethtype_key = false;
1766 const struct nlattr *ma = NULL;
1768 bool first_field = true;
1770 ofpbuf_init(&ofp, 100);
1771 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1772 bool is_nested_attr;
1773 bool is_wildcard = false;
1774 int attr_type = nl_attr_type(a);
1776 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1777 has_ethtype_key = true;
1780 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1782 if (mask && mask_len) {
1783 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1784 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1787 if (verbose || !is_wildcard || is_nested_attr) {
1788 if (is_wildcard && !ma) {
1789 ma = generate_all_wildcard_mask(&ofp, a);
1792 ds_put_char(ds, ',');
1794 format_odp_key_attr(a, ma, portno_names, ds, verbose);
1795 first_field = false;
1799 ofpbuf_uninit(&ofp);
1804 if (left == key_len) {
1805 ds_put_cstr(ds, "<empty>");
1807 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1808 for (i = 0; i < left; i++) {
1809 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1811 ds_put_char(ds, ')');
1813 if (!has_ethtype_key) {
1814 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1816 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1817 ntohs(nl_attr_get_be16(ma)));
1821 ds_put_cstr(ds, "<empty>");
1825 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1826 * OVS_KEY_ATTR_* attributes in 'key'. */
1828 odp_flow_key_format(const struct nlattr *key,
1829 size_t key_len, struct ds *ds)
1831 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
1835 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1837 if (!strcasecmp(s, "no")) {
1838 *type = OVS_FRAG_TYPE_NONE;
1839 } else if (!strcasecmp(s, "first")) {
1840 *type = OVS_FRAG_TYPE_FIRST;
1841 } else if (!strcasecmp(s, "later")) {
1842 *type = OVS_FRAG_TYPE_LATER;
1852 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
1853 uint8_t (*mask)[ETH_ADDR_LEN])
1857 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
1861 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
1862 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
1865 memset(mask, 0xff, sizeof *mask);
1874 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
1878 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
1882 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
1883 IP_SCAN_ARGS(mask), &n)) {
1886 *mask = OVS_BE32_MAX;
1895 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
1898 char ipv6_s[IPV6_SCAN_LEN + 1];
1900 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
1901 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
1905 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
1906 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
1909 memset(mask, 0xff, sizeof *mask);
1918 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
1923 if (ovs_scan(s, "%i%n", &key_, &n)
1924 && (key_ & ~IPV6_LABEL_MASK) == 0) {
1929 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
1930 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
1932 *mask = htonl(mask_);
1934 *mask = htonl(IPV6_LABEL_MASK);
1943 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
1947 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
1951 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
1963 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
1967 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
1971 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
1983 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
1985 uint16_t key_, mask_;
1988 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
1993 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
1995 *mask = htons(mask_);
1997 *mask = OVS_BE16_MAX;
2006 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
2008 uint64_t key_, mask_;
2011 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
2014 *key = htonll(key_);
2016 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
2018 *mask = htonll(mask_);
2020 *mask = OVS_BE64_MAX;
2029 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
2031 uint32_t flags, fmask;
2034 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2035 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2036 if (n >= 0 && s[n] == ')') {
2047 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2049 uint32_t flags, fmask;
2052 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2053 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2055 *key = htons(flags);
2057 *mask = htons(fmask);
2065 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2069 enum ovs_frag_type frag_type;
2071 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2072 && ovs_frag_type_from_string(frag, &frag_type)) {
2085 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2086 const struct simap *port_names)
2090 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2094 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2101 } else if (port_names) {
2102 const struct simap_node *node;
2105 len = strcspn(s, ")");
2106 node = simap_find_len(port_names, s, len);
2119 /* Helper for vlan parsing. */
2120 struct ovs_key_vlan__ {
2125 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2127 const uint16_t mask = ((1U << bits) - 1) << offset;
2129 if (value >> bits) {
2133 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2138 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2141 uint16_t key_, mask_;
2144 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2147 if (set_be16_bf(key, bits, offset, key_)) {
2149 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2152 if (!set_be16_bf(mask, bits, offset, mask_)) {
2156 *mask |= htons(((1U << bits) - 1) << offset);
2166 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2168 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2172 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2174 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2178 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2180 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2185 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2187 const uint32_t mask = ((1U << bits) - 1) << offset;
2189 if (value >> bits) {
2193 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2198 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2201 uint32_t key_, mask_;
2204 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2207 if (set_be32_bf(key, bits, offset, key_)) {
2209 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2212 if (!set_be32_bf(mask, bits, offset, mask_)) {
2216 *mask |= htonl(((1U << bits) - 1) << offset);
2226 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2228 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2232 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2234 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2238 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2240 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2244 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2246 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2249 /* ATTR is compile-time constant, so only the case with correct data type
2250 * will be used. However, the compiler complains about the data type for
2251 * the other cases, so we must cast to make the compiler silent. */
2252 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2253 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2254 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2256 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2259 #define SCAN_IF(NAME) \
2260 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2261 const char *start = s; \
2266 /* Usually no special initialization is needed. */
2267 #define SCAN_BEGIN(NAME, TYPE) \
2270 memset(&skey, 0, sizeof skey); \
2271 memset(&smask, 0, sizeof smask); \
2275 /* VLAN needs special initialization. */
2276 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2278 TYPE skey = KEY_INIT; \
2279 TYPE smask = MASK_INIT; \
2283 /* Scan unnamed entry as 'TYPE' */
2284 #define SCAN_TYPE(TYPE, KEY, MASK) \
2285 len = scan_##TYPE(s, KEY, MASK); \
2291 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2292 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2293 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2294 s += strlen(NAME); \
2295 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2299 #define SCAN_FINISH() \
2300 } while (*s++ == ',' && len != 0); \
2301 if (s[-1] != ')') { \
2305 #define SCAN_FINISH_SINGLE() \
2307 if (*s++ != ')') { \
2311 #define SCAN_PUT(ATTR) \
2312 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2313 SCAN_PUT_ATTR(key, ATTR, skey); \
2315 SCAN_PUT_ATTR(mask, ATTR, smask); \
2319 #define SCAN_END(ATTR) \
2325 #define SCAN_END_SINGLE(ATTR) \
2326 SCAN_FINISH_SINGLE(); \
2331 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2332 SCAN_BEGIN(NAME, TYPE) { \
2333 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2334 } SCAN_END_SINGLE(ATTR)
2336 #define SCAN_SINGLE_NO_MASK(NAME, TYPE, SCAN_AS, ATTR) \
2337 SCAN_BEGIN(NAME, TYPE) { \
2338 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2339 } SCAN_END_SINGLE(ATTR)
2341 /* scan_port needs one extra argument. */
2342 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2343 SCAN_BEGIN(NAME, TYPE) { \
2344 len = scan_port(s, &skey, &smask, port_names); \
2349 } SCAN_END_SINGLE(ATTR)
2352 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2353 struct ofpbuf *key, struct ofpbuf *mask)
2355 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2356 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2357 SCAN_SINGLE_NO_MASK("recirc_id(", uint32_t, u32, OVS_KEY_ATTR_RECIRC_ID);
2358 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2360 SCAN_BEGIN("tunnel(", struct flow_tnl) {
2361 SCAN_FIELD("tun_id=", be64, tun_id);
2362 SCAN_FIELD("src=", ipv4, ip_src);
2363 SCAN_FIELD("dst=", ipv4, ip_dst);
2364 SCAN_FIELD("tos=", u8, ip_tos);
2365 SCAN_FIELD("ttl=", u8, ip_ttl);
2366 SCAN_FIELD("tp_src=", be16, tp_src);
2367 SCAN_FIELD("tp_dst=", be16, tp_dst);
2368 SCAN_FIELD("flags(", tun_flags, flags);
2369 } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2371 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2373 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2374 SCAN_FIELD("src=", eth, eth_src);
2375 SCAN_FIELD("dst=", eth, eth_dst);
2376 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2378 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2379 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2380 SCAN_FIELD("vid=", vid, tci);
2381 SCAN_FIELD("pcp=", pcp, tci);
2382 SCAN_FIELD("cfi=", cfi, tci);
2383 } SCAN_END(OVS_KEY_ATTR_VLAN);
2385 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2387 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2388 SCAN_FIELD("label=", mpls_label, mpls_lse);
2389 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2390 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2391 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2392 } SCAN_END(OVS_KEY_ATTR_MPLS);
2394 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2395 SCAN_FIELD("src=", ipv4, ipv4_src);
2396 SCAN_FIELD("dst=", ipv4, ipv4_dst);
2397 SCAN_FIELD("proto=", u8, ipv4_proto);
2398 SCAN_FIELD("tos=", u8, ipv4_tos);
2399 SCAN_FIELD("ttl=", u8, ipv4_ttl);
2400 SCAN_FIELD("frag=", frag, ipv4_frag);
2401 } SCAN_END(OVS_KEY_ATTR_IPV4);
2403 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2404 SCAN_FIELD("src=", ipv6, ipv6_src);
2405 SCAN_FIELD("dst=", ipv6, ipv6_dst);
2406 SCAN_FIELD("label=", ipv6_label, ipv6_label);
2407 SCAN_FIELD("proto=", u8, ipv6_proto);
2408 SCAN_FIELD("tclass=", u8, ipv6_tclass);
2409 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2410 SCAN_FIELD("frag=", frag, ipv6_frag);
2411 } SCAN_END(OVS_KEY_ATTR_IPV6);
2413 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2414 SCAN_FIELD("src=", be16, tcp_src);
2415 SCAN_FIELD("dst=", be16, tcp_dst);
2416 } SCAN_END(OVS_KEY_ATTR_TCP);
2418 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2420 SCAN_BEGIN("udp(", struct ovs_key_udp) {
2421 SCAN_FIELD("src=", be16, udp_src);
2422 SCAN_FIELD("dst=", be16, udp_dst);
2423 } SCAN_END(OVS_KEY_ATTR_UDP);
2425 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2426 SCAN_FIELD("src=", be16, sctp_src);
2427 SCAN_FIELD("dst=", be16, sctp_dst);
2428 } SCAN_END(OVS_KEY_ATTR_SCTP);
2430 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2431 SCAN_FIELD("type=", u8, icmp_type);
2432 SCAN_FIELD("code=", u8, icmp_code);
2433 } SCAN_END(OVS_KEY_ATTR_ICMP);
2435 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2436 SCAN_FIELD("type=", u8, icmpv6_type);
2437 SCAN_FIELD("code=", u8, icmpv6_code);
2438 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2440 SCAN_BEGIN("arp(", struct ovs_key_arp) {
2441 SCAN_FIELD("sip=", ipv4, arp_sip);
2442 SCAN_FIELD("tip=", ipv4, arp_tip);
2443 SCAN_FIELD("op=", be16, arp_op);
2444 SCAN_FIELD("sha=", eth, arp_sha);
2445 SCAN_FIELD("tha=", eth, arp_tha);
2446 } SCAN_END(OVS_KEY_ATTR_ARP);
2448 SCAN_BEGIN("nd(", struct ovs_key_nd) {
2449 SCAN_FIELD("target=", ipv6, nd_target);
2450 SCAN_FIELD("sll=", eth, nd_sll);
2451 SCAN_FIELD("tll=", eth, nd_tll);
2452 } SCAN_END(OVS_KEY_ATTR_ND);
2454 /* Encap open-coded. */
2455 if (!strncmp(s, "encap(", 6)) {
2456 const char *start = s;
2457 size_t encap, encap_mask = 0;
2459 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2461 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2468 s += strspn(s, ", \t\r\n");
2471 } else if (*s == ')') {
2475 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2483 nl_msg_end_nested(key, encap);
2485 nl_msg_end_nested(mask, encap_mask);
2494 /* Parses the string representation of a datapath flow key, in the
2495 * format output by odp_flow_key_format(). Returns 0 if successful,
2496 * otherwise a positive errno value. On success, the flow key is
2497 * appended to 'key' as a series of Netlink attributes. On failure, no
2498 * data is appended to 'key'. Either way, 'key''s data might be
2501 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2502 * to a port number. (Port names may be used instead of port numbers in
2505 * On success, the attributes appended to 'key' are individually syntactically
2506 * valid, but they may not be valid as a sequence. 'key' might, for example,
2507 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2509 odp_flow_from_string(const char *s, const struct simap *port_names,
2510 struct ofpbuf *key, struct ofpbuf *mask)
2512 const size_t old_size = ofpbuf_size(key);
2516 s += strspn(s, delimiters);
2521 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2523 ofpbuf_set_size(key, old_size);
2533 ovs_to_odp_frag(uint8_t nw_frag, bool is_mask)
2536 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
2537 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
2538 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
2539 * must use a zero mask for the netlink frag field, and all ones mask
2541 return (nw_frag & FLOW_NW_FRAG_ANY) ? UINT8_MAX : 0;
2543 return !(nw_frag & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_NONE
2544 : nw_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
2545 : OVS_FRAG_TYPE_FIRST;
2548 static void get_ethernet_key(const struct flow *, struct ovs_key_ethernet *);
2549 static void put_ethernet_key(const struct ovs_key_ethernet *, struct flow *);
2550 static void get_ipv4_key(const struct flow *, struct ovs_key_ipv4 *,
2552 static void put_ipv4_key(const struct ovs_key_ipv4 *, struct flow *,
2554 static void get_ipv6_key(const struct flow *, struct ovs_key_ipv6 *,
2556 static void put_ipv6_key(const struct ovs_key_ipv6 *, struct flow *,
2558 static void get_arp_key(const struct flow *, struct ovs_key_arp *);
2559 static void put_arp_key(const struct ovs_key_arp *, struct flow *);
2561 /* These share the same layout. */
2563 struct ovs_key_tcp tcp;
2564 struct ovs_key_udp udp;
2565 struct ovs_key_sctp sctp;
2568 static void get_tp_key(const struct flow *, union ovs_key_tp *);
2569 static void put_tp_key(const union ovs_key_tp *, struct flow *);
2572 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2573 const struct flow *mask, odp_port_t odp_in_port,
2574 size_t max_mpls_depth, bool recirc, bool export_mask)
2576 struct ovs_key_ethernet *eth_key;
2578 const struct flow *data = export_mask ? mask : flow;
2580 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2582 if (flow->tunnel.ip_dst || export_mask) {
2583 tun_key_to_attr(buf, &data->tunnel);
2586 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2589 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2590 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2593 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2594 * is not the magical value "ODPP_NONE". */
2595 if (export_mask || odp_in_port != ODPP_NONE) {
2596 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2599 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2601 get_ethernet_key(data, eth_key);
2603 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2605 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2607 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2609 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2610 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2611 if (flow->vlan_tci == htons(0)) {
2618 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2619 /* For backwards compatibility with kernels that don't support
2620 * wildcarding, the following convention is used to encode the
2621 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2624 * -------- -------- -------
2625 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2626 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2627 * <none> 0xffff Any non-Ethernet II frame (except valid
2628 * 802.3 SNAP packet with valid eth_type).
2631 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2636 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2638 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2639 struct ovs_key_ipv4 *ipv4_key;
2641 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2643 get_ipv4_key(data, ipv4_key, export_mask);
2644 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2645 struct ovs_key_ipv6 *ipv6_key;
2647 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2649 get_ipv6_key(data, ipv6_key, export_mask);
2650 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2651 flow->dl_type == htons(ETH_TYPE_RARP)) {
2652 struct ovs_key_arp *arp_key;
2654 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2656 get_arp_key(data, arp_key);
2657 } else if (eth_type_mpls(flow->dl_type)) {
2658 struct ovs_key_mpls *mpls_key;
2661 n = flow_count_mpls_labels(flow, NULL);
2662 n = MIN(n, max_mpls_depth);
2663 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2664 n * sizeof *mpls_key);
2665 for (i = 0; i < n; i++) {
2666 mpls_key[i].mpls_lse = data->mpls_lse[i];
2670 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2671 if (flow->nw_proto == IPPROTO_TCP) {
2672 union ovs_key_tp *tcp_key;
2674 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2676 get_tp_key(data, tcp_key);
2677 if (data->tcp_flags) {
2678 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
2680 } else if (flow->nw_proto == IPPROTO_UDP) {
2681 union ovs_key_tp *udp_key;
2683 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2685 get_tp_key(data, udp_key);
2686 } else if (flow->nw_proto == IPPROTO_SCTP) {
2687 union ovs_key_tp *sctp_key;
2689 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2691 get_tp_key(data, sctp_key);
2692 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2693 && flow->nw_proto == IPPROTO_ICMP) {
2694 struct ovs_key_icmp *icmp_key;
2696 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2698 icmp_key->icmp_type = ntohs(data->tp_src);
2699 icmp_key->icmp_code = ntohs(data->tp_dst);
2700 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2701 && flow->nw_proto == IPPROTO_ICMPV6) {
2702 struct ovs_key_icmpv6 *icmpv6_key;
2704 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2705 sizeof *icmpv6_key);
2706 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2707 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2709 if (flow->tp_dst == htons(0)
2710 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
2711 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
2712 && (!export_mask || (data->tp_src == htons(0xffff)
2713 && data->tp_dst == htons(0xffff)))) {
2715 struct ovs_key_nd *nd_key;
2717 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2719 memcpy(nd_key->nd_target, &data->nd_target,
2720 sizeof nd_key->nd_target);
2721 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2722 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2729 nl_msg_end_nested(buf, encap);
2733 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2734 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2735 * number rather than a datapath port number). Instead, if 'odp_in_port'
2736 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2739 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2740 * capable of being expanded to allow for that much space.
2742 * 'recirc' indicates support for recirculation fields. If this is true, then
2743 * these fields will always be serialised. */
2745 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2746 const struct flow *mask, odp_port_t odp_in_port,
2749 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
2753 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2754 * 'buf'. 'flow' is used as a template to determine how to interpret
2755 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2756 * it doesn't indicate whether the other fields should be interpreted as
2757 * ARP, IPv4, IPv6, etc.
2759 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2760 * capable of being expanded to allow for that much space.
2762 * 'recirc' indicates support for recirculation fields. If this is true, then
2763 * these fields will always be serialised. */
2765 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2766 const struct flow *flow, uint32_t odp_in_port_mask,
2767 size_t max_mpls_depth, bool recirc)
2769 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
2770 max_mpls_depth, recirc, true);
2773 /* Generate ODP flow key from the given packet metadata */
2775 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
2777 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
2779 if (md->tunnel.ip_dst) {
2780 tun_key_to_attr(buf, &md->tunnel);
2783 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
2785 /* Add an ingress port attribute if 'odp_in_port' is not the magical
2786 * value "ODPP_NONE". */
2787 if (md->in_port.odp_port != ODPP_NONE) {
2788 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
2792 /* Generate packet metadata from the given ODP flow key. */
2794 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
2795 struct pkt_metadata *md)
2797 const struct nlattr *nla;
2799 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
2800 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
2801 1u << OVS_KEY_ATTR_IN_PORT;
2803 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
2805 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2806 uint16_t type = nl_attr_type(nla);
2807 size_t len = nl_attr_get_size(nla);
2808 int expected_len = odp_flow_key_attr_len(type);
2810 if (len != expected_len && expected_len >= 0) {
2815 case OVS_KEY_ATTR_RECIRC_ID:
2816 md->recirc_id = nl_attr_get_u32(nla);
2817 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
2819 case OVS_KEY_ATTR_DP_HASH:
2820 md->dp_hash = nl_attr_get_u32(nla);
2821 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
2823 case OVS_KEY_ATTR_PRIORITY:
2824 md->skb_priority = nl_attr_get_u32(nla);
2825 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
2827 case OVS_KEY_ATTR_SKB_MARK:
2828 md->pkt_mark = nl_attr_get_u32(nla);
2829 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
2831 case OVS_KEY_ATTR_TUNNEL: {
2832 enum odp_key_fitness res;
2834 res = odp_tun_key_from_attr(nla, &md->tunnel);
2835 if (res == ODP_FIT_ERROR) {
2836 memset(&md->tunnel, 0, sizeof md->tunnel);
2837 } else if (res == ODP_FIT_PERFECT) {
2838 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
2842 case OVS_KEY_ATTR_IN_PORT:
2843 md->in_port.odp_port = nl_attr_get_odp_port(nla);
2844 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
2850 if (!wanted_attrs) {
2851 return; /* Have everything. */
2857 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2859 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2860 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2861 key_len / sizeof(uint32_t), 0);
2865 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2866 uint64_t attrs, int out_of_range_attr,
2867 const struct nlattr *key, size_t key_len)
2872 if (VLOG_DROP_DBG(rl)) {
2877 for (i = 0; i < 64; i++) {
2878 if (attrs & (UINT64_C(1) << i)) {
2879 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2881 ds_put_format(&s, " %s",
2882 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2885 if (out_of_range_attr) {
2886 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2889 ds_put_cstr(&s, ": ");
2890 odp_flow_key_format(key, key_len, &s);
2892 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2897 odp_to_ovs_frag(uint8_t odp_frag, bool is_mask)
2899 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2902 return odp_frag ? FLOW_NW_FRAG_MASK : 0;
2905 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2906 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2907 return 0xff; /* Error. */
2910 return (odp_frag == OVS_FRAG_TYPE_NONE) ? 0
2911 : (odp_frag == OVS_FRAG_TYPE_FIRST) ? FLOW_NW_FRAG_ANY
2912 : FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER;
2916 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2917 const struct nlattr *attrs[], uint64_t *present_attrsp,
2918 int *out_of_range_attrp)
2920 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2921 const struct nlattr *nla;
2922 uint64_t present_attrs;
2925 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2927 *out_of_range_attrp = 0;
2928 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2929 uint16_t type = nl_attr_type(nla);
2930 size_t len = nl_attr_get_size(nla);
2931 int expected_len = odp_flow_key_attr_len(type);
2933 if (len != expected_len && expected_len >= 0) {
2934 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2936 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
2937 "length %d", ovs_key_attr_to_string(type, namebuf,
2943 if (type > OVS_KEY_ATTR_MAX) {
2944 *out_of_range_attrp = type;
2946 if (present_attrs & (UINT64_C(1) << type)) {
2947 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2949 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2950 ovs_key_attr_to_string(type,
2951 namebuf, sizeof namebuf));
2955 present_attrs |= UINT64_C(1) << type;
2960 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2964 *present_attrsp = present_attrs;
2968 static enum odp_key_fitness
2969 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2970 uint64_t expected_attrs,
2971 const struct nlattr *key, size_t key_len)
2973 uint64_t missing_attrs;
2974 uint64_t extra_attrs;
2976 missing_attrs = expected_attrs & ~present_attrs;
2977 if (missing_attrs) {
2978 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2979 log_odp_key_attributes(&rl, "expected but not present",
2980 missing_attrs, 0, key, key_len);
2981 return ODP_FIT_TOO_LITTLE;
2984 extra_attrs = present_attrs & ~expected_attrs;
2985 if (extra_attrs || out_of_range_attr) {
2986 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2987 log_odp_key_attributes(&rl, "present but not expected",
2988 extra_attrs, out_of_range_attr, key, key_len);
2989 return ODP_FIT_TOO_MUCH;
2992 return ODP_FIT_PERFECT;
2996 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2997 uint64_t present_attrs, uint64_t *expected_attrs,
2998 struct flow *flow, const struct flow *src_flow)
3000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3001 bool is_mask = flow != src_flow;
3003 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3004 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3005 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3006 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3007 ntohs(flow->dl_type));
3010 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3011 flow->dl_type != htons(0xffff)) {
3014 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3017 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3018 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3019 /* See comments in odp_flow_key_from_flow__(). */
3020 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3027 static enum odp_key_fitness
3028 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3029 uint64_t present_attrs, int out_of_range_attr,
3030 uint64_t expected_attrs, struct flow *flow,
3031 const struct nlattr *key, size_t key_len,
3032 const struct flow *src_flow)
3034 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3035 bool is_mask = src_flow != flow;
3036 const void *check_start = NULL;
3037 size_t check_len = 0;
3038 enum ovs_key_attr expected_bit = 0xff;
3040 if (eth_type_mpls(src_flow->dl_type)) {
3041 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3042 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3043 int n = size / sizeof(ovs_be32);
3046 if (!size || size % sizeof(ovs_be32)) {
3047 return ODP_FIT_ERROR;
3051 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3053 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
3054 return ODP_FIT_TOO_LITTLE;
3056 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3057 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3058 return ODP_FIT_ERROR;
3060 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3063 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3064 flow->mpls_lse[i] = mpls_lse[i];
3066 if (n > FLOW_MAX_MPLS_LABELS) {
3067 return ODP_FIT_TOO_MUCH;
3071 /* BOS may be set only in the innermost label. */
3072 for (i = 0; i < n - 1; i++) {
3073 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3074 return ODP_FIT_ERROR;
3078 /* BOS must be set in the innermost label. */
3079 if (n < FLOW_MAX_MPLS_LABELS
3080 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3081 return ODP_FIT_TOO_LITTLE;
3086 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3088 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3090 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3091 const struct ovs_key_ipv4 *ipv4_key;
3093 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3094 put_ipv4_key(ipv4_key, flow, is_mask);
3095 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3096 return ODP_FIT_ERROR;
3099 check_start = ipv4_key;
3100 check_len = sizeof *ipv4_key;
3101 expected_bit = OVS_KEY_ATTR_IPV4;
3104 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3106 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3108 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3109 const struct ovs_key_ipv6 *ipv6_key;
3111 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3112 put_ipv6_key(ipv6_key, flow, is_mask);
3113 if (flow->nw_frag > FLOW_NW_FRAG_MASK) {
3114 return ODP_FIT_ERROR;
3117 check_start = ipv6_key;
3118 check_len = sizeof *ipv6_key;
3119 expected_bit = OVS_KEY_ATTR_IPV6;
3122 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3123 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3125 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3127 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3128 const struct ovs_key_arp *arp_key;
3130 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3131 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3132 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3133 "key", ntohs(arp_key->arp_op));
3134 return ODP_FIT_ERROR;
3136 put_arp_key(arp_key, flow);
3138 check_start = arp_key;
3139 check_len = sizeof *arp_key;
3140 expected_bit = OVS_KEY_ATTR_ARP;
3146 if (check_len > 0) { /* Happens only when 'is_mask'. */
3147 if (!is_all_zeros(check_start, check_len) &&
3148 flow->dl_type != htons(0xffff)) {
3149 return ODP_FIT_ERROR;
3151 expected_attrs |= UINT64_C(1) << expected_bit;
3155 expected_bit = OVS_KEY_ATTR_UNSPEC;
3156 if (src_flow->nw_proto == IPPROTO_TCP
3157 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3158 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3159 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3161 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3163 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3164 const union ovs_key_tp *tcp_key;
3166 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3167 put_tp_key(tcp_key, flow);
3168 expected_bit = OVS_KEY_ATTR_TCP;
3170 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3171 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3172 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3174 } else if (src_flow->nw_proto == IPPROTO_UDP
3175 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3176 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3177 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3179 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3181 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3182 const union ovs_key_tp *udp_key;
3184 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3185 put_tp_key(udp_key, flow);
3186 expected_bit = OVS_KEY_ATTR_UDP;
3188 } else if (src_flow->nw_proto == IPPROTO_SCTP
3189 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3190 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3191 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3193 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3195 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3196 const union ovs_key_tp *sctp_key;
3198 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3199 put_tp_key(sctp_key, flow);
3200 expected_bit = OVS_KEY_ATTR_SCTP;
3202 } else if (src_flow->nw_proto == IPPROTO_ICMP
3203 && src_flow->dl_type == htons(ETH_TYPE_IP)
3204 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3206 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3208 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3209 const struct ovs_key_icmp *icmp_key;
3211 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3212 flow->tp_src = htons(icmp_key->icmp_type);
3213 flow->tp_dst = htons(icmp_key->icmp_code);
3214 expected_bit = OVS_KEY_ATTR_ICMP;
3216 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3217 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3218 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3220 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3222 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3223 const struct ovs_key_icmpv6 *icmpv6_key;
3225 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3226 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3227 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3228 expected_bit = OVS_KEY_ATTR_ICMPV6;
3229 if (src_flow->tp_dst == htons(0) &&
3230 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3231 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3233 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3235 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3236 const struct ovs_key_nd *nd_key;
3238 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3239 memcpy(&flow->nd_target, nd_key->nd_target,
3240 sizeof flow->nd_target);
3241 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3242 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3244 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3245 (flow->tp_src != htons(0xffff) ||
3246 flow->tp_dst != htons(0xffff))) {
3247 return ODP_FIT_ERROR;
3249 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3256 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3257 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3258 return ODP_FIT_ERROR;
3260 expected_attrs |= UINT64_C(1) << expected_bit;
3265 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3269 /* Parse 802.1Q header then encapsulated L3 attributes. */
3270 static enum odp_key_fitness
3271 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3272 uint64_t present_attrs, int out_of_range_attr,
3273 uint64_t expected_attrs, struct flow *flow,
3274 const struct nlattr *key, size_t key_len,
3275 const struct flow *src_flow)
3277 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3278 bool is_mask = src_flow != flow;
3280 const struct nlattr *encap
3281 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3282 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3283 enum odp_key_fitness encap_fitness;
3284 enum odp_key_fitness fitness;
3286 /* Calculate fitness of outer attributes. */
3288 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3289 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3291 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3292 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3294 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3295 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3298 fitness = check_expectations(present_attrs, out_of_range_attr,
3299 expected_attrs, key, key_len);
3302 * Remove the TPID from dl_type since it's not the real Ethertype. */
3303 flow->dl_type = htons(0);
3304 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3305 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3308 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3309 return ODP_FIT_TOO_LITTLE;
3310 } else if (flow->vlan_tci == htons(0)) {
3311 /* Corner case for a truncated 802.1Q header. */
3312 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3313 return ODP_FIT_TOO_MUCH;
3316 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3317 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3318 "but CFI bit is not set", ntohs(flow->vlan_tci));
3319 return ODP_FIT_ERROR;
3322 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3327 /* Now parse the encapsulated attributes. */
3328 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3329 attrs, &present_attrs, &out_of_range_attr)) {
3330 return ODP_FIT_ERROR;
3334 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3335 return ODP_FIT_ERROR;
3337 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3338 expected_attrs, flow, key, key_len,
3341 /* The overall fitness is the worse of the outer and inner attributes. */
3342 return MAX(fitness, encap_fitness);
3345 static enum odp_key_fitness
3346 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3347 struct flow *flow, const struct flow *src_flow)
3349 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3350 uint64_t expected_attrs;
3351 uint64_t present_attrs;
3352 int out_of_range_attr;
3353 bool is_mask = src_flow != flow;
3355 memset(flow, 0, sizeof *flow);
3357 /* Parse attributes. */
3358 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3359 &out_of_range_attr)) {
3360 return ODP_FIT_ERROR;
3365 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3366 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3367 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3368 } else if (is_mask) {
3369 /* Always exact match recirc_id if it is not specified. */
3370 flow->recirc_id = UINT32_MAX;
3373 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3374 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3375 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3377 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3378 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3379 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3382 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3383 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3384 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3387 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3388 enum odp_key_fitness res;
3390 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3391 if (res == ODP_FIT_ERROR) {
3392 return ODP_FIT_ERROR;
3393 } else if (res == ODP_FIT_PERFECT) {
3394 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3398 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3399 flow->in_port.odp_port
3400 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3401 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3402 } else if (!is_mask) {
3403 flow->in_port.odp_port = ODPP_NONE;
3406 /* Ethernet header. */
3407 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3408 const struct ovs_key_ethernet *eth_key;
3410 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3411 put_ethernet_key(eth_key, flow);
3413 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3417 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3420 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3421 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3423 return ODP_FIT_ERROR;
3427 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3428 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3429 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3430 expected_attrs, flow, key, key_len, src_flow);
3433 flow->vlan_tci = htons(0xffff);
3434 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3435 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3436 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3439 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3440 expected_attrs, flow, key, key_len, src_flow);
3443 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3444 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3445 * 'key' fits our expectations for what a flow key should contain.
3447 * The 'in_port' will be the datapath's understanding of the port. The
3448 * caller will need to translate with odp_port_to_ofp_port() if the
3449 * OpenFlow port is needed.
3451 * This function doesn't take the packet itself as an argument because none of
3452 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3453 * it is always possible to infer which additional attribute(s) should appear
3454 * by looking at the attributes for lower-level protocols, e.g. if the network
3455 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3456 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3457 * must be absent. */
3458 enum odp_key_fitness
3459 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3462 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3465 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3466 * structure in 'mask'. 'flow' must be a previously translated flow
3467 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3468 * 'key' fits our expectations for what a flow key should contain. */
3469 enum odp_key_fitness
3470 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3471 struct flow *mask, const struct flow *flow)
3473 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3476 /* Returns 'fitness' as a string, for use in debug messages. */
3478 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3481 case ODP_FIT_PERFECT:
3483 case ODP_FIT_TOO_MUCH:
3485 case ODP_FIT_TOO_LITTLE:
3486 return "too_little";
3494 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3495 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3496 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3497 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3498 * null, then the return value is not meaningful.) */
3500 odp_put_userspace_action(uint32_t pid,
3501 const void *userdata, size_t userdata_size,
3502 odp_port_t tunnel_out_port,
3503 struct ofpbuf *odp_actions)
3505 size_t userdata_ofs;
3508 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3509 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3511 userdata_ofs = ofpbuf_size(odp_actions) + NLA_HDRLEN;
3513 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3514 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3517 * - The kernel rejected shorter userdata with -ERANGE.
3519 * - The kernel silently dropped userdata beyond the first 8 bytes.
3521 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3522 * separately disable features that required more than 8 bytes.) */
3523 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3524 MAX(8, userdata_size)),
3525 userdata, userdata_size);
3529 if (tunnel_out_port != ODPP_NONE) {
3530 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3533 nl_msg_end_nested(odp_actions, offset);
3535 return userdata_ofs;
3539 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3540 struct ofpbuf *odp_actions)
3542 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3543 tun_key_to_attr(odp_actions, tunnel);
3544 nl_msg_end_nested(odp_actions, offset);
3547 /* The commit_odp_actions() function and its helpers. */
3550 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3551 const void *key, size_t key_size)
3553 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3554 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3555 nl_msg_end_nested(odp_actions, offset);
3558 /* Masked set actions have a mask following the data within the netlink
3559 * attribute. The unmasked bits in the data will be cleared as the data
3560 * is copied to the action. */
3562 commit_masked_set_action(struct ofpbuf *odp_actions,
3563 enum ovs_key_attr key_type,
3564 const void *key_, const void *mask_, size_t key_size)
3566 size_t offset = nl_msg_start_nested(odp_actions,
3567 OVS_ACTION_ATTR_SET_MASKED);
3568 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3569 const char *key = key_, *mask = mask_;
3571 memcpy(data + key_size, mask, key_size);
3572 /* Clear unmasked bits while copying. */
3573 while (key_size--) {
3574 *data++ = *key++ & *mask++;
3576 nl_msg_end_nested(odp_actions, offset);
3579 /* If any of the flow key data that ODP actions can modify are different in
3580 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3581 * 'odp_actions' that change the flow tunneling information in key from
3582 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3583 * same way. In other words, operates the same as commit_odp_actions(), but
3584 * only on tunneling information. */
3586 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3587 struct ofpbuf *odp_actions)
3589 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3590 if (flow->tunnel.ip_dst) {
3591 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3594 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3595 odp_put_tunnel_action(&base->tunnel, odp_actions);
3600 commit(enum ovs_key_attr attr, bool use_masked_set,
3601 const void *key, void *base, void *mask, size_t size,
3602 struct ofpbuf *odp_actions)
3604 if (memcmp(key, base, size)) {
3605 bool fully_masked = odp_mask_is_exact(attr, mask, size);
3607 if (use_masked_set && !fully_masked) {
3608 commit_masked_set_action(odp_actions, attr, key, mask, size);
3610 if (!fully_masked) {
3611 memset(mask, 0xff, size);
3613 commit_set_action(odp_actions, attr, key, size);
3615 memcpy(base, key, size);
3618 /* Mask bits are set when we have either read or set the corresponding
3619 * values. Masked bits will be exact-matched, no need to set them
3620 * if the value did not actually change. */
3626 get_ethernet_key(const struct flow *flow, struct ovs_key_ethernet *eth)
3628 memcpy(eth->eth_src, flow->dl_src, ETH_ADDR_LEN);
3629 memcpy(eth->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
3633 put_ethernet_key(const struct ovs_key_ethernet *eth, struct flow *flow)
3635 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
3636 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
3640 commit_set_ether_addr_action(const struct flow *flow, struct flow *base_flow,
3641 struct ofpbuf *odp_actions,
3642 struct flow_wildcards *wc,
3645 struct ovs_key_ethernet key, base, mask;
3647 get_ethernet_key(flow, &key);
3648 get_ethernet_key(base_flow, &base);
3649 get_ethernet_key(&wc->masks, &mask);
3651 if (commit(OVS_KEY_ATTR_ETHERNET, use_masked,
3652 &key, &base, &mask, sizeof key, odp_actions)) {
3653 put_ethernet_key(&base, base_flow);
3654 put_ethernet_key(&mask, &wc->masks);
3659 pop_vlan(struct flow *base,
3660 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3662 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3664 if (base->vlan_tci & htons(VLAN_CFI)) {
3665 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3671 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
3672 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3674 if (base->vlan_tci == vlan_tci) {
3678 pop_vlan(base, odp_actions, wc);
3679 if (vlan_tci & htons(VLAN_CFI)) {
3680 struct ovs_action_push_vlan vlan;
3682 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3683 vlan.vlan_tci = vlan_tci;
3684 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3685 &vlan, sizeof vlan);
3687 base->vlan_tci = vlan_tci;
3690 /* Wildcarding already done at action translation time. */
3692 commit_mpls_action(const struct flow *flow, struct flow *base,
3693 struct ofpbuf *odp_actions)
3695 int base_n = flow_count_mpls_labels(base, NULL);
3696 int flow_n = flow_count_mpls_labels(flow, NULL);
3697 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
3700 while (base_n > common_n) {
3701 if (base_n - 1 == common_n && flow_n > common_n) {
3702 /* If there is only one more LSE in base than there are common
3703 * between base and flow; and flow has at least one more LSE than
3704 * is common then the topmost LSE of base may be updated using
3706 struct ovs_key_mpls mpls_key;
3708 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
3709 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3710 &mpls_key, sizeof mpls_key);
3711 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
3714 /* Otherwise, if there more LSEs in base than are common between
3715 * base and flow then pop the topmost one. */
3719 /* If all the LSEs are to be popped and this is not the outermost
3720 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
3721 * POP_MPLS action instead of flow->dl_type.
3723 * This is because the POP_MPLS action requires its ethertype
3724 * argument to be an MPLS ethernet type but in this case
3725 * flow->dl_type will be a non-MPLS ethernet type.
3727 * When the final POP_MPLS action occurs it use flow->dl_type and
3728 * the and the resulting packet will have the desired dl_type. */
3729 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
3730 dl_type = htons(ETH_TYPE_MPLS);
3732 dl_type = flow->dl_type;
3734 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
3735 popped = flow_pop_mpls(base, base_n, flow->dl_type, NULL);
3741 /* If, after the above popping and setting, there are more LSEs in flow
3742 * than base then some LSEs need to be pushed. */
3743 while (base_n < flow_n) {
3744 struct ovs_action_push_mpls *mpls;
3746 mpls = nl_msg_put_unspec_zero(odp_actions,
3747 OVS_ACTION_ATTR_PUSH_MPLS,
3749 mpls->mpls_ethertype = flow->dl_type;
3750 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
3751 flow_push_mpls(base, base_n, mpls->mpls_ethertype, NULL);
3752 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
3758 get_ipv4_key(const struct flow *flow, struct ovs_key_ipv4 *ipv4, bool is_mask)
3760 ipv4->ipv4_src = flow->nw_src;
3761 ipv4->ipv4_dst = flow->nw_dst;
3762 ipv4->ipv4_proto = flow->nw_proto;
3763 ipv4->ipv4_tos = flow->nw_tos;
3764 ipv4->ipv4_ttl = flow->nw_ttl;
3765 ipv4->ipv4_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
3769 put_ipv4_key(const struct ovs_key_ipv4 *ipv4, struct flow *flow, bool is_mask)
3771 flow->nw_src = ipv4->ipv4_src;
3772 flow->nw_dst = ipv4->ipv4_dst;
3773 flow->nw_proto = ipv4->ipv4_proto;
3774 flow->nw_tos = ipv4->ipv4_tos;
3775 flow->nw_ttl = ipv4->ipv4_ttl;
3776 flow->nw_frag = odp_to_ovs_frag(ipv4->ipv4_frag, is_mask);
3780 commit_set_ipv4_action(const struct flow *flow, struct flow *base_flow,
3781 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
3784 struct ovs_key_ipv4 key, mask, base;
3786 /* Check that nw_proto and nw_frag remain unchanged. */
3787 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
3788 flow->nw_frag == base_flow->nw_frag);
3790 get_ipv4_key(flow, &key, false);
3791 get_ipv4_key(base_flow, &base, false);
3792 get_ipv4_key(&wc->masks, &mask, true);
3793 mask.ipv4_proto = 0; /* Not writeable. */
3794 mask.ipv4_frag = 0; /* Not writable. */
3796 if (commit(OVS_KEY_ATTR_IPV4, use_masked, &key, &base, &mask, sizeof key,
3798 put_ipv4_key(&base, base_flow, false);
3799 if (mask.ipv4_proto != 0) { /* Mask was changed by commit(). */
3800 put_ipv4_key(&mask, &wc->masks, true);
3806 get_ipv6_key(const struct flow *flow, struct ovs_key_ipv6 *ipv6, bool is_mask)
3808 memcpy(ipv6->ipv6_src, &flow->ipv6_src, sizeof ipv6->ipv6_src);
3809 memcpy(ipv6->ipv6_dst, &flow->ipv6_dst, sizeof ipv6->ipv6_dst);
3810 ipv6->ipv6_label = flow->ipv6_label;
3811 ipv6->ipv6_proto = flow->nw_proto;
3812 ipv6->ipv6_tclass = flow->nw_tos;
3813 ipv6->ipv6_hlimit = flow->nw_ttl;
3814 ipv6->ipv6_frag = ovs_to_odp_frag(flow->nw_frag, is_mask);
3818 put_ipv6_key(const struct ovs_key_ipv6 *ipv6, struct flow *flow, bool is_mask)
3820 memcpy(&flow->ipv6_src, ipv6->ipv6_src, sizeof flow->ipv6_src);
3821 memcpy(&flow->ipv6_dst, ipv6->ipv6_dst, sizeof flow->ipv6_dst);
3822 flow->ipv6_label = ipv6->ipv6_label;
3823 flow->nw_proto = ipv6->ipv6_proto;
3824 flow->nw_tos = ipv6->ipv6_tclass;
3825 flow->nw_ttl = ipv6->ipv6_hlimit;
3826 flow->nw_frag = odp_to_ovs_frag(ipv6->ipv6_frag, is_mask);
3830 commit_set_ipv6_action(const struct flow *flow, struct flow *base_flow,
3831 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
3834 struct ovs_key_ipv6 key, mask, base;
3836 /* Check that nw_proto and nw_frag remain unchanged. */
3837 ovs_assert(flow->nw_proto == base_flow->nw_proto &&
3838 flow->nw_frag == base_flow->nw_frag);
3840 get_ipv6_key(flow, &key, false);
3841 get_ipv6_key(base_flow, &base, false);
3842 get_ipv6_key(&wc->masks, &mask, true);
3843 mask.ipv6_proto = 0; /* Not writeable. */
3844 mask.ipv6_frag = 0; /* Not writable. */
3846 if (commit(OVS_KEY_ATTR_IPV6, use_masked, &key, &base, &mask, sizeof key,
3848 put_ipv6_key(&base, base_flow, false);
3849 if (mask.ipv6_proto != 0) { /* Mask was changed by commit(). */
3850 put_ipv6_key(&mask, &wc->masks, true);
3856 get_arp_key(const struct flow *flow, struct ovs_key_arp *arp)
3858 /* ARP key has padding, clear it. */
3859 memset(arp, 0, sizeof *arp);
3861 arp->arp_sip = flow->nw_src;
3862 arp->arp_tip = flow->nw_dst;
3863 arp->arp_op = htons(flow->nw_proto);
3864 memcpy(arp->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3865 memcpy(arp->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3869 put_arp_key(const struct ovs_key_arp *arp, struct flow *flow)
3871 flow->nw_src = arp->arp_sip;
3872 flow->nw_dst = arp->arp_tip;
3873 flow->nw_proto = ntohs(arp->arp_op);
3874 memcpy(flow->arp_sha, arp->arp_sha, ETH_ADDR_LEN);
3875 memcpy(flow->arp_tha, arp->arp_tha, ETH_ADDR_LEN);
3878 static enum slow_path_reason
3879 commit_set_arp_action(const struct flow *flow, struct flow *base_flow,
3880 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3882 struct ovs_key_arp key, mask, base;
3884 get_arp_key(flow, &key);
3885 get_arp_key(base_flow, &base);
3886 get_arp_key(&wc->masks, &mask);
3888 if (commit(OVS_KEY_ATTR_ARP, true, &key, &base, &mask, sizeof key,
3890 put_arp_key(&base, base_flow);
3891 put_arp_key(&mask, &wc->masks);
3897 static enum slow_path_reason
3898 commit_set_nw_action(const struct flow *flow, struct flow *base,
3899 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
3902 /* Check if 'flow' really has an L3 header. */
3903 if (!flow->nw_proto) {
3907 switch (ntohs(base->dl_type)) {
3909 commit_set_ipv4_action(flow, base, odp_actions, wc, use_masked);
3913 commit_set_ipv6_action(flow, base, odp_actions, wc, use_masked);
3917 return commit_set_arp_action(flow, base, odp_actions, wc);
3923 /* TCP, UDP, and SCTP keys have the same layout. */
3924 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_udp) &&
3925 sizeof(struct ovs_key_tcp) == sizeof(struct ovs_key_sctp));
3928 get_tp_key(const struct flow *flow, union ovs_key_tp *tp)
3930 tp->tcp.tcp_src = flow->tp_src;
3931 tp->tcp.tcp_dst = flow->tp_dst;
3935 put_tp_key(const union ovs_key_tp *tp, struct flow *flow)
3937 flow->tp_src = tp->tcp.tcp_src;
3938 flow->tp_dst = tp->tcp.tcp_dst;
3942 commit_set_port_action(const struct flow *flow, struct flow *base_flow,
3943 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
3946 enum ovs_key_attr key_type;
3947 union ovs_key_tp key, mask, base;
3949 /* Check if 'flow' really has an L3 header. */
3950 if (!flow->nw_proto) {
3954 if (!is_ip_any(base_flow)) {
3958 if (flow->nw_proto == IPPROTO_TCP) {
3959 key_type = OVS_KEY_ATTR_TCP;
3960 } else if (flow->nw_proto == IPPROTO_UDP) {
3961 key_type = OVS_KEY_ATTR_UDP;
3962 } else if (flow->nw_proto == IPPROTO_SCTP) {
3963 key_type = OVS_KEY_ATTR_SCTP;
3968 get_tp_key(flow, &key);
3969 get_tp_key(base_flow, &base);
3970 get_tp_key(&wc->masks, &mask);
3972 if (commit(key_type, use_masked, &key, &base, &mask, sizeof key,
3974 put_tp_key(&base, base_flow);
3975 put_tp_key(&mask, &wc->masks);
3980 commit_set_priority_action(const struct flow *flow, struct flow *base_flow,
3981 struct ofpbuf *odp_actions,
3982 struct flow_wildcards *wc,
3985 uint32_t key, mask, base;
3987 key = flow->skb_priority;
3988 base = base_flow->skb_priority;
3989 mask = wc->masks.skb_priority;
3991 if (commit(OVS_KEY_ATTR_PRIORITY, use_masked, &key, &base, &mask,
3992 sizeof key, odp_actions)) {
3993 base_flow->skb_priority = base;
3994 wc->masks.skb_priority = mask;
3999 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base_flow,
4000 struct ofpbuf *odp_actions,
4001 struct flow_wildcards *wc,
4004 uint32_t key, mask, base;
4006 key = flow->pkt_mark;
4007 base = base_flow->pkt_mark;
4008 mask = wc->masks.pkt_mark;
4010 if (commit(OVS_KEY_ATTR_SKB_MARK, use_masked, &key, &base, &mask,
4011 sizeof key, odp_actions)) {
4012 base_flow->pkt_mark = base;
4013 wc->masks.pkt_mark = mask;
4017 /* If any of the flow key data that ODP actions can modify are different in
4018 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4019 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4020 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4021 * in addition to this function if needed. Sets fields in 'wc' that are
4022 * used as part of the action.
4024 * Returns a reason to force processing the flow's packets into the userspace
4025 * slow path, if there is one, otherwise 0. */
4026 enum slow_path_reason
4027 commit_odp_actions(const struct flow *flow, struct flow *base,
4028 struct ofpbuf *odp_actions, struct flow_wildcards *wc,
4031 enum slow_path_reason slow;
4033 commit_set_ether_addr_action(flow, base, odp_actions, wc, use_masked);
4034 slow = commit_set_nw_action(flow, base, odp_actions, wc, use_masked);
4035 commit_set_port_action(flow, base, odp_actions, wc, use_masked);
4036 commit_mpls_action(flow, base, odp_actions);
4037 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4038 commit_set_priority_action(flow, base, odp_actions, wc, use_masked);
4039 commit_set_pkt_mark_action(flow, base, odp_actions, wc, use_masked);