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);
768 if (!strncmp(s, "userspace(", 10)) {
769 return parse_odp_userspace_action(s, actions);
772 if (!strncmp(s, "set(", 4)) {
775 struct nlattr mask[128 / sizeof(struct nlattr)];
776 struct ofpbuf maskbuf;
777 struct nlattr *nested, *key;
780 /* 'mask' is big enough to hold any key. */
781 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
783 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
784 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
788 if (s[retval + 4] != ')') {
792 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
795 size = nl_attr_get_size(mask);
796 if (size == nl_attr_get_size(key)) {
797 /* Change to masked set action if not fully masked. */
798 if (!is_all_ones(mask + 1, size)) {
799 key->nla_len += size;
800 ofpbuf_put(actions, mask + 1, size);
801 /* 'actions' may have been reallocated by ofpbuf_put(). */
802 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
803 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
807 nl_msg_end_nested(actions, start_ofs);
812 struct ovs_action_push_vlan push;
813 int tpid = ETH_TYPE_VLAN;
818 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
819 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
820 &vid, &pcp, &cfi, &n)
821 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
822 &tpid, &vid, &pcp, &n)
823 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
824 &tpid, &vid, &pcp, &cfi, &n)) {
825 push.vlan_tpid = htons(tpid);
826 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
827 | (pcp << VLAN_PCP_SHIFT)
828 | (cfi ? VLAN_CFI : 0));
829 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
836 if (!strncmp(s, "pop_vlan", 8)) {
837 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
845 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
846 && percentage >= 0. && percentage <= 100.0) {
847 size_t sample_ofs, actions_ofs;
850 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
851 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
852 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
853 (probability <= 0 ? 0
854 : probability >= UINT32_MAX ? UINT32_MAX
857 actions_ofs = nl_msg_start_nested(actions,
858 OVS_SAMPLE_ATTR_ACTIONS);
862 n += strspn(s + n, delimiters);
867 retval = parse_odp_action(s + n, port_names, actions);
873 nl_msg_end_nested(actions, actions_ofs);
874 nl_msg_end_nested(actions, sample_ofs);
876 return s[n + 1] == ')' ? n + 2 : -EINVAL;
883 /* Parses the string representation of datapath actions, in the format output
884 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
885 * value. On success, the ODP actions are appended to 'actions' as a series of
886 * Netlink attributes. On failure, no data is appended to 'actions'. Either
887 * way, 'actions''s data might be reallocated. */
889 odp_actions_from_string(const char *s, const struct simap *port_names,
890 struct ofpbuf *actions)
894 if (!strcasecmp(s, "drop")) {
898 old_size = ofpbuf_size(actions);
902 s += strspn(s, delimiters);
907 retval = parse_odp_action(s, port_names, actions);
908 if (retval < 0 || !strchr(delimiters, s[retval])) {
909 ofpbuf_set_size(actions, old_size);
918 /* Returns the correct length of the payload for a flow key attribute of the
919 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
920 * is variable length. */
922 odp_flow_key_attr_len(uint16_t type)
924 if (type > OVS_KEY_ATTR_MAX) {
928 switch ((enum ovs_key_attr) type) {
929 case OVS_KEY_ATTR_ENCAP: return -2;
930 case OVS_KEY_ATTR_PRIORITY: return 4;
931 case OVS_KEY_ATTR_SKB_MARK: return 4;
932 case OVS_KEY_ATTR_DP_HASH: return 4;
933 case OVS_KEY_ATTR_RECIRC_ID: return 4;
934 case OVS_KEY_ATTR_TUNNEL: return -2;
935 case OVS_KEY_ATTR_IN_PORT: return 4;
936 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
937 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
938 case OVS_KEY_ATTR_ETHERTYPE: return 2;
939 case OVS_KEY_ATTR_MPLS: return -2;
940 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
941 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
942 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
943 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
944 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
945 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
946 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
947 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
948 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
949 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
951 case OVS_KEY_ATTR_UNSPEC:
952 case __OVS_KEY_ATTR_MAX:
960 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
962 size_t len = nl_attr_get_size(a);
964 const uint8_t *unspec;
967 unspec = nl_attr_get(a);
968 for (i = 0; i < len; i++) {
970 ds_put_char(ds, ' ');
972 ds_put_format(ds, "%02x", unspec[i]);
978 ovs_frag_type_to_string(enum ovs_frag_type type)
981 case OVS_FRAG_TYPE_NONE:
983 case OVS_FRAG_TYPE_FIRST:
985 case OVS_FRAG_TYPE_LATER:
987 case __OVS_FRAG_TYPE_MAX:
994 tunnel_key_attr_len(int type)
997 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
998 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
999 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
1000 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
1001 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
1002 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
1003 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
1004 case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
1005 case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
1006 case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
1007 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
1008 case __OVS_TUNNEL_KEY_ATTR_MAX:
1014 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
1016 parse_geneve_opts(const struct nlattr *attr)
1018 int opts_len = nl_attr_get_size(attr);
1019 const struct geneve_opt *opt = nl_attr_get(attr);
1021 while (opts_len > 0) {
1024 if (opts_len < sizeof(*opt)) {
1028 len = sizeof(*opt) + opt->length * 4;
1029 if (len > opts_len) {
1033 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
1035 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
1040 opt = opt + len / sizeof(*opt);
1047 enum odp_key_fitness
1048 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
1051 const struct nlattr *a;
1053 bool unknown = false;
1055 NL_NESTED_FOR_EACH(a, left, attr) {
1056 uint16_t type = nl_attr_type(a);
1057 size_t len = nl_attr_get_size(a);
1058 int expected_len = tunnel_key_attr_len(type);
1060 if (len != expected_len && expected_len >= 0) {
1061 return ODP_FIT_ERROR;
1065 case OVS_TUNNEL_KEY_ATTR_ID:
1066 tun->tun_id = nl_attr_get_be64(a);
1067 tun->flags |= FLOW_TNL_F_KEY;
1069 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1070 tun->ip_src = nl_attr_get_be32(a);
1072 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1073 tun->ip_dst = nl_attr_get_be32(a);
1075 case OVS_TUNNEL_KEY_ATTR_TOS:
1076 tun->ip_tos = nl_attr_get_u8(a);
1078 case OVS_TUNNEL_KEY_ATTR_TTL:
1079 tun->ip_ttl = nl_attr_get_u8(a);
1082 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1083 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
1085 case OVS_TUNNEL_KEY_ATTR_CSUM:
1086 tun->flags |= FLOW_TNL_F_CSUM;
1088 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1089 tun->tp_src = nl_attr_get_be16(a);
1091 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1092 tun->tp_dst = nl_attr_get_be16(a);
1094 case OVS_TUNNEL_KEY_ATTR_OAM:
1095 tun->flags |= FLOW_TNL_F_OAM;
1097 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1098 if (parse_geneve_opts(a)) {
1099 return ODP_FIT_ERROR;
1101 /* It is necessary to reproduce options exactly (including order)
1102 * so it's easiest to just echo them back. */
1107 /* Allow this to show up as unexpected, if there are unknown
1108 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1115 return ODP_FIT_ERROR;
1118 return ODP_FIT_TOO_MUCH;
1120 return ODP_FIT_PERFECT;
1124 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1128 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1130 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1131 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1132 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1134 if (tun_key->ip_src) {
1135 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1137 if (tun_key->ip_dst) {
1138 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1140 if (tun_key->ip_tos) {
1141 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1143 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1144 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1145 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1147 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1148 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1150 if (tun_key->tp_src) {
1151 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1153 if (tun_key->tp_dst) {
1154 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1156 if (tun_key->flags & FLOW_TNL_F_OAM) {
1157 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1160 nl_msg_end_nested(a, tun_key_ofs);
1164 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1166 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1170 odp_mask_attr_is_exact(const struct nlattr *ma)
1173 enum ovs_key_attr attr = nl_attr_type(ma);
1175 if (attr == OVS_KEY_ATTR_TCP_FLAGS) {
1176 is_exact = TCP_FLAGS(nl_attr_get_be16(ma)) == TCP_FLAGS(OVS_BE16_MAX);
1177 } else if (attr == OVS_KEY_ATTR_IPV6) {
1178 const struct ovs_key_ipv6 *mask = nl_attr_get(ma);
1181 ((mask->ipv6_label & htonl(IPV6_LABEL_MASK))
1182 == htonl(IPV6_LABEL_MASK))
1183 && mask->ipv6_proto == UINT8_MAX
1184 && mask->ipv6_tclass == UINT8_MAX
1185 && mask->ipv6_hlimit == UINT8_MAX
1186 && mask->ipv6_frag == UINT8_MAX
1187 && ipv6_mask_is_exact((const struct in6_addr *)mask->ipv6_src)
1188 && ipv6_mask_is_exact((const struct in6_addr *)mask->ipv6_dst);
1189 } else if (attr == OVS_KEY_ATTR_TUNNEL) {
1190 struct flow_tnl tun_mask;
1192 memset(&tun_mask, 0, sizeof tun_mask);
1193 odp_tun_key_from_attr(ma, &tun_mask);
1194 is_exact = tun_mask.flags == FLOW_TNL_F_MASK
1195 && tun_mask.tun_id == OVS_BE64_MAX
1196 && tun_mask.ip_src == OVS_BE32_MAX
1197 && tun_mask.ip_dst == OVS_BE32_MAX
1198 && tun_mask.ip_tos == UINT8_MAX
1199 && tun_mask.ip_ttl == UINT8_MAX
1200 && tun_mask.tp_src == OVS_BE16_MAX
1201 && tun_mask.tp_dst == OVS_BE16_MAX;
1203 is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
1210 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1213 struct odp_portno_names *odp_portno_names;
1215 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1216 odp_portno_names->port_no = port_no;
1217 odp_portno_names->name = xstrdup(port_name);
1218 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1219 hash_odp_port(port_no));
1223 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1225 struct odp_portno_names *odp_portno_names;
1227 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1228 hash_odp_port(port_no), portno_names) {
1229 if (odp_portno_names->port_no == port_no) {
1230 return odp_portno_names->name;
1237 odp_portno_names_destroy(struct hmap *portno_names)
1239 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1240 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1241 hmap_node, portno_names) {
1242 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1243 free(odp_portno_names->name);
1244 free(odp_portno_names);
1248 /* Format helpers. */
1251 format_eth(struct ds *ds, const char *name, const uint8_t key[ETH_ADDR_LEN],
1252 const uint8_t (*mask)[ETH_ADDR_LEN], bool verbose)
1254 bool mask_empty = mask && eth_addr_is_zero(*mask);
1256 if (verbose || !mask_empty) {
1257 bool mask_full = !mask || eth_mask_is_exact(*mask);
1260 ds_put_format(ds, "%s="ETH_ADDR_FMT",", name, ETH_ADDR_ARGS(key));
1262 ds_put_format(ds, "%s=", name);
1263 eth_format_masked(key, *mask, ds);
1264 ds_put_char(ds, ',');
1270 format_be64(struct ds *ds, const char *name, ovs_be64 key,
1271 const ovs_be64 *mask, bool verbose)
1273 bool mask_empty = mask && !*mask;
1275 if (verbose || !mask_empty) {
1276 bool mask_full = !mask || *mask == OVS_BE64_MAX;
1278 ds_put_format(ds, "%s=0x%"PRIx64, name, ntohll(key));
1279 if (!mask_full) { /* Partially masked. */
1280 ds_put_format(ds, "/%#"PRIx64, ntohll(*mask));
1282 ds_put_char(ds, ',');
1287 format_ipv4(struct ds *ds, const char *name, ovs_be32 key,
1288 const ovs_be32 *mask, bool verbose)
1290 bool mask_empty = mask && !*mask;
1292 if (verbose || !mask_empty) {
1293 bool mask_full = !mask || *mask == OVS_BE32_MAX;
1295 ds_put_format(ds, "%s="IP_FMT, name, IP_ARGS(key));
1296 if (!mask_full) { /* Partially masked. */
1297 ds_put_format(ds, "/"IP_FMT, IP_ARGS(*mask));
1299 ds_put_char(ds, ',');
1304 format_ipv6(struct ds *ds, const char *name, const ovs_be32 key_[4],
1305 const ovs_be32 (*mask_)[4], bool verbose)
1307 char buf[INET6_ADDRSTRLEN];
1308 const struct in6_addr *key = (const struct in6_addr *)key_;
1309 const struct in6_addr *mask = mask_ ? (const struct in6_addr *)*mask_
1311 bool mask_empty = mask && ipv6_mask_is_any(mask);
1313 if (verbose || !mask_empty) {
1314 bool mask_full = !mask || ipv6_mask_is_exact(mask);
1316 inet_ntop(AF_INET6, key, buf, sizeof buf);
1317 ds_put_format(ds, "%s=%s", name, buf);
1318 if (!mask_full) { /* Partially masked. */
1319 inet_ntop(AF_INET6, mask, buf, sizeof buf);
1320 ds_put_format(ds, "/%s", buf);
1322 ds_put_char(ds, ',');
1327 format_ipv6_label(struct ds *ds, const char *name, ovs_be32 key,
1328 const ovs_be32 *mask, bool verbose)
1330 bool mask_empty = mask && !*mask;
1332 if (verbose || !mask_empty) {
1333 bool mask_full = !mask
1334 || (*mask & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK);
1336 ds_put_format(ds, "%s=%#"PRIx32, name, ntohl(key));
1337 if (!mask_full) { /* Partially masked. */
1338 ds_put_format(ds, "/%#"PRIx32, ntohl(*mask));
1340 ds_put_char(ds, ',');
1345 format_u8x(struct ds *ds, const char *name, uint8_t key,
1346 const uint8_t *mask, bool verbose)
1348 bool mask_empty = mask && !*mask;
1350 if (verbose || !mask_empty) {
1351 bool mask_full = !mask || *mask == UINT8_MAX;
1353 ds_put_format(ds, "%s=%#"PRIx8, name, key);
1354 if (!mask_full) { /* Partially masked. */
1355 ds_put_format(ds, "/%#"PRIx8, *mask);
1357 ds_put_char(ds, ',');
1362 format_u8u(struct ds *ds, const char *name, uint8_t key,
1363 const uint8_t *mask, bool verbose)
1365 bool mask_empty = mask && !*mask;
1367 if (verbose || !mask_empty) {
1368 bool mask_full = !mask || *mask == UINT8_MAX;
1370 ds_put_format(ds, "%s=%"PRIu8, name, key);
1371 if (!mask_full) { /* Partially masked. */
1372 ds_put_format(ds, "/%#"PRIx8, *mask);
1374 ds_put_char(ds, ',');
1379 format_be16(struct ds *ds, const char *name, ovs_be16 key,
1380 const ovs_be16 *mask, bool verbose)
1382 bool mask_empty = mask && !*mask;
1384 if (verbose || !mask_empty) {
1385 bool mask_full = !mask || *mask == OVS_BE16_MAX;
1387 ds_put_format(ds, "%s=%"PRIu16, name, ntohs(key));
1388 if (!mask_full) { /* Partially masked. */
1389 ds_put_format(ds, "/%#"PRIx16, ntohs(*mask));
1391 ds_put_char(ds, ',');
1396 format_tun_flags(struct ds *ds, const char *name, uint16_t key,
1397 const uint16_t *mask, bool verbose)
1399 bool mask_empty = mask && !*mask;
1401 if (verbose || !mask_empty) {
1402 bool mask_full = !mask || (*mask & FLOW_TNL_F_MASK) == FLOW_TNL_F_MASK;
1404 ds_put_cstr(ds, name);
1405 ds_put_char(ds, '(');
1406 if (!mask_full) { /* Partially masked. */
1407 format_flags_masked(ds, NULL, flow_tun_flag_to_string, key, *mask);
1408 } else { /* Fully masked. */
1409 format_flags(ds, flow_tun_flag_to_string, key, ',');
1411 ds_put_cstr(ds, "),");
1416 format_frag(struct ds *ds, const char *name, uint8_t key,
1417 const uint8_t *mask, bool verbose)
1419 bool mask_empty = mask && !*mask;
1421 /* ODP frag is an enumeration field; partial masks are not meaningful. */
1422 if (verbose || !mask_empty) {
1423 bool mask_full = !mask || *mask == UINT8_MAX;
1425 if (!mask_full) { /* Partially masked. */
1426 ds_put_format(ds, "error: partial mask not supported for frag (%#"
1429 ds_put_format(ds, "%s=%s,", name, ovs_frag_type_to_string(key));
1434 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
1437 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1438 const struct hmap *portno_names, struct ds *ds,
1441 enum ovs_key_attr attr = nl_attr_type(a);
1442 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1446 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1448 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1451 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1452 if (expected_len != -2) {
1453 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1454 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1456 if (bad_key_len || bad_mask_len) {
1458 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1459 nl_attr_get_size(a), expected_len);
1461 format_generic_odp_key(a, ds);
1463 ds_put_char(ds, '/');
1465 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1466 nl_attr_get_size(ma), expected_len);
1468 format_generic_odp_key(ma, ds);
1470 ds_put_char(ds, ')');
1476 ds_put_char(ds, '(');
1478 case OVS_KEY_ATTR_ENCAP:
1479 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1480 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1481 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1483 } else if (nl_attr_get_size(a)) {
1484 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1489 case OVS_KEY_ATTR_PRIORITY:
1490 case OVS_KEY_ATTR_SKB_MARK:
1491 case OVS_KEY_ATTR_DP_HASH:
1492 case OVS_KEY_ATTR_RECIRC_ID:
1493 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1495 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1499 case OVS_KEY_ATTR_TUNNEL: {
1500 struct flow_tnl key, mask_;
1501 struct flow_tnl *mask = ma ? &mask_ : NULL;
1504 memset(mask, 0, sizeof *mask);
1505 odp_tun_key_from_attr(ma, mask);
1507 memset(&key, 0, sizeof key);
1508 if (odp_tun_key_from_attr(a, &key) == ODP_FIT_ERROR) {
1509 ds_put_format(ds, "error");
1512 format_be64(ds, "tun_id", key.tun_id, MASK(mask, tun_id), verbose);
1513 format_ipv4(ds, "src", key.ip_src, MASK(mask, ip_src), verbose);
1514 format_ipv4(ds, "dst", key.ip_dst, MASK(mask, ip_dst), verbose);
1515 format_u8x(ds, "tos", key.ip_tos, MASK(mask, ip_tos), verbose);
1516 format_u8u(ds, "ttl", key.ip_ttl, MASK(mask, ip_ttl), verbose);
1517 format_be16(ds, "tp_src", key.tp_src, MASK(mask, tp_src), verbose);
1518 format_be16(ds, "tp_dst", key.tp_dst, MASK(mask, tp_dst), verbose);
1519 format_tun_flags(ds, "flags", key.flags, MASK(mask, flags), verbose);
1523 case OVS_KEY_ATTR_IN_PORT:
1524 if (portno_names && verbose && is_exact) {
1525 char *name = odp_portno_names_get(portno_names,
1526 u32_to_odp(nl_attr_get_u32(a)));
1528 ds_put_format(ds, "%s", name);
1530 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1533 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1535 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1540 case OVS_KEY_ATTR_ETHERNET: {
1541 const struct ovs_key_ethernet *mask = ma ? nl_attr_get(ma) : NULL;
1542 const struct ovs_key_ethernet *key = nl_attr_get(a);
1544 format_eth(ds, "src", key->eth_src, MASK(mask, eth_src), verbose);
1545 format_eth(ds, "dst", key->eth_dst, MASK(mask, eth_dst), verbose);
1549 case OVS_KEY_ATTR_VLAN:
1550 format_vlan_tci(ds, nl_attr_get_be16(a),
1551 ma ? nl_attr_get_be16(ma) : OVS_BE16_MAX, verbose);
1554 case OVS_KEY_ATTR_MPLS: {
1555 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1556 const struct ovs_key_mpls *mpls_mask = NULL;
1557 size_t size = nl_attr_get_size(a);
1559 if (!size || size % sizeof *mpls_key) {
1560 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1564 mpls_mask = nl_attr_get(ma);
1565 if (size != nl_attr_get_size(ma)) {
1566 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1567 "mask length %"PRIuSIZE")",
1568 size, nl_attr_get_size(ma));
1572 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1575 case OVS_KEY_ATTR_ETHERTYPE:
1576 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1578 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1582 case OVS_KEY_ATTR_IPV4: {
1583 const struct ovs_key_ipv4 *key = nl_attr_get(a);
1584 const struct ovs_key_ipv4 *mask = ma ? nl_attr_get(ma) : NULL;
1586 format_ipv4(ds, "src", key->ipv4_src, MASK(mask, ipv4_src), verbose);
1587 format_ipv4(ds, "dst", key->ipv4_dst, MASK(mask, ipv4_dst), verbose);
1588 format_u8u(ds, "proto", key->ipv4_proto, MASK(mask, ipv4_proto),
1590 format_u8x(ds, "tos", key->ipv4_tos, MASK(mask, ipv4_tos), verbose);
1591 format_u8u(ds, "ttl", key->ipv4_ttl, MASK(mask, ipv4_ttl), verbose);
1592 format_frag(ds, "frag", key->ipv4_frag, MASK(mask, ipv4_frag),
1597 case OVS_KEY_ATTR_IPV6: {
1598 const struct ovs_key_ipv6 *key = nl_attr_get(a);
1599 const struct ovs_key_ipv6 *mask = ma ? nl_attr_get(ma) : NULL;
1601 format_ipv6(ds, "src", key->ipv6_src, MASK(mask, ipv6_src), verbose);
1602 format_ipv6(ds, "dst", key->ipv6_dst, MASK(mask, ipv6_dst), verbose);
1603 format_ipv6_label(ds, "label", key->ipv6_label, MASK(mask, ipv6_label),
1605 format_u8u(ds, "proto", key->ipv6_proto, MASK(mask, ipv6_proto),
1607 format_u8x(ds, "tclass", key->ipv6_tclass, MASK(mask, ipv6_tclass),
1609 format_u8u(ds, "hlimit", key->ipv6_hlimit, MASK(mask, ipv6_hlimit),
1611 format_frag(ds, "frag", key->ipv6_frag, MASK(mask, ipv6_frag),
1616 /* These have the same structure and format. */
1617 case OVS_KEY_ATTR_TCP:
1618 case OVS_KEY_ATTR_UDP:
1619 case OVS_KEY_ATTR_SCTP: {
1620 const struct ovs_key_tcp *key = nl_attr_get(a);
1621 const struct ovs_key_tcp *mask = ma ? nl_attr_get(ma) : NULL;
1623 format_be16(ds, "src", key->tcp_src, MASK(mask, tcp_src), verbose);
1624 format_be16(ds, "dst", key->tcp_dst, MASK(mask, tcp_dst), verbose);
1628 case OVS_KEY_ATTR_TCP_FLAGS:
1630 format_flags_masked(ds, NULL, packet_tcp_flag_to_string,
1631 ntohs(nl_attr_get_be16(a)),
1632 ntohs(nl_attr_get_be16(ma)));
1634 format_flags(ds, packet_tcp_flag_to_string,
1635 ntohs(nl_attr_get_be16(a)), ',');
1639 case OVS_KEY_ATTR_ICMP: {
1640 const struct ovs_key_icmp *key = nl_attr_get(a);
1641 const struct ovs_key_icmp *mask = ma ? nl_attr_get(ma) : NULL;
1643 format_u8u(ds, "type", key->icmp_type, MASK(mask, icmp_type), verbose);
1644 format_u8u(ds, "code", key->icmp_code, MASK(mask, icmp_code), verbose);
1648 case OVS_KEY_ATTR_ICMPV6: {
1649 const struct ovs_key_icmpv6 *key = nl_attr_get(a);
1650 const struct ovs_key_icmpv6 *mask = ma ? nl_attr_get(ma) : NULL;
1652 format_u8u(ds, "type", key->icmpv6_type, MASK(mask, icmpv6_type),
1654 format_u8u(ds, "code", key->icmpv6_code, MASK(mask, icmpv6_code),
1659 case OVS_KEY_ATTR_ARP: {
1660 const struct ovs_key_arp *mask = ma ? nl_attr_get(ma) : NULL;
1661 const struct ovs_key_arp *key = nl_attr_get(a);
1663 format_ipv4(ds, "sip", key->arp_sip, MASK(mask, arp_sip), verbose);
1664 format_ipv4(ds, "tip", key->arp_tip, MASK(mask, arp_tip), verbose);
1665 format_be16(ds, "op", key->arp_op, MASK(mask, arp_op), verbose);
1666 format_eth(ds, "sha", key->arp_sha, MASK(mask, arp_sha), verbose);
1667 format_eth(ds, "tha", key->arp_tha, MASK(mask, arp_tha), verbose);
1671 case OVS_KEY_ATTR_ND: {
1672 const struct ovs_key_nd *mask = ma ? nl_attr_get(ma) : NULL;
1673 const struct ovs_key_nd *key = nl_attr_get(a);
1675 format_ipv6(ds, "target", key->nd_target, MASK(mask, nd_target),
1677 format_eth(ds, "sll", key->nd_sll, MASK(mask, nd_sll), verbose);
1678 format_eth(ds, "tll", key->nd_tll, MASK(mask, nd_tll), verbose);
1683 case OVS_KEY_ATTR_UNSPEC:
1684 case __OVS_KEY_ATTR_MAX:
1686 format_generic_odp_key(a, ds);
1688 ds_put_char(ds, '/');
1689 format_generic_odp_key(ma, ds);
1693 ds_put_char(ds, ')');
1696 static struct nlattr *
1697 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1699 const struct nlattr *a;
1701 int type = nl_attr_type(key);
1702 int size = nl_attr_get_size(key);
1704 if (odp_flow_key_attr_len(type) >=0) {
1705 nl_msg_put_unspec_zero(ofp, type, size);
1709 nested_mask = nl_msg_start_nested(ofp, type);
1710 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1711 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1713 nl_msg_end_nested(ofp, nested_mask);
1716 return ofpbuf_base(ofp);
1719 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1720 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1721 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
1722 * non-null and 'verbose' is true, translates odp port number to its name. */
1724 odp_flow_format(const struct nlattr *key, size_t key_len,
1725 const struct nlattr *mask, size_t mask_len,
1726 const struct hmap *portno_names, struct ds *ds, bool verbose)
1729 const struct nlattr *a;
1731 bool has_ethtype_key = false;
1732 const struct nlattr *ma = NULL;
1734 bool first_field = true;
1736 ofpbuf_init(&ofp, 100);
1737 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1738 bool is_nested_attr;
1739 bool is_wildcard = false;
1740 int attr_type = nl_attr_type(a);
1742 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1743 has_ethtype_key = true;
1746 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1748 if (mask && mask_len) {
1749 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1750 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1753 if (verbose || !is_wildcard || is_nested_attr) {
1754 if (is_wildcard && !ma) {
1755 ma = generate_all_wildcard_mask(&ofp, a);
1758 ds_put_char(ds, ',');
1760 format_odp_key_attr(a, ma, portno_names, ds, verbose);
1761 first_field = false;
1765 ofpbuf_uninit(&ofp);
1770 if (left == key_len) {
1771 ds_put_cstr(ds, "<empty>");
1773 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1774 for (i = 0; i < left; i++) {
1775 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1777 ds_put_char(ds, ')');
1779 if (!has_ethtype_key) {
1780 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1782 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1783 ntohs(nl_attr_get_be16(ma)));
1787 ds_put_cstr(ds, "<empty>");
1791 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1792 * OVS_KEY_ATTR_* attributes in 'key'. */
1794 odp_flow_key_format(const struct nlattr *key,
1795 size_t key_len, struct ds *ds)
1797 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
1801 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1803 if (!strcasecmp(s, "no")) {
1804 *type = OVS_FRAG_TYPE_NONE;
1805 } else if (!strcasecmp(s, "first")) {
1806 *type = OVS_FRAG_TYPE_FIRST;
1807 } else if (!strcasecmp(s, "later")) {
1808 *type = OVS_FRAG_TYPE_LATER;
1818 scan_eth(const char *s, uint8_t (*key)[ETH_ADDR_LEN],
1819 uint8_t (*mask)[ETH_ADDR_LEN])
1823 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*key), &n)) {
1827 if (ovs_scan(s + len, "/"ETH_ADDR_SCAN_FMT"%n",
1828 ETH_ADDR_SCAN_ARGS(*mask), &n)) {
1831 memset(mask, 0xff, sizeof *mask);
1840 scan_ipv4(const char *s, ovs_be32 *key, ovs_be32 *mask)
1844 if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(key), &n)) {
1848 if (ovs_scan(s + len, "/"IP_SCAN_FMT"%n",
1849 IP_SCAN_ARGS(mask), &n)) {
1852 *mask = OVS_BE32_MAX;
1861 scan_ipv6(const char *s, ovs_be32 (*key)[4], ovs_be32 (*mask)[4])
1864 char ipv6_s[IPV6_SCAN_LEN + 1];
1866 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &n)
1867 && inet_pton(AF_INET6, ipv6_s, key) == 1) {
1871 if (ovs_scan(s + len, "/"IPV6_SCAN_FMT"%n", ipv6_s, &n)
1872 && inet_pton(AF_INET6, ipv6_s, mask) == 1) {
1875 memset(mask, 0xff, sizeof *mask);
1884 scan_ipv6_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
1889 if (ovs_scan(s, "%i%n", &key_, &n)
1890 && (key_ & ~IPV6_LABEL_MASK) == 0) {
1895 if (ovs_scan(s + len, "/%i%n", &mask_, &n)
1896 && (mask_ & ~IPV6_LABEL_MASK) == 0) {
1898 *mask = htonl(mask_);
1900 *mask = htonl(IPV6_LABEL_MASK);
1909 scan_u8(const char *s, uint8_t *key, uint8_t *mask)
1913 if (ovs_scan(s, "%"SCNi8"%n", key, &n)) {
1917 if (ovs_scan(s + len, "/%"SCNi8"%n", mask, &n)) {
1929 scan_u32(const char *s, uint32_t *key, uint32_t *mask)
1933 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
1937 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
1949 scan_be16(const char *s, ovs_be16 *key, ovs_be16 *mask)
1951 uint16_t key_, mask_;
1954 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
1959 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
1961 *mask = htons(mask_);
1963 *mask = OVS_BE16_MAX;
1972 scan_be64(const char *s, ovs_be64 *key, ovs_be64 *mask)
1974 uint64_t key_, mask_;
1977 if (ovs_scan(s, "%"SCNi64"%n", &key_, &n)) {
1980 *key = htonll(key_);
1982 if (ovs_scan(s + len, "/%"SCNi64"%n", &mask_, &n)) {
1984 *mask = htonll(mask_);
1986 *mask = OVS_BE64_MAX;
1995 scan_tun_flags(const char *s, uint16_t *key, uint16_t *mask)
1997 uint32_t flags, fmask;
2000 n = parse_flags(s, flow_tun_flag_to_string, &flags,
2001 FLOW_TNL_F_MASK, mask ? &fmask : NULL);
2002 if (n >= 0 && s[n] == ')') {
2013 scan_tcp_flags(const char *s, ovs_be16 *key, ovs_be16 *mask)
2015 uint32_t flags, fmask;
2018 n = parse_flags(s, packet_tcp_flag_to_string, &flags,
2019 TCP_FLAGS(OVS_BE16_MAX), mask ? &fmask : NULL);
2021 *key = htons(flags);
2023 *mask = htons(fmask);
2031 scan_frag(const char *s, uint8_t *key, uint8_t *mask)
2035 enum ovs_frag_type frag_type;
2037 if (ovs_scan(s, "%7[a-z]%n", frag, &n)
2038 && ovs_frag_type_from_string(frag, &frag_type)) {
2051 scan_port(const char *s, uint32_t *key, uint32_t *mask,
2052 const struct simap *port_names)
2056 if (ovs_scan(s, "%"SCNi32"%n", key, &n)) {
2060 if (ovs_scan(s + len, "/%"SCNi32"%n", mask, &n)) {
2067 } else if (port_names) {
2068 const struct simap_node *node;
2071 len = strcspn(s, ")");
2072 node = simap_find_len(port_names, s, len);
2085 /* Helper for vlan parsing. */
2086 struct ovs_key_vlan__ {
2091 set_be16_bf(ovs_be16 *bf, uint8_t bits, uint8_t offset, uint16_t value)
2093 const uint16_t mask = ((1U << bits) - 1) << offset;
2095 if (value >> bits) {
2099 *bf = htons((ntohs(*bf) & ~mask) | (value << offset));
2104 scan_be16_bf(const char *s, ovs_be16 *key, ovs_be16 *mask, uint8_t bits,
2107 uint16_t key_, mask_;
2110 if (ovs_scan(s, "%"SCNi16"%n", &key_, &n)) {
2113 if (set_be16_bf(key, bits, offset, key_)) {
2115 if (ovs_scan(s + len, "/%"SCNi16"%n", &mask_, &n)) {
2118 if (!set_be16_bf(mask, bits, offset, mask_)) {
2122 *mask |= htons(((1U << bits) - 1) << offset);
2132 scan_vid(const char *s, ovs_be16 *key, ovs_be16 *mask)
2134 return scan_be16_bf(s, key, mask, 12, VLAN_VID_SHIFT);
2138 scan_pcp(const char *s, ovs_be16 *key, ovs_be16 *mask)
2140 return scan_be16_bf(s, key, mask, 3, VLAN_PCP_SHIFT);
2144 scan_cfi(const char *s, ovs_be16 *key, ovs_be16 *mask)
2146 return scan_be16_bf(s, key, mask, 1, VLAN_CFI_SHIFT);
2151 set_be32_bf(ovs_be32 *bf, uint8_t bits, uint8_t offset, uint32_t value)
2153 const uint32_t mask = ((1U << bits) - 1) << offset;
2155 if (value >> bits) {
2159 *bf = htonl((ntohl(*bf) & ~mask) | (value << offset));
2164 scan_be32_bf(const char *s, ovs_be32 *key, ovs_be32 *mask, uint8_t bits,
2167 uint32_t key_, mask_;
2170 if (ovs_scan(s, "%"SCNi32"%n", &key_, &n)) {
2173 if (set_be32_bf(key, bits, offset, key_)) {
2175 if (ovs_scan(s + len, "/%"SCNi32"%n", &mask_, &n)) {
2178 if (!set_be32_bf(mask, bits, offset, mask_)) {
2182 *mask |= htonl(((1U << bits) - 1) << offset);
2192 scan_mpls_label(const char *s, ovs_be32 *key, ovs_be32 *mask)
2194 return scan_be32_bf(s, key, mask, 20, MPLS_LABEL_SHIFT);
2198 scan_mpls_tc(const char *s, ovs_be32 *key, ovs_be32 *mask)
2200 return scan_be32_bf(s, key, mask, 3, MPLS_TC_SHIFT);
2204 scan_mpls_ttl(const char *s, ovs_be32 *key, ovs_be32 *mask)
2206 return scan_be32_bf(s, key, mask, 8, MPLS_TTL_SHIFT);
2210 scan_mpls_bos(const char *s, ovs_be32 *key, ovs_be32 *mask)
2212 return scan_be32_bf(s, key, mask, 1, MPLS_BOS_SHIFT);
2215 /* ATTR is compile-time constant, so only the case with correct data type
2216 * will be used. However, the compiler complains about the data type for
2217 * the other cases, so we must cast to make the compiler silent. */
2218 #define SCAN_PUT_ATTR(BUF, ATTR, DATA) \
2219 if ((ATTR) == OVS_KEY_ATTR_TUNNEL) { \
2220 tun_key_to_attr(BUF, (const struct flow_tnl *)(void *)&(DATA)); \
2222 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
2225 #define SCAN_IF(NAME) \
2226 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2227 const char *start = s; \
2232 /* Usually no special initialization is needed. */
2233 #define SCAN_BEGIN(NAME, TYPE) \
2236 memset(&skey, 0, sizeof skey); \
2237 memset(&smask, 0, sizeof smask); \
2241 /* VLAN needs special initialization. */
2242 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
2244 TYPE skey = KEY_INIT; \
2245 TYPE smask = MASK_INIT; \
2249 /* Scan unnamed entry as 'TYPE' */
2250 #define SCAN_TYPE(TYPE, KEY, MASK) \
2251 len = scan_##TYPE(s, KEY, MASK); \
2257 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
2258 #define SCAN_FIELD(NAME, TYPE, FIELD) \
2259 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
2260 s += strlen(NAME); \
2261 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
2265 #define SCAN_FINISH() \
2266 } while (*s++ == ',' && len != 0); \
2267 if (s[-1] != ')') { \
2271 #define SCAN_FINISH_SINGLE() \
2273 if (*s++ != ')') { \
2277 #define SCAN_PUT(ATTR) \
2278 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
2279 SCAN_PUT_ATTR(key, ATTR, skey); \
2281 SCAN_PUT_ATTR(mask, ATTR, smask); \
2285 #define SCAN_END(ATTR) \
2291 #define SCAN_END_SINGLE(ATTR) \
2292 SCAN_FINISH_SINGLE(); \
2297 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
2298 SCAN_BEGIN(NAME, TYPE) { \
2299 SCAN_TYPE(SCAN_AS, &skey, &smask); \
2300 } SCAN_END_SINGLE(ATTR)
2302 #define SCAN_SINGLE_NO_MASK(NAME, TYPE, SCAN_AS, ATTR) \
2303 SCAN_BEGIN(NAME, TYPE) { \
2304 SCAN_TYPE(SCAN_AS, &skey, NULL); \
2305 } SCAN_END_SINGLE(ATTR)
2307 /* scan_port needs one extra argument. */
2308 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
2309 SCAN_BEGIN(NAME, TYPE) { \
2310 len = scan_port(s, &skey, &smask, port_names); \
2315 } SCAN_END_SINGLE(ATTR)
2318 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
2319 struct ofpbuf *key, struct ofpbuf *mask)
2321 SCAN_SINGLE("skb_priority(", uint32_t, u32, OVS_KEY_ATTR_PRIORITY);
2322 SCAN_SINGLE("skb_mark(", uint32_t, u32, OVS_KEY_ATTR_SKB_MARK);
2323 SCAN_SINGLE_NO_MASK("recirc_id(", uint32_t, u32, OVS_KEY_ATTR_RECIRC_ID);
2324 SCAN_SINGLE("dp_hash(", uint32_t, u32, OVS_KEY_ATTR_DP_HASH);
2326 SCAN_BEGIN("tunnel(", struct flow_tnl) {
2327 SCAN_FIELD("tun_id=", be64, tun_id);
2328 SCAN_FIELD("src=", ipv4, ip_src);
2329 SCAN_FIELD("dst=", ipv4, ip_dst);
2330 SCAN_FIELD("tos=", u8, ip_tos);
2331 SCAN_FIELD("ttl=", u8, ip_ttl);
2332 SCAN_FIELD("tp_src=", be16, tp_src);
2333 SCAN_FIELD("tp_dst=", be16, tp_dst);
2334 SCAN_FIELD("flags(", tun_flags, flags);
2335 } SCAN_END(OVS_KEY_ATTR_TUNNEL);
2337 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT);
2339 SCAN_BEGIN("eth(", struct ovs_key_ethernet) {
2340 SCAN_FIELD("src=", eth, eth_src);
2341 SCAN_FIELD("dst=", eth, eth_dst);
2342 } SCAN_END(OVS_KEY_ATTR_ETHERNET);
2344 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__,
2345 { htons(VLAN_CFI) }, { htons(VLAN_CFI) }) {
2346 SCAN_FIELD("vid=", vid, tci);
2347 SCAN_FIELD("pcp=", pcp, tci);
2348 SCAN_FIELD("cfi=", cfi, tci);
2349 } SCAN_END(OVS_KEY_ATTR_VLAN);
2351 SCAN_SINGLE("eth_type(", ovs_be16, be16, OVS_KEY_ATTR_ETHERTYPE);
2353 SCAN_BEGIN("mpls(", struct ovs_key_mpls) {
2354 SCAN_FIELD("label=", mpls_label, mpls_lse);
2355 SCAN_FIELD("tc=", mpls_tc, mpls_lse);
2356 SCAN_FIELD("ttl=", mpls_ttl, mpls_lse);
2357 SCAN_FIELD("bos=", mpls_bos, mpls_lse);
2358 } SCAN_END(OVS_KEY_ATTR_MPLS);
2360 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4) {
2361 SCAN_FIELD("src=", ipv4, ipv4_src);
2362 SCAN_FIELD("dst=", ipv4, ipv4_dst);
2363 SCAN_FIELD("proto=", u8, ipv4_proto);
2364 SCAN_FIELD("tos=", u8, ipv4_tos);
2365 SCAN_FIELD("ttl=", u8, ipv4_ttl);
2366 SCAN_FIELD("frag=", frag, ipv4_frag);
2367 } SCAN_END(OVS_KEY_ATTR_IPV4);
2369 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6) {
2370 SCAN_FIELD("src=", ipv6, ipv6_src);
2371 SCAN_FIELD("dst=", ipv6, ipv6_dst);
2372 SCAN_FIELD("label=", ipv6_label, ipv6_label);
2373 SCAN_FIELD("proto=", u8, ipv6_proto);
2374 SCAN_FIELD("tclass=", u8, ipv6_tclass);
2375 SCAN_FIELD("hlimit=", u8, ipv6_hlimit);
2376 SCAN_FIELD("frag=", frag, ipv6_frag);
2377 } SCAN_END(OVS_KEY_ATTR_IPV6);
2379 SCAN_BEGIN("tcp(", struct ovs_key_tcp) {
2380 SCAN_FIELD("src=", be16, tcp_src);
2381 SCAN_FIELD("dst=", be16, tcp_dst);
2382 } SCAN_END(OVS_KEY_ATTR_TCP);
2384 SCAN_SINGLE("tcp_flags(", ovs_be16, tcp_flags, OVS_KEY_ATTR_TCP_FLAGS);
2386 SCAN_BEGIN("udp(", struct ovs_key_udp) {
2387 SCAN_FIELD("src=", be16, udp_src);
2388 SCAN_FIELD("dst=", be16, udp_dst);
2389 } SCAN_END(OVS_KEY_ATTR_UDP);
2391 SCAN_BEGIN("sctp(", struct ovs_key_sctp) {
2392 SCAN_FIELD("src=", be16, sctp_src);
2393 SCAN_FIELD("dst=", be16, sctp_dst);
2394 } SCAN_END(OVS_KEY_ATTR_SCTP);
2396 SCAN_BEGIN("icmp(", struct ovs_key_icmp) {
2397 SCAN_FIELD("type=", u8, icmp_type);
2398 SCAN_FIELD("code=", u8, icmp_code);
2399 } SCAN_END(OVS_KEY_ATTR_ICMP);
2401 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6) {
2402 SCAN_FIELD("type=", u8, icmpv6_type);
2403 SCAN_FIELD("code=", u8, icmpv6_code);
2404 } SCAN_END(OVS_KEY_ATTR_ICMPV6);
2406 SCAN_BEGIN("arp(", struct ovs_key_arp) {
2407 SCAN_FIELD("sip=", ipv4, arp_sip);
2408 SCAN_FIELD("tip=", ipv4, arp_tip);
2409 SCAN_FIELD("op=", be16, arp_op);
2410 SCAN_FIELD("sha=", eth, arp_sha);
2411 SCAN_FIELD("tha=", eth, arp_tha);
2412 } SCAN_END(OVS_KEY_ATTR_ARP);
2414 SCAN_BEGIN("nd(", struct ovs_key_nd) {
2415 SCAN_FIELD("target=", ipv6, nd_target);
2416 SCAN_FIELD("sll=", eth, nd_sll);
2417 SCAN_FIELD("tll=", eth, nd_tll);
2418 } SCAN_END(OVS_KEY_ATTR_ND);
2420 /* Encap open-coded. */
2421 if (!strncmp(s, "encap(", 6)) {
2422 const char *start = s;
2423 size_t encap, encap_mask = 0;
2425 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2427 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2434 s += strspn(s, ", \t\r\n");
2437 } else if (*s == ')') {
2441 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2449 nl_msg_end_nested(key, encap);
2451 nl_msg_end_nested(mask, encap_mask);
2460 /* Parses the string representation of a datapath flow key, in the
2461 * format output by odp_flow_key_format(). Returns 0 if successful,
2462 * otherwise a positive errno value. On success, the flow key is
2463 * appended to 'key' as a series of Netlink attributes. On failure, no
2464 * data is appended to 'key'. Either way, 'key''s data might be
2467 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2468 * to a port number. (Port names may be used instead of port numbers in
2471 * On success, the attributes appended to 'key' are individually syntactically
2472 * valid, but they may not be valid as a sequence. 'key' might, for example,
2473 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2475 odp_flow_from_string(const char *s, const struct simap *port_names,
2476 struct ofpbuf *key, struct ofpbuf *mask)
2478 const size_t old_size = ofpbuf_size(key);
2482 s += strspn(s, delimiters);
2487 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2489 ofpbuf_set_size(key, old_size);
2499 ovs_to_odp_frag(uint8_t nw_frag)
2501 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2502 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2503 : OVS_FRAG_TYPE_LATER);
2507 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2509 uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2511 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2512 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2518 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2519 const struct flow *mask, odp_port_t odp_in_port,
2520 size_t max_mpls_depth, bool recirc, bool export_mask)
2522 struct ovs_key_ethernet *eth_key;
2524 const struct flow *data = export_mask ? mask : flow;
2526 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2528 if (flow->tunnel.ip_dst || export_mask) {
2529 tun_key_to_attr(buf, &data->tunnel);
2532 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2535 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2536 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2539 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2540 * is not the magical value "ODPP_NONE". */
2541 if (export_mask || odp_in_port != ODPP_NONE) {
2542 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2545 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2547 memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2548 memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2550 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2552 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2554 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2556 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2557 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2558 if (flow->vlan_tci == htons(0)) {
2565 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2566 /* For backwards compatibility with kernels that don't support
2567 * wildcarding, the following convention is used to encode the
2568 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2571 * -------- -------- -------
2572 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2573 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2574 * <none> 0xffff Any non-Ethernet II frame (except valid
2575 * 802.3 SNAP packet with valid eth_type).
2578 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2583 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2585 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2586 struct ovs_key_ipv4 *ipv4_key;
2588 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2590 ipv4_key->ipv4_src = data->nw_src;
2591 ipv4_key->ipv4_dst = data->nw_dst;
2592 ipv4_key->ipv4_proto = data->nw_proto;
2593 ipv4_key->ipv4_tos = data->nw_tos;
2594 ipv4_key->ipv4_ttl = data->nw_ttl;
2595 ipv4_key->ipv4_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2596 : ovs_to_odp_frag(data->nw_frag);
2597 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2598 struct ovs_key_ipv6 *ipv6_key;
2600 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2602 memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2603 memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2604 ipv6_key->ipv6_label = data->ipv6_label;
2605 ipv6_key->ipv6_proto = data->nw_proto;
2606 ipv6_key->ipv6_tclass = data->nw_tos;
2607 ipv6_key->ipv6_hlimit = data->nw_ttl;
2608 ipv6_key->ipv6_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2609 : ovs_to_odp_frag(data->nw_frag);
2610 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2611 flow->dl_type == htons(ETH_TYPE_RARP)) {
2612 struct ovs_key_arp *arp_key;
2614 arp_key = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_ARP,
2616 arp_key->arp_sip = data->nw_src;
2617 arp_key->arp_tip = data->nw_dst;
2618 arp_key->arp_op = htons(data->nw_proto);
2619 memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2620 memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2621 } else if (eth_type_mpls(flow->dl_type)) {
2622 struct ovs_key_mpls *mpls_key;
2625 n = flow_count_mpls_labels(flow, NULL);
2626 n = MIN(n, max_mpls_depth);
2627 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2628 n * sizeof *mpls_key);
2629 for (i = 0; i < n; i++) {
2630 mpls_key[i].mpls_lse = data->mpls_lse[i];
2634 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2635 if (flow->nw_proto == IPPROTO_TCP) {
2636 struct ovs_key_tcp *tcp_key;
2638 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2640 tcp_key->tcp_src = data->tp_src;
2641 tcp_key->tcp_dst = data->tp_dst;
2643 if (data->tcp_flags) {
2644 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
2646 } else if (flow->nw_proto == IPPROTO_UDP) {
2647 struct ovs_key_udp *udp_key;
2649 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2651 udp_key->udp_src = data->tp_src;
2652 udp_key->udp_dst = data->tp_dst;
2653 } else if (flow->nw_proto == IPPROTO_SCTP) {
2654 struct ovs_key_sctp *sctp_key;
2656 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2658 sctp_key->sctp_src = data->tp_src;
2659 sctp_key->sctp_dst = data->tp_dst;
2660 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2661 && flow->nw_proto == IPPROTO_ICMP) {
2662 struct ovs_key_icmp *icmp_key;
2664 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2666 icmp_key->icmp_type = ntohs(data->tp_src);
2667 icmp_key->icmp_code = ntohs(data->tp_dst);
2668 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2669 && flow->nw_proto == IPPROTO_ICMPV6) {
2670 struct ovs_key_icmpv6 *icmpv6_key;
2672 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2673 sizeof *icmpv6_key);
2674 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2675 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2677 if (flow->tp_dst == htons(0)
2678 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
2679 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
2680 && (!export_mask || (data->tp_src == htons(0xffff)
2681 && data->tp_dst == htons(0xffff)))) {
2683 struct ovs_key_nd *nd_key;
2685 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2687 memcpy(nd_key->nd_target, &data->nd_target,
2688 sizeof nd_key->nd_target);
2689 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2690 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2697 nl_msg_end_nested(buf, encap);
2701 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2702 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2703 * number rather than a datapath port number). Instead, if 'odp_in_port'
2704 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2707 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2708 * capable of being expanded to allow for that much space.
2710 * 'recirc' indicates support for recirculation fields. If this is true, then
2711 * these fields will always be serialised. */
2713 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2714 const struct flow *mask, odp_port_t odp_in_port,
2717 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
2721 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2722 * 'buf'. 'flow' is used as a template to determine how to interpret
2723 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2724 * it doesn't indicate whether the other fields should be interpreted as
2725 * ARP, IPv4, IPv6, etc.
2727 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2728 * capable of being expanded to allow for that much space.
2730 * 'recirc' indicates support for recirculation fields. If this is true, then
2731 * these fields will always be serialised. */
2733 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2734 const struct flow *flow, uint32_t odp_in_port_mask,
2735 size_t max_mpls_depth, bool recirc)
2737 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
2738 max_mpls_depth, recirc, true);
2741 /* Generate ODP flow key from the given packet metadata */
2743 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
2745 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
2747 if (md->tunnel.ip_dst) {
2748 tun_key_to_attr(buf, &md->tunnel);
2751 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
2753 /* Add an ingress port attribute if 'odp_in_port' is not the magical
2754 * value "ODPP_NONE". */
2755 if (md->in_port.odp_port != ODPP_NONE) {
2756 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
2760 /* Generate packet metadata from the given ODP flow key. */
2762 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
2763 struct pkt_metadata *md)
2765 const struct nlattr *nla;
2767 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
2768 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
2769 1u << OVS_KEY_ATTR_IN_PORT;
2771 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
2773 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2774 uint16_t type = nl_attr_type(nla);
2775 size_t len = nl_attr_get_size(nla);
2776 int expected_len = odp_flow_key_attr_len(type);
2778 if (len != expected_len && expected_len >= 0) {
2783 case OVS_KEY_ATTR_RECIRC_ID:
2784 md->recirc_id = nl_attr_get_u32(nla);
2785 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
2787 case OVS_KEY_ATTR_DP_HASH:
2788 md->dp_hash = nl_attr_get_u32(nla);
2789 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
2791 case OVS_KEY_ATTR_PRIORITY:
2792 md->skb_priority = nl_attr_get_u32(nla);
2793 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
2795 case OVS_KEY_ATTR_SKB_MARK:
2796 md->pkt_mark = nl_attr_get_u32(nla);
2797 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
2799 case OVS_KEY_ATTR_TUNNEL: {
2800 enum odp_key_fitness res;
2802 res = odp_tun_key_from_attr(nla, &md->tunnel);
2803 if (res == ODP_FIT_ERROR) {
2804 memset(&md->tunnel, 0, sizeof md->tunnel);
2805 } else if (res == ODP_FIT_PERFECT) {
2806 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
2810 case OVS_KEY_ATTR_IN_PORT:
2811 md->in_port.odp_port = nl_attr_get_odp_port(nla);
2812 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
2818 if (!wanted_attrs) {
2819 return; /* Have everything. */
2825 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2827 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2828 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2829 key_len / sizeof(uint32_t), 0);
2833 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2834 uint64_t attrs, int out_of_range_attr,
2835 const struct nlattr *key, size_t key_len)
2840 if (VLOG_DROP_DBG(rl)) {
2845 for (i = 0; i < 64; i++) {
2846 if (attrs & (UINT64_C(1) << i)) {
2847 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2849 ds_put_format(&s, " %s",
2850 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2853 if (out_of_range_attr) {
2854 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2857 ds_put_cstr(&s, ": ");
2858 odp_flow_key_format(key, key_len, &s);
2860 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2865 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2867 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2869 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2870 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2874 if (odp_frag != OVS_FRAG_TYPE_NONE) {
2875 flow->nw_frag |= FLOW_NW_FRAG_ANY;
2876 if (odp_frag == OVS_FRAG_TYPE_LATER) {
2877 flow->nw_frag |= FLOW_NW_FRAG_LATER;
2884 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2885 const struct nlattr *attrs[], uint64_t *present_attrsp,
2886 int *out_of_range_attrp)
2888 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2889 const struct nlattr *nla;
2890 uint64_t present_attrs;
2893 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2895 *out_of_range_attrp = 0;
2896 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2897 uint16_t type = nl_attr_type(nla);
2898 size_t len = nl_attr_get_size(nla);
2899 int expected_len = odp_flow_key_attr_len(type);
2901 if (len != expected_len && expected_len >= 0) {
2902 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2904 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
2905 "length %d", ovs_key_attr_to_string(type, namebuf,
2911 if (type > OVS_KEY_ATTR_MAX) {
2912 *out_of_range_attrp = type;
2914 if (present_attrs & (UINT64_C(1) << type)) {
2915 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2917 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2918 ovs_key_attr_to_string(type,
2919 namebuf, sizeof namebuf));
2923 present_attrs |= UINT64_C(1) << type;
2928 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2932 *present_attrsp = present_attrs;
2936 static enum odp_key_fitness
2937 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2938 uint64_t expected_attrs,
2939 const struct nlattr *key, size_t key_len)
2941 uint64_t missing_attrs;
2942 uint64_t extra_attrs;
2944 missing_attrs = expected_attrs & ~present_attrs;
2945 if (missing_attrs) {
2946 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2947 log_odp_key_attributes(&rl, "expected but not present",
2948 missing_attrs, 0, key, key_len);
2949 return ODP_FIT_TOO_LITTLE;
2952 extra_attrs = present_attrs & ~expected_attrs;
2953 if (extra_attrs || out_of_range_attr) {
2954 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2955 log_odp_key_attributes(&rl, "present but not expected",
2956 extra_attrs, out_of_range_attr, key, key_len);
2957 return ODP_FIT_TOO_MUCH;
2960 return ODP_FIT_PERFECT;
2964 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2965 uint64_t present_attrs, uint64_t *expected_attrs,
2966 struct flow *flow, const struct flow *src_flow)
2968 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2969 bool is_mask = flow != src_flow;
2971 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2972 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2973 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2974 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2975 ntohs(flow->dl_type));
2978 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
2979 flow->dl_type != htons(0xffff)) {
2982 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2985 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2986 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
2987 /* See comments in odp_flow_key_from_flow__(). */
2988 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
2995 static enum odp_key_fitness
2996 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2997 uint64_t present_attrs, int out_of_range_attr,
2998 uint64_t expected_attrs, struct flow *flow,
2999 const struct nlattr *key, size_t key_len,
3000 const struct flow *src_flow)
3002 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3003 bool is_mask = src_flow != flow;
3004 const void *check_start = NULL;
3005 size_t check_len = 0;
3006 enum ovs_key_attr expected_bit = 0xff;
3008 if (eth_type_mpls(src_flow->dl_type)) {
3009 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3010 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3011 int n = size / sizeof(ovs_be32);
3014 if (!size || size % sizeof(ovs_be32)) {
3015 return ODP_FIT_ERROR;
3019 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3021 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
3022 return ODP_FIT_TOO_LITTLE;
3024 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3025 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3026 return ODP_FIT_ERROR;
3028 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3031 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3032 flow->mpls_lse[i] = mpls_lse[i];
3034 if (n > FLOW_MAX_MPLS_LABELS) {
3035 return ODP_FIT_TOO_MUCH;
3039 /* BOS may be set only in the innermost label. */
3040 for (i = 0; i < n - 1; i++) {
3041 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3042 return ODP_FIT_ERROR;
3046 /* BOS must be set in the innermost label. */
3047 if (n < FLOW_MAX_MPLS_LABELS
3048 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3049 return ODP_FIT_TOO_LITTLE;
3054 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3056 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3058 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3059 const struct ovs_key_ipv4 *ipv4_key;
3061 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3062 flow->nw_src = ipv4_key->ipv4_src;
3063 flow->nw_dst = ipv4_key->ipv4_dst;
3064 flow->nw_proto = ipv4_key->ipv4_proto;
3065 flow->nw_tos = ipv4_key->ipv4_tos;
3066 flow->nw_ttl = ipv4_key->ipv4_ttl;
3068 flow->nw_frag = ipv4_key->ipv4_frag;
3069 check_start = ipv4_key;
3070 check_len = sizeof *ipv4_key;
3071 expected_bit = OVS_KEY_ATTR_IPV4;
3072 } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
3073 return ODP_FIT_ERROR;
3076 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3078 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3080 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3081 const struct ovs_key_ipv6 *ipv6_key;
3083 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3084 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
3085 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
3086 flow->ipv6_label = ipv6_key->ipv6_label;
3087 flow->nw_proto = ipv6_key->ipv6_proto;
3088 flow->nw_tos = ipv6_key->ipv6_tclass;
3089 flow->nw_ttl = ipv6_key->ipv6_hlimit;
3091 flow->nw_frag = ipv6_key->ipv6_frag;
3092 check_start = ipv6_key;
3093 check_len = sizeof *ipv6_key;
3094 expected_bit = OVS_KEY_ATTR_IPV6;
3095 } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
3096 return ODP_FIT_ERROR;
3099 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3100 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3102 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3104 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3105 const struct ovs_key_arp *arp_key;
3107 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3108 flow->nw_src = arp_key->arp_sip;
3109 flow->nw_dst = arp_key->arp_tip;
3110 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3111 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3112 "key", ntohs(arp_key->arp_op));
3113 return ODP_FIT_ERROR;
3115 flow->nw_proto = ntohs(arp_key->arp_op);
3116 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
3117 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
3120 check_start = arp_key;
3121 check_len = sizeof *arp_key;
3122 expected_bit = OVS_KEY_ATTR_ARP;
3128 if (check_len > 0) { /* Happens only when 'is_mask'. */
3129 if (!is_all_zeros(check_start, check_len) &&
3130 flow->dl_type != htons(0xffff)) {
3131 return ODP_FIT_ERROR;
3133 expected_attrs |= UINT64_C(1) << expected_bit;
3137 expected_bit = OVS_KEY_ATTR_UNSPEC;
3138 if (src_flow->nw_proto == IPPROTO_TCP
3139 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3140 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3141 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3143 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3145 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3146 const struct ovs_key_tcp *tcp_key;
3148 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3149 flow->tp_src = tcp_key->tcp_src;
3150 flow->tp_dst = tcp_key->tcp_dst;
3151 expected_bit = OVS_KEY_ATTR_TCP;
3153 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3154 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3155 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3157 } else if (src_flow->nw_proto == IPPROTO_UDP
3158 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3159 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3160 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3162 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3164 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3165 const struct ovs_key_udp *udp_key;
3167 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3168 flow->tp_src = udp_key->udp_src;
3169 flow->tp_dst = udp_key->udp_dst;
3170 expected_bit = OVS_KEY_ATTR_UDP;
3172 } else if (src_flow->nw_proto == IPPROTO_SCTP
3173 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3174 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3175 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3177 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3179 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3180 const struct ovs_key_sctp *sctp_key;
3182 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3183 flow->tp_src = sctp_key->sctp_src;
3184 flow->tp_dst = sctp_key->sctp_dst;
3185 expected_bit = OVS_KEY_ATTR_SCTP;
3187 } else if (src_flow->nw_proto == IPPROTO_ICMP
3188 && src_flow->dl_type == htons(ETH_TYPE_IP)
3189 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3191 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3193 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3194 const struct ovs_key_icmp *icmp_key;
3196 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3197 flow->tp_src = htons(icmp_key->icmp_type);
3198 flow->tp_dst = htons(icmp_key->icmp_code);
3199 expected_bit = OVS_KEY_ATTR_ICMP;
3201 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3202 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3203 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3205 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3207 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3208 const struct ovs_key_icmpv6 *icmpv6_key;
3210 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3211 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3212 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3213 expected_bit = OVS_KEY_ATTR_ICMPV6;
3214 if (src_flow->tp_dst == htons(0) &&
3215 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3216 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3218 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3220 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3221 const struct ovs_key_nd *nd_key;
3223 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3224 memcpy(&flow->nd_target, nd_key->nd_target,
3225 sizeof flow->nd_target);
3226 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3227 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3229 if (!is_all_zeros(nd_key, sizeof *nd_key) &&
3230 (flow->tp_src != htons(0xffff) ||
3231 flow->tp_dst != htons(0xffff))) {
3232 return ODP_FIT_ERROR;
3234 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3241 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3242 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3243 return ODP_FIT_ERROR;
3245 expected_attrs |= UINT64_C(1) << expected_bit;
3250 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3254 /* Parse 802.1Q header then encapsulated L3 attributes. */
3255 static enum odp_key_fitness
3256 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3257 uint64_t present_attrs, int out_of_range_attr,
3258 uint64_t expected_attrs, struct flow *flow,
3259 const struct nlattr *key, size_t key_len,
3260 const struct flow *src_flow)
3262 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3263 bool is_mask = src_flow != flow;
3265 const struct nlattr *encap
3266 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3267 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3268 enum odp_key_fitness encap_fitness;
3269 enum odp_key_fitness fitness;
3271 /* Calculate fitness of outer attributes. */
3273 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3274 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3276 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3277 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3279 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3280 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3283 fitness = check_expectations(present_attrs, out_of_range_attr,
3284 expected_attrs, key, key_len);
3287 * Remove the TPID from dl_type since it's not the real Ethertype. */
3288 flow->dl_type = htons(0);
3289 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3290 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3293 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3294 return ODP_FIT_TOO_LITTLE;
3295 } else if (flow->vlan_tci == htons(0)) {
3296 /* Corner case for a truncated 802.1Q header. */
3297 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3298 return ODP_FIT_TOO_MUCH;
3301 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3302 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3303 "but CFI bit is not set", ntohs(flow->vlan_tci));
3304 return ODP_FIT_ERROR;
3307 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3312 /* Now parse the encapsulated attributes. */
3313 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3314 attrs, &present_attrs, &out_of_range_attr)) {
3315 return ODP_FIT_ERROR;
3319 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3320 return ODP_FIT_ERROR;
3322 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3323 expected_attrs, flow, key, key_len,
3326 /* The overall fitness is the worse of the outer and inner attributes. */
3327 return MAX(fitness, encap_fitness);
3330 static enum odp_key_fitness
3331 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3332 struct flow *flow, const struct flow *src_flow)
3334 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3335 uint64_t expected_attrs;
3336 uint64_t present_attrs;
3337 int out_of_range_attr;
3338 bool is_mask = src_flow != flow;
3340 memset(flow, 0, sizeof *flow);
3342 /* Parse attributes. */
3343 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3344 &out_of_range_attr)) {
3345 return ODP_FIT_ERROR;
3350 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3351 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3352 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3353 } else if (is_mask) {
3354 /* Always exact match recirc_id if it is not specified. */
3355 flow->recirc_id = UINT32_MAX;
3358 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3359 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3360 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3362 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3363 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3364 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3367 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3368 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3369 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3372 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3373 enum odp_key_fitness res;
3375 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3376 if (res == ODP_FIT_ERROR) {
3377 return ODP_FIT_ERROR;
3378 } else if (res == ODP_FIT_PERFECT) {
3379 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3383 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3384 flow->in_port.odp_port
3385 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3386 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3387 } else if (!is_mask) {
3388 flow->in_port.odp_port = ODPP_NONE;
3391 /* Ethernet header. */
3392 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3393 const struct ovs_key_ethernet *eth_key;
3395 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3396 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3397 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3399 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3403 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3406 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3407 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3409 return ODP_FIT_ERROR;
3413 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3414 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3415 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3416 expected_attrs, flow, key, key_len, src_flow);
3419 flow->vlan_tci = htons(0xffff);
3420 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3421 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3422 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3425 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3426 expected_attrs, flow, key, key_len, src_flow);
3429 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3430 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3431 * 'key' fits our expectations for what a flow key should contain.
3433 * The 'in_port' will be the datapath's understanding of the port. The
3434 * caller will need to translate with odp_port_to_ofp_port() if the
3435 * OpenFlow port is needed.
3437 * This function doesn't take the packet itself as an argument because none of
3438 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3439 * it is always possible to infer which additional attribute(s) should appear
3440 * by looking at the attributes for lower-level protocols, e.g. if the network
3441 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3442 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3443 * must be absent. */
3444 enum odp_key_fitness
3445 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3448 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3451 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3452 * structure in 'mask'. 'flow' must be a previously translated flow
3453 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3454 * 'key' fits our expectations for what a flow key should contain. */
3455 enum odp_key_fitness
3456 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3457 struct flow *mask, const struct flow *flow)
3459 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3462 /* Returns 'fitness' as a string, for use in debug messages. */
3464 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3467 case ODP_FIT_PERFECT:
3469 case ODP_FIT_TOO_MUCH:
3471 case ODP_FIT_TOO_LITTLE:
3472 return "too_little";
3480 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3481 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3482 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3483 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3484 * null, then the return value is not meaningful.) */
3486 odp_put_userspace_action(uint32_t pid,
3487 const void *userdata, size_t userdata_size,
3488 odp_port_t tunnel_out_port,
3489 struct ofpbuf *odp_actions)
3491 size_t userdata_ofs;
3494 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3495 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3497 userdata_ofs = ofpbuf_size(odp_actions) + NLA_HDRLEN;
3499 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3500 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3503 * - The kernel rejected shorter userdata with -ERANGE.
3505 * - The kernel silently dropped userdata beyond the first 8 bytes.
3507 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3508 * separately disable features that required more than 8 bytes.) */
3509 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3510 MAX(8, userdata_size)),
3511 userdata, userdata_size);
3515 if (tunnel_out_port != ODPP_NONE) {
3516 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3519 nl_msg_end_nested(odp_actions, offset);
3521 return userdata_ofs;
3525 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3526 struct ofpbuf *odp_actions)
3528 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3529 tun_key_to_attr(odp_actions, tunnel);
3530 nl_msg_end_nested(odp_actions, offset);
3533 /* The commit_odp_actions() function and its helpers. */
3536 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3537 const void *key, size_t key_size)
3539 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3540 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3541 nl_msg_end_nested(odp_actions, offset);
3544 /* Masked set actions have a mask following the data within the netlink
3545 * attribute. The unmasked bits in the data will be cleared as the data
3546 * is copied to the action. */
3548 commit_masked_set_action(struct ofpbuf *odp_actions,
3549 enum ovs_key_attr key_type,
3550 const void *key_, const void *mask_, size_t key_size)
3552 size_t offset = nl_msg_start_nested(odp_actions,
3553 OVS_ACTION_ATTR_SET_MASKED);
3554 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3555 const char *key = key_, *mask = mask_;
3557 memcpy(data + key_size, mask, key_size);
3558 /* Clear unmasked bits while copying. */
3559 while (key_size--) {
3560 *data++ = *key++ & *mask++;
3562 nl_msg_end_nested(odp_actions, offset);
3566 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3567 struct ofpbuf *odp_actions)
3569 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3573 /* If any of the flow key data that ODP actions can modify are different in
3574 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3575 * 'odp_actions' that change the flow tunneling information in key from
3576 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3577 * same way. In other words, operates the same as commit_odp_actions(), but
3578 * only on tunneling information. */
3580 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3581 struct ofpbuf *odp_actions)
3583 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3584 if (flow->tunnel.ip_dst) {
3585 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3588 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3589 odp_put_tunnel_action(&base->tunnel, odp_actions);
3594 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3595 struct ofpbuf *odp_actions,
3596 struct flow_wildcards *wc)
3598 struct ovs_key_ethernet eth_key;
3600 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3601 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3605 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3606 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3608 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3609 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3611 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3612 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3614 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3615 ð_key, sizeof(eth_key));
3619 pop_vlan(struct flow *base,
3620 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3622 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3624 if (base->vlan_tci & htons(VLAN_CFI)) {
3625 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3631 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
3632 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3634 if (base->vlan_tci == vlan_tci) {
3638 pop_vlan(base, odp_actions, wc);
3639 if (vlan_tci & htons(VLAN_CFI)) {
3640 struct ovs_action_push_vlan vlan;
3642 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3643 vlan.vlan_tci = vlan_tci;
3644 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3645 &vlan, sizeof vlan);
3647 base->vlan_tci = vlan_tci;
3651 commit_mpls_action(const struct flow *flow, struct flow *base,
3652 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3654 int base_n = flow_count_mpls_labels(base, wc);
3655 int flow_n = flow_count_mpls_labels(flow, wc);
3656 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
3659 while (base_n > common_n) {
3660 if (base_n - 1 == common_n && flow_n > common_n) {
3661 /* If there is only one more LSE in base than there are common
3662 * between base and flow; and flow has at least one more LSE than
3663 * is common then the topmost LSE of base may be updated using
3665 struct ovs_key_mpls mpls_key;
3667 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
3668 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3669 &mpls_key, sizeof mpls_key);
3670 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
3673 /* Otherwise, if there more LSEs in base than are common between
3674 * base and flow then pop the topmost one. */
3678 /* If all the LSEs are to be popped and this is not the outermost
3679 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
3680 * POP_MPLS action instead of flow->dl_type.
3682 * This is because the POP_MPLS action requires its ethertype
3683 * argument to be an MPLS ethernet type but in this case
3684 * flow->dl_type will be a non-MPLS ethernet type.
3686 * When the final POP_MPLS action occurs it use flow->dl_type and
3687 * the and the resulting packet will have the desired dl_type. */
3688 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
3689 dl_type = htons(ETH_TYPE_MPLS);
3691 dl_type = flow->dl_type;
3693 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
3694 popped = flow_pop_mpls(base, base_n, flow->dl_type, wc);
3700 /* If, after the above popping and setting, there are more LSEs in flow
3701 * than base then some LSEs need to be pushed. */
3702 while (base_n < flow_n) {
3703 struct ovs_action_push_mpls *mpls;
3705 mpls = nl_msg_put_unspec_zero(odp_actions,
3706 OVS_ACTION_ATTR_PUSH_MPLS,
3708 mpls->mpls_ethertype = flow->dl_type;
3709 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
3710 flow_push_mpls(base, base_n, mpls->mpls_ethertype, wc);
3711 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
3717 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3718 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3720 struct ovs_key_ipv4 ipv4_key;
3722 if (base->nw_src == flow->nw_src &&
3723 base->nw_dst == flow->nw_dst &&
3724 base->nw_tos == flow->nw_tos &&
3725 base->nw_ttl == flow->nw_ttl &&
3726 base->nw_frag == flow->nw_frag) {
3730 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3731 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3732 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3733 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3734 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3735 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3737 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3738 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3739 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3740 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3741 ipv4_key.ipv4_proto = base->nw_proto;
3742 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3744 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3745 &ipv4_key, sizeof(ipv4_key));
3749 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3750 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3752 struct ovs_key_ipv6 ipv6_key;
3754 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3755 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3756 base->ipv6_label == flow->ipv6_label &&
3757 base->nw_tos == flow->nw_tos &&
3758 base->nw_ttl == flow->nw_ttl &&
3759 base->nw_frag == flow->nw_frag) {
3763 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3764 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3765 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3766 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3767 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3768 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3769 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3771 base->ipv6_src = flow->ipv6_src;
3772 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3773 base->ipv6_dst = flow->ipv6_dst;
3774 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3776 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3777 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3778 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3779 ipv6_key.ipv6_proto = base->nw_proto;
3780 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3782 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3783 &ipv6_key, sizeof(ipv6_key));
3786 static enum slow_path_reason
3787 commit_set_arp_action(const struct flow *flow, struct flow *base,
3788 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3790 struct ovs_key_arp arp_key;
3792 if (base->nw_src == flow->nw_src &&
3793 base->nw_dst == flow->nw_dst &&
3794 base->nw_proto == flow->nw_proto &&
3795 eth_addr_equals(base->arp_sha, flow->arp_sha) &&
3796 eth_addr_equals(base->arp_tha, flow->arp_tha)) {
3800 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3801 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3802 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3803 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
3804 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
3806 base->nw_src = flow->nw_src;
3807 base->nw_dst = flow->nw_dst;
3808 base->nw_proto = flow->nw_proto;
3809 memcpy(base->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3810 memcpy(base->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3812 arp_key.arp_sip = base->nw_src;
3813 arp_key.arp_tip = base->nw_dst;
3814 arp_key.arp_op = htons(base->nw_proto);
3815 memcpy(arp_key.arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3816 memcpy(arp_key.arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3818 commit_set_action(odp_actions, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
3823 static enum slow_path_reason
3824 commit_set_nw_action(const struct flow *flow, struct flow *base,
3825 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3827 /* Check if 'flow' really has an L3 header. */
3828 if (!flow->nw_proto) {
3832 switch (ntohs(base->dl_type)) {
3834 commit_set_ipv4_action(flow, base, odp_actions, wc);
3838 commit_set_ipv6_action(flow, base, odp_actions, wc);
3842 return commit_set_arp_action(flow, base, odp_actions, wc);
3849 commit_set_port_action(const struct flow *flow, struct flow *base,
3850 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3852 /* Check if 'flow' really has an L3 header. */
3853 if (!flow->nw_proto) {
3857 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3861 if (base->tp_src == flow->tp_src &&
3862 base->tp_dst == flow->tp_dst) {
3866 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3867 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3869 if (flow->nw_proto == IPPROTO_TCP) {
3870 struct ovs_key_tcp port_key;
3872 port_key.tcp_src = base->tp_src = flow->tp_src;
3873 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3875 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3876 &port_key, sizeof(port_key));
3878 } else if (flow->nw_proto == IPPROTO_UDP) {
3879 struct ovs_key_udp port_key;
3881 port_key.udp_src = base->tp_src = flow->tp_src;
3882 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3884 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3885 &port_key, sizeof(port_key));
3886 } else if (flow->nw_proto == IPPROTO_SCTP) {
3887 struct ovs_key_sctp port_key;
3889 port_key.sctp_src = base->tp_src = flow->tp_src;
3890 port_key.sctp_dst = base->tp_dst = flow->tp_dst;
3892 commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
3893 &port_key, sizeof(port_key));
3898 commit_set_priority_action(const struct flow *flow, struct flow *base,
3899 struct ofpbuf *odp_actions,
3900 struct flow_wildcards *wc)
3902 if (base->skb_priority == flow->skb_priority) {
3906 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3907 base->skb_priority = flow->skb_priority;
3909 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3910 &base->skb_priority, sizeof(base->skb_priority));
3914 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
3915 struct ofpbuf *odp_actions,
3916 struct flow_wildcards *wc)
3918 if (base->pkt_mark == flow->pkt_mark) {
3922 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
3923 base->pkt_mark = flow->pkt_mark;
3925 odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
3928 /* If any of the flow key data that ODP actions can modify are different in
3929 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3930 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3931 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3932 * in addition to this function if needed. Sets fields in 'wc' that are
3933 * used as part of the action.
3935 * Returns a reason to force processing the flow's packets into the userspace
3936 * slow path, if there is one, otherwise 0. */
3937 enum slow_path_reason
3938 commit_odp_actions(const struct flow *flow, struct flow *base,
3939 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3941 enum slow_path_reason slow;
3943 commit_set_ether_addr_action(flow, base, odp_actions, wc);
3944 slow = commit_set_nw_action(flow, base, odp_actions, wc);
3945 commit_set_port_action(flow, base, odp_actions, wc);
3946 commit_mpls_action(flow, base, odp_actions, wc);
3947 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
3948 commit_set_priority_action(flow, base, odp_actions, wc);
3949 commit_set_pkt_mark_action(flow, base, odp_actions, wc);