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),
220 while (s[n] != ')') {
221 unsigned long long int flags;
225 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
226 n += n0 + (s[n + n0] == ',');
231 for (bit = 1; bit; bit <<= 1) {
232 const char *name = bit_to_string(bit);
240 if (!strncmp(s + n, name, len) &&
241 (s[n + len] == ',' || s[n + len] == ')')) {
243 n += len + (s[n + len] == ',');
259 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
261 static const struct nl_policy ovs_userspace_policy[] = {
262 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
263 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_UNSPEC,
265 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = { .type = NL_A_U32,
268 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
269 const struct nlattr *userdata_attr;
270 const struct nlattr *tunnel_out_port_attr;
272 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
273 ds_put_cstr(ds, "userspace(error)");
277 ds_put_format(ds, "userspace(pid=%"PRIu32,
278 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
280 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
283 const uint8_t *userdata = nl_attr_get(userdata_attr);
284 size_t userdata_len = nl_attr_get_size(userdata_attr);
285 bool userdata_unspec = true;
286 union user_action_cookie cookie;
288 if (userdata_len >= sizeof cookie.type
289 && userdata_len <= sizeof cookie) {
291 memset(&cookie, 0, sizeof cookie);
292 memcpy(&cookie, userdata, userdata_len);
294 userdata_unspec = false;
296 if (userdata_len == sizeof cookie.sflow
297 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
298 ds_put_format(ds, ",sFlow("
299 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
300 vlan_tci_to_vid(cookie.sflow.vlan_tci),
301 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
302 cookie.sflow.output);
303 } else if (userdata_len == sizeof cookie.slow_path
304 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
305 ds_put_cstr(ds, ",slow_path(");
306 format_flags(ds, slow_path_reason_to_string,
307 cookie.slow_path.reason, ',');
308 ds_put_format(ds, ")");
309 } else if (userdata_len == sizeof cookie.flow_sample
310 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
311 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
312 ",collector_set_id=%"PRIu32
313 ",obs_domain_id=%"PRIu32
314 ",obs_point_id=%"PRIu32")",
315 cookie.flow_sample.probability,
316 cookie.flow_sample.collector_set_id,
317 cookie.flow_sample.obs_domain_id,
318 cookie.flow_sample.obs_point_id);
319 } else if (userdata_len >= sizeof cookie.ipfix
320 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
321 ds_put_format(ds, ",ipfix(output_port=%"PRIu32")",
322 cookie.ipfix.output_odp_port);
324 userdata_unspec = true;
328 if (userdata_unspec) {
330 ds_put_format(ds, ",userdata(");
331 for (i = 0; i < userdata_len; i++) {
332 ds_put_format(ds, "%02x", userdata[i]);
334 ds_put_char(ds, ')');
338 tunnel_out_port_attr = a[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT];
339 if (tunnel_out_port_attr) {
340 ds_put_format(ds, ",tunnel_out_port=%"PRIu32,
341 nl_attr_get_u32(tunnel_out_port_attr));
344 ds_put_char(ds, ')');
348 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
350 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
351 vlan_tci_to_vid(vlan_tci),
352 vlan_tci_to_pcp(vlan_tci));
353 if (!(vlan_tci & htons(VLAN_CFI))) {
354 ds_put_cstr(ds, ",cfi=0");
359 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
361 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
362 mpls_lse_to_label(mpls_lse),
363 mpls_lse_to_tc(mpls_lse),
364 mpls_lse_to_ttl(mpls_lse),
365 mpls_lse_to_bos(mpls_lse));
369 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
370 const struct ovs_key_mpls *mpls_mask, int n)
373 ovs_be32 key = mpls_key->mpls_lse;
375 if (mpls_mask == NULL) {
376 format_mpls_lse(ds, key);
378 ovs_be32 mask = mpls_mask->mpls_lse;
380 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
381 mpls_lse_to_label(key), mpls_lse_to_label(mask),
382 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
383 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
384 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
389 for (i = 0; i < n; i++) {
390 ds_put_format(ds, "lse%d=%#"PRIx32,
391 i, ntohl(mpls_key[i].mpls_lse));
393 ds_put_format(ds, "/%#"PRIx32, ntohl(mpls_mask[i].mpls_lse));
395 ds_put_char(ds, ',');
402 format_odp_recirc_action(struct ds *ds, uint32_t recirc_id)
404 ds_put_format(ds, "recirc(%"PRIu32")", recirc_id);
408 format_odp_hash_action(struct ds *ds, const struct ovs_action_hash *hash_act)
410 ds_put_format(ds, "hash(");
412 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
413 ds_put_format(ds, "hash_l4(%"PRIu32")", hash_act->hash_basis);
415 ds_put_format(ds, "Unknown hash algorithm(%"PRIu32")",
418 ds_put_format(ds, ")");
422 format_odp_action(struct ds *ds, const struct nlattr *a)
425 enum ovs_action_attr type = nl_attr_type(a);
426 const struct ovs_action_push_vlan *vlan;
429 expected_len = odp_action_len(nl_attr_type(a));
430 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
431 ds_put_format(ds, "bad length %"PRIuSIZE", expected %d for: ",
432 nl_attr_get_size(a), expected_len);
433 format_generic_odp_action(ds, a);
438 case OVS_ACTION_ATTR_OUTPUT:
439 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
441 case OVS_ACTION_ATTR_USERSPACE:
442 format_odp_userspace_action(ds, a);
444 case OVS_ACTION_ATTR_RECIRC:
445 format_odp_recirc_action(ds, nl_attr_get_u32(a));
447 case OVS_ACTION_ATTR_HASH:
448 format_odp_hash_action(ds, nl_attr_get(a));
450 case OVS_ACTION_ATTR_SET_MASKED:
452 size = nl_attr_get_size(a) / 2;
453 ds_put_cstr(ds, "set(");
455 /* Masked set action not supported for tunnel key, which is bigger. */
456 if (size <= sizeof(struct ovs_key_ipv6)) {
457 struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
458 sizeof(struct nlattr))];
459 struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6),
460 sizeof(struct nlattr))];
462 mask->nla_type = attr->nla_type = nl_attr_type(a);
463 mask->nla_len = attr->nla_len = NLA_HDRLEN + size;
464 memcpy(attr + 1, (char *)(a + 1), size);
465 memcpy(mask + 1, (char *)(a + 1) + size, size);
466 format_odp_key_attr(attr, mask, NULL, ds, true);
468 format_odp_key_attr(a, NULL, NULL, ds, true);
470 ds_put_cstr(ds, ")");
472 case OVS_ACTION_ATTR_SET:
473 ds_put_cstr(ds, "set(");
474 format_odp_key_attr(nl_attr_get(a), NULL, NULL, ds, true);
475 ds_put_cstr(ds, ")");
477 case OVS_ACTION_ATTR_PUSH_VLAN:
478 vlan = nl_attr_get(a);
479 ds_put_cstr(ds, "push_vlan(");
480 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
481 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
483 format_vlan_tci(ds, vlan->vlan_tci);
484 ds_put_char(ds, ')');
486 case OVS_ACTION_ATTR_POP_VLAN:
487 ds_put_cstr(ds, "pop_vlan");
489 case OVS_ACTION_ATTR_PUSH_MPLS: {
490 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
491 ds_put_cstr(ds, "push_mpls(");
492 format_mpls_lse(ds, mpls->mpls_lse);
493 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
496 case OVS_ACTION_ATTR_POP_MPLS: {
497 ovs_be16 ethertype = nl_attr_get_be16(a);
498 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
501 case OVS_ACTION_ATTR_SAMPLE:
502 format_odp_sample_action(ds, a);
504 case OVS_ACTION_ATTR_UNSPEC:
505 case __OVS_ACTION_ATTR_MAX:
507 format_generic_odp_action(ds, a);
513 format_odp_actions(struct ds *ds, const struct nlattr *actions,
517 const struct nlattr *a;
520 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
522 ds_put_char(ds, ',');
524 format_odp_action(ds, a);
529 if (left == actions_len) {
530 ds_put_cstr(ds, "<empty>");
532 ds_put_format(ds, ",***%u leftover bytes*** (", left);
533 for (i = 0; i < left; i++) {
534 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
536 ds_put_char(ds, ')');
539 ds_put_cstr(ds, "drop");
543 /* Separate out parse_odp_userspace_action() function. */
545 parse_odp_userspace_action(const char *s, struct ofpbuf *actions)
548 union user_action_cookie cookie;
550 odp_port_t tunnel_out_port;
552 void *user_data = NULL;
553 size_t user_data_size = 0;
555 if (!ovs_scan(s, "userspace(pid=%"SCNi32"%n", &pid, &n)) {
561 uint32_t probability;
562 uint32_t collector_set_id;
563 uint32_t obs_domain_id;
564 uint32_t obs_point_id;
567 if (ovs_scan(&s[n], ",sFlow(vid=%i,"
568 "pcp=%i,output=%"SCNi32")%n",
569 &vid, &pcp, &output, &n1)) {
573 tci = vid | (pcp << VLAN_PCP_SHIFT);
578 cookie.type = USER_ACTION_COOKIE_SFLOW;
579 cookie.sflow.vlan_tci = htons(tci);
580 cookie.sflow.output = output;
582 user_data_size = sizeof cookie.sflow;
583 } else if (ovs_scan(&s[n], ",slow_path%n",
588 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
589 cookie.slow_path.unused = 0;
590 cookie.slow_path.reason = 0;
592 res = parse_flags(&s[n], slow_path_reason_to_string,
593 &cookie.slow_path.reason);
600 user_data_size = sizeof cookie.slow_path;
601 } else if (ovs_scan(&s[n], ",flow_sample(probability=%"SCNi32","
602 "collector_set_id=%"SCNi32","
603 "obs_domain_id=%"SCNi32","
604 "obs_point_id=%"SCNi32")%n",
605 &probability, &collector_set_id,
606 &obs_domain_id, &obs_point_id, &n1)) {
609 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
610 cookie.flow_sample.probability = probability;
611 cookie.flow_sample.collector_set_id = collector_set_id;
612 cookie.flow_sample.obs_domain_id = obs_domain_id;
613 cookie.flow_sample.obs_point_id = obs_point_id;
615 user_data_size = sizeof cookie.flow_sample;
616 } else if (ovs_scan(&s[n], ",ipfix(output_port=%"SCNi32")%n",
619 cookie.type = USER_ACTION_COOKIE_IPFIX;
620 cookie.ipfix.output_odp_port = u32_to_odp(output);
622 user_data_size = sizeof cookie.ipfix;
623 } else if (ovs_scan(&s[n], ",userdata(%n",
628 ofpbuf_init(&buf, 16);
629 end = ofpbuf_put_hex(&buf, &s[n], NULL);
633 user_data = ofpbuf_data(&buf);
634 user_data_size = ofpbuf_size(&buf);
641 if (ovs_scan(&s[n], ",tunnel_out_port=%"SCNi32")%n",
642 &tunnel_out_port, &n1)) {
643 odp_put_userspace_action(pid, user_data, user_data_size, tunnel_out_port, actions);
645 } else if (s[n] == ')') {
646 odp_put_userspace_action(pid, user_data, user_data_size, ODPP_NONE, actions);
655 parse_odp_action(const char *s, const struct simap *port_names,
656 struct ofpbuf *actions)
662 if (ovs_scan(s, "%"SCNi32"%n", &port, &n)) {
663 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
669 int len = strcspn(s, delimiters);
670 struct simap_node *node;
672 node = simap_find_len(port_names, s, len);
674 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
679 if (!strncmp(s, "userspace(", 10)) {
680 return parse_odp_userspace_action(s, actions);
683 if (!strncmp(s, "set(", 4)) {
686 struct nlattr mask[128 / sizeof(struct nlattr)];
687 struct ofpbuf maskbuf;
688 struct nlattr *nested, *key;
691 /* 'mask' is big enough to hold any key. */
692 ofpbuf_use_stack(&maskbuf, mask, sizeof mask);
694 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
695 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, &maskbuf);
699 if (s[retval + 4] != ')') {
703 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
706 size = nl_attr_get_size(mask);
707 if (size == nl_attr_get_size(key)) {
708 /* Change to masked set action if not fully masked. */
709 if (!is_all_ones((uint8_t *)(mask + 1), size)) {
710 key->nla_len += size;
711 ofpbuf_put(actions, mask + 1, size);
712 /* 'actions' may have been reallocated by ofpbuf_put(). */
713 nested = ofpbuf_at_assert(actions, start_ofs, sizeof *nested);
714 nested->nla_type = OVS_ACTION_ATTR_SET_MASKED;
718 nl_msg_end_nested(actions, start_ofs);
723 struct ovs_action_push_vlan push;
724 int tpid = ETH_TYPE_VLAN;
729 if (ovs_scan(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)
730 || ovs_scan(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
731 &vid, &pcp, &cfi, &n)
732 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
733 &tpid, &vid, &pcp, &n)
734 || ovs_scan(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
735 &tpid, &vid, &pcp, &cfi, &n)) {
736 push.vlan_tpid = htons(tpid);
737 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
738 | (pcp << VLAN_PCP_SHIFT)
739 | (cfi ? VLAN_CFI : 0));
740 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
747 if (!strncmp(s, "pop_vlan", 8)) {
748 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
756 if (ovs_scan(s, "sample(sample=%lf%%,actions(%n", &percentage, &n)
757 && percentage >= 0. && percentage <= 100.0) {
758 size_t sample_ofs, actions_ofs;
761 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
762 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
763 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
764 (probability <= 0 ? 0
765 : probability >= UINT32_MAX ? UINT32_MAX
768 actions_ofs = nl_msg_start_nested(actions,
769 OVS_SAMPLE_ATTR_ACTIONS);
773 n += strspn(s + n, delimiters);
778 retval = parse_odp_action(s + n, port_names, actions);
784 nl_msg_end_nested(actions, actions_ofs);
785 nl_msg_end_nested(actions, sample_ofs);
787 return s[n + 1] == ')' ? n + 2 : -EINVAL;
794 /* Parses the string representation of datapath actions, in the format output
795 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
796 * value. On success, the ODP actions are appended to 'actions' as a series of
797 * Netlink attributes. On failure, no data is appended to 'actions'. Either
798 * way, 'actions''s data might be reallocated. */
800 odp_actions_from_string(const char *s, const struct simap *port_names,
801 struct ofpbuf *actions)
805 if (!strcasecmp(s, "drop")) {
809 old_size = ofpbuf_size(actions);
813 s += strspn(s, delimiters);
818 retval = parse_odp_action(s, port_names, actions);
819 if (retval < 0 || !strchr(delimiters, s[retval])) {
820 ofpbuf_set_size(actions, old_size);
829 /* Returns the correct length of the payload for a flow key attribute of the
830 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
831 * is variable length. */
833 odp_flow_key_attr_len(uint16_t type)
835 if (type > OVS_KEY_ATTR_MAX) {
839 switch ((enum ovs_key_attr) type) {
840 case OVS_KEY_ATTR_ENCAP: return -2;
841 case OVS_KEY_ATTR_PRIORITY: return 4;
842 case OVS_KEY_ATTR_SKB_MARK: return 4;
843 case OVS_KEY_ATTR_DP_HASH: return 4;
844 case OVS_KEY_ATTR_RECIRC_ID: return 4;
845 case OVS_KEY_ATTR_TUNNEL: return -2;
846 case OVS_KEY_ATTR_IN_PORT: return 4;
847 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
848 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
849 case OVS_KEY_ATTR_ETHERTYPE: return 2;
850 case OVS_KEY_ATTR_MPLS: return -2;
851 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
852 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
853 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
854 case OVS_KEY_ATTR_TCP_FLAGS: return 2;
855 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
856 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
857 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
858 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
859 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
860 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
862 case OVS_KEY_ATTR_UNSPEC:
863 case __OVS_KEY_ATTR_MAX:
871 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
873 size_t len = nl_attr_get_size(a);
875 const uint8_t *unspec;
878 unspec = nl_attr_get(a);
879 for (i = 0; i < len; i++) {
881 ds_put_char(ds, ' ');
883 ds_put_format(ds, "%02x", unspec[i]);
889 ovs_frag_type_to_string(enum ovs_frag_type type)
892 case OVS_FRAG_TYPE_NONE:
894 case OVS_FRAG_TYPE_FIRST:
896 case OVS_FRAG_TYPE_LATER:
898 case __OVS_FRAG_TYPE_MAX:
905 tunnel_key_attr_len(int type)
908 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
909 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
910 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
911 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
912 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
913 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
914 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
915 case OVS_TUNNEL_KEY_ATTR_TP_SRC: return 2;
916 case OVS_TUNNEL_KEY_ATTR_TP_DST: return 2;
917 case OVS_TUNNEL_KEY_ATTR_OAM: return 0;
918 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: return -2;
919 case __OVS_TUNNEL_KEY_ATTR_MAX:
925 #define GENEVE_OPT(class, type) ((OVS_FORCE uint32_t)(class) << 8 | (type))
927 parse_geneve_opts(const struct nlattr *attr)
929 int opts_len = nl_attr_get_size(attr);
930 const struct geneve_opt *opt = nl_attr_get(attr);
932 while (opts_len > 0) {
935 if (opts_len < sizeof(*opt)) {
939 len = sizeof(*opt) + opt->length * 4;
940 if (len > opts_len) {
944 switch (GENEVE_OPT(opt->opt_class, opt->type)) {
946 if (opt->type & GENEVE_CRIT_OPT_TYPE) {
951 opt = opt + len / sizeof(*opt);
959 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
962 const struct nlattr *a;
964 bool unknown = false;
966 NL_NESTED_FOR_EACH(a, left, attr) {
967 uint16_t type = nl_attr_type(a);
968 size_t len = nl_attr_get_size(a);
969 int expected_len = tunnel_key_attr_len(type);
971 if (len != expected_len && expected_len >= 0) {
972 return ODP_FIT_ERROR;
976 case OVS_TUNNEL_KEY_ATTR_ID:
977 tun->tun_id = nl_attr_get_be64(a);
978 tun->flags |= FLOW_TNL_F_KEY;
980 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
981 tun->ip_src = nl_attr_get_be32(a);
983 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
984 tun->ip_dst = nl_attr_get_be32(a);
986 case OVS_TUNNEL_KEY_ATTR_TOS:
987 tun->ip_tos = nl_attr_get_u8(a);
989 case OVS_TUNNEL_KEY_ATTR_TTL:
990 tun->ip_ttl = nl_attr_get_u8(a);
993 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
994 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
996 case OVS_TUNNEL_KEY_ATTR_CSUM:
997 tun->flags |= FLOW_TNL_F_CSUM;
999 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
1000 tun->tp_src = nl_attr_get_be16(a);
1002 case OVS_TUNNEL_KEY_ATTR_TP_DST:
1003 tun->tp_dst = nl_attr_get_be16(a);
1005 case OVS_TUNNEL_KEY_ATTR_OAM:
1006 tun->flags |= FLOW_TNL_F_OAM;
1008 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: {
1009 if (parse_geneve_opts(a)) {
1010 return ODP_FIT_ERROR;
1012 /* It is necessary to reproduce options exactly (including order)
1013 * so it's easiest to just echo them back. */
1018 /* Allow this to show up as unexpected, if there are unknown
1019 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1026 return ODP_FIT_ERROR;
1029 return ODP_FIT_TOO_MUCH;
1031 return ODP_FIT_PERFECT;
1035 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
1039 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
1041 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1042 if (tun_key->tun_id || tun_key->flags & FLOW_TNL_F_KEY) {
1043 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
1045 if (tun_key->ip_src) {
1046 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
1048 if (tun_key->ip_dst) {
1049 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
1051 if (tun_key->ip_tos) {
1052 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
1054 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
1055 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
1056 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
1058 if (tun_key->flags & FLOW_TNL_F_CSUM) {
1059 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
1061 if (tun_key->tp_src) {
1062 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src);
1064 if (tun_key->tp_dst) {
1065 nl_msg_put_be16(a, OVS_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst);
1067 if (tun_key->flags & FLOW_TNL_F_OAM) {
1068 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_OAM);
1071 nl_msg_end_nested(a, tun_key_ofs);
1075 odp_mask_attr_is_wildcard(const struct nlattr *ma)
1077 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
1081 odp_mask_attr_is_exact(const struct nlattr *ma)
1083 bool is_exact = false;
1084 enum ovs_key_attr attr = nl_attr_type(ma);
1086 if (attr == OVS_KEY_ATTR_TUNNEL) {
1087 /* XXX this is a hack for now. Should change
1088 * the exact match dection to per field
1089 * instead of per attribute.
1091 struct flow_tnl tun_mask;
1092 memset(&tun_mask, 0, sizeof tun_mask);
1093 odp_tun_key_from_attr(ma, &tun_mask);
1094 if (tun_mask.flags == (FLOW_TNL_F_KEY
1095 | FLOW_TNL_F_DONT_FRAGMENT
1097 | FLOW_TNL_F_OAM)) {
1098 /* The flags are exact match, check the remaining fields. */
1099 tun_mask.flags = 0xffff;
1100 is_exact = is_all_ones((uint8_t *)&tun_mask,
1101 offsetof(struct flow_tnl, ip_ttl));
1104 is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
1111 odp_portno_names_set(struct hmap *portno_names, odp_port_t port_no,
1114 struct odp_portno_names *odp_portno_names;
1116 odp_portno_names = xmalloc(sizeof *odp_portno_names);
1117 odp_portno_names->port_no = port_no;
1118 odp_portno_names->name = xstrdup(port_name);
1119 hmap_insert(portno_names, &odp_portno_names->hmap_node,
1120 hash_odp_port(port_no));
1124 odp_portno_names_get(const struct hmap *portno_names, odp_port_t port_no)
1126 struct odp_portno_names *odp_portno_names;
1128 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names, hmap_node,
1129 hash_odp_port(port_no), portno_names) {
1130 if (odp_portno_names->port_no == port_no) {
1131 return odp_portno_names->name;
1138 odp_portno_names_destroy(struct hmap *portno_names)
1140 struct odp_portno_names *odp_portno_names, *odp_portno_names_next;
1141 HMAP_FOR_EACH_SAFE (odp_portno_names, odp_portno_names_next,
1142 hmap_node, portno_names) {
1143 hmap_remove(portno_names, &odp_portno_names->hmap_node);
1144 free(odp_portno_names->name);
1145 free(odp_portno_names);
1150 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
1151 const struct hmap *portno_names, struct ds *ds,
1154 struct flow_tnl tun_key;
1155 enum ovs_key_attr attr = nl_attr_type(a);
1156 char namebuf[OVS_KEY_ATTR_BUFSIZE];
1160 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
1162 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
1165 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
1166 if (expected_len != -2) {
1167 bool bad_key_len = nl_attr_get_size(a) != expected_len;
1168 bool bad_mask_len = ma && nl_attr_get_size(ma) != expected_len;
1170 if (bad_key_len || bad_mask_len) {
1172 ds_put_format(ds, "(bad key length %"PRIuSIZE", expected %d)(",
1173 nl_attr_get_size(a), expected_len);
1175 format_generic_odp_key(a, ds);
1177 ds_put_char(ds, '/');
1179 ds_put_format(ds, "(bad mask length %"PRIuSIZE", expected %d)(",
1180 nl_attr_get_size(ma), expected_len);
1182 format_generic_odp_key(ma, ds);
1184 ds_put_char(ds, ')');
1190 ds_put_char(ds, '(');
1192 case OVS_KEY_ATTR_ENCAP:
1193 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
1194 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
1195 nl_attr_get(ma), nl_attr_get_size(ma), NULL, ds,
1197 } else if (nl_attr_get_size(a)) {
1198 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, NULL,
1203 case OVS_KEY_ATTR_PRIORITY:
1204 case OVS_KEY_ATTR_SKB_MARK:
1205 case OVS_KEY_ATTR_DP_HASH:
1206 case OVS_KEY_ATTR_RECIRC_ID:
1207 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
1209 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1213 case OVS_KEY_ATTR_TUNNEL:
1214 memset(&tun_key, 0, sizeof tun_key);
1215 if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
1216 ds_put_format(ds, "error");
1217 } else if (!is_exact) {
1218 struct flow_tnl tun_mask;
1220 memset(&tun_mask, 0, sizeof tun_mask);
1221 odp_tun_key_from_attr(ma, &tun_mask);
1222 ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
1223 ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1224 ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
1226 ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
1227 IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1228 IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1229 tun_key.ip_tos, tun_mask.ip_tos,
1230 tun_key.ip_ttl, tun_mask.ip_ttl);
1232 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1234 /* XXX This code is correct, but enabling it would break the unit
1235 test. Disable it for now until the input parser is fixed.
1237 ds_put_char(ds, '/');
1238 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1240 ds_put_char(ds, ')');
1242 ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1243 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1244 ntohll(tun_key.tun_id),
1245 IP_ARGS(tun_key.ip_src),
1246 IP_ARGS(tun_key.ip_dst),
1247 tun_key.ip_tos, tun_key.ip_ttl);
1249 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1250 ds_put_char(ds, ')');
1254 case OVS_KEY_ATTR_IN_PORT:
1255 if (portno_names && verbose && is_exact) {
1256 char *name = odp_portno_names_get(portno_names,
1257 u32_to_odp(nl_attr_get_u32(a)));
1259 ds_put_format(ds, "%s", name);
1261 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1264 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1266 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1271 case OVS_KEY_ATTR_ETHERNET:
1273 const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1274 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1276 ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1277 ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1278 ETH_ADDR_ARGS(eth_key->eth_src),
1279 ETH_ADDR_ARGS(eth_mask->eth_src),
1280 ETH_ADDR_ARGS(eth_key->eth_dst),
1281 ETH_ADDR_ARGS(eth_mask->eth_dst));
1283 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1285 ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1286 ETH_ADDR_ARGS(eth_key->eth_src),
1287 ETH_ADDR_ARGS(eth_key->eth_dst));
1291 case OVS_KEY_ATTR_VLAN:
1293 ovs_be16 vlan_tci = nl_attr_get_be16(a);
1295 ovs_be16 mask = nl_attr_get_be16(ma);
1296 ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1297 vlan_tci_to_vid(vlan_tci),
1298 vlan_tci_to_vid(mask),
1299 vlan_tci_to_pcp(vlan_tci),
1300 vlan_tci_to_pcp(mask),
1301 vlan_tci_to_cfi(vlan_tci),
1302 vlan_tci_to_cfi(mask));
1304 format_vlan_tci(ds, vlan_tci);
1309 case OVS_KEY_ATTR_MPLS: {
1310 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1311 const struct ovs_key_mpls *mpls_mask = NULL;
1312 size_t size = nl_attr_get_size(a);
1314 if (!size || size % sizeof *mpls_key) {
1315 ds_put_format(ds, "(bad key length %"PRIuSIZE")", size);
1319 mpls_mask = nl_attr_get(ma);
1320 if (size != nl_attr_get_size(ma)) {
1321 ds_put_format(ds, "(key length %"PRIuSIZE" != "
1322 "mask length %"PRIuSIZE")",
1323 size, nl_attr_get_size(ma));
1327 format_mpls(ds, mpls_key, mpls_mask, size / sizeof *mpls_key);
1331 case OVS_KEY_ATTR_ETHERTYPE:
1332 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1334 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1338 case OVS_KEY_ATTR_IPV4:
1340 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1341 const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1343 ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1344 ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1345 ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1346 IP_ARGS(ipv4_key->ipv4_src),
1347 IP_ARGS(ipv4_mask->ipv4_src),
1348 IP_ARGS(ipv4_key->ipv4_dst),
1349 IP_ARGS(ipv4_mask->ipv4_dst),
1350 ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1351 ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1352 ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1353 ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1354 ipv4_mask->ipv4_frag);
1356 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1358 ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1359 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1360 IP_ARGS(ipv4_key->ipv4_src),
1361 IP_ARGS(ipv4_key->ipv4_dst),
1362 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1364 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1368 case OVS_KEY_ATTR_IPV6:
1370 const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1371 char src_str[INET6_ADDRSTRLEN];
1372 char dst_str[INET6_ADDRSTRLEN];
1373 char src_mask[INET6_ADDRSTRLEN];
1374 char dst_mask[INET6_ADDRSTRLEN];
1376 ipv6_key = nl_attr_get(a);
1377 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1378 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1380 ipv6_mask = nl_attr_get(ma);
1381 inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1382 inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1384 ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1385 ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1386 ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1387 src_str, src_mask, dst_str, dst_mask,
1388 ntohl(ipv6_key->ipv6_label),
1389 ntohl(ipv6_mask->ipv6_label),
1390 ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1391 ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1392 ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1393 ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1394 ipv6_mask->ipv6_frag);
1396 const struct ovs_key_ipv6 *ipv6_key;
1397 char src_str[INET6_ADDRSTRLEN];
1398 char dst_str[INET6_ADDRSTRLEN];
1400 ipv6_key = nl_attr_get(a);
1401 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1402 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1404 ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1405 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1406 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1407 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1408 ipv6_key->ipv6_hlimit,
1409 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1413 case OVS_KEY_ATTR_TCP:
1415 const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1416 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1418 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1419 ",dst=%"PRIu16"/%#"PRIx16,
1420 ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1421 ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1423 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1425 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1426 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1430 case OVS_KEY_ATTR_TCP_FLAGS:
1431 ds_put_format(ds, "0x%03"PRIx16, ntohs(nl_attr_get_be16(a)));
1433 ds_put_format(ds, "/0x%03"PRIx16, ntohs(nl_attr_get_be16(ma)));
1437 case OVS_KEY_ATTR_UDP:
1439 const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1440 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1442 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1443 ",dst=%"PRIu16"/%#"PRIx16,
1444 ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1445 ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1447 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1449 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1450 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1454 case OVS_KEY_ATTR_SCTP:
1456 const struct ovs_key_sctp *sctp_mask = nl_attr_get(ma);
1457 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1459 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1460 ",dst=%"PRIu16"/%#"PRIx16,
1461 ntohs(sctp_key->sctp_src), ntohs(sctp_mask->sctp_src),
1462 ntohs(sctp_key->sctp_dst), ntohs(sctp_mask->sctp_dst));
1464 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1466 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1467 ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst));
1471 case OVS_KEY_ATTR_ICMP:
1473 const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1474 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1476 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1477 icmp_key->icmp_type, icmp_mask->icmp_type,
1478 icmp_key->icmp_code, icmp_mask->icmp_code);
1480 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1482 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1483 icmp_key->icmp_type, icmp_key->icmp_code);
1487 case OVS_KEY_ATTR_ICMPV6:
1489 const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1490 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1492 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1493 icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1494 icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1496 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1498 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1499 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1503 case OVS_KEY_ATTR_ARP:
1505 const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1506 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1508 ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1509 ",op=%"PRIu16"/%#"PRIx16
1510 ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1511 ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1512 IP_ARGS(arp_key->arp_sip),
1513 IP_ARGS(arp_mask->arp_sip),
1514 IP_ARGS(arp_key->arp_tip),
1515 IP_ARGS(arp_mask->arp_tip),
1516 ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1517 ETH_ADDR_ARGS(arp_key->arp_sha),
1518 ETH_ADDR_ARGS(arp_mask->arp_sha),
1519 ETH_ADDR_ARGS(arp_key->arp_tha),
1520 ETH_ADDR_ARGS(arp_mask->arp_tha));
1522 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1524 ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1525 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1526 IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1527 ntohs(arp_key->arp_op),
1528 ETH_ADDR_ARGS(arp_key->arp_sha),
1529 ETH_ADDR_ARGS(arp_key->arp_tha));
1533 case OVS_KEY_ATTR_ND: {
1534 const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1535 char target[INET6_ADDRSTRLEN];
1537 nd_key = nl_attr_get(a);
1539 nd_mask = nl_attr_get(ma);
1542 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1543 ds_put_format(ds, "target=%s", target);
1545 inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1546 ds_put_format(ds, "/%s", target);
1549 if (!eth_addr_is_zero(nd_key->nd_sll)) {
1550 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1551 ETH_ADDR_ARGS(nd_key->nd_sll));
1553 ds_put_format(ds, "/"ETH_ADDR_FMT,
1554 ETH_ADDR_ARGS(nd_mask->nd_sll));
1557 if (!eth_addr_is_zero(nd_key->nd_tll)) {
1558 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1559 ETH_ADDR_ARGS(nd_key->nd_tll));
1561 ds_put_format(ds, "/"ETH_ADDR_FMT,
1562 ETH_ADDR_ARGS(nd_mask->nd_tll));
1567 case OVS_KEY_ATTR_UNSPEC:
1568 case __OVS_KEY_ATTR_MAX:
1570 format_generic_odp_key(a, ds);
1572 ds_put_char(ds, '/');
1573 format_generic_odp_key(ma, ds);
1577 ds_put_char(ds, ')');
1580 static struct nlattr *
1581 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1583 const struct nlattr *a;
1585 int type = nl_attr_type(key);
1586 int size = nl_attr_get_size(key);
1588 if (odp_flow_key_attr_len(type) >=0) {
1589 nl_msg_put_unspec_zero(ofp, type, size);
1593 nested_mask = nl_msg_start_nested(ofp, type);
1594 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1595 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1597 nl_msg_end_nested(ofp, nested_mask);
1600 return ofpbuf_base(ofp);
1603 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1604 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1605 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
1606 * non-null and 'verbose' is true, translates odp port number to its name. */
1608 odp_flow_format(const struct nlattr *key, size_t key_len,
1609 const struct nlattr *mask, size_t mask_len,
1610 const struct hmap *portno_names, struct ds *ds, bool verbose)
1613 const struct nlattr *a;
1615 bool has_ethtype_key = false;
1616 const struct nlattr *ma = NULL;
1618 bool first_field = true;
1620 ofpbuf_init(&ofp, 100);
1621 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1622 bool is_nested_attr;
1623 bool is_wildcard = false;
1624 int attr_type = nl_attr_type(a);
1626 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1627 has_ethtype_key = true;
1630 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1632 if (mask && mask_len) {
1633 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1634 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1637 if (verbose || !is_wildcard || is_nested_attr) {
1638 if (is_wildcard && !ma) {
1639 ma = generate_all_wildcard_mask(&ofp, a);
1642 ds_put_char(ds, ',');
1644 format_odp_key_attr(a, ma, portno_names, ds, verbose);
1645 first_field = false;
1649 ofpbuf_uninit(&ofp);
1654 if (left == key_len) {
1655 ds_put_cstr(ds, "<empty>");
1657 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1658 for (i = 0; i < left; i++) {
1659 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1661 ds_put_char(ds, ')');
1663 if (!has_ethtype_key) {
1664 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1666 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1667 ntohs(nl_attr_get_be16(ma)));
1671 ds_put_cstr(ds, "<empty>");
1675 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1676 * OVS_KEY_ATTR_* attributes in 'key'. */
1678 odp_flow_key_format(const struct nlattr *key,
1679 size_t key_len, struct ds *ds)
1681 odp_flow_format(key, key_len, NULL, 0, NULL, ds, true);
1685 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1686 const uint8_t *nd_tll, struct ofpbuf *key)
1689 memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1693 memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1696 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1700 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1701 const uint8_t *nd_tll, struct ofpbuf *key)
1703 struct ovs_key_nd nd_key;
1705 memset(&nd_key, 0, sizeof nd_key);
1707 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1711 put_nd(&nd_key, nd_sll, nd_tll, key);
1716 put_nd_mask(int n, const char *nd_target_s,
1717 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1719 struct ovs_key_nd nd_mask;
1721 memset(&nd_mask, 0xff, sizeof nd_mask);
1723 if (strlen(nd_target_s) != 0 &&
1724 inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1728 put_nd(&nd_mask, nd_sll, nd_tll, mask);
1733 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1735 if (!strcasecmp(s, "no")) {
1736 *type = OVS_FRAG_TYPE_NONE;
1737 } else if (!strcasecmp(s, "first")) {
1738 *type = OVS_FRAG_TYPE_FIRST;
1739 } else if (!strcasecmp(s, "later")) {
1740 *type = OVS_FRAG_TYPE_LATER;
1748 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1750 return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1751 (mpls_tc << MPLS_TC_SHIFT) |
1752 (mpls_ttl << MPLS_TTL_SHIFT) |
1753 (mpls_bos << MPLS_BOS_SHIFT)));
1757 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1758 struct ofpbuf *key, struct ofpbuf *mask)
1762 uint32_t priority_mask;
1765 if (mask && ovs_scan(s, "skb_priority(%"SCNi32"/%"SCNi32")%n",
1766 &priority, &priority_mask, &n)) {
1767 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1768 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1770 } else if (ovs_scan(s, "skb_priority(%"SCNi32")%n", &priority, &n)) {
1771 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1773 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1784 if (mask && ovs_scan(s, "skb_mark(%"SCNi32"/%"SCNi32")%n", &mark,
1786 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1787 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1789 } else if (ovs_scan(s, "skb_mark(%"SCNi32")%n", &mark, &n)) {
1790 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1792 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1802 if (ovs_scan(s, "recirc_id(%"SCNi32")%n", &recirc_id, &n)) {
1803 nl_msg_put_u32(key, OVS_KEY_ATTR_RECIRC_ID, recirc_id);
1805 nl_msg_put_u32(mask, OVS_KEY_ATTR_RECIRC_ID, UINT32_MAX);
1813 uint32_t dp_hash_mask;
1816 if (mask && ovs_scan(s, "dp_hash(%"SCNi32"/%"SCNi32")%n", &dp_hash,
1817 &dp_hash_mask, &n)) {
1818 nl_msg_put_u32(key, OVS_KEY_ATTR_DP_HASH, dp_hash);
1819 nl_msg_put_u32(mask, OVS_KEY_ATTR_DP_HASH, dp_hash_mask);
1821 } else if (ovs_scan(s, "dp_hash(%"SCNi32")%n", &dp_hash, &n)) {
1822 nl_msg_put_u32(key, OVS_KEY_ATTR_DP_HASH, dp_hash);
1824 nl_msg_put_u32(mask, OVS_KEY_ATTR_DP_HASH, UINT32_MAX);
1831 uint64_t tun_id, tun_id_mask;
1832 struct flow_tnl tun_key, tun_key_mask;
1835 memset(&tun_key, 0, sizeof tun_key);
1836 memset(&tun_key_mask, 0, sizeof tun_key_mask);
1838 if (mask && ovs_scan(s, "tunnel(tun_id=%"SCNi64"/%"SCNi64","
1839 "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1840 "/"IP_SCAN_FMT",tos=%"SCNi8"/%"SCNi8","
1841 "ttl=%"SCNi8"/%"SCNi8",flags%n",
1842 &tun_id, &tun_id_mask,
1843 IP_SCAN_ARGS(&tun_key.ip_src),
1844 IP_SCAN_ARGS(&tun_key_mask.ip_src),
1845 IP_SCAN_ARGS(&tun_key.ip_dst),
1846 IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1847 &tun_key.ip_tos, &tun_key_mask.ip_tos,
1848 &tun_key.ip_ttl, &tun_key_mask.ip_ttl, &n)) {
1852 tun_key.tun_id = htonll(tun_id);
1853 tun_key_mask.tun_id = htonll(tun_id_mask);
1854 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1855 tun_key.flags = flags;
1856 tun_key_mask.flags = UINT16_MAX;
1866 tun_key_to_attr(key, &tun_key);
1868 tun_key_to_attr(mask, &tun_key_mask);
1871 } else if (ovs_scan(s, "tunnel(tun_id=%"SCNi64","
1872 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1873 ",tos=%"SCNi8",ttl=%"SCNi8",flags%n", &tun_id,
1874 IP_SCAN_ARGS(&tun_key.ip_src),
1875 IP_SCAN_ARGS(&tun_key.ip_dst),
1876 &tun_key.ip_tos, &tun_key.ip_ttl, &n)) {
1880 tun_key.tun_id = htonll(tun_id);
1881 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1882 tun_key.flags = flags;
1892 tun_key_to_attr(key, &tun_key);
1895 memset(&tun_key, 0xff, sizeof tun_key);
1896 tun_key_to_attr(mask, &tun_key);
1904 uint32_t in_port_mask;
1907 if (mask && ovs_scan(s, "in_port(%"SCNi32"/%"SCNi32")%n",
1908 &in_port, &in_port_mask, &n)) {
1909 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1910 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1912 } else if (ovs_scan(s, "in_port(%"SCNi32")%n", &in_port, &n)) {
1913 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1915 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1922 if (port_names && !strncmp(s, "in_port(", 8)) {
1924 const struct simap_node *node;
1928 name_len = strcspn(name, ")");
1929 node = simap_find_len(port_names, name, name_len);
1931 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1934 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1936 return 8 + name_len + 1;
1941 struct ovs_key_ethernet eth_key;
1942 struct ovs_key_ethernet eth_key_mask;
1945 if (mask && ovs_scan(s,
1946 "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1947 "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1948 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1949 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1950 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1951 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n)) {
1952 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1953 ð_key, sizeof eth_key);
1954 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1955 ð_key_mask, sizeof eth_key_mask);
1957 } else if (ovs_scan(s, "eth(src="ETH_ADDR_SCAN_FMT","
1958 "dst="ETH_ADDR_SCAN_FMT")%n",
1959 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1960 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n)) {
1961 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1962 ð_key, sizeof eth_key);
1965 memset(ð_key, 0xff, sizeof eth_key);
1966 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1967 ð_key, sizeof eth_key);
1979 if (mask && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i)%n",
1980 &vid, &vid_mask, &pcp, &pcp_mask, &n)) {
1981 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1982 htons((vid << VLAN_VID_SHIFT) |
1983 (pcp << VLAN_PCP_SHIFT) |
1985 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1986 htons((vid_mask << VLAN_VID_SHIFT) |
1987 (pcp_mask << VLAN_PCP_SHIFT) |
1988 (1 << VLAN_CFI_SHIFT)));
1990 } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n)) {
1991 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1992 htons((vid << VLAN_VID_SHIFT) |
1993 (pcp << VLAN_PCP_SHIFT) |
1996 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
2000 && ovs_scan(s, "vlan(vid=%i/%i,pcp=%i/%i,cfi=%i/%i)%n",
2001 &vid, &vid_mask, &pcp, &pcp_mask,
2002 &cfi, &cfi_mask, &n)) {
2003 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
2004 htons((vid << VLAN_VID_SHIFT) |
2005 (pcp << VLAN_PCP_SHIFT) |
2006 (cfi ? VLAN_CFI : 0)));
2007 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
2008 htons((vid_mask << VLAN_VID_SHIFT) |
2009 (pcp_mask << VLAN_PCP_SHIFT) |
2010 (cfi_mask << VLAN_CFI_SHIFT)));
2012 } else if (ovs_scan(s, "vlan(vid=%i,pcp=%i,cfi=%i)%n",
2013 &vid, &pcp, &cfi, &n)) {
2014 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
2015 htons((vid << VLAN_VID_SHIFT) |
2016 (pcp << VLAN_PCP_SHIFT) |
2017 (cfi ? VLAN_CFI : 0)));
2019 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
2030 if (mask && ovs_scan(s, "eth_type(%i/%i)%n",
2031 ð_type, ð_type_mask, &n)) {
2032 if (eth_type != 0) {
2033 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
2035 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
2037 } else if (ovs_scan(s, "eth_type(%i)%n", ð_type, &n)) {
2038 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
2040 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2047 int label, tc, ttl, bos;
2048 int label_mask, tc_mask, ttl_mask, bos_mask;
2051 if (mask && ovs_scan(s, "mpls(label=%i/%i,tc=%i/%i,"
2052 "ttl=%i/%i,bos=%i/%i)%n",
2053 &label, &label_mask, &tc, &tc_mask,
2054 &ttl, &ttl_mask, &bos, &bos_mask, &n)) {
2055 struct ovs_key_mpls *mpls, *mpls_mask;
2057 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
2059 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
2061 mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
2063 mpls_mask->mpls_lse = mpls_lse_from_components(
2064 label_mask, tc_mask, ttl_mask, bos_mask);
2066 } else if (ovs_scan(s, "mpls(label=%i,tc=%i,ttl=%i,bos=%i)%n",
2067 &label, &tc, &ttl, &bos, &n)) {
2068 struct ovs_key_mpls *mpls;
2070 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
2072 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
2074 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
2076 mpls->mpls_lse = OVS_BE32_MAX;
2084 struct ovs_key_ipv4 ipv4_key;
2085 struct ovs_key_ipv4 ipv4_mask;
2088 enum ovs_frag_type ipv4_frag;
2092 && ovs_scan(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
2093 "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
2094 "proto=%"SCNi8"/%"SCNi8","
2095 "tos=%"SCNi8"/%"SCNi8","
2096 "ttl=%"SCNi8"/%"SCNi8","
2097 "frag=%7[a-z]/%"SCNi8")%n",
2098 IP_SCAN_ARGS(&ipv4_key.ipv4_src),
2099 IP_SCAN_ARGS(&ipv4_mask.ipv4_src),
2100 IP_SCAN_ARGS(&ipv4_key.ipv4_dst),
2101 IP_SCAN_ARGS(&ipv4_mask.ipv4_dst),
2102 &ipv4_key.ipv4_proto, &ipv4_mask.ipv4_proto,
2103 &ipv4_key.ipv4_tos, &ipv4_mask.ipv4_tos,
2104 &ipv4_key.ipv4_ttl, &ipv4_mask.ipv4_ttl,
2105 frag, &ipv4_mask.ipv4_frag, &n)
2106 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
2107 ipv4_key.ipv4_frag = ipv4_frag;
2108 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
2109 &ipv4_key, sizeof ipv4_key);
2111 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
2112 &ipv4_mask, sizeof ipv4_mask);
2114 } else if (ovs_scan(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
2115 "proto=%"SCNi8",tos=%"SCNi8",ttl=%"SCNi8","
2117 IP_SCAN_ARGS(&ipv4_key.ipv4_src),
2118 IP_SCAN_ARGS(&ipv4_key.ipv4_dst),
2119 &ipv4_key.ipv4_proto,
2123 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
2124 ipv4_key.ipv4_frag = ipv4_frag;
2125 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
2126 &ipv4_key, sizeof ipv4_key);
2129 memset(&ipv4_key, 0xff, sizeof ipv4_key);
2130 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
2131 &ipv4_key, sizeof ipv4_key);
2138 char ipv6_src_s[IPV6_SCAN_LEN + 1];
2139 char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
2140 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
2141 char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
2142 int ipv6_label, ipv6_label_mask;
2143 int ipv6_proto, ipv6_proto_mask;
2144 int ipv6_tclass, ipv6_tclass_mask;
2145 int ipv6_hlimit, ipv6_hlimit_mask;
2147 enum ovs_frag_type ipv6_frag;
2151 if (mask && ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
2152 IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
2153 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
2154 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
2155 ipv6_src_s, ipv6_src_mask_s,
2156 ipv6_dst_s, ipv6_dst_mask_s,
2157 &ipv6_label, &ipv6_label_mask, &ipv6_proto,
2158 &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
2159 &ipv6_hlimit, &ipv6_hlimit_mask, frag,
2160 &ipv6_frag_mask, &n)
2161 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
2162 struct ovs_key_ipv6 ipv6_key;
2163 struct ovs_key_ipv6 ipv6_mask;
2165 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
2166 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
2167 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
2168 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
2172 ipv6_key.ipv6_label = htonl(ipv6_label);
2173 ipv6_key.ipv6_proto = ipv6_proto;
2174 ipv6_key.ipv6_tclass = ipv6_tclass;
2175 ipv6_key.ipv6_hlimit = ipv6_hlimit;
2176 ipv6_key.ipv6_frag = ipv6_frag;
2177 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
2178 &ipv6_key, sizeof ipv6_key);
2180 ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
2181 ipv6_mask.ipv6_proto = ipv6_proto_mask;
2182 ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
2183 ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
2184 ipv6_mask.ipv6_frag = ipv6_frag_mask;
2185 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
2186 &ipv6_mask, sizeof ipv6_mask);
2188 } else if (ovs_scan(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
2189 "label=%i,proto=%i,tclass=%i,hlimit=%i,"
2191 ipv6_src_s, ipv6_dst_s, &ipv6_label,
2192 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n)
2193 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
2194 struct ovs_key_ipv6 ipv6_key;
2196 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
2197 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
2200 ipv6_key.ipv6_label = htonl(ipv6_label);
2201 ipv6_key.ipv6_proto = ipv6_proto;
2202 ipv6_key.ipv6_tclass = ipv6_tclass;
2203 ipv6_key.ipv6_hlimit = ipv6_hlimit;
2204 ipv6_key.ipv6_frag = ipv6_frag;
2205 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
2206 &ipv6_key, sizeof ipv6_key);
2209 memset(&ipv6_key, 0xff, sizeof ipv6_key);
2210 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
2211 &ipv6_key, sizeof ipv6_key);
2224 if (mask && ovs_scan(s, "tcp(src=%i/%i,dst=%i/%i)%n",
2225 &tcp_src, &tcp_src_mask, &tcp_dst,
2226 &tcp_dst_mask, &n)) {
2227 struct ovs_key_tcp tcp_key;
2228 struct ovs_key_tcp tcp_mask;
2230 tcp_key.tcp_src = htons(tcp_src);
2231 tcp_key.tcp_dst = htons(tcp_dst);
2232 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2234 tcp_mask.tcp_src = htons(tcp_src_mask);
2235 tcp_mask.tcp_dst = htons(tcp_dst_mask);
2236 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2237 &tcp_mask, sizeof tcp_mask);
2239 } else if (ovs_scan(s, "tcp(src=%i,dst=%i)%n",
2240 &tcp_src, &tcp_dst, &n)) {
2241 struct ovs_key_tcp tcp_key;
2243 tcp_key.tcp_src = htons(tcp_src);
2244 tcp_key.tcp_dst = htons(tcp_dst);
2245 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2248 memset(&tcp_key, 0xff, sizeof tcp_key);
2249 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2250 &tcp_key, sizeof tcp_key);
2257 uint16_t tcp_flags, tcp_flags_mask;
2260 if (mask && ovs_scan(s, "tcp_flags(%"SCNi16"/%"SCNi16")%n",
2261 &tcp_flags, &tcp_flags_mask, &n) > 0 && n > 0) {
2262 nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags));
2263 nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags_mask));
2265 } else if (ovs_scan(s, "tcp_flags(%"SCNi16")%n", &tcp_flags, &n)) {
2266 nl_msg_put_be16(key, OVS_KEY_ATTR_TCP_FLAGS, htons(tcp_flags));
2268 nl_msg_put_be16(mask, OVS_KEY_ATTR_TCP_FLAGS,
2282 if (mask && ovs_scan(s, "udp(src=%i/%i,dst=%i/%i)%n",
2283 &udp_src, &udp_src_mask,
2284 &udp_dst, &udp_dst_mask, &n)) {
2285 struct ovs_key_udp udp_key;
2286 struct ovs_key_udp udp_mask;
2288 udp_key.udp_src = htons(udp_src);
2289 udp_key.udp_dst = htons(udp_dst);
2290 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2292 udp_mask.udp_src = htons(udp_src_mask);
2293 udp_mask.udp_dst = htons(udp_dst_mask);
2294 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2295 &udp_mask, sizeof udp_mask);
2298 if (ovs_scan(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n)) {
2299 struct ovs_key_udp udp_key;
2301 udp_key.udp_src = htons(udp_src);
2302 udp_key.udp_dst = htons(udp_dst);
2303 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2306 memset(&udp_key, 0xff, sizeof udp_key);
2307 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2320 if (mask && ovs_scan(s, "sctp(src=%i/%i,dst=%i/%i)%n",
2321 &sctp_src, &sctp_src_mask,
2322 &sctp_dst, &sctp_dst_mask, &n)) {
2323 struct ovs_key_sctp sctp_key;
2324 struct ovs_key_sctp sctp_mask;
2326 sctp_key.sctp_src = htons(sctp_src);
2327 sctp_key.sctp_dst = htons(sctp_dst);
2328 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2330 sctp_mask.sctp_src = htons(sctp_src_mask);
2331 sctp_mask.sctp_dst = htons(sctp_dst_mask);
2332 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP,
2333 &sctp_mask, sizeof sctp_mask);
2336 if (ovs_scan(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n)) {
2337 struct ovs_key_sctp sctp_key;
2339 sctp_key.sctp_src = htons(sctp_src);
2340 sctp_key.sctp_dst = htons(sctp_dst);
2341 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2344 memset(&sctp_key, 0xff, sizeof sctp_key);
2345 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2352 struct ovs_key_icmp icmp_key;
2353 struct ovs_key_icmp icmp_mask;
2356 if (mask && ovs_scan(s, "icmp(type=%"SCNi8"/%"SCNi8","
2357 "code=%"SCNi8"/%"SCNi8")%n",
2358 &icmp_key.icmp_type, &icmp_mask.icmp_type,
2359 &icmp_key.icmp_code, &icmp_mask.icmp_code, &n)) {
2360 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2361 &icmp_key, sizeof icmp_key);
2362 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2363 &icmp_mask, sizeof icmp_mask);
2365 } else if (ovs_scan(s, "icmp(type=%"SCNi8",code=%"SCNi8")%n",
2366 &icmp_key.icmp_type, &icmp_key.icmp_code, &n)) {
2367 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2368 &icmp_key, sizeof icmp_key);
2370 memset(&icmp_key, 0xff, sizeof icmp_key);
2371 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2379 struct ovs_key_icmpv6 icmpv6_key;
2380 struct ovs_key_icmpv6 icmpv6_mask;
2383 if (mask && ovs_scan(s, "icmpv6(type=%"SCNi8"/%"SCNi8","
2384 "code=%"SCNi8"/%"SCNi8")%n",
2385 &icmpv6_key.icmpv6_type, &icmpv6_mask.icmpv6_type,
2386 &icmpv6_key.icmpv6_code, &icmpv6_mask.icmpv6_code,
2388 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2389 &icmpv6_key, sizeof icmpv6_key);
2390 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2391 sizeof icmpv6_mask);
2393 } else if (ovs_scan(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2394 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,
2396 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2397 &icmpv6_key, sizeof icmpv6_key);
2400 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2401 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2409 struct ovs_key_arp arp_key;
2410 struct ovs_key_arp arp_mask;
2411 uint16_t arp_op, arp_op_mask;
2414 if (mask && ovs_scan(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2415 "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2416 "op=%"SCNi16"/%"SCNi16","
2417 "sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2418 "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2419 IP_SCAN_ARGS(&arp_key.arp_sip),
2420 IP_SCAN_ARGS(&arp_mask.arp_sip),
2421 IP_SCAN_ARGS(&arp_key.arp_tip),
2422 IP_SCAN_ARGS(&arp_mask.arp_tip),
2423 &arp_op, &arp_op_mask,
2424 ETH_ADDR_SCAN_ARGS(arp_key.arp_sha),
2425 ETH_ADDR_SCAN_ARGS(arp_mask.arp_sha),
2426 ETH_ADDR_SCAN_ARGS(arp_key.arp_tha),
2427 ETH_ADDR_SCAN_ARGS(arp_mask.arp_tha), &n)) {
2428 arp_key.arp_op = htons(arp_op);
2429 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2430 arp_mask.arp_op = htons(arp_op_mask);
2431 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2432 &arp_mask, sizeof arp_mask);
2434 } else if (ovs_scan(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2435 "op=%"SCNi16",sha="ETH_ADDR_SCAN_FMT","
2436 "tha="ETH_ADDR_SCAN_FMT")%n",
2437 IP_SCAN_ARGS(&arp_key.arp_sip),
2438 IP_SCAN_ARGS(&arp_key.arp_tip),
2440 ETH_ADDR_SCAN_ARGS(arp_key.arp_sha),
2441 ETH_ADDR_SCAN_ARGS(arp_key.arp_tha), &n)) {
2442 arp_key.arp_op = htons(arp_op);
2443 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2446 memset(&arp_key, 0xff, sizeof arp_key);
2447 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2448 &arp_key, sizeof arp_key);
2455 char nd_target_s[IPV6_SCAN_LEN + 1];
2456 char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2457 uint8_t nd_sll[ETH_ADDR_LEN];
2458 uint8_t nd_sll_mask[ETH_ADDR_LEN];
2459 uint8_t nd_tll[ETH_ADDR_LEN];
2460 uint8_t nd_tll_mask[ETH_ADDR_LEN];
2463 nd_target_mask_s[0] = 0;
2464 memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2465 memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2467 if (mask && ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2468 nd_target_s, nd_target_mask_s, &n)) {
2469 put_nd_key(n, nd_target_s, NULL, NULL, key);
2470 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2471 } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT")%n",
2473 put_nd_key(n, nd_target_s, NULL, NULL, key);
2475 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2478 ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2479 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2480 nd_target_s, nd_target_mask_s,
2481 ETH_ADDR_SCAN_ARGS(nd_sll),
2482 ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n)) {
2483 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2484 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2485 } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT","
2486 "sll="ETH_ADDR_SCAN_FMT")%n",
2487 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n)) {
2488 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2490 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2493 ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2494 ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2495 nd_target_s, nd_target_mask_s,
2496 ETH_ADDR_SCAN_ARGS(nd_tll),
2497 ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n)) {
2498 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2499 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2500 } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT","
2501 "tll="ETH_ADDR_SCAN_FMT")%n",
2502 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n)) {
2503 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2505 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2508 ovs_scan(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2509 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2510 "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2511 nd_target_s, nd_target_mask_s,
2512 ETH_ADDR_SCAN_ARGS(nd_sll),
2513 ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2514 ETH_ADDR_SCAN_ARGS(nd_tll),
2515 ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2517 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2518 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2519 } else if (ovs_scan(s, "nd(target="IPV6_SCAN_FMT","
2520 "sll="ETH_ADDR_SCAN_FMT","
2521 "tll="ETH_ADDR_SCAN_FMT")%n",
2522 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2523 ETH_ADDR_SCAN_ARGS(nd_tll), &n)) {
2524 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2526 put_nd_mask(n, nd_target_mask_s,
2527 nd_sll_mask, nd_tll_mask, mask);
2536 if (!strncmp(s, "encap(", 6)) {
2537 const char *start = s;
2538 size_t encap, encap_mask = 0;
2540 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2542 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2549 s += strspn(s, ", \t\r\n");
2552 } else if (*s == ')') {
2556 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2564 nl_msg_end_nested(key, encap);
2566 nl_msg_end_nested(mask, encap_mask);
2575 /* Parses the string representation of a datapath flow key, in the
2576 * format output by odp_flow_key_format(). Returns 0 if successful,
2577 * otherwise a positive errno value. On success, the flow key is
2578 * appended to 'key' as a series of Netlink attributes. On failure, no
2579 * data is appended to 'key'. Either way, 'key''s data might be
2582 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2583 * to a port number. (Port names may be used instead of port numbers in
2586 * On success, the attributes appended to 'key' are individually syntactically
2587 * valid, but they may not be valid as a sequence. 'key' might, for example,
2588 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2590 odp_flow_from_string(const char *s, const struct simap *port_names,
2591 struct ofpbuf *key, struct ofpbuf *mask)
2593 const size_t old_size = ofpbuf_size(key);
2597 s += strspn(s, delimiters);
2602 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2604 ofpbuf_set_size(key, old_size);
2614 ovs_to_odp_frag(uint8_t nw_frag)
2616 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2617 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2618 : OVS_FRAG_TYPE_LATER);
2622 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2624 uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2626 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2627 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2633 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *flow,
2634 const struct flow *mask, odp_port_t odp_in_port,
2635 size_t max_mpls_depth, bool recirc, bool export_mask)
2637 struct ovs_key_ethernet *eth_key;
2639 const struct flow *data = export_mask ? mask : flow;
2641 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2643 if (flow->tunnel.ip_dst || export_mask) {
2644 tun_key_to_attr(buf, &data->tunnel);
2647 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2650 nl_msg_put_u32(buf, OVS_KEY_ATTR_RECIRC_ID, data->recirc_id);
2651 nl_msg_put_u32(buf, OVS_KEY_ATTR_DP_HASH, data->dp_hash);
2654 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2655 * is not the magical value "ODPP_NONE". */
2656 if (export_mask || odp_in_port != ODPP_NONE) {
2657 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2660 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2662 memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2663 memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2665 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2667 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2669 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2671 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2672 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2673 if (flow->vlan_tci == htons(0)) {
2680 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2681 /* For backwards compatibility with kernels that don't support
2682 * wildcarding, the following convention is used to encode the
2683 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2686 * -------- -------- -------
2687 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2688 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2689 * <none> 0xffff Any non-Ethernet II frame (except valid
2690 * 802.3 SNAP packet with valid eth_type).
2693 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2698 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2700 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2701 struct ovs_key_ipv4 *ipv4_key;
2703 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2705 ipv4_key->ipv4_src = data->nw_src;
2706 ipv4_key->ipv4_dst = data->nw_dst;
2707 ipv4_key->ipv4_proto = data->nw_proto;
2708 ipv4_key->ipv4_tos = data->nw_tos;
2709 ipv4_key->ipv4_ttl = data->nw_ttl;
2710 ipv4_key->ipv4_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2711 : ovs_to_odp_frag(data->nw_frag);
2712 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2713 struct ovs_key_ipv6 *ipv6_key;
2715 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2717 memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2718 memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2719 ipv6_key->ipv6_label = data->ipv6_label;
2720 ipv6_key->ipv6_proto = data->nw_proto;
2721 ipv6_key->ipv6_tclass = data->nw_tos;
2722 ipv6_key->ipv6_hlimit = data->nw_ttl;
2723 ipv6_key->ipv6_frag = export_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2724 : ovs_to_odp_frag(data->nw_frag);
2725 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2726 flow->dl_type == htons(ETH_TYPE_RARP)) {
2727 struct ovs_key_arp *arp_key;
2729 arp_key = nl_msg_put_unspec_zero(buf, OVS_KEY_ATTR_ARP,
2731 arp_key->arp_sip = data->nw_src;
2732 arp_key->arp_tip = data->nw_dst;
2733 arp_key->arp_op = htons(data->nw_proto);
2734 memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2735 memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2736 } else if (eth_type_mpls(flow->dl_type)) {
2737 struct ovs_key_mpls *mpls_key;
2740 n = flow_count_mpls_labels(flow, NULL);
2741 n = MIN(n, max_mpls_depth);
2742 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2743 n * sizeof *mpls_key);
2744 for (i = 0; i < n; i++) {
2745 mpls_key[i].mpls_lse = data->mpls_lse[i];
2749 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2750 if (flow->nw_proto == IPPROTO_TCP) {
2751 struct ovs_key_tcp *tcp_key;
2753 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2755 tcp_key->tcp_src = data->tp_src;
2756 tcp_key->tcp_dst = data->tp_dst;
2758 if (data->tcp_flags) {
2759 nl_msg_put_be16(buf, OVS_KEY_ATTR_TCP_FLAGS, data->tcp_flags);
2761 } else if (flow->nw_proto == IPPROTO_UDP) {
2762 struct ovs_key_udp *udp_key;
2764 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2766 udp_key->udp_src = data->tp_src;
2767 udp_key->udp_dst = data->tp_dst;
2768 } else if (flow->nw_proto == IPPROTO_SCTP) {
2769 struct ovs_key_sctp *sctp_key;
2771 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2773 sctp_key->sctp_src = data->tp_src;
2774 sctp_key->sctp_dst = data->tp_dst;
2775 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2776 && flow->nw_proto == IPPROTO_ICMP) {
2777 struct ovs_key_icmp *icmp_key;
2779 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2781 icmp_key->icmp_type = ntohs(data->tp_src);
2782 icmp_key->icmp_code = ntohs(data->tp_dst);
2783 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2784 && flow->nw_proto == IPPROTO_ICMPV6) {
2785 struct ovs_key_icmpv6 *icmpv6_key;
2787 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2788 sizeof *icmpv6_key);
2789 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2790 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2792 if (flow->tp_dst == htons(0)
2793 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)
2794 || flow->tp_src == htons(ND_NEIGHBOR_ADVERT))
2795 && (!export_mask || (data->tp_src == htons(0xffff)
2796 && data->tp_dst == htons(0xffff)))) {
2798 struct ovs_key_nd *nd_key;
2800 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2802 memcpy(nd_key->nd_target, &data->nd_target,
2803 sizeof nd_key->nd_target);
2804 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2805 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2812 nl_msg_end_nested(buf, encap);
2816 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2817 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2818 * number rather than a datapath port number). Instead, if 'odp_in_port'
2819 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2822 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2823 * capable of being expanded to allow for that much space.
2825 * 'recirc' indicates support for recirculation fields. If this is true, then
2826 * these fields will always be serialised. */
2828 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2829 const struct flow *mask, odp_port_t odp_in_port,
2832 odp_flow_key_from_flow__(buf, flow, mask, odp_in_port, SIZE_MAX, recirc,
2836 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2837 * 'buf'. 'flow' is used as a template to determine how to interpret
2838 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2839 * it doesn't indicate whether the other fields should be interpreted as
2840 * ARP, IPv4, IPv6, etc.
2842 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2843 * capable of being expanded to allow for that much space.
2845 * 'recirc' indicates support for recirculation fields. If this is true, then
2846 * these fields will always be serialised. */
2848 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2849 const struct flow *flow, uint32_t odp_in_port_mask,
2850 size_t max_mpls_depth, bool recirc)
2852 odp_flow_key_from_flow__(buf, flow, mask, u32_to_odp(odp_in_port_mask),
2853 max_mpls_depth, recirc, true);
2856 /* Generate ODP flow key from the given packet metadata */
2858 odp_key_from_pkt_metadata(struct ofpbuf *buf, const struct pkt_metadata *md)
2860 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, md->skb_priority);
2862 if (md->tunnel.ip_dst) {
2863 tun_key_to_attr(buf, &md->tunnel);
2866 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, md->pkt_mark);
2868 /* Add an ingress port attribute if 'odp_in_port' is not the magical
2869 * value "ODPP_NONE". */
2870 if (md->in_port.odp_port != ODPP_NONE) {
2871 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, md->in_port.odp_port);
2875 /* Generate packet metadata from the given ODP flow key. */
2877 odp_key_to_pkt_metadata(const struct nlattr *key, size_t key_len,
2878 struct pkt_metadata *md)
2880 const struct nlattr *nla;
2882 uint32_t wanted_attrs = 1u << OVS_KEY_ATTR_PRIORITY |
2883 1u << OVS_KEY_ATTR_SKB_MARK | 1u << OVS_KEY_ATTR_TUNNEL |
2884 1u << OVS_KEY_ATTR_IN_PORT;
2886 *md = PKT_METADATA_INITIALIZER(ODPP_NONE);
2888 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2889 uint16_t type = nl_attr_type(nla);
2890 size_t len = nl_attr_get_size(nla);
2891 int expected_len = odp_flow_key_attr_len(type);
2893 if (len != expected_len && expected_len >= 0) {
2898 case OVS_KEY_ATTR_RECIRC_ID:
2899 md->recirc_id = nl_attr_get_u32(nla);
2900 wanted_attrs &= ~(1u << OVS_KEY_ATTR_RECIRC_ID);
2902 case OVS_KEY_ATTR_DP_HASH:
2903 md->dp_hash = nl_attr_get_u32(nla);
2904 wanted_attrs &= ~(1u << OVS_KEY_ATTR_DP_HASH);
2906 case OVS_KEY_ATTR_PRIORITY:
2907 md->skb_priority = nl_attr_get_u32(nla);
2908 wanted_attrs &= ~(1u << OVS_KEY_ATTR_PRIORITY);
2910 case OVS_KEY_ATTR_SKB_MARK:
2911 md->pkt_mark = nl_attr_get_u32(nla);
2912 wanted_attrs &= ~(1u << OVS_KEY_ATTR_SKB_MARK);
2914 case OVS_KEY_ATTR_TUNNEL: {
2915 enum odp_key_fitness res;
2917 res = odp_tun_key_from_attr(nla, &md->tunnel);
2918 if (res == ODP_FIT_ERROR) {
2919 memset(&md->tunnel, 0, sizeof md->tunnel);
2920 } else if (res == ODP_FIT_PERFECT) {
2921 wanted_attrs &= ~(1u << OVS_KEY_ATTR_TUNNEL);
2925 case OVS_KEY_ATTR_IN_PORT:
2926 md->in_port.odp_port = nl_attr_get_odp_port(nla);
2927 wanted_attrs &= ~(1u << OVS_KEY_ATTR_IN_PORT);
2933 if (!wanted_attrs) {
2934 return; /* Have everything. */
2940 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2942 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2943 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2944 key_len / sizeof(uint32_t), 0);
2948 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2949 uint64_t attrs, int out_of_range_attr,
2950 const struct nlattr *key, size_t key_len)
2955 if (VLOG_DROP_DBG(rl)) {
2960 for (i = 0; i < 64; i++) {
2961 if (attrs & (UINT64_C(1) << i)) {
2962 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2964 ds_put_format(&s, " %s",
2965 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2968 if (out_of_range_attr) {
2969 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2972 ds_put_cstr(&s, ": ");
2973 odp_flow_key_format(key, key_len, &s);
2975 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2980 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2982 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2984 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2985 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2989 if (odp_frag != OVS_FRAG_TYPE_NONE) {
2990 flow->nw_frag |= FLOW_NW_FRAG_ANY;
2991 if (odp_frag == OVS_FRAG_TYPE_LATER) {
2992 flow->nw_frag |= FLOW_NW_FRAG_LATER;
2999 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
3000 const struct nlattr *attrs[], uint64_t *present_attrsp,
3001 int *out_of_range_attrp)
3003 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3004 const struct nlattr *nla;
3005 uint64_t present_attrs;
3008 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
3010 *out_of_range_attrp = 0;
3011 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
3012 uint16_t type = nl_attr_type(nla);
3013 size_t len = nl_attr_get_size(nla);
3014 int expected_len = odp_flow_key_attr_len(type);
3016 if (len != expected_len && expected_len >= 0) {
3017 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3019 VLOG_ERR_RL(&rl, "attribute %s has length %"PRIuSIZE" but should have "
3020 "length %d", ovs_key_attr_to_string(type, namebuf,
3026 if (type > OVS_KEY_ATTR_MAX) {
3027 *out_of_range_attrp = type;
3029 if (present_attrs & (UINT64_C(1) << type)) {
3030 char namebuf[OVS_KEY_ATTR_BUFSIZE];
3032 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
3033 ovs_key_attr_to_string(type,
3034 namebuf, sizeof namebuf));
3038 present_attrs |= UINT64_C(1) << type;
3043 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
3047 *present_attrsp = present_attrs;
3051 static enum odp_key_fitness
3052 check_expectations(uint64_t present_attrs, int out_of_range_attr,
3053 uint64_t expected_attrs,
3054 const struct nlattr *key, size_t key_len)
3056 uint64_t missing_attrs;
3057 uint64_t extra_attrs;
3059 missing_attrs = expected_attrs & ~present_attrs;
3060 if (missing_attrs) {
3061 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3062 log_odp_key_attributes(&rl, "expected but not present",
3063 missing_attrs, 0, key, key_len);
3064 return ODP_FIT_TOO_LITTLE;
3067 extra_attrs = present_attrs & ~expected_attrs;
3068 if (extra_attrs || out_of_range_attr) {
3069 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3070 log_odp_key_attributes(&rl, "present but not expected",
3071 extra_attrs, out_of_range_attr, key, key_len);
3072 return ODP_FIT_TOO_MUCH;
3075 return ODP_FIT_PERFECT;
3079 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3080 uint64_t present_attrs, uint64_t *expected_attrs,
3081 struct flow *flow, const struct flow *src_flow)
3083 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3084 bool is_mask = flow != src_flow;
3086 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
3087 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
3088 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
3089 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
3090 ntohs(flow->dl_type));
3093 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
3094 flow->dl_type != htons(0xffff)) {
3097 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
3100 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
3101 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
3102 /* See comments in odp_flow_key_from_flow__(). */
3103 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
3110 static enum odp_key_fitness
3111 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3112 uint64_t present_attrs, int out_of_range_attr,
3113 uint64_t expected_attrs, struct flow *flow,
3114 const struct nlattr *key, size_t key_len,
3115 const struct flow *src_flow)
3117 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3118 bool is_mask = src_flow != flow;
3119 const void *check_start = NULL;
3120 size_t check_len = 0;
3121 enum ovs_key_attr expected_bit = 0xff;
3123 if (eth_type_mpls(src_flow->dl_type)) {
3124 size_t size = nl_attr_get_size(attrs[OVS_KEY_ATTR_MPLS]);
3125 const ovs_be32 *mpls_lse = nl_attr_get(attrs[OVS_KEY_ATTR_MPLS]);
3126 int n = size / sizeof(ovs_be32);
3129 if (!size || size % sizeof(ovs_be32)) {
3130 return ODP_FIT_ERROR;
3134 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3136 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
3137 return ODP_FIT_TOO_LITTLE;
3139 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
3140 if (flow->mpls_lse[0] && flow->dl_type != htons(0xffff)) {
3141 return ODP_FIT_ERROR;
3143 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
3146 for (i = 0; i < n && i < FLOW_MAX_MPLS_LABELS; i++) {
3147 flow->mpls_lse[i] = mpls_lse[i];
3149 if (n > FLOW_MAX_MPLS_LABELS) {
3150 return ODP_FIT_TOO_MUCH;
3154 /* BOS may be set only in the innermost label. */
3155 for (i = 0; i < n - 1; i++) {
3156 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
3157 return ODP_FIT_ERROR;
3161 /* BOS must be set in the innermost label. */
3162 if (n < FLOW_MAX_MPLS_LABELS
3163 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
3164 return ODP_FIT_TOO_LITTLE;
3169 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
3171 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
3173 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
3174 const struct ovs_key_ipv4 *ipv4_key;
3176 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
3177 flow->nw_src = ipv4_key->ipv4_src;
3178 flow->nw_dst = ipv4_key->ipv4_dst;
3179 flow->nw_proto = ipv4_key->ipv4_proto;
3180 flow->nw_tos = ipv4_key->ipv4_tos;
3181 flow->nw_ttl = ipv4_key->ipv4_ttl;
3183 flow->nw_frag = ipv4_key->ipv4_frag;
3184 check_start = ipv4_key;
3185 check_len = sizeof *ipv4_key;
3186 expected_bit = OVS_KEY_ATTR_IPV4;
3187 } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
3188 return ODP_FIT_ERROR;
3191 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
3193 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
3195 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
3196 const struct ovs_key_ipv6 *ipv6_key;
3198 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
3199 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
3200 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
3201 flow->ipv6_label = ipv6_key->ipv6_label;
3202 flow->nw_proto = ipv6_key->ipv6_proto;
3203 flow->nw_tos = ipv6_key->ipv6_tclass;
3204 flow->nw_ttl = ipv6_key->ipv6_hlimit;
3206 flow->nw_frag = ipv6_key->ipv6_frag;
3207 check_start = ipv6_key;
3208 check_len = sizeof *ipv6_key;
3209 expected_bit = OVS_KEY_ATTR_IPV6;
3210 } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
3211 return ODP_FIT_ERROR;
3214 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
3215 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
3217 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
3219 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
3220 const struct ovs_key_arp *arp_key;
3222 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
3223 flow->nw_src = arp_key->arp_sip;
3224 flow->nw_dst = arp_key->arp_tip;
3225 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
3226 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
3227 "key", ntohs(arp_key->arp_op));
3228 return ODP_FIT_ERROR;
3230 flow->nw_proto = ntohs(arp_key->arp_op);
3231 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
3232 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
3235 check_start = arp_key;
3236 check_len = sizeof *arp_key;
3237 expected_bit = OVS_KEY_ATTR_ARP;
3243 if (check_len > 0) { /* Happens only when 'is_mask'. */
3244 if (!is_all_zeros(check_start, check_len) &&
3245 flow->dl_type != htons(0xffff)) {
3246 return ODP_FIT_ERROR;
3248 expected_attrs |= UINT64_C(1) << expected_bit;
3252 expected_bit = OVS_KEY_ATTR_UNSPEC;
3253 if (src_flow->nw_proto == IPPROTO_TCP
3254 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3255 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3256 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3258 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
3260 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
3261 const struct ovs_key_tcp *tcp_key;
3263 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
3264 flow->tp_src = tcp_key->tcp_src;
3265 flow->tp_dst = tcp_key->tcp_dst;
3266 expected_bit = OVS_KEY_ATTR_TCP;
3268 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS)) {
3269 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS;
3270 flow->tcp_flags = nl_attr_get_be16(attrs[OVS_KEY_ATTR_TCP_FLAGS]);
3272 } else if (src_flow->nw_proto == IPPROTO_UDP
3273 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3274 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3275 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3277 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
3279 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
3280 const struct ovs_key_udp *udp_key;
3282 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
3283 flow->tp_src = udp_key->udp_src;
3284 flow->tp_dst = udp_key->udp_dst;
3285 expected_bit = OVS_KEY_ATTR_UDP;
3287 } else if (src_flow->nw_proto == IPPROTO_SCTP
3288 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
3289 src_flow->dl_type == htons(ETH_TYPE_IPV6))
3290 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3292 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
3294 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
3295 const struct ovs_key_sctp *sctp_key;
3297 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
3298 flow->tp_src = sctp_key->sctp_src;
3299 flow->tp_dst = sctp_key->sctp_dst;
3300 expected_bit = OVS_KEY_ATTR_SCTP;
3302 } else if (src_flow->nw_proto == IPPROTO_ICMP
3303 && src_flow->dl_type == htons(ETH_TYPE_IP)
3304 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3306 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
3308 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
3309 const struct ovs_key_icmp *icmp_key;
3311 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
3312 flow->tp_src = htons(icmp_key->icmp_type);
3313 flow->tp_dst = htons(icmp_key->icmp_code);
3314 expected_bit = OVS_KEY_ATTR_ICMP;
3316 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
3317 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
3318 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3320 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
3322 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
3323 const struct ovs_key_icmpv6 *icmpv6_key;
3325 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
3326 flow->tp_src = htons(icmpv6_key->icmpv6_type);
3327 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
3328 expected_bit = OVS_KEY_ATTR_ICMPV6;
3329 if (src_flow->tp_dst == htons(0) &&
3330 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
3331 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
3333 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3335 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
3336 const struct ovs_key_nd *nd_key;
3338 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
3339 memcpy(&flow->nd_target, nd_key->nd_target,
3340 sizeof flow->nd_target);
3341 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
3342 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
3344 if (!is_all_zeros((const uint8_t *) nd_key,
3346 (flow->tp_src != htons(0xffff) ||
3347 flow->tp_dst != htons(0xffff))) {
3348 return ODP_FIT_ERROR;
3350 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3357 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3358 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3359 return ODP_FIT_ERROR;
3361 expected_attrs |= UINT64_C(1) << expected_bit;
3366 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3370 /* Parse 802.1Q header then encapsulated L3 attributes. */
3371 static enum odp_key_fitness
3372 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3373 uint64_t present_attrs, int out_of_range_attr,
3374 uint64_t expected_attrs, struct flow *flow,
3375 const struct nlattr *key, size_t key_len,
3376 const struct flow *src_flow)
3378 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3379 bool is_mask = src_flow != flow;
3381 const struct nlattr *encap
3382 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3383 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3384 enum odp_key_fitness encap_fitness;
3385 enum odp_key_fitness fitness;
3387 /* Calculate fitness of outer attributes. */
3389 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3390 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3392 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3393 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3395 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3396 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3399 fitness = check_expectations(present_attrs, out_of_range_attr,
3400 expected_attrs, key, key_len);
3403 * Remove the TPID from dl_type since it's not the real Ethertype. */
3404 flow->dl_type = htons(0);
3405 flow->vlan_tci = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)
3406 ? nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN])
3409 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3410 return ODP_FIT_TOO_LITTLE;
3411 } else if (flow->vlan_tci == htons(0)) {
3412 /* Corner case for a truncated 802.1Q header. */
3413 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3414 return ODP_FIT_TOO_MUCH;
3417 } else if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3418 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3419 "but CFI bit is not set", ntohs(flow->vlan_tci));
3420 return ODP_FIT_ERROR;
3423 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3428 /* Now parse the encapsulated attributes. */
3429 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3430 attrs, &present_attrs, &out_of_range_attr)) {
3431 return ODP_FIT_ERROR;
3435 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3436 return ODP_FIT_ERROR;
3438 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3439 expected_attrs, flow, key, key_len,
3442 /* The overall fitness is the worse of the outer and inner attributes. */
3443 return MAX(fitness, encap_fitness);
3446 static enum odp_key_fitness
3447 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3448 struct flow *flow, const struct flow *src_flow)
3450 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3451 uint64_t expected_attrs;
3452 uint64_t present_attrs;
3453 int out_of_range_attr;
3454 bool is_mask = src_flow != flow;
3456 memset(flow, 0, sizeof *flow);
3458 /* Parse attributes. */
3459 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3460 &out_of_range_attr)) {
3461 return ODP_FIT_ERROR;
3466 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID)) {
3467 flow->recirc_id = nl_attr_get_u32(attrs[OVS_KEY_ATTR_RECIRC_ID]);
3468 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID;
3469 } else if (is_mask) {
3470 /* Always exact match recirc_id if it is not specified. */
3471 flow->recirc_id = UINT32_MAX;
3474 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH)) {
3475 flow->dp_hash = nl_attr_get_u32(attrs[OVS_KEY_ATTR_DP_HASH]);
3476 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH;
3478 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3479 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3480 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3483 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3484 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3485 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3488 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3489 enum odp_key_fitness res;
3491 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3492 if (res == ODP_FIT_ERROR) {
3493 return ODP_FIT_ERROR;
3494 } else if (res == ODP_FIT_PERFECT) {
3495 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3499 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3500 flow->in_port.odp_port
3501 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3502 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3503 } else if (!is_mask) {
3504 flow->in_port.odp_port = ODPP_NONE;
3507 /* Ethernet header. */
3508 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3509 const struct ovs_key_ethernet *eth_key;
3511 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3512 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3513 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3515 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3519 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3522 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3523 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3525 return ODP_FIT_ERROR;
3529 ? (src_flow->vlan_tci & htons(VLAN_CFI)) != 0
3530 : src_flow->dl_type == htons(ETH_TYPE_VLAN)) {
3531 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3532 expected_attrs, flow, key, key_len, src_flow);
3535 flow->vlan_tci = htons(0xffff);
3536 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3537 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3538 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3541 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3542 expected_attrs, flow, key, key_len, src_flow);
3545 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3546 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3547 * 'key' fits our expectations for what a flow key should contain.
3549 * The 'in_port' will be the datapath's understanding of the port. The
3550 * caller will need to translate with odp_port_to_ofp_port() if the
3551 * OpenFlow port is needed.
3553 * This function doesn't take the packet itself as an argument because none of
3554 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3555 * it is always possible to infer which additional attribute(s) should appear
3556 * by looking at the attributes for lower-level protocols, e.g. if the network
3557 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3558 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3559 * must be absent. */
3560 enum odp_key_fitness
3561 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3564 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3567 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3568 * structure in 'mask'. 'flow' must be a previously translated flow
3569 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3570 * 'key' fits our expectations for what a flow key should contain. */
3571 enum odp_key_fitness
3572 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3573 struct flow *mask, const struct flow *flow)
3575 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3578 /* Returns 'fitness' as a string, for use in debug messages. */
3580 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3583 case ODP_FIT_PERFECT:
3585 case ODP_FIT_TOO_MUCH:
3587 case ODP_FIT_TOO_LITTLE:
3588 return "too_little";
3596 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3597 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3598 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3599 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3600 * null, then the return value is not meaningful.) */
3602 odp_put_userspace_action(uint32_t pid,
3603 const void *userdata, size_t userdata_size,
3604 odp_port_t tunnel_out_port,
3605 struct ofpbuf *odp_actions)
3607 size_t userdata_ofs;
3610 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3611 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3613 userdata_ofs = ofpbuf_size(odp_actions) + NLA_HDRLEN;
3615 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
3616 * module before Linux 3.10 required the userdata to be exactly 8 bytes
3619 * - The kernel rejected shorter userdata with -ERANGE.
3621 * - The kernel silently dropped userdata beyond the first 8 bytes.
3623 * Thus, for maximum compatibility, always put at least 8 bytes. (We
3624 * separately disable features that required more than 8 bytes.) */
3625 memcpy(nl_msg_put_unspec_zero(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3626 MAX(8, userdata_size)),
3627 userdata, userdata_size);
3631 if (tunnel_out_port != ODPP_NONE) {
3632 nl_msg_put_odp_port(odp_actions, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT,
3635 nl_msg_end_nested(odp_actions, offset);
3637 return userdata_ofs;
3641 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3642 struct ofpbuf *odp_actions)
3644 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3645 tun_key_to_attr(odp_actions, tunnel);
3646 nl_msg_end_nested(odp_actions, offset);
3649 /* The commit_odp_actions() function and its helpers. */
3652 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3653 const void *key, size_t key_size)
3655 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3656 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3657 nl_msg_end_nested(odp_actions, offset);
3660 /* Masked set actions have a mask following the data within the netlink
3661 * attribute. The unmasked bits in the data will be cleared as the data
3662 * is copied to the action. */
3664 commit_masked_set_action(struct ofpbuf *odp_actions,
3665 enum ovs_key_attr key_type,
3666 const void *key_, const void *mask_, size_t key_size)
3668 size_t offset = nl_msg_start_nested(odp_actions,
3669 OVS_ACTION_ATTR_SET_MASKED);
3670 char *data = nl_msg_put_unspec_uninit(odp_actions, key_type, key_size * 2);
3671 const char *key = key_, *mask = mask_;
3673 memcpy(data + key_size, mask, key_size);
3674 /* Clear unmasked bits while copying. */
3675 while (key_size--) {
3676 *data++ = *key++ & *mask++;
3678 nl_msg_end_nested(odp_actions, offset);
3682 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3683 struct ofpbuf *odp_actions)
3685 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3689 /* If any of the flow key data that ODP actions can modify are different in
3690 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3691 * 'odp_actions' that change the flow tunneling information in key from
3692 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3693 * same way. In other words, operates the same as commit_odp_actions(), but
3694 * only on tunneling information. */
3696 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3697 struct ofpbuf *odp_actions)
3699 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3700 if (flow->tunnel.ip_dst) {
3701 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3704 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3705 odp_put_tunnel_action(&base->tunnel, odp_actions);
3710 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3711 struct ofpbuf *odp_actions,
3712 struct flow_wildcards *wc)
3714 struct ovs_key_ethernet eth_key;
3716 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3717 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3721 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3722 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3724 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3725 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3727 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3728 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3730 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3731 ð_key, sizeof(eth_key));
3735 pop_vlan(struct flow *base,
3736 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3738 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3740 if (base->vlan_tci & htons(VLAN_CFI)) {
3741 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3747 commit_vlan_action(ovs_be16 vlan_tci, struct flow *base,
3748 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3750 if (base->vlan_tci == vlan_tci) {
3754 pop_vlan(base, odp_actions, wc);
3755 if (vlan_tci & htons(VLAN_CFI)) {
3756 struct ovs_action_push_vlan vlan;
3758 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3759 vlan.vlan_tci = vlan_tci;
3760 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3761 &vlan, sizeof vlan);
3763 base->vlan_tci = vlan_tci;
3767 commit_mpls_action(const struct flow *flow, struct flow *base,
3768 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3770 int base_n = flow_count_mpls_labels(base, wc);
3771 int flow_n = flow_count_mpls_labels(flow, wc);
3772 int common_n = flow_count_common_mpls_labels(flow, flow_n, base, base_n,
3775 while (base_n > common_n) {
3776 if (base_n - 1 == common_n && flow_n > common_n) {
3777 /* If there is only one more LSE in base than there are common
3778 * between base and flow; and flow has at least one more LSE than
3779 * is common then the topmost LSE of base may be updated using
3781 struct ovs_key_mpls mpls_key;
3783 mpls_key.mpls_lse = flow->mpls_lse[flow_n - base_n];
3784 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3785 &mpls_key, sizeof mpls_key);
3786 flow_set_mpls_lse(base, 0, mpls_key.mpls_lse);
3789 /* Otherwise, if there more LSEs in base than are common between
3790 * base and flow then pop the topmost one. */
3794 /* If all the LSEs are to be popped and this is not the outermost
3795 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
3796 * POP_MPLS action instead of flow->dl_type.
3798 * This is because the POP_MPLS action requires its ethertype
3799 * argument to be an MPLS ethernet type but in this case
3800 * flow->dl_type will be a non-MPLS ethernet type.
3802 * When the final POP_MPLS action occurs it use flow->dl_type and
3803 * the and the resulting packet will have the desired dl_type. */
3804 if ((!eth_type_mpls(flow->dl_type)) && base_n > 1) {
3805 dl_type = htons(ETH_TYPE_MPLS);
3807 dl_type = flow->dl_type;
3809 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, dl_type);
3810 popped = flow_pop_mpls(base, base_n, flow->dl_type, wc);
3816 /* If, after the above popping and setting, there are more LSEs in flow
3817 * than base then some LSEs need to be pushed. */
3818 while (base_n < flow_n) {
3819 struct ovs_action_push_mpls *mpls;
3821 mpls = nl_msg_put_unspec_zero(odp_actions,
3822 OVS_ACTION_ATTR_PUSH_MPLS,
3824 mpls->mpls_ethertype = flow->dl_type;
3825 mpls->mpls_lse = flow->mpls_lse[flow_n - base_n - 1];
3826 flow_push_mpls(base, base_n, mpls->mpls_ethertype, wc);
3827 flow_set_mpls_lse(base, 0, mpls->mpls_lse);
3833 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3834 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3836 struct ovs_key_ipv4 ipv4_key;
3838 if (base->nw_src == flow->nw_src &&
3839 base->nw_dst == flow->nw_dst &&
3840 base->nw_tos == flow->nw_tos &&
3841 base->nw_ttl == flow->nw_ttl &&
3842 base->nw_frag == flow->nw_frag) {
3846 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3847 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3848 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3849 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3850 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3851 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3853 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3854 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3855 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3856 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3857 ipv4_key.ipv4_proto = base->nw_proto;
3858 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3860 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3861 &ipv4_key, sizeof(ipv4_key));
3865 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3866 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3868 struct ovs_key_ipv6 ipv6_key;
3870 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3871 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3872 base->ipv6_label == flow->ipv6_label &&
3873 base->nw_tos == flow->nw_tos &&
3874 base->nw_ttl == flow->nw_ttl &&
3875 base->nw_frag == flow->nw_frag) {
3879 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3880 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3881 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3882 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3883 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3884 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3885 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3887 base->ipv6_src = flow->ipv6_src;
3888 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3889 base->ipv6_dst = flow->ipv6_dst;
3890 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3892 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3893 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3894 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3895 ipv6_key.ipv6_proto = base->nw_proto;
3896 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3898 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3899 &ipv6_key, sizeof(ipv6_key));
3902 static enum slow_path_reason
3903 commit_set_arp_action(const struct flow *flow, struct flow *base,
3904 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3906 struct ovs_key_arp arp_key;
3908 if (base->nw_src == flow->nw_src &&
3909 base->nw_dst == flow->nw_dst &&
3910 base->nw_proto == flow->nw_proto &&
3911 eth_addr_equals(base->arp_sha, flow->arp_sha) &&
3912 eth_addr_equals(base->arp_tha, flow->arp_tha)) {
3916 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3917 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3918 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3919 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
3920 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
3922 base->nw_src = flow->nw_src;
3923 base->nw_dst = flow->nw_dst;
3924 base->nw_proto = flow->nw_proto;
3925 memcpy(base->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3926 memcpy(base->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3928 arp_key.arp_sip = base->nw_src;
3929 arp_key.arp_tip = base->nw_dst;
3930 arp_key.arp_op = htons(base->nw_proto);
3931 memcpy(arp_key.arp_sha, flow->arp_sha, ETH_ADDR_LEN);
3932 memcpy(arp_key.arp_tha, flow->arp_tha, ETH_ADDR_LEN);
3934 commit_set_action(odp_actions, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
3939 static enum slow_path_reason
3940 commit_set_nw_action(const struct flow *flow, struct flow *base,
3941 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3943 /* Check if 'flow' really has an L3 header. */
3944 if (!flow->nw_proto) {
3948 switch (ntohs(base->dl_type)) {
3950 commit_set_ipv4_action(flow, base, odp_actions, wc);
3954 commit_set_ipv6_action(flow, base, odp_actions, wc);
3958 return commit_set_arp_action(flow, base, odp_actions, wc);
3965 commit_set_port_action(const struct flow *flow, struct flow *base,
3966 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3968 /* Check if 'flow' really has an L3 header. */
3969 if (!flow->nw_proto) {
3973 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3977 if (base->tp_src == flow->tp_src &&
3978 base->tp_dst == flow->tp_dst) {
3982 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3983 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3985 if (flow->nw_proto == IPPROTO_TCP) {
3986 struct ovs_key_tcp port_key;
3988 port_key.tcp_src = base->tp_src = flow->tp_src;
3989 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3991 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3992 &port_key, sizeof(port_key));
3994 } else if (flow->nw_proto == IPPROTO_UDP) {
3995 struct ovs_key_udp port_key;
3997 port_key.udp_src = base->tp_src = flow->tp_src;
3998 port_key.udp_dst = base->tp_dst = flow->tp_dst;
4000 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
4001 &port_key, sizeof(port_key));
4002 } else if (flow->nw_proto == IPPROTO_SCTP) {
4003 struct ovs_key_sctp port_key;
4005 port_key.sctp_src = base->tp_src = flow->tp_src;
4006 port_key.sctp_dst = base->tp_dst = flow->tp_dst;
4008 commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
4009 &port_key, sizeof(port_key));
4014 commit_set_priority_action(const struct flow *flow, struct flow *base,
4015 struct ofpbuf *odp_actions,
4016 struct flow_wildcards *wc)
4018 if (base->skb_priority == flow->skb_priority) {
4022 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
4023 base->skb_priority = flow->skb_priority;
4025 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
4026 &base->skb_priority, sizeof(base->skb_priority));
4030 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
4031 struct ofpbuf *odp_actions,
4032 struct flow_wildcards *wc)
4034 if (base->pkt_mark == flow->pkt_mark) {
4038 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
4039 base->pkt_mark = flow->pkt_mark;
4041 odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
4044 /* If any of the flow key data that ODP actions can modify are different in
4045 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
4046 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
4047 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
4048 * in addition to this function if needed. Sets fields in 'wc' that are
4049 * used as part of the action.
4051 * Returns a reason to force processing the flow's packets into the userspace
4052 * slow path, if there is one, otherwise 0. */
4053 enum slow_path_reason
4054 commit_odp_actions(const struct flow *flow, struct flow *base,
4055 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4057 enum slow_path_reason slow;
4059 commit_set_ether_addr_action(flow, base, odp_actions, wc);
4060 slow = commit_set_nw_action(flow, base, odp_actions, wc);
4061 commit_set_port_action(flow, base, odp_actions, wc);
4062 commit_mpls_action(flow, base, odp_actions, wc);
4063 commit_vlan_action(flow->vlan_tci, base, odp_actions, wc);
4064 commit_set_priority_action(flow, base, odp_actions, wc);
4065 commit_set_pkt_mark_action(flow, base, odp_actions, wc);