2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
29 #include "dynamic-string.h"
39 VLOG_DEFINE_THIS_MODULE(odp_util);
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42 * Since this is fairly non-specific for the OVS userspace components,
43 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44 * interactions with the datapath.
47 /* The set of characters that may separate one action or one key attribute
49 static const char *delimiters = ", \t\r\n";
51 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
52 struct ofpbuf *, struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a,
54 const struct nlattr *ma, struct ds *ds,
57 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
60 * - For an action whose argument has a fixed length, returned that
61 * nonnegative length in bytes.
63 * - For an action with a variable-length argument, returns -2.
65 * - For an invalid 'type', returns -1. */
67 odp_action_len(uint16_t type)
69 if (type > OVS_ACTION_ATTR_MAX) {
73 switch ((enum ovs_action_attr) type) {
74 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
75 case OVS_ACTION_ATTR_USERSPACE: return -2;
76 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
77 case OVS_ACTION_ATTR_POP_VLAN: return 0;
78 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
79 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
80 case OVS_ACTION_ATTR_SET: return -2;
81 case OVS_ACTION_ATTR_SAMPLE: return -2;
83 case OVS_ACTION_ATTR_UNSPEC:
84 case __OVS_ACTION_ATTR_MAX:
91 /* Returns a string form of 'attr'. The return value is either a statically
92 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
93 * should be at least OVS_KEY_ATTR_BUFSIZE. */
94 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
96 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
99 case OVS_KEY_ATTR_UNSPEC: return "unspec";
100 case OVS_KEY_ATTR_ENCAP: return "encap";
101 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
102 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
103 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
104 case OVS_KEY_ATTR_IN_PORT: return "in_port";
105 case OVS_KEY_ATTR_ETHERNET: return "eth";
106 case OVS_KEY_ATTR_VLAN: return "vlan";
107 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
108 case OVS_KEY_ATTR_IPV4: return "ipv4";
109 case OVS_KEY_ATTR_IPV6: return "ipv6";
110 case OVS_KEY_ATTR_TCP: return "tcp";
111 case OVS_KEY_ATTR_UDP: return "udp";
112 case OVS_KEY_ATTR_ICMP: return "icmp";
113 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
114 case OVS_KEY_ATTR_ARP: return "arp";
115 case OVS_KEY_ATTR_ND: return "nd";
116 case OVS_KEY_ATTR_MPLS: return "mpls";
118 case __OVS_KEY_ATTR_MAX:
120 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
126 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
128 size_t len = nl_attr_get_size(a);
130 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
132 const uint8_t *unspec;
135 unspec = nl_attr_get(a);
136 for (i = 0; i < len; i++) {
137 ds_put_char(ds, i ? ' ': '(');
138 ds_put_format(ds, "%02x", unspec[i]);
140 ds_put_char(ds, ')');
145 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
147 static const struct nl_policy ovs_sample_policy[] = {
148 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */
149 { NL_A_U32, 0, 0, false }, /* OVS_SAMPLE_ATTR_PROBABILITY */
150 { NL_A_NESTED, 0, 0, false }, /* OVS_SAMPLE_ATTR_ACTIONS */
152 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
154 const struct nlattr *nla_acts;
157 ds_put_cstr(ds, "sample");
159 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
160 ds_put_cstr(ds, "(error)");
164 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
167 ds_put_format(ds, "(sample=%.1f%%,", percentage);
169 ds_put_cstr(ds, "actions(");
170 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
171 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
172 format_odp_actions(ds, nla_acts, len);
173 ds_put_format(ds, "))");
177 slow_path_reason_to_string(enum slow_path_reason reason)
188 case SLOW_CONTROLLER:
196 static enum slow_path_reason
197 string_to_slow_path_reason(const char *string)
199 enum slow_path_reason i;
201 for (i = 1; i < __SLOW_MAX; i++) {
202 if (!strcmp(string, slow_path_reason_to_string(i))) {
211 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
222 while (s[n] != ')') {
223 unsigned long long int flags;
227 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
228 n += n0 + (s[n + n0] == ',');
233 for (bit = 1; bit; bit <<= 1) {
234 const char *name = bit_to_string(bit);
242 if (!strncmp(s + n, name, len) &&
243 (s[n + len] == ',' || s[n + len] == ')')) {
245 n += len + (s[n + len] == ',');
261 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
263 static const struct nl_policy ovs_userspace_policy[] = {
264 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */
265 { NL_A_U32, 0, 0, false }, /* OVS_USERSPACE_ATTR_PID */
266 { NL_A_UNSPEC, 0, 0, true }, /* OVS_USERSPACE_ATTR_USERDATA */
268 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
269 const struct nlattr *userdata_attr;
271 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
272 ds_put_cstr(ds, "userspace(error)");
276 ds_put_format(ds, "userspace(pid=%"PRIu32,
277 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
279 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
282 const uint8_t *userdata = nl_attr_get(userdata_attr);
283 size_t userdata_len = nl_attr_get_size(userdata_attr);
284 bool userdata_unspec = true;
285 union user_action_cookie cookie;
287 if (userdata_len >= sizeof cookie.type
288 && userdata_len <= sizeof cookie) {
290 memset(&cookie, 0, sizeof cookie);
291 memcpy(&cookie, userdata, userdata_len);
293 userdata_unspec = false;
295 if (userdata_len == sizeof cookie.sflow
296 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
297 ds_put_format(ds, ",sFlow("
298 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
299 vlan_tci_to_vid(cookie.sflow.vlan_tci),
300 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
301 cookie.sflow.output);
302 } else if (userdata_len == sizeof cookie.slow_path
303 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
305 reason = slow_path_reason_to_string(cookie.slow_path.reason);
306 reason = reason ? reason : "";
307 ds_put_format(ds, ",slow_path(%s)", reason);
308 } else if (userdata_len == sizeof cookie.flow_sample
309 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
310 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
311 ",collector_set_id=%"PRIu32
312 ",obs_domain_id=%"PRIu32
313 ",obs_point_id=%"PRIu32")",
314 cookie.flow_sample.probability,
315 cookie.flow_sample.collector_set_id,
316 cookie.flow_sample.obs_domain_id,
317 cookie.flow_sample.obs_point_id);
318 } else if (userdata_len == sizeof cookie.ipfix
319 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
320 ds_put_format(ds, ",ipfix");
322 userdata_unspec = true;
326 if (userdata_unspec) {
328 ds_put_format(ds, ",userdata(");
329 for (i = 0; i < userdata_len; i++) {
330 ds_put_format(ds, "%02x", userdata[i]);
332 ds_put_char(ds, ')');
336 ds_put_char(ds, ')');
340 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
342 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
343 vlan_tci_to_vid(vlan_tci),
344 vlan_tci_to_pcp(vlan_tci));
345 if (!(vlan_tci & htons(VLAN_CFI))) {
346 ds_put_cstr(ds, ",cfi=0");
351 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
353 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
354 mpls_lse_to_label(mpls_lse),
355 mpls_lse_to_tc(mpls_lse),
356 mpls_lse_to_ttl(mpls_lse),
357 mpls_lse_to_bos(mpls_lse));
361 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
362 const struct ovs_key_mpls *mpls_mask)
364 ovs_be32 key = mpls_key->mpls_lse;
366 if (mpls_mask == NULL) {
367 format_mpls_lse(ds, key);
369 ovs_be32 mask = mpls_mask->mpls_lse;
371 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
372 mpls_lse_to_label(key), mpls_lse_to_label(mask),
373 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
374 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
375 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
380 format_odp_action(struct ds *ds, const struct nlattr *a)
383 enum ovs_action_attr type = nl_attr_type(a);
384 const struct ovs_action_push_vlan *vlan;
386 expected_len = odp_action_len(nl_attr_type(a));
387 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
388 ds_put_format(ds, "bad length %zu, expected %d for: ",
389 nl_attr_get_size(a), expected_len);
390 format_generic_odp_action(ds, a);
395 case OVS_ACTION_ATTR_OUTPUT:
396 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
398 case OVS_ACTION_ATTR_USERSPACE:
399 format_odp_userspace_action(ds, a);
401 case OVS_ACTION_ATTR_SET:
402 ds_put_cstr(ds, "set(");
403 format_odp_key_attr(nl_attr_get(a), NULL, ds, true);
404 ds_put_cstr(ds, ")");
406 case OVS_ACTION_ATTR_PUSH_VLAN:
407 vlan = nl_attr_get(a);
408 ds_put_cstr(ds, "push_vlan(");
409 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
410 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
412 format_vlan_tci(ds, vlan->vlan_tci);
413 ds_put_char(ds, ')');
415 case OVS_ACTION_ATTR_POP_VLAN:
416 ds_put_cstr(ds, "pop_vlan");
418 case OVS_ACTION_ATTR_PUSH_MPLS: {
419 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
420 ds_put_cstr(ds, "push_mpls(");
421 format_mpls_lse(ds, mpls->mpls_lse);
422 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
425 case OVS_ACTION_ATTR_POP_MPLS: {
426 ovs_be16 ethertype = nl_attr_get_be16(a);
427 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
430 case OVS_ACTION_ATTR_SAMPLE:
431 format_odp_sample_action(ds, a);
433 case OVS_ACTION_ATTR_UNSPEC:
434 case __OVS_ACTION_ATTR_MAX:
436 format_generic_odp_action(ds, a);
442 format_odp_actions(struct ds *ds, const struct nlattr *actions,
446 const struct nlattr *a;
449 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
451 ds_put_char(ds, ',');
453 format_odp_action(ds, a);
458 if (left == actions_len) {
459 ds_put_cstr(ds, "<empty>");
461 ds_put_format(ds, ",***%u leftover bytes*** (", left);
462 for (i = 0; i < left; i++) {
463 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
465 ds_put_char(ds, ')');
468 ds_put_cstr(ds, "drop");
473 parse_odp_action(const char *s, const struct simap *port_names,
474 struct ofpbuf *actions)
476 /* Many of the sscanf calls in this function use oversized destination
477 * fields because some sscanf() implementations truncate the range of %i
478 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
479 * value of 0x7fff. The other alternatives are to allow only a single
480 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
483 * The tun_id parser has to use an alternative approach because there is no
484 * type larger than 64 bits. */
487 unsigned long long int port;
490 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
491 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
497 int len = strcspn(s, delimiters);
498 struct simap_node *node;
500 node = simap_find_len(port_names, s, len);
502 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
508 unsigned long long int pid;
509 unsigned long long int output;
510 unsigned long long int probability;
511 unsigned long long int collector_set_id;
512 unsigned long long int obs_domain_id;
513 unsigned long long int obs_point_id;
517 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
518 odp_put_userspace_action(pid, NULL, 0, actions);
520 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
521 "pcp=%i,output=%lli))%n",
522 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
523 union user_action_cookie cookie;
526 tci = vid | (pcp << VLAN_PCP_SHIFT);
531 cookie.type = USER_ACTION_COOKIE_SFLOW;
532 cookie.sflow.vlan_tci = htons(tci);
533 cookie.sflow.output = output;
534 odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
537 } else if (sscanf(s, "userspace(pid=%lli,slow_path(%n", &pid, &n) > 0
539 union user_action_cookie cookie;
542 if (s[n] == ')' && s[n + 1] == ')') {
545 } else if (sscanf(s + n, "%31[^)]))", reason) > 0) {
546 n += strlen(reason) + 2;
551 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
552 cookie.slow_path.unused = 0;
553 cookie.slow_path.reason = string_to_slow_path_reason(reason);
555 if (reason[0] && !cookie.slow_path.reason) {
559 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
562 } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
563 "collector_set_id=%lli,obs_domain_id=%lli,"
564 "obs_point_id=%lli))%n",
565 &pid, &probability, &collector_set_id,
566 &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
567 union user_action_cookie cookie;
569 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
570 cookie.flow_sample.probability = probability;
571 cookie.flow_sample.collector_set_id = collector_set_id;
572 cookie.flow_sample.obs_domain_id = obs_domain_id;
573 cookie.flow_sample.obs_point_id = obs_point_id;
574 odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
577 } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
579 union user_action_cookie cookie;
581 cookie.type = USER_ACTION_COOKIE_IPFIX;
582 odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
585 } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
590 ofpbuf_init(&buf, 16);
591 end = ofpbuf_put_hex(&buf, &s[n], NULL);
592 if (end[0] == ')' && end[1] == ')') {
593 odp_put_userspace_action(pid, buf.data, buf.size, actions);
595 return (end + 2) - s;
600 if (!strncmp(s, "set(", 4)) {
604 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
605 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL);
609 if (s[retval + 4] != ')') {
612 nl_msg_end_nested(actions, start_ofs);
617 struct ovs_action_push_vlan push;
618 int tpid = ETH_TYPE_VLAN;
623 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
625 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
626 &vid, &pcp, &cfi, &n) > 0 && n > 0)
627 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
628 &tpid, &vid, &pcp, &n) > 0 && n > 0)
629 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
630 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
631 push.vlan_tpid = htons(tpid);
632 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
633 | (pcp << VLAN_PCP_SHIFT)
634 | (cfi ? VLAN_CFI : 0));
635 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
642 if (!strncmp(s, "pop_vlan", 8)) {
643 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
651 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
652 && percentage >= 0. && percentage <= 100.0
654 size_t sample_ofs, actions_ofs;
657 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
658 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
659 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
660 (probability <= 0 ? 0
661 : probability >= UINT32_MAX ? UINT32_MAX
664 actions_ofs = nl_msg_start_nested(actions,
665 OVS_SAMPLE_ATTR_ACTIONS);
669 n += strspn(s + n, delimiters);
674 retval = parse_odp_action(s + n, port_names, actions);
680 nl_msg_end_nested(actions, actions_ofs);
681 nl_msg_end_nested(actions, sample_ofs);
683 return s[n + 1] == ')' ? n + 2 : -EINVAL;
690 /* Parses the string representation of datapath actions, in the format output
691 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
692 * value. On success, the ODP actions are appended to 'actions' as a series of
693 * Netlink attributes. On failure, no data is appended to 'actions'. Either
694 * way, 'actions''s data might be reallocated. */
696 odp_actions_from_string(const char *s, const struct simap *port_names,
697 struct ofpbuf *actions)
701 if (!strcasecmp(s, "drop")) {
705 old_size = actions->size;
709 s += strspn(s, delimiters);
714 retval = parse_odp_action(s, port_names, actions);
715 if (retval < 0 || !strchr(delimiters, s[retval])) {
716 actions->size = old_size;
725 /* Returns the correct length of the payload for a flow key attribute of the
726 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
727 * is variable length. */
729 odp_flow_key_attr_len(uint16_t type)
731 if (type > OVS_KEY_ATTR_MAX) {
735 switch ((enum ovs_key_attr) type) {
736 case OVS_KEY_ATTR_ENCAP: return -2;
737 case OVS_KEY_ATTR_PRIORITY: return 4;
738 case OVS_KEY_ATTR_SKB_MARK: return 4;
739 case OVS_KEY_ATTR_TUNNEL: return -2;
740 case OVS_KEY_ATTR_IN_PORT: return 4;
741 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
742 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
743 case OVS_KEY_ATTR_ETHERTYPE: return 2;
744 case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls);
745 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
746 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
747 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
748 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
749 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
750 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
751 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
752 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
754 case OVS_KEY_ATTR_UNSPEC:
755 case __OVS_KEY_ATTR_MAX:
763 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
765 size_t len = nl_attr_get_size(a);
767 const uint8_t *unspec;
770 unspec = nl_attr_get(a);
771 for (i = 0; i < len; i++) {
773 ds_put_char(ds, ' ');
775 ds_put_format(ds, "%02x", unspec[i]);
781 ovs_frag_type_to_string(enum ovs_frag_type type)
784 case OVS_FRAG_TYPE_NONE:
786 case OVS_FRAG_TYPE_FIRST:
788 case OVS_FRAG_TYPE_LATER:
790 case __OVS_FRAG_TYPE_MAX:
797 tunnel_key_attr_len(int type)
800 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
801 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
802 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
803 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
804 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
805 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
806 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
807 case __OVS_TUNNEL_KEY_ATTR_MAX:
814 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
817 const struct nlattr *a;
819 bool unknown = false;
821 NL_NESTED_FOR_EACH(a, left, attr) {
822 uint16_t type = nl_attr_type(a);
823 size_t len = nl_attr_get_size(a);
824 int expected_len = tunnel_key_attr_len(type);
826 if (len != expected_len && expected_len >= 0) {
827 return ODP_FIT_ERROR;
831 case OVS_TUNNEL_KEY_ATTR_ID:
832 tun->tun_id = nl_attr_get_be64(a);
833 tun->flags |= FLOW_TNL_F_KEY;
835 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
836 tun->ip_src = nl_attr_get_be32(a);
838 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
839 tun->ip_dst = nl_attr_get_be32(a);
841 case OVS_TUNNEL_KEY_ATTR_TOS:
842 tun->ip_tos = nl_attr_get_u8(a);
844 case OVS_TUNNEL_KEY_ATTR_TTL:
845 tun->ip_ttl = nl_attr_get_u8(a);
848 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
849 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
851 case OVS_TUNNEL_KEY_ATTR_CSUM:
852 tun->flags |= FLOW_TNL_F_CSUM;
855 /* Allow this to show up as unexpected, if there are unknown
856 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
863 return ODP_FIT_ERROR;
866 return ODP_FIT_TOO_MUCH;
868 return ODP_FIT_PERFECT;
872 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
876 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
878 if (tun_key->flags & FLOW_TNL_F_KEY) {
879 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
881 if (tun_key->ip_src) {
882 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
884 if (tun_key->ip_dst) {
885 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
887 if (tun_key->ip_tos) {
888 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
890 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
891 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
892 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
894 if (tun_key->flags & FLOW_TNL_F_CSUM) {
895 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
898 nl_msg_end_nested(a, tun_key_ofs);
902 odp_mask_attr_is_wildcard(const struct nlattr *ma)
904 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
908 odp_mask_attr_is_exact(const struct nlattr *ma)
910 bool is_exact = false;
911 enum ovs_key_attr attr = nl_attr_type(ma);
913 if (attr == OVS_KEY_ATTR_TUNNEL) {
914 /* XXX this is a hack for now. Should change
915 * the exact match dection to per field
916 * instead of per attribute.
918 struct flow_tnl tun_mask;
919 memset(&tun_mask, 0, sizeof tun_mask);
920 odp_tun_key_from_attr(ma, &tun_mask);
921 if (tun_mask.flags == (FLOW_TNL_F_KEY
922 | FLOW_TNL_F_DONT_FRAGMENT
923 | FLOW_TNL_F_CSUM)) {
924 /* The flags are exact match, check the remaining fields. */
925 tun_mask.flags = 0xffff;
926 is_exact = is_all_ones((uint8_t *)&tun_mask,
927 offsetof(struct flow_tnl, ip_ttl));
930 is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
938 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
939 struct ds *ds, bool verbose)
941 struct flow_tnl tun_key;
942 enum ovs_key_attr attr = nl_attr_type(a);
943 char namebuf[OVS_KEY_ATTR_BUFSIZE];
947 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
949 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
952 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
953 if (expected_len != -2) {
954 bool bad_key_len = nl_attr_get_size(a) != expected_len;
955 bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
957 if (bad_key_len || bad_mask_len) {
959 ds_put_format(ds, "(bad key length %zu, expected %d)(",
961 odp_flow_key_attr_len(nl_attr_type(a)));
963 format_generic_odp_key(a, ds);
965 ds_put_char(ds, '/');
966 ds_put_format(ds, "(bad mask length %zu, expected %d)(",
967 nl_attr_get_size(ma),
968 odp_flow_key_attr_len(nl_attr_type(ma)));
970 format_generic_odp_key(ma, ds);
971 ds_put_char(ds, ')');
977 ds_put_char(ds, '(');
979 case OVS_KEY_ATTR_ENCAP:
980 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
981 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
982 nl_attr_get(ma), nl_attr_get_size(ma), ds, verbose);
983 } else if (nl_attr_get_size(a)) {
984 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, ds,
989 case OVS_KEY_ATTR_PRIORITY:
990 case OVS_KEY_ATTR_SKB_MARK:
991 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
993 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
997 case OVS_KEY_ATTR_TUNNEL:
998 memset(&tun_key, 0, sizeof tun_key);
999 if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
1000 ds_put_format(ds, "error");
1001 } else if (!is_exact) {
1002 struct flow_tnl tun_mask;
1004 memset(&tun_mask, 0, sizeof tun_mask);
1005 odp_tun_key_from_attr(ma, &tun_mask);
1006 ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
1007 ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1008 ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
1010 ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
1011 IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1012 IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1013 tun_key.ip_tos, tun_mask.ip_tos,
1014 tun_key.ip_ttl, tun_mask.ip_ttl);
1016 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1018 /* XXX This code is correct, but enabling it would break the unit
1019 test. Disable it for now until the input parser is fixed.
1021 ds_put_char(ds, '/');
1022 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1024 ds_put_char(ds, ')');
1026 ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1027 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1028 ntohll(tun_key.tun_id),
1029 IP_ARGS(tun_key.ip_src),
1030 IP_ARGS(tun_key.ip_dst),
1031 tun_key.ip_tos, tun_key.ip_ttl);
1033 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1034 ds_put_char(ds, ')');
1038 case OVS_KEY_ATTR_IN_PORT:
1039 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1041 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1045 case OVS_KEY_ATTR_ETHERNET:
1047 const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1048 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1050 ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1051 ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1052 ETH_ADDR_ARGS(eth_key->eth_src),
1053 ETH_ADDR_ARGS(eth_mask->eth_src),
1054 ETH_ADDR_ARGS(eth_key->eth_dst),
1055 ETH_ADDR_ARGS(eth_mask->eth_dst));
1057 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1059 ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1060 ETH_ADDR_ARGS(eth_key->eth_src),
1061 ETH_ADDR_ARGS(eth_key->eth_dst));
1065 case OVS_KEY_ATTR_VLAN:
1067 ovs_be16 vlan_tci = nl_attr_get_be16(a);
1069 ovs_be16 mask = nl_attr_get_be16(ma);
1070 ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1071 vlan_tci_to_vid(vlan_tci),
1072 vlan_tci_to_vid(mask),
1073 vlan_tci_to_pcp(vlan_tci),
1074 vlan_tci_to_pcp(mask),
1075 vlan_tci_to_cfi(vlan_tci),
1076 vlan_tci_to_cfi(mask));
1078 format_vlan_tci(ds, vlan_tci);
1083 case OVS_KEY_ATTR_MPLS: {
1084 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1085 const struct ovs_key_mpls *mpls_mask = NULL;
1087 mpls_mask = nl_attr_get(ma);
1089 format_mpls(ds, mpls_key, mpls_mask);
1093 case OVS_KEY_ATTR_ETHERTYPE:
1094 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1096 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1100 case OVS_KEY_ATTR_IPV4:
1102 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1103 const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1105 ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1106 ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1107 ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1108 IP_ARGS(ipv4_key->ipv4_src),
1109 IP_ARGS(ipv4_mask->ipv4_src),
1110 IP_ARGS(ipv4_key->ipv4_dst),
1111 IP_ARGS(ipv4_mask->ipv4_dst),
1112 ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1113 ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1114 ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1115 ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1116 ipv4_mask->ipv4_frag);
1118 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1120 ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1121 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1122 IP_ARGS(ipv4_key->ipv4_src),
1123 IP_ARGS(ipv4_key->ipv4_dst),
1124 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1126 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1130 case OVS_KEY_ATTR_IPV6:
1132 const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1133 char src_str[INET6_ADDRSTRLEN];
1134 char dst_str[INET6_ADDRSTRLEN];
1135 char src_mask[INET6_ADDRSTRLEN];
1136 char dst_mask[INET6_ADDRSTRLEN];
1138 ipv6_key = nl_attr_get(a);
1139 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1140 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1142 ipv6_mask = nl_attr_get(ma);
1143 inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1144 inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1146 ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1147 ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1148 ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1149 src_str, src_mask, dst_str, dst_mask,
1150 ntohl(ipv6_key->ipv6_label),
1151 ntohl(ipv6_mask->ipv6_label),
1152 ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1153 ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1154 ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1155 ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1156 ipv6_mask->ipv6_frag);
1158 const struct ovs_key_ipv6 *ipv6_key;
1159 char src_str[INET6_ADDRSTRLEN];
1160 char dst_str[INET6_ADDRSTRLEN];
1162 ipv6_key = nl_attr_get(a);
1163 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1164 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1166 ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1167 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1168 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1169 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1170 ipv6_key->ipv6_hlimit,
1171 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1175 case OVS_KEY_ATTR_TCP:
1177 const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1178 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1180 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1181 ",dst=%"PRIu16"/%#"PRIx16,
1182 ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1183 ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1185 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1187 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1188 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1192 case OVS_KEY_ATTR_UDP:
1194 const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1195 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1197 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1198 ",dst=%"PRIu16"/%#"PRIx16,
1199 ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1200 ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1202 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1204 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1205 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1209 case OVS_KEY_ATTR_ICMP:
1211 const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1212 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1214 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1215 icmp_key->icmp_type, icmp_mask->icmp_type,
1216 icmp_key->icmp_code, icmp_mask->icmp_code);
1218 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1220 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1221 icmp_key->icmp_type, icmp_key->icmp_code);
1225 case OVS_KEY_ATTR_ICMPV6:
1227 const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1228 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1230 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1231 icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1232 icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1234 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1236 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1237 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1241 case OVS_KEY_ATTR_ARP:
1243 const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1244 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1246 ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1247 ",op=%"PRIu16"/%#"PRIx16
1248 ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1249 ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1250 IP_ARGS(arp_key->arp_sip),
1251 IP_ARGS(arp_mask->arp_sip),
1252 IP_ARGS(arp_key->arp_tip),
1253 IP_ARGS(arp_mask->arp_tip),
1254 ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1255 ETH_ADDR_ARGS(arp_key->arp_sha),
1256 ETH_ADDR_ARGS(arp_mask->arp_sha),
1257 ETH_ADDR_ARGS(arp_key->arp_tha),
1258 ETH_ADDR_ARGS(arp_mask->arp_tha));
1260 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1262 ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1263 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1264 IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1265 ntohs(arp_key->arp_op),
1266 ETH_ADDR_ARGS(arp_key->arp_sha),
1267 ETH_ADDR_ARGS(arp_key->arp_tha));
1271 case OVS_KEY_ATTR_ND: {
1272 const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1273 char target[INET6_ADDRSTRLEN];
1275 nd_key = nl_attr_get(a);
1277 nd_mask = nl_attr_get(ma);
1280 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1281 ds_put_format(ds, "target=%s", target);
1283 inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1284 ds_put_format(ds, "/%s", target);
1287 if (!eth_addr_is_zero(nd_key->nd_sll)) {
1288 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1289 ETH_ADDR_ARGS(nd_key->nd_sll));
1291 ds_put_format(ds, "/"ETH_ADDR_FMT,
1292 ETH_ADDR_ARGS(nd_mask->nd_sll));
1295 if (!eth_addr_is_zero(nd_key->nd_tll)) {
1296 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1297 ETH_ADDR_ARGS(nd_key->nd_tll));
1299 ds_put_format(ds, "/"ETH_ADDR_FMT,
1300 ETH_ADDR_ARGS(nd_mask->nd_tll));
1306 case OVS_KEY_ATTR_UNSPEC:
1307 case __OVS_KEY_ATTR_MAX:
1309 format_generic_odp_key(a, ds);
1311 ds_put_char(ds, '/');
1312 format_generic_odp_key(ma, ds);
1316 ds_put_char(ds, ')');
1319 static struct nlattr *
1320 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1322 const struct nlattr *a;
1324 int type = nl_attr_type(key);
1325 int size = nl_attr_get_size(key);
1327 if (odp_flow_key_attr_len(type) >=0) {
1328 memset(nl_msg_put_unspec_uninit(ofp, type, size), 0, size);
1332 nested_mask = nl_msg_start_nested(ofp, type);
1333 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1334 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1336 nl_msg_end_nested(ofp, nested_mask);
1342 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1343 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1344 * 'mask_len' bytes of 'mask' which apply to 'key'. */
1346 odp_flow_format(const struct nlattr *key, size_t key_len,
1347 const struct nlattr *mask, size_t mask_len,
1348 struct ds *ds, bool verbose)
1351 const struct nlattr *a;
1353 bool has_ethtype_key = false;
1354 const struct nlattr *ma = NULL;
1356 bool first_field = true;
1358 ofpbuf_init(&ofp, 100);
1359 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1360 bool is_nested_attr;
1361 bool is_wildcard = false;
1362 int attr_type = nl_attr_type(a);
1364 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1365 has_ethtype_key = true;
1368 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1370 if (mask && mask_len) {
1371 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1372 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1375 if (verbose || !is_wildcard || is_nested_attr) {
1376 if (is_wildcard && !ma) {
1377 ma = generate_all_wildcard_mask(&ofp, a);
1380 ds_put_char(ds, ',');
1382 format_odp_key_attr(a, ma, ds, verbose);
1383 first_field = false;
1387 ofpbuf_uninit(&ofp);
1392 if (left == key_len) {
1393 ds_put_cstr(ds, "<empty>");
1395 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1396 for (i = 0; i < left; i++) {
1397 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1399 ds_put_char(ds, ')');
1401 if (!has_ethtype_key) {
1402 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1404 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1405 ntohs(nl_attr_get_be16(ma)));
1409 ds_put_cstr(ds, "<empty>");
1413 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1414 * OVS_KEY_ATTR_* attributes in 'key'. */
1416 odp_flow_key_format(const struct nlattr *key,
1417 size_t key_len, struct ds *ds)
1419 odp_flow_format(key, key_len, NULL, 0, ds, true);
1423 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1424 const uint8_t *nd_tll, struct ofpbuf *key)
1427 memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1431 memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1434 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1438 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1439 const uint8_t *nd_tll, struct ofpbuf *key)
1441 struct ovs_key_nd nd_key;
1443 memset(&nd_key, 0, sizeof nd_key);
1445 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1449 put_nd(&nd_key, nd_sll, nd_tll, key);
1454 put_nd_mask(int n, const char *nd_target_s,
1455 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1457 struct ovs_key_nd nd_mask;
1459 memset(&nd_mask, 0xff, sizeof nd_mask);
1461 if (strlen(nd_target_s) != 0 &&
1462 inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1466 put_nd(&nd_mask, nd_sll, nd_tll, mask);
1471 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1473 if (!strcasecmp(s, "no")) {
1474 *type = OVS_FRAG_TYPE_NONE;
1475 } else if (!strcasecmp(s, "first")) {
1476 *type = OVS_FRAG_TYPE_FIRST;
1477 } else if (!strcasecmp(s, "later")) {
1478 *type = OVS_FRAG_TYPE_LATER;
1486 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1488 return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1489 (mpls_tc << MPLS_TC_SHIFT) |
1490 (mpls_ttl << MPLS_TTL_SHIFT) |
1491 (mpls_bos << MPLS_BOS_SHIFT)));
1495 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1496 struct ofpbuf *key, struct ofpbuf *mask)
1498 /* Many of the sscanf calls in this function use oversized destination
1499 * fields because some sscanf() implementations truncate the range of %i
1500 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1501 * value of 0x7fff. The other alternatives are to allow only a single
1502 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1505 * The tun_id parser has to use an alternative approach because there is no
1506 * type larger than 64 bits. */
1509 unsigned long long int priority;
1510 unsigned long long int priority_mask;
1513 if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority,
1514 &priority_mask, &n) > 0 && n > 0) {
1515 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1516 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1518 } else if (sscanf(s, "skb_priority(%lli)%n",
1519 &priority, &n) > 0 && n > 0) {
1520 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1522 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1529 unsigned long long int mark;
1530 unsigned long long int mark_mask;
1533 if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark,
1534 &mark_mask, &n) > 0 && n > 0) {
1535 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1536 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1538 } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
1539 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1541 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1549 int tos, tos_mask, ttl, ttl_mask;
1550 struct flow_tnl tun_key, tun_key_mask;
1551 unsigned long long tun_id_mask;
1554 if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1555 "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1556 "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n",
1557 tun_id_s, &tun_id_mask,
1558 IP_SCAN_ARGS(&tun_key.ip_src),
1559 IP_SCAN_ARGS(&tun_key_mask.ip_src),
1560 IP_SCAN_ARGS(&tun_key.ip_dst),
1561 IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1562 &tos, &tos_mask, &ttl, &ttl_mask,
1567 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1568 tun_key_mask.tun_id = htonll(tun_id_mask);
1569 tun_key.ip_tos = tos;
1570 tun_key_mask.ip_tos = tos_mask;
1571 tun_key.ip_ttl = ttl;
1572 tun_key_mask.ip_ttl = ttl_mask;
1573 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1574 tun_key.flags = flags;
1575 tun_key_mask.flags = UINT16_MAX;
1585 tun_key_to_attr(key, &tun_key);
1587 tun_key_to_attr(mask, &tun_key_mask);
1590 } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1591 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1592 ",tos=%i,ttl=%i,flags%n", tun_id_s,
1593 IP_SCAN_ARGS(&tun_key.ip_src),
1594 IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1599 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1600 tun_key.ip_tos = tos;
1601 tun_key.ip_ttl = ttl;
1602 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1603 tun_key.flags = flags;
1613 tun_key_to_attr(key, &tun_key);
1616 memset(&tun_key, 0xff, sizeof tun_key);
1617 tun_key_to_attr(mask, &tun_key);
1624 unsigned long long int in_port;
1625 unsigned long long int in_port_mask;
1628 if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port,
1629 &in_port_mask, &n) > 0 && n > 0) {
1630 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1631 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1633 } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1634 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1636 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1643 if (port_names && !strncmp(s, "in_port(", 8)) {
1645 const struct simap_node *node;
1649 name_len = strcspn(s, ")");
1650 node = simap_find_len(port_names, name, name_len);
1652 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1655 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1657 return 8 + name_len + 1;
1662 struct ovs_key_ethernet eth_key;
1663 struct ovs_key_ethernet eth_key_mask;
1666 if (mask && sscanf(s,
1667 "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1668 "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1669 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1670 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1671 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1672 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) {
1674 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1675 ð_key, sizeof eth_key);
1676 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1677 ð_key_mask, sizeof eth_key_mask);
1679 } else if (sscanf(s,
1680 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1681 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1682 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1683 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1684 ð_key, sizeof eth_key);
1687 memset(ð_key, 0xff, sizeof eth_key);
1688 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1689 ð_key, sizeof eth_key);
1696 uint16_t vid, vid_mask;
1701 if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n",
1702 &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) {
1703 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1704 htons((vid << VLAN_VID_SHIFT) |
1705 (pcp << VLAN_PCP_SHIFT) |
1707 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1708 htons((vid_mask << VLAN_VID_SHIFT) |
1709 (pcp_mask << VLAN_PCP_SHIFT) |
1710 (1 << VLAN_CFI_SHIFT)));
1712 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
1713 &vid, &pcp, &n) > 0 && n > 0)) {
1714 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1715 htons((vid << VLAN_VID_SHIFT) |
1716 (pcp << VLAN_PCP_SHIFT) |
1719 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1722 } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n",
1723 &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) {
1724 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1725 htons((vid << VLAN_VID_SHIFT) |
1726 (pcp << VLAN_PCP_SHIFT) |
1727 (cfi ? VLAN_CFI : 0)));
1728 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1729 htons((vid_mask << VLAN_VID_SHIFT) |
1730 (pcp_mask << VLAN_PCP_SHIFT) |
1731 (cfi_mask << VLAN_CFI_SHIFT)));
1733 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1734 &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
1735 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1736 htons((vid << VLAN_VID_SHIFT) |
1737 (pcp << VLAN_PCP_SHIFT) |
1738 (cfi ? VLAN_CFI : 0)));
1740 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, htons(UINT16_MAX));
1751 if (mask && sscanf(s, "eth_type(%i/%i)%n",
1752 ð_type, ð_type_mask, &n) > 0 && n > 0) {
1753 if (eth_type != 0) {
1754 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1756 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1758 } else if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
1759 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1761 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE,
1769 int label, tc, ttl, bos;
1770 int label_mask, tc_mask, ttl_mask, bos_mask;
1773 if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1774 &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1775 struct ovs_key_mpls *mpls, *mpls_mask;
1777 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1779 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1781 mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1783 mpls_mask->mpls_lse = mpls_lse_from_components(
1784 label_mask, tc_mask, ttl_mask, bos_mask);
1786 } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1787 &label, &tc, &ttl, &bos, &n) > 0 &&
1789 struct ovs_key_mpls *mpls;
1791 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1793 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1795 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1797 mpls->mpls_lse = htonl(UINT32_MAX);
1805 ovs_be32 ipv4_src, ipv4_src_mask;
1806 ovs_be32 ipv4_dst, ipv4_dst_mask;
1807 int ipv4_proto, ipv4_proto_mask;
1808 int ipv4_tos, ipv4_tos_mask;
1809 int ipv4_ttl, ipv4_ttl_mask;
1812 enum ovs_frag_type ipv4_frag;
1815 if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1816 "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1817 "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1818 "frag=%7[a-z]/%i)%n",
1819 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1820 IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1821 &ipv4_proto, &ipv4_proto_mask,
1822 &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1823 frag, &ipv4_frag_mask, &n) > 0
1825 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1826 struct ovs_key_ipv4 ipv4_key;
1827 struct ovs_key_ipv4 ipv4_mask;
1829 ipv4_key.ipv4_src = ipv4_src;
1830 ipv4_key.ipv4_dst = ipv4_dst;
1831 ipv4_key.ipv4_proto = ipv4_proto;
1832 ipv4_key.ipv4_tos = ipv4_tos;
1833 ipv4_key.ipv4_ttl = ipv4_ttl;
1834 ipv4_key.ipv4_frag = ipv4_frag;
1835 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1836 &ipv4_key, sizeof ipv4_key);
1838 ipv4_mask.ipv4_src = ipv4_src_mask;
1839 ipv4_mask.ipv4_dst = ipv4_dst_mask;
1840 ipv4_mask.ipv4_proto = ipv4_proto_mask;
1841 ipv4_mask.ipv4_tos = ipv4_tos_mask;
1842 ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1843 ipv4_mask.ipv4_frag = ipv4_frag_mask;
1844 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1845 &ipv4_mask, sizeof ipv4_mask);
1847 } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1848 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1849 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1850 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1852 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1853 struct ovs_key_ipv4 ipv4_key;
1855 ipv4_key.ipv4_src = ipv4_src;
1856 ipv4_key.ipv4_dst = ipv4_dst;
1857 ipv4_key.ipv4_proto = ipv4_proto;
1858 ipv4_key.ipv4_tos = ipv4_tos;
1859 ipv4_key.ipv4_ttl = ipv4_ttl;
1860 ipv4_key.ipv4_frag = ipv4_frag;
1861 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1862 &ipv4_key, sizeof ipv4_key);
1865 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1866 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1867 &ipv4_key, sizeof ipv4_key);
1874 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1875 char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1876 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1877 char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1878 int ipv6_label, ipv6_label_mask;
1879 int ipv6_proto, ipv6_proto_mask;
1880 int ipv6_tclass, ipv6_tclass_mask;
1881 int ipv6_hlimit, ipv6_hlimit_mask;
1883 enum ovs_frag_type ipv6_frag;
1887 if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1888 IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1889 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1890 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1891 ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1892 &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1893 &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1894 &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1895 &ipv6_frag_mask, &n) > 0
1897 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1898 struct ovs_key_ipv6 ipv6_key;
1899 struct ovs_key_ipv6 ipv6_mask;
1901 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1902 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1903 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1904 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1908 ipv6_key.ipv6_label = htonl(ipv6_label);
1909 ipv6_key.ipv6_proto = ipv6_proto;
1910 ipv6_key.ipv6_tclass = ipv6_tclass;
1911 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1912 ipv6_key.ipv6_frag = ipv6_frag;
1913 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1914 &ipv6_key, sizeof ipv6_key);
1916 ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1917 ipv6_mask.ipv6_proto = ipv6_proto_mask;
1918 ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1919 ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1920 ipv6_mask.ipv6_frag = ipv6_frag_mask;
1921 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1922 &ipv6_mask, sizeof ipv6_mask);
1924 } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1925 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1926 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1927 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1929 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1930 struct ovs_key_ipv6 ipv6_key;
1932 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1933 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1936 ipv6_key.ipv6_label = htonl(ipv6_label);
1937 ipv6_key.ipv6_proto = ipv6_proto;
1938 ipv6_key.ipv6_tclass = ipv6_tclass;
1939 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1940 ipv6_key.ipv6_frag = ipv6_frag;
1941 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1942 &ipv6_key, sizeof ipv6_key);
1945 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1946 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1947 &ipv6_key, sizeof ipv6_key);
1960 if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
1961 &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
1963 struct ovs_key_tcp tcp_key;
1964 struct ovs_key_tcp tcp_mask;
1966 tcp_key.tcp_src = htons(tcp_src);
1967 tcp_key.tcp_dst = htons(tcp_dst);
1968 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1970 tcp_mask.tcp_src = htons(tcp_src_mask);
1971 tcp_mask.tcp_dst = htons(tcp_dst_mask);
1972 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1973 &tcp_mask, sizeof tcp_mask);
1975 } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1977 struct ovs_key_tcp tcp_key;
1979 tcp_key.tcp_src = htons(tcp_src);
1980 tcp_key.tcp_dst = htons(tcp_dst);
1981 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1984 memset(&tcp_key, 0xff, sizeof tcp_key);
1985 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1986 &tcp_key, sizeof tcp_key);
1999 if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
2000 &udp_src, &udp_src_mask,
2001 &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
2002 struct ovs_key_udp udp_key;
2003 struct ovs_key_udp udp_mask;
2005 udp_key.udp_src = htons(udp_src);
2006 udp_key.udp_dst = htons(udp_dst);
2007 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2009 udp_mask.udp_src = htons(udp_src_mask);
2010 udp_mask.udp_dst = htons(udp_dst_mask);
2011 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2012 &udp_mask, sizeof udp_mask);
2015 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
2017 struct ovs_key_udp udp_key;
2019 udp_key.udp_src = htons(udp_src);
2020 udp_key.udp_dst = htons(udp_dst);
2021 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2024 memset(&udp_key, 0xff, sizeof udp_key);
2025 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2038 if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
2039 &icmp_type, &icmp_type_mask,
2040 &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
2041 struct ovs_key_icmp icmp_key;
2042 struct ovs_key_icmp icmp_mask;
2044 icmp_key.icmp_type = icmp_type;
2045 icmp_key.icmp_code = icmp_code;
2046 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2047 &icmp_key, sizeof icmp_key);
2049 icmp_mask.icmp_type = icmp_type_mask;
2050 icmp_mask.icmp_code = icmp_code_mask;
2051 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2052 &icmp_mask, sizeof icmp_mask);
2054 } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
2055 &icmp_type, &icmp_code, &n) > 0
2057 struct ovs_key_icmp icmp_key;
2059 icmp_key.icmp_type = icmp_type;
2060 icmp_key.icmp_code = icmp_code;
2061 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2062 &icmp_key, sizeof icmp_key);
2064 memset(&icmp_key, 0xff, sizeof icmp_key);
2065 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2073 struct ovs_key_icmpv6 icmpv6_key;
2074 struct ovs_key_icmpv6 icmpv6_mask;
2075 int icmpv6_type_mask;
2076 int icmpv6_code_mask;
2079 if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
2080 &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
2081 &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
2083 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2084 &icmpv6_key, sizeof icmpv6_key);
2086 icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2087 icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2088 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2089 sizeof icmpv6_mask);
2091 } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2092 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2094 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2095 &icmpv6_key, sizeof icmpv6_key);
2098 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2099 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2107 ovs_be32 arp_sip, arp_sip_mask;
2108 ovs_be32 arp_tip, arp_tip_mask;
2109 int arp_op, arp_op_mask;
2110 uint8_t arp_sha[ETH_ADDR_LEN];
2111 uint8_t arp_sha_mask[ETH_ADDR_LEN];
2112 uint8_t arp_tha[ETH_ADDR_LEN];
2113 uint8_t arp_tha_mask[ETH_ADDR_LEN];
2116 if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2117 "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2118 "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2119 "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2120 IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2121 IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2122 &arp_op, &arp_op_mask,
2123 ETH_ADDR_SCAN_ARGS(arp_sha),
2124 ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2125 ETH_ADDR_SCAN_ARGS(arp_tha),
2126 ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2127 struct ovs_key_arp arp_key;
2128 struct ovs_key_arp arp_mask;
2130 memset(&arp_key, 0, sizeof arp_key);
2131 arp_key.arp_sip = arp_sip;
2132 arp_key.arp_tip = arp_tip;
2133 arp_key.arp_op = htons(arp_op);
2134 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2135 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2136 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2138 arp_mask.arp_sip = arp_sip_mask;
2139 arp_mask.arp_tip = arp_tip_mask;
2140 arp_mask.arp_op = htons(arp_op_mask);
2141 memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2142 memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2143 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2144 &arp_mask, sizeof arp_mask);
2146 } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2147 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2148 IP_SCAN_ARGS(&arp_sip),
2149 IP_SCAN_ARGS(&arp_tip),
2151 ETH_ADDR_SCAN_ARGS(arp_sha),
2152 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2153 struct ovs_key_arp arp_key;
2155 memset(&arp_key, 0, sizeof arp_key);
2156 arp_key.arp_sip = arp_sip;
2157 arp_key.arp_tip = arp_tip;
2158 arp_key.arp_op = htons(arp_op);
2159 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2160 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2161 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2164 memset(&arp_key, 0xff, sizeof arp_key);
2165 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2166 &arp_key, sizeof arp_key);
2173 char nd_target_s[IPV6_SCAN_LEN + 1];
2174 char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2175 uint8_t nd_sll[ETH_ADDR_LEN];
2176 uint8_t nd_sll_mask[ETH_ADDR_LEN];
2177 uint8_t nd_tll[ETH_ADDR_LEN];
2178 uint8_t nd_tll_mask[ETH_ADDR_LEN];
2181 nd_target_mask_s[0] = 0;
2182 memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2183 memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2185 if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2186 nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2187 put_nd_key(n, nd_target_s, NULL, NULL, key);
2188 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2189 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2190 nd_target_s, &n) > 0 && n > 0) {
2191 put_nd_key(n, nd_target_s, NULL, NULL, key);
2193 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2195 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2196 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2197 nd_target_s, nd_target_mask_s,
2198 ETH_ADDR_SCAN_ARGS(nd_sll),
2199 ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2200 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2201 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2202 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2203 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2205 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2207 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2209 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2210 ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2211 nd_target_s, nd_target_mask_s,
2212 ETH_ADDR_SCAN_ARGS(nd_tll),
2213 ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2214 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2215 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2216 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2217 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2219 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2221 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2223 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2224 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2225 "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2226 nd_target_s, nd_target_mask_s,
2227 ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2228 ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2231 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2232 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2233 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2234 "tll="ETH_ADDR_SCAN_FMT")%n",
2235 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2236 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2238 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2240 put_nd_mask(n, nd_target_mask_s,
2241 nd_sll_mask, nd_tll_mask, mask);
2250 if (!strncmp(s, "encap(", 6)) {
2251 const char *start = s;
2252 size_t encap, encap_mask = 0;
2254 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2256 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2263 s += strspn(s, ", \t\r\n");
2266 } else if (*s == ')') {
2270 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2278 nl_msg_end_nested(key, encap);
2280 nl_msg_end_nested(mask, encap_mask);
2289 /* Parses the string representation of a datapath flow key, in the
2290 * format output by odp_flow_key_format(). Returns 0 if successful,
2291 * otherwise a positive errno value. On success, the flow key is
2292 * appended to 'key' as a series of Netlink attributes. On failure, no
2293 * data is appended to 'key'. Either way, 'key''s data might be
2296 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2297 * to a port number. (Port names may be used instead of port numbers in
2300 * On success, the attributes appended to 'key' are individually syntactically
2301 * valid, but they may not be valid as a sequence. 'key' might, for example,
2302 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2304 odp_flow_from_string(const char *s, const struct simap *port_names,
2305 struct ofpbuf *key, struct ofpbuf *mask)
2307 const size_t old_size = key->size;
2311 s += strspn(s, delimiters);
2316 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2318 key->size = old_size;
2328 ovs_to_odp_frag(uint8_t nw_frag)
2330 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2331 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2332 : OVS_FRAG_TYPE_LATER);
2336 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2338 uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2340 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2341 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2347 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2348 const struct flow *flow, odp_port_t odp_in_port)
2351 struct ovs_key_ethernet *eth_key;
2354 /* We assume that if 'data' and 'flow' are not the same, we should
2355 * treat 'data' as a mask. */
2356 is_mask = (data != flow);
2358 if (flow->skb_priority) {
2359 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2362 if (flow->tunnel.ip_dst || is_mask) {
2363 tun_key_to_attr(buf, &data->tunnel);
2366 if (flow->skb_mark) {
2367 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->skb_mark);
2370 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2371 * is not the magical value "ODPP_NONE". */
2372 if (is_mask || odp_in_port != ODPP_NONE) {
2373 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2376 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2378 memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2379 memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2381 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2383 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2385 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2387 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2388 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2389 if (flow->vlan_tci == htons(0)) {
2396 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2397 /* For backwards compatibility with kernels that don't support
2398 * wildcarding, the following convention is used to encode the
2399 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2402 * -------- -------- -------
2403 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2404 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2405 * <none> 0xffff Any non-Ethernet II frame (except valid
2406 * 802.3 SNAP packet with valid eth_type).
2409 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2414 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2416 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2417 struct ovs_key_ipv4 *ipv4_key;
2419 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2421 ipv4_key->ipv4_src = data->nw_src;
2422 ipv4_key->ipv4_dst = data->nw_dst;
2423 ipv4_key->ipv4_proto = data->nw_proto;
2424 ipv4_key->ipv4_tos = data->nw_tos;
2425 ipv4_key->ipv4_ttl = data->nw_ttl;
2426 ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2427 : ovs_to_odp_frag(data->nw_frag);
2428 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2429 struct ovs_key_ipv6 *ipv6_key;
2431 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2433 memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2434 memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2435 ipv6_key->ipv6_label = data->ipv6_label;
2436 ipv6_key->ipv6_proto = data->nw_proto;
2437 ipv6_key->ipv6_tclass = data->nw_tos;
2438 ipv6_key->ipv6_hlimit = data->nw_ttl;
2439 ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2440 : ovs_to_odp_frag(data->nw_frag);
2441 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2442 flow->dl_type == htons(ETH_TYPE_RARP)) {
2443 struct ovs_key_arp *arp_key;
2445 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2447 memset(arp_key, 0, sizeof *arp_key);
2448 arp_key->arp_sip = data->nw_src;
2449 arp_key->arp_tip = data->nw_dst;
2450 arp_key->arp_op = htons(data->nw_proto);
2451 memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2452 memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2455 if (flow->mpls_depth) {
2456 struct ovs_key_mpls *mpls_key;
2458 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2460 mpls_key->mpls_lse = data->mpls_lse;
2463 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2464 if (flow->nw_proto == IPPROTO_TCP) {
2465 struct ovs_key_tcp *tcp_key;
2467 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2469 tcp_key->tcp_src = data->tp_src;
2470 tcp_key->tcp_dst = data->tp_dst;
2471 } else if (flow->nw_proto == IPPROTO_UDP) {
2472 struct ovs_key_udp *udp_key;
2474 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2476 udp_key->udp_src = data->tp_src;
2477 udp_key->udp_dst = data->tp_dst;
2478 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2479 && flow->nw_proto == IPPROTO_ICMP) {
2480 struct ovs_key_icmp *icmp_key;
2482 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2484 icmp_key->icmp_type = ntohs(data->tp_src);
2485 icmp_key->icmp_code = ntohs(data->tp_dst);
2486 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2487 && flow->nw_proto == IPPROTO_ICMPV6) {
2488 struct ovs_key_icmpv6 *icmpv6_key;
2490 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2491 sizeof *icmpv6_key);
2492 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2493 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2495 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
2496 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
2497 struct ovs_key_nd *nd_key;
2499 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2501 memcpy(nd_key->nd_target, &data->nd_target,
2502 sizeof nd_key->nd_target);
2503 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2504 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2511 nl_msg_end_nested(buf, encap);
2515 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2516 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2517 * number rather than a datapath port number). Instead, if 'odp_in_port'
2518 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2521 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2522 * capable of being expanded to allow for that much space. */
2524 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2525 odp_port_t odp_in_port)
2527 odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2530 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2531 * 'buf'. 'flow' is used as a template to determine how to interpret
2532 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2533 * it doesn't indicate whether the other fields should be interpreted as
2534 * ARP, IPv4, IPv6, etc.
2536 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2537 * capable of being expanded to allow for that much space. */
2539 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2540 const struct flow *flow, uint32_t odp_in_port_mask)
2542 odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask));
2546 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2548 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2549 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2550 key_len / sizeof(uint32_t), 0);
2554 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2555 uint64_t attrs, int out_of_range_attr,
2556 const struct nlattr *key, size_t key_len)
2561 if (VLOG_DROP_DBG(rl)) {
2566 for (i = 0; i < 64; i++) {
2567 if (attrs & (UINT64_C(1) << i)) {
2568 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2570 ds_put_format(&s, " %s",
2571 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2574 if (out_of_range_attr) {
2575 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2578 ds_put_cstr(&s, ": ");
2579 odp_flow_key_format(key, key_len, &s);
2581 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2586 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2588 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2590 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2591 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2595 if (odp_frag != OVS_FRAG_TYPE_NONE) {
2596 flow->nw_frag |= FLOW_NW_FRAG_ANY;
2597 if (odp_frag == OVS_FRAG_TYPE_LATER) {
2598 flow->nw_frag |= FLOW_NW_FRAG_LATER;
2605 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2606 const struct nlattr *attrs[], uint64_t *present_attrsp,
2607 int *out_of_range_attrp)
2609 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2610 const struct nlattr *nla;
2611 uint64_t present_attrs;
2614 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2616 *out_of_range_attrp = 0;
2617 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2618 uint16_t type = nl_attr_type(nla);
2619 size_t len = nl_attr_get_size(nla);
2620 int expected_len = odp_flow_key_attr_len(type);
2622 if (len != expected_len && expected_len >= 0) {
2623 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2625 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2626 "length %d", ovs_key_attr_to_string(type, namebuf,
2632 if (type > OVS_KEY_ATTR_MAX) {
2633 *out_of_range_attrp = type;
2635 if (present_attrs & (UINT64_C(1) << type)) {
2636 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2638 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2639 ovs_key_attr_to_string(type,
2640 namebuf, sizeof namebuf));
2644 present_attrs |= UINT64_C(1) << type;
2649 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2653 *present_attrsp = present_attrs;
2657 static enum odp_key_fitness
2658 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2659 uint64_t expected_attrs,
2660 const struct nlattr *key, size_t key_len)
2662 uint64_t missing_attrs;
2663 uint64_t extra_attrs;
2665 missing_attrs = expected_attrs & ~present_attrs;
2666 if (missing_attrs) {
2667 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2668 log_odp_key_attributes(&rl, "expected but not present",
2669 missing_attrs, 0, key, key_len);
2670 return ODP_FIT_TOO_LITTLE;
2673 extra_attrs = present_attrs & ~expected_attrs;
2674 if (extra_attrs || out_of_range_attr) {
2675 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2676 log_odp_key_attributes(&rl, "present but not expected",
2677 extra_attrs, out_of_range_attr, key, key_len);
2678 return ODP_FIT_TOO_MUCH;
2681 return ODP_FIT_PERFECT;
2685 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2686 uint64_t present_attrs, uint64_t *expected_attrs,
2689 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2691 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2692 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2693 if (ntohs(flow->dl_type) < 1536) {
2694 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2695 ntohs(flow->dl_type));
2698 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2700 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2705 static enum odp_key_fitness
2706 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2707 uint64_t present_attrs, int out_of_range_attr,
2708 uint64_t expected_attrs, struct flow *flow,
2709 const struct nlattr *key, size_t key_len)
2711 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2713 if (eth_type_mpls(flow->dl_type)) {
2714 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2716 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2717 return ODP_FIT_TOO_LITTLE;
2719 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2721 } else if (flow->dl_type == htons(ETH_TYPE_IP)) {
2722 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2723 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2724 const struct ovs_key_ipv4 *ipv4_key;
2726 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2727 flow->nw_src = ipv4_key->ipv4_src;
2728 flow->nw_dst = ipv4_key->ipv4_dst;
2729 flow->nw_proto = ipv4_key->ipv4_proto;
2730 flow->nw_tos = ipv4_key->ipv4_tos;
2731 flow->nw_ttl = ipv4_key->ipv4_ttl;
2732 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2733 return ODP_FIT_ERROR;
2736 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2737 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2738 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2739 const struct ovs_key_ipv6 *ipv6_key;
2741 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2742 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2743 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2744 flow->ipv6_label = ipv6_key->ipv6_label;
2745 flow->nw_proto = ipv6_key->ipv6_proto;
2746 flow->nw_tos = ipv6_key->ipv6_tclass;
2747 flow->nw_ttl = ipv6_key->ipv6_hlimit;
2748 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2749 return ODP_FIT_ERROR;
2752 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2753 flow->dl_type == htons(ETH_TYPE_RARP)) {
2754 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2755 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2756 const struct ovs_key_arp *arp_key;
2758 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2759 flow->nw_src = arp_key->arp_sip;
2760 flow->nw_dst = arp_key->arp_tip;
2761 if (arp_key->arp_op & htons(0xff00)) {
2762 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2763 "key", ntohs(arp_key->arp_op));
2764 return ODP_FIT_ERROR;
2766 flow->nw_proto = ntohs(arp_key->arp_op);
2767 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2768 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2772 if (flow->nw_proto == IPPROTO_TCP
2773 && (flow->dl_type == htons(ETH_TYPE_IP) ||
2774 flow->dl_type == htons(ETH_TYPE_IPV6))
2775 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2776 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2777 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2778 const struct ovs_key_tcp *tcp_key;
2780 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2781 flow->tp_src = tcp_key->tcp_src;
2782 flow->tp_dst = tcp_key->tcp_dst;
2784 } else if (flow->nw_proto == IPPROTO_UDP
2785 && (flow->dl_type == htons(ETH_TYPE_IP) ||
2786 flow->dl_type == htons(ETH_TYPE_IPV6))
2787 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2788 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2789 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2790 const struct ovs_key_udp *udp_key;
2792 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2793 flow->tp_src = udp_key->udp_src;
2794 flow->tp_dst = udp_key->udp_dst;
2796 } else if (flow->nw_proto == IPPROTO_ICMP
2797 && flow->dl_type == htons(ETH_TYPE_IP)
2798 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2799 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
2800 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
2801 const struct ovs_key_icmp *icmp_key;
2803 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
2804 flow->tp_src = htons(icmp_key->icmp_type);
2805 flow->tp_dst = htons(icmp_key->icmp_code);
2807 } else if (flow->nw_proto == IPPROTO_ICMPV6
2808 && flow->dl_type == htons(ETH_TYPE_IPV6)
2809 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2810 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
2811 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
2812 const struct ovs_key_icmpv6 *icmpv6_key;
2814 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
2815 flow->tp_src = htons(icmpv6_key->icmpv6_type);
2816 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
2818 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2819 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2820 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2821 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
2822 const struct ovs_key_nd *nd_key;
2824 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
2825 memcpy(&flow->nd_target, nd_key->nd_target,
2826 sizeof flow->nd_target);
2827 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
2828 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
2834 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
2838 /* Parse 802.1Q header then encapsulated L3 attributes. */
2839 static enum odp_key_fitness
2840 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2841 uint64_t present_attrs, int out_of_range_attr,
2842 uint64_t expected_attrs, struct flow *flow,
2843 const struct nlattr *key, size_t key_len)
2845 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2847 const struct nlattr *encap
2848 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
2849 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
2850 enum odp_key_fitness encap_fitness;
2851 enum odp_key_fitness fitness;
2854 /* Calculate fitness of outer attributes. */
2855 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
2856 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
2857 fitness = check_expectations(present_attrs, out_of_range_attr,
2858 expected_attrs, key, key_len);
2860 /* Get the VLAN TCI value. */
2861 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
2862 return ODP_FIT_TOO_LITTLE;
2864 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
2865 if (tci == htons(0)) {
2866 /* Corner case for a truncated 802.1Q header. */
2867 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
2868 return ODP_FIT_TOO_MUCH;
2871 } else if (!(tci & htons(VLAN_CFI))) {
2872 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
2873 "but CFI bit is not set", ntohs(tci));
2874 return ODP_FIT_ERROR;
2878 * Remove the TPID from dl_type since it's not the real Ethertype. */
2879 flow->vlan_tci = tci;
2880 flow->dl_type = htons(0);
2882 /* Now parse the encapsulated attributes. */
2883 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
2884 attrs, &present_attrs, &out_of_range_attr)) {
2885 return ODP_FIT_ERROR;
2889 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2890 return ODP_FIT_ERROR;
2892 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2893 expected_attrs, flow, key, key_len);
2895 /* The overall fitness is the worse of the outer and inner attributes. */
2896 return MAX(fitness, encap_fitness);
2899 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2900 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2901 * 'key' fits our expectations for what a flow key should contain.
2903 * The 'in_port' will be the datapath's understanding of the port. The
2904 * caller will need to translate with odp_port_to_ofp_port() if the
2905 * OpenFlow port is needed.
2907 * This function doesn't take the packet itself as an argument because none of
2908 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2909 * it is always possible to infer which additional attribute(s) should appear
2910 * by looking at the attributes for lower-level protocols, e.g. if the network
2911 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2912 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2913 * must be absent. */
2914 enum odp_key_fitness
2915 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
2918 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
2919 uint64_t expected_attrs;
2920 uint64_t present_attrs;
2921 int out_of_range_attr;
2923 memset(flow, 0, sizeof *flow);
2925 /* Parse attributes. */
2926 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
2927 &out_of_range_attr)) {
2928 return ODP_FIT_ERROR;
2933 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
2934 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
2935 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
2938 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
2939 flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
2940 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
2943 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
2944 enum odp_key_fitness res;
2946 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
2947 if (res == ODP_FIT_ERROR) {
2948 return ODP_FIT_ERROR;
2949 } else if (res == ODP_FIT_PERFECT) {
2950 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
2954 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
2955 flow->in_port.odp_port
2956 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
2957 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
2959 flow->in_port.odp_port = ODPP_NONE;
2962 /* Ethernet header. */
2963 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
2964 const struct ovs_key_ethernet *eth_key;
2966 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
2967 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
2968 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
2970 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
2972 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2973 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
2974 return ODP_FIT_ERROR;
2977 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
2978 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
2979 expected_attrs, flow, key, key_len);
2981 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
2982 expected_attrs, flow, key, key_len);
2985 /* Returns 'fitness' as a string, for use in debug messages. */
2987 odp_key_fitness_to_string(enum odp_key_fitness fitness)
2990 case ODP_FIT_PERFECT:
2992 case ODP_FIT_TOO_MUCH:
2994 case ODP_FIT_TOO_LITTLE:
2995 return "too_little";
3003 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3004 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3005 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3006 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3007 * null, then the return value is not meaningful.) */
3009 odp_put_userspace_action(uint32_t pid,
3010 const void *userdata, size_t userdata_size,
3011 struct ofpbuf *odp_actions)
3013 size_t userdata_ofs;
3016 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3017 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3019 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3020 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3021 userdata, userdata_size);
3025 nl_msg_end_nested(odp_actions, offset);
3027 return userdata_ofs;
3031 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3032 struct ofpbuf *odp_actions)
3034 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3035 tun_key_to_attr(odp_actions, tunnel);
3036 nl_msg_end_nested(odp_actions, offset);
3039 /* The commit_odp_actions() function and its helpers. */
3042 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3043 const void *key, size_t key_size)
3045 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3046 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3047 nl_msg_end_nested(odp_actions, offset);
3051 odp_put_skb_mark_action(const uint32_t skb_mark,
3052 struct ofpbuf *odp_actions)
3054 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &skb_mark,
3058 /* If any of the flow key data that ODP actions can modify are different in
3059 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3060 * 'odp_actions' that change the flow tunneling information in key from
3061 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3062 * same way. In other words, operates the same as commit_odp_actions(), but
3063 * only on tunneling information. */
3065 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3066 struct ofpbuf *odp_actions)
3068 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3069 if (flow->tunnel.ip_dst) {
3070 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3073 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3074 odp_put_tunnel_action(&base->tunnel, odp_actions);
3079 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3080 struct ofpbuf *odp_actions,
3081 struct flow_wildcards *wc)
3083 struct ovs_key_ethernet eth_key;
3085 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3086 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3090 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3091 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3093 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3094 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3096 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3097 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3099 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3100 ð_key, sizeof(eth_key));
3104 commit_vlan_action(const struct flow *flow, struct flow *base,
3105 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3107 if (base->vlan_tci == flow->vlan_tci) {
3111 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3113 if (base->vlan_tci & htons(VLAN_CFI)) {
3114 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3117 if (flow->vlan_tci & htons(VLAN_CFI)) {
3118 struct ovs_action_push_vlan vlan;
3120 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3121 vlan.vlan_tci = flow->vlan_tci;
3122 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3123 &vlan, sizeof vlan);
3125 base->vlan_tci = flow->vlan_tci;
3129 commit_mpls_action(const struct flow *flow, struct flow *base,
3130 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3132 if (flow->mpls_lse == base->mpls_lse &&
3133 flow->mpls_depth == base->mpls_depth) {
3137 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3139 if (flow->mpls_depth < base->mpls_depth) {
3140 if (base->mpls_depth - flow->mpls_depth > 1) {
3141 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3142 VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
3143 " a single mpls_pop action");
3146 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3147 } else if (flow->mpls_depth > base->mpls_depth) {
3148 struct ovs_action_push_mpls *mpls;
3150 if (flow->mpls_depth - base->mpls_depth > 1) {
3151 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3152 VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
3153 " a single mpls_push action");
3156 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3158 memset(mpls, 0, sizeof *mpls);
3159 mpls->mpls_ethertype = flow->dl_type;
3160 mpls->mpls_lse = flow->mpls_lse;
3162 struct ovs_key_mpls mpls_key;
3164 mpls_key.mpls_lse = flow->mpls_lse;
3165 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3166 &mpls_key, sizeof(mpls_key));
3169 base->dl_type = flow->dl_type;
3170 base->mpls_lse = flow->mpls_lse;
3171 base->mpls_depth = flow->mpls_depth;
3175 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3176 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3178 struct ovs_key_ipv4 ipv4_key;
3180 if (base->nw_src == flow->nw_src &&
3181 base->nw_dst == flow->nw_dst &&
3182 base->nw_tos == flow->nw_tos &&
3183 base->nw_ttl == flow->nw_ttl &&
3184 base->nw_frag == flow->nw_frag) {
3188 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3189 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3190 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3191 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3192 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3193 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3195 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3196 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3197 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3198 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3199 ipv4_key.ipv4_proto = base->nw_proto;
3200 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3202 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3203 &ipv4_key, sizeof(ipv4_key));
3207 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3208 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3210 struct ovs_key_ipv6 ipv6_key;
3212 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3213 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3214 base->ipv6_label == flow->ipv6_label &&
3215 base->nw_tos == flow->nw_tos &&
3216 base->nw_ttl == flow->nw_ttl &&
3217 base->nw_frag == flow->nw_frag) {
3221 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3222 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3223 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3224 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3225 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3226 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3227 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3229 base->ipv6_src = flow->ipv6_src;
3230 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3231 base->ipv6_dst = flow->ipv6_dst;
3232 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3234 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3235 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3236 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3237 ipv6_key.ipv6_proto = base->nw_proto;
3238 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3240 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3241 &ipv6_key, sizeof(ipv6_key));
3245 commit_set_nw_action(const struct flow *flow, struct flow *base,
3246 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3248 /* Check if flow really have an IP header. */
3249 if (!flow->nw_proto) {
3253 if (base->dl_type == htons(ETH_TYPE_IP)) {
3254 commit_set_ipv4_action(flow, base, odp_actions, wc);
3255 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
3256 commit_set_ipv6_action(flow, base, odp_actions, wc);
3261 commit_set_port_action(const struct flow *flow, struct flow *base,
3262 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3264 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3268 if (base->tp_src == flow->tp_src &&
3269 base->tp_dst == flow->tp_dst) {
3273 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3274 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3276 if (flow->nw_proto == IPPROTO_TCP) {
3277 struct ovs_key_tcp port_key;
3279 port_key.tcp_src = base->tp_src = flow->tp_src;
3280 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3282 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3283 &port_key, sizeof(port_key));
3285 } else if (flow->nw_proto == IPPROTO_UDP) {
3286 struct ovs_key_udp port_key;
3288 port_key.udp_src = base->tp_src = flow->tp_src;
3289 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3291 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3292 &port_key, sizeof(port_key));
3297 commit_set_priority_action(const struct flow *flow, struct flow *base,
3298 struct ofpbuf *odp_actions,
3299 struct flow_wildcards *wc)
3301 if (base->skb_priority == flow->skb_priority) {
3305 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3306 base->skb_priority = flow->skb_priority;
3308 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3309 &base->skb_priority, sizeof(base->skb_priority));
3313 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
3314 struct ofpbuf *odp_actions,
3315 struct flow_wildcards *wc)
3317 if (base->skb_mark == flow->skb_mark) {
3321 memset(&wc->masks.skb_mark, 0xff, sizeof wc->masks.skb_mark);
3322 base->skb_mark = flow->skb_mark;
3324 odp_put_skb_mark_action(base->skb_mark, odp_actions);
3326 /* If any of the flow key data that ODP actions can modify are different in
3327 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3328 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3329 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3330 * in addition to this function if needed. Sets fields in 'wc' that are
3331 * used as part of the action. */
3333 commit_odp_actions(const struct flow *flow, struct flow *base,
3334 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3336 commit_set_ether_addr_action(flow, base, odp_actions, wc);
3337 commit_vlan_action(flow, base, odp_actions, wc);
3338 commit_set_nw_action(flow, base, odp_actions, wc);
3339 commit_set_port_action(flow, base, odp_actions, wc);
3340 /* Committing MPLS actions should occur after committing nw and port
3341 * actions. This is because committing MPLS actions may alter a packet so
3342 * that it is no longer IP and thus nw and port actions are no longer valid.
3344 commit_mpls_action(flow, base, odp_actions, wc);
3345 commit_set_priority_action(flow, base, odp_actions, wc);
3346 commit_set_skb_mark_action(flow, base, odp_actions, wc);