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_SCTP: return "sctp";
113 case OVS_KEY_ATTR_ICMP: return "icmp";
114 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
115 case OVS_KEY_ATTR_ARP: return "arp";
116 case OVS_KEY_ATTR_ND: return "nd";
117 case OVS_KEY_ATTR_MPLS: return "mpls";
119 case __OVS_KEY_ATTR_MAX:
121 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
127 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
129 size_t len = nl_attr_get_size(a);
131 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
133 const uint8_t *unspec;
136 unspec = nl_attr_get(a);
137 for (i = 0; i < len; i++) {
138 ds_put_char(ds, i ? ' ': '(');
139 ds_put_format(ds, "%02x", unspec[i]);
141 ds_put_char(ds, ')');
146 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
148 static const struct nl_policy ovs_sample_policy[] = {
149 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */
150 { NL_A_U32, 0, 0, false }, /* OVS_SAMPLE_ATTR_PROBABILITY */
151 { NL_A_NESTED, 0, 0, false }, /* OVS_SAMPLE_ATTR_ACTIONS */
153 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
155 const struct nlattr *nla_acts;
158 ds_put_cstr(ds, "sample");
160 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
161 ds_put_cstr(ds, "(error)");
165 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
168 ds_put_format(ds, "(sample=%.1f%%,", percentage);
170 ds_put_cstr(ds, "actions(");
171 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
172 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
173 format_odp_actions(ds, nla_acts, len);
174 ds_put_format(ds, "))");
178 slow_path_reason_to_string(enum slow_path_reason reason)
189 case SLOW_CONTROLLER:
197 static enum slow_path_reason
198 string_to_slow_path_reason(const char *string)
200 enum slow_path_reason i;
202 for (i = 1; i < __SLOW_MAX; i++) {
203 if (!strcmp(string, slow_path_reason_to_string(i))) {
212 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
223 while (s[n] != ')') {
224 unsigned long long int flags;
228 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
229 n += n0 + (s[n + n0] == ',');
234 for (bit = 1; bit; bit <<= 1) {
235 const char *name = bit_to_string(bit);
243 if (!strncmp(s + n, name, len) &&
244 (s[n + len] == ',' || s[n + len] == ')')) {
246 n += len + (s[n + len] == ',');
262 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
264 static const struct nl_policy ovs_userspace_policy[] = {
265 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */
266 { NL_A_U32, 0, 0, false }, /* OVS_USERSPACE_ATTR_PID */
267 { NL_A_UNSPEC, 0, 0, true }, /* OVS_USERSPACE_ATTR_USERDATA */
269 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
270 const struct nlattr *userdata_attr;
272 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
273 ds_put_cstr(ds, "userspace(error)");
277 ds_put_format(ds, "userspace(pid=%"PRIu32,
278 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
280 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
283 const uint8_t *userdata = nl_attr_get(userdata_attr);
284 size_t userdata_len = nl_attr_get_size(userdata_attr);
285 bool userdata_unspec = true;
286 union user_action_cookie cookie;
288 if (userdata_len >= sizeof cookie.type
289 && userdata_len <= sizeof cookie) {
291 memset(&cookie, 0, sizeof cookie);
292 memcpy(&cookie, userdata, userdata_len);
294 userdata_unspec = false;
296 if (userdata_len == sizeof cookie.sflow
297 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
298 ds_put_format(ds, ",sFlow("
299 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
300 vlan_tci_to_vid(cookie.sflow.vlan_tci),
301 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
302 cookie.sflow.output);
303 } else if (userdata_len == sizeof cookie.slow_path
304 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
306 reason = slow_path_reason_to_string(cookie.slow_path.reason);
307 reason = reason ? reason : "";
308 ds_put_format(ds, ",slow_path(%s)", reason);
309 } else if (userdata_len == sizeof cookie.flow_sample
310 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
311 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
312 ",collector_set_id=%"PRIu32
313 ",obs_domain_id=%"PRIu32
314 ",obs_point_id=%"PRIu32")",
315 cookie.flow_sample.probability,
316 cookie.flow_sample.collector_set_id,
317 cookie.flow_sample.obs_domain_id,
318 cookie.flow_sample.obs_point_id);
319 } else if (userdata_len == sizeof cookie.ipfix
320 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
321 ds_put_format(ds, ",ipfix");
323 userdata_unspec = true;
327 if (userdata_unspec) {
329 ds_put_format(ds, ",userdata(");
330 for (i = 0; i < userdata_len; i++) {
331 ds_put_format(ds, "%02x", userdata[i]);
333 ds_put_char(ds, ')');
337 ds_put_char(ds, ')');
341 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
343 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
344 vlan_tci_to_vid(vlan_tci),
345 vlan_tci_to_pcp(vlan_tci));
346 if (!(vlan_tci & htons(VLAN_CFI))) {
347 ds_put_cstr(ds, ",cfi=0");
352 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
354 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
355 mpls_lse_to_label(mpls_lse),
356 mpls_lse_to_tc(mpls_lse),
357 mpls_lse_to_ttl(mpls_lse),
358 mpls_lse_to_bos(mpls_lse));
362 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
363 const struct ovs_key_mpls *mpls_mask)
365 ovs_be32 key = mpls_key->mpls_lse;
367 if (mpls_mask == NULL) {
368 format_mpls_lse(ds, key);
370 ovs_be32 mask = mpls_mask->mpls_lse;
372 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
373 mpls_lse_to_label(key), mpls_lse_to_label(mask),
374 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
375 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
376 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
381 format_odp_action(struct ds *ds, const struct nlattr *a)
384 enum ovs_action_attr type = nl_attr_type(a);
385 const struct ovs_action_push_vlan *vlan;
387 expected_len = odp_action_len(nl_attr_type(a));
388 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
389 ds_put_format(ds, "bad length %zu, expected %d for: ",
390 nl_attr_get_size(a), expected_len);
391 format_generic_odp_action(ds, a);
396 case OVS_ACTION_ATTR_OUTPUT:
397 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
399 case OVS_ACTION_ATTR_USERSPACE:
400 format_odp_userspace_action(ds, a);
402 case OVS_ACTION_ATTR_SET:
403 ds_put_cstr(ds, "set(");
404 format_odp_key_attr(nl_attr_get(a), NULL, ds, true);
405 ds_put_cstr(ds, ")");
407 case OVS_ACTION_ATTR_PUSH_VLAN:
408 vlan = nl_attr_get(a);
409 ds_put_cstr(ds, "push_vlan(");
410 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
411 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
413 format_vlan_tci(ds, vlan->vlan_tci);
414 ds_put_char(ds, ')');
416 case OVS_ACTION_ATTR_POP_VLAN:
417 ds_put_cstr(ds, "pop_vlan");
419 case OVS_ACTION_ATTR_PUSH_MPLS: {
420 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
421 ds_put_cstr(ds, "push_mpls(");
422 format_mpls_lse(ds, mpls->mpls_lse);
423 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
426 case OVS_ACTION_ATTR_POP_MPLS: {
427 ovs_be16 ethertype = nl_attr_get_be16(a);
428 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
431 case OVS_ACTION_ATTR_SAMPLE:
432 format_odp_sample_action(ds, a);
434 case OVS_ACTION_ATTR_UNSPEC:
435 case __OVS_ACTION_ATTR_MAX:
437 format_generic_odp_action(ds, a);
443 format_odp_actions(struct ds *ds, const struct nlattr *actions,
447 const struct nlattr *a;
450 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
452 ds_put_char(ds, ',');
454 format_odp_action(ds, a);
459 if (left == actions_len) {
460 ds_put_cstr(ds, "<empty>");
462 ds_put_format(ds, ",***%u leftover bytes*** (", left);
463 for (i = 0; i < left; i++) {
464 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
466 ds_put_char(ds, ')');
469 ds_put_cstr(ds, "drop");
474 parse_odp_action(const char *s, const struct simap *port_names,
475 struct ofpbuf *actions)
477 /* Many of the sscanf calls in this function use oversized destination
478 * fields because some sscanf() implementations truncate the range of %i
479 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
480 * value of 0x7fff. The other alternatives are to allow only a single
481 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
484 * The tun_id parser has to use an alternative approach because there is no
485 * type larger than 64 bits. */
488 unsigned long long int port;
491 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
492 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
498 int len = strcspn(s, delimiters);
499 struct simap_node *node;
501 node = simap_find_len(port_names, s, len);
503 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
509 unsigned long long int pid;
510 unsigned long long int output;
511 unsigned long long int probability;
512 unsigned long long int collector_set_id;
513 unsigned long long int obs_domain_id;
514 unsigned long long int obs_point_id;
518 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
519 odp_put_userspace_action(pid, NULL, 0, actions);
521 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
522 "pcp=%i,output=%lli))%n",
523 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
524 union user_action_cookie cookie;
527 tci = vid | (pcp << VLAN_PCP_SHIFT);
532 cookie.type = USER_ACTION_COOKIE_SFLOW;
533 cookie.sflow.vlan_tci = htons(tci);
534 cookie.sflow.output = output;
535 odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
538 } else if (sscanf(s, "userspace(pid=%lli,slow_path(%n", &pid, &n) > 0
540 union user_action_cookie cookie;
543 if (s[n] == ')' && s[n + 1] == ')') {
546 } else if (sscanf(s + n, "%31[^)]))", reason) > 0) {
547 n += strlen(reason) + 2;
552 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
553 cookie.slow_path.unused = 0;
554 cookie.slow_path.reason = string_to_slow_path_reason(reason);
556 if (reason[0] && !cookie.slow_path.reason) {
560 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
563 } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
564 "collector_set_id=%lli,obs_domain_id=%lli,"
565 "obs_point_id=%lli))%n",
566 &pid, &probability, &collector_set_id,
567 &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
568 union user_action_cookie cookie;
570 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
571 cookie.flow_sample.probability = probability;
572 cookie.flow_sample.collector_set_id = collector_set_id;
573 cookie.flow_sample.obs_domain_id = obs_domain_id;
574 cookie.flow_sample.obs_point_id = obs_point_id;
575 odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
578 } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
580 union user_action_cookie cookie;
582 cookie.type = USER_ACTION_COOKIE_IPFIX;
583 odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
586 } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
591 ofpbuf_init(&buf, 16);
592 end = ofpbuf_put_hex(&buf, &s[n], NULL);
593 if (end[0] == ')' && end[1] == ')') {
594 odp_put_userspace_action(pid, buf.data, buf.size, actions);
596 return (end + 2) - s;
601 if (!strncmp(s, "set(", 4)) {
605 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
606 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL);
610 if (s[retval + 4] != ')') {
613 nl_msg_end_nested(actions, start_ofs);
618 struct ovs_action_push_vlan push;
619 int tpid = ETH_TYPE_VLAN;
624 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
626 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
627 &vid, &pcp, &cfi, &n) > 0 && n > 0)
628 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
629 &tpid, &vid, &pcp, &n) > 0 && n > 0)
630 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
631 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
632 push.vlan_tpid = htons(tpid);
633 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
634 | (pcp << VLAN_PCP_SHIFT)
635 | (cfi ? VLAN_CFI : 0));
636 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
643 if (!strncmp(s, "pop_vlan", 8)) {
644 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
652 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
653 && percentage >= 0. && percentage <= 100.0
655 size_t sample_ofs, actions_ofs;
658 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
659 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
660 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
661 (probability <= 0 ? 0
662 : probability >= UINT32_MAX ? UINT32_MAX
665 actions_ofs = nl_msg_start_nested(actions,
666 OVS_SAMPLE_ATTR_ACTIONS);
670 n += strspn(s + n, delimiters);
675 retval = parse_odp_action(s + n, port_names, actions);
681 nl_msg_end_nested(actions, actions_ofs);
682 nl_msg_end_nested(actions, sample_ofs);
684 return s[n + 1] == ')' ? n + 2 : -EINVAL;
691 /* Parses the string representation of datapath actions, in the format output
692 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
693 * value. On success, the ODP actions are appended to 'actions' as a series of
694 * Netlink attributes. On failure, no data is appended to 'actions'. Either
695 * way, 'actions''s data might be reallocated. */
697 odp_actions_from_string(const char *s, const struct simap *port_names,
698 struct ofpbuf *actions)
702 if (!strcasecmp(s, "drop")) {
706 old_size = actions->size;
710 s += strspn(s, delimiters);
715 retval = parse_odp_action(s, port_names, actions);
716 if (retval < 0 || !strchr(delimiters, s[retval])) {
717 actions->size = old_size;
726 /* Returns the correct length of the payload for a flow key attribute of the
727 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
728 * is variable length. */
730 odp_flow_key_attr_len(uint16_t type)
732 if (type > OVS_KEY_ATTR_MAX) {
736 switch ((enum ovs_key_attr) type) {
737 case OVS_KEY_ATTR_ENCAP: return -2;
738 case OVS_KEY_ATTR_PRIORITY: return 4;
739 case OVS_KEY_ATTR_SKB_MARK: return 4;
740 case OVS_KEY_ATTR_TUNNEL: return -2;
741 case OVS_KEY_ATTR_IN_PORT: return 4;
742 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
743 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
744 case OVS_KEY_ATTR_ETHERTYPE: return 2;
745 case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls);
746 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
747 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
748 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
749 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
750 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
751 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
752 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
753 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
754 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
756 case OVS_KEY_ATTR_UNSPEC:
757 case __OVS_KEY_ATTR_MAX:
765 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
767 size_t len = nl_attr_get_size(a);
769 const uint8_t *unspec;
772 unspec = nl_attr_get(a);
773 for (i = 0; i < len; i++) {
775 ds_put_char(ds, ' ');
777 ds_put_format(ds, "%02x", unspec[i]);
783 ovs_frag_type_to_string(enum ovs_frag_type type)
786 case OVS_FRAG_TYPE_NONE:
788 case OVS_FRAG_TYPE_FIRST:
790 case OVS_FRAG_TYPE_LATER:
792 case __OVS_FRAG_TYPE_MAX:
799 tunnel_key_attr_len(int type)
802 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
803 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
804 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
805 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
806 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
807 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
808 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
809 case __OVS_TUNNEL_KEY_ATTR_MAX:
816 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
819 const struct nlattr *a;
821 bool unknown = false;
823 NL_NESTED_FOR_EACH(a, left, attr) {
824 uint16_t type = nl_attr_type(a);
825 size_t len = nl_attr_get_size(a);
826 int expected_len = tunnel_key_attr_len(type);
828 if (len != expected_len && expected_len >= 0) {
829 return ODP_FIT_ERROR;
833 case OVS_TUNNEL_KEY_ATTR_ID:
834 tun->tun_id = nl_attr_get_be64(a);
835 tun->flags |= FLOW_TNL_F_KEY;
837 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
838 tun->ip_src = nl_attr_get_be32(a);
840 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
841 tun->ip_dst = nl_attr_get_be32(a);
843 case OVS_TUNNEL_KEY_ATTR_TOS:
844 tun->ip_tos = nl_attr_get_u8(a);
846 case OVS_TUNNEL_KEY_ATTR_TTL:
847 tun->ip_ttl = nl_attr_get_u8(a);
850 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
851 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
853 case OVS_TUNNEL_KEY_ATTR_CSUM:
854 tun->flags |= FLOW_TNL_F_CSUM;
857 /* Allow this to show up as unexpected, if there are unknown
858 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
865 return ODP_FIT_ERROR;
868 return ODP_FIT_TOO_MUCH;
870 return ODP_FIT_PERFECT;
874 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
878 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
880 if (tun_key->flags & FLOW_TNL_F_KEY) {
881 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
883 if (tun_key->ip_src) {
884 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
886 if (tun_key->ip_dst) {
887 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
889 if (tun_key->ip_tos) {
890 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
892 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
893 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
894 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
896 if (tun_key->flags & FLOW_TNL_F_CSUM) {
897 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
900 nl_msg_end_nested(a, tun_key_ofs);
904 odp_mask_attr_is_wildcard(const struct nlattr *ma)
906 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
910 odp_mask_attr_is_exact(const struct nlattr *ma)
912 bool is_exact = false;
913 enum ovs_key_attr attr = nl_attr_type(ma);
915 if (attr == OVS_KEY_ATTR_TUNNEL) {
916 /* XXX this is a hack for now. Should change
917 * the exact match dection to per field
918 * instead of per attribute.
920 struct flow_tnl tun_mask;
921 memset(&tun_mask, 0, sizeof tun_mask);
922 odp_tun_key_from_attr(ma, &tun_mask);
923 if (tun_mask.flags == (FLOW_TNL_F_KEY
924 | FLOW_TNL_F_DONT_FRAGMENT
925 | FLOW_TNL_F_CSUM)) {
926 /* The flags are exact match, check the remaining fields. */
927 tun_mask.flags = 0xffff;
928 is_exact = is_all_ones((uint8_t *)&tun_mask,
929 offsetof(struct flow_tnl, ip_ttl));
932 is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
940 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
941 struct ds *ds, bool verbose)
943 struct flow_tnl tun_key;
944 enum ovs_key_attr attr = nl_attr_type(a);
945 char namebuf[OVS_KEY_ATTR_BUFSIZE];
949 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
951 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
954 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
955 if (expected_len != -2) {
956 bool bad_key_len = nl_attr_get_size(a) != expected_len;
957 bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
959 if (bad_key_len || bad_mask_len) {
961 ds_put_format(ds, "(bad key length %zu, expected %d)(",
963 odp_flow_key_attr_len(nl_attr_type(a)));
965 format_generic_odp_key(a, ds);
967 ds_put_char(ds, '/');
968 ds_put_format(ds, "(bad mask length %zu, expected %d)(",
969 nl_attr_get_size(ma),
970 odp_flow_key_attr_len(nl_attr_type(ma)));
972 format_generic_odp_key(ma, ds);
973 ds_put_char(ds, ')');
979 ds_put_char(ds, '(');
981 case OVS_KEY_ATTR_ENCAP:
982 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
983 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
984 nl_attr_get(ma), nl_attr_get_size(ma), ds, verbose);
985 } else if (nl_attr_get_size(a)) {
986 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, ds,
991 case OVS_KEY_ATTR_PRIORITY:
992 case OVS_KEY_ATTR_SKB_MARK:
993 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
995 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
999 case OVS_KEY_ATTR_TUNNEL:
1000 memset(&tun_key, 0, sizeof tun_key);
1001 if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
1002 ds_put_format(ds, "error");
1003 } else if (!is_exact) {
1004 struct flow_tnl tun_mask;
1006 memset(&tun_mask, 0, sizeof tun_mask);
1007 odp_tun_key_from_attr(ma, &tun_mask);
1008 ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
1009 ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1010 ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
1012 ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
1013 IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1014 IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1015 tun_key.ip_tos, tun_mask.ip_tos,
1016 tun_key.ip_ttl, tun_mask.ip_ttl);
1018 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1020 /* XXX This code is correct, but enabling it would break the unit
1021 test. Disable it for now until the input parser is fixed.
1023 ds_put_char(ds, '/');
1024 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1026 ds_put_char(ds, ')');
1028 ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1029 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1030 ntohll(tun_key.tun_id),
1031 IP_ARGS(tun_key.ip_src),
1032 IP_ARGS(tun_key.ip_dst),
1033 tun_key.ip_tos, tun_key.ip_ttl);
1035 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1036 ds_put_char(ds, ')');
1040 case OVS_KEY_ATTR_IN_PORT:
1041 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1043 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1047 case OVS_KEY_ATTR_ETHERNET:
1049 const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1050 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1052 ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1053 ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1054 ETH_ADDR_ARGS(eth_key->eth_src),
1055 ETH_ADDR_ARGS(eth_mask->eth_src),
1056 ETH_ADDR_ARGS(eth_key->eth_dst),
1057 ETH_ADDR_ARGS(eth_mask->eth_dst));
1059 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1061 ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1062 ETH_ADDR_ARGS(eth_key->eth_src),
1063 ETH_ADDR_ARGS(eth_key->eth_dst));
1067 case OVS_KEY_ATTR_VLAN:
1069 ovs_be16 vlan_tci = nl_attr_get_be16(a);
1071 ovs_be16 mask = nl_attr_get_be16(ma);
1072 ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1073 vlan_tci_to_vid(vlan_tci),
1074 vlan_tci_to_vid(mask),
1075 vlan_tci_to_pcp(vlan_tci),
1076 vlan_tci_to_pcp(mask),
1077 vlan_tci_to_cfi(vlan_tci),
1078 vlan_tci_to_cfi(mask));
1080 format_vlan_tci(ds, vlan_tci);
1085 case OVS_KEY_ATTR_MPLS: {
1086 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1087 const struct ovs_key_mpls *mpls_mask = NULL;
1089 mpls_mask = nl_attr_get(ma);
1091 format_mpls(ds, mpls_key, mpls_mask);
1095 case OVS_KEY_ATTR_ETHERTYPE:
1096 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1098 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1102 case OVS_KEY_ATTR_IPV4:
1104 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1105 const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1107 ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1108 ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1109 ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1110 IP_ARGS(ipv4_key->ipv4_src),
1111 IP_ARGS(ipv4_mask->ipv4_src),
1112 IP_ARGS(ipv4_key->ipv4_dst),
1113 IP_ARGS(ipv4_mask->ipv4_dst),
1114 ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1115 ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1116 ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1117 ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1118 ipv4_mask->ipv4_frag);
1120 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1122 ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1123 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1124 IP_ARGS(ipv4_key->ipv4_src),
1125 IP_ARGS(ipv4_key->ipv4_dst),
1126 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1128 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1132 case OVS_KEY_ATTR_IPV6:
1134 const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1135 char src_str[INET6_ADDRSTRLEN];
1136 char dst_str[INET6_ADDRSTRLEN];
1137 char src_mask[INET6_ADDRSTRLEN];
1138 char dst_mask[INET6_ADDRSTRLEN];
1140 ipv6_key = nl_attr_get(a);
1141 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1142 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1144 ipv6_mask = nl_attr_get(ma);
1145 inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1146 inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1148 ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1149 ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1150 ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1151 src_str, src_mask, dst_str, dst_mask,
1152 ntohl(ipv6_key->ipv6_label),
1153 ntohl(ipv6_mask->ipv6_label),
1154 ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1155 ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1156 ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1157 ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1158 ipv6_mask->ipv6_frag);
1160 const struct ovs_key_ipv6 *ipv6_key;
1161 char src_str[INET6_ADDRSTRLEN];
1162 char dst_str[INET6_ADDRSTRLEN];
1164 ipv6_key = nl_attr_get(a);
1165 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1166 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1168 ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1169 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1170 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1171 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1172 ipv6_key->ipv6_hlimit,
1173 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1177 case OVS_KEY_ATTR_TCP:
1179 const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1180 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1182 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1183 ",dst=%"PRIu16"/%#"PRIx16,
1184 ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1185 ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1187 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1189 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1190 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1194 case OVS_KEY_ATTR_UDP:
1196 const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1197 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1199 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1200 ",dst=%"PRIu16"/%#"PRIx16,
1201 ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1202 ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1204 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1206 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1207 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1211 case OVS_KEY_ATTR_SCTP:
1213 const struct ovs_key_sctp *sctp_mask = nl_attr_get(ma);
1214 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1216 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1217 ",dst=%"PRIu16"/%#"PRIx16,
1218 ntohs(sctp_key->sctp_src), ntohs(sctp_mask->sctp_src),
1219 ntohs(sctp_key->sctp_dst), ntohs(sctp_mask->sctp_dst));
1221 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1223 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
1224 ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst));
1228 case OVS_KEY_ATTR_ICMP:
1230 const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1231 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1233 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1234 icmp_key->icmp_type, icmp_mask->icmp_type,
1235 icmp_key->icmp_code, icmp_mask->icmp_code);
1237 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1239 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1240 icmp_key->icmp_type, icmp_key->icmp_code);
1244 case OVS_KEY_ATTR_ICMPV6:
1246 const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1247 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1249 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1250 icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1251 icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1253 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1255 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1256 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1260 case OVS_KEY_ATTR_ARP:
1262 const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1263 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1265 ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1266 ",op=%"PRIu16"/%#"PRIx16
1267 ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1268 ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1269 IP_ARGS(arp_key->arp_sip),
1270 IP_ARGS(arp_mask->arp_sip),
1271 IP_ARGS(arp_key->arp_tip),
1272 IP_ARGS(arp_mask->arp_tip),
1273 ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1274 ETH_ADDR_ARGS(arp_key->arp_sha),
1275 ETH_ADDR_ARGS(arp_mask->arp_sha),
1276 ETH_ADDR_ARGS(arp_key->arp_tha),
1277 ETH_ADDR_ARGS(arp_mask->arp_tha));
1279 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1281 ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1282 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1283 IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1284 ntohs(arp_key->arp_op),
1285 ETH_ADDR_ARGS(arp_key->arp_sha),
1286 ETH_ADDR_ARGS(arp_key->arp_tha));
1290 case OVS_KEY_ATTR_ND: {
1291 const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1292 char target[INET6_ADDRSTRLEN];
1294 nd_key = nl_attr_get(a);
1296 nd_mask = nl_attr_get(ma);
1299 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1300 ds_put_format(ds, "target=%s", target);
1302 inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1303 ds_put_format(ds, "/%s", target);
1306 if (!eth_addr_is_zero(nd_key->nd_sll)) {
1307 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1308 ETH_ADDR_ARGS(nd_key->nd_sll));
1310 ds_put_format(ds, "/"ETH_ADDR_FMT,
1311 ETH_ADDR_ARGS(nd_mask->nd_sll));
1314 if (!eth_addr_is_zero(nd_key->nd_tll)) {
1315 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1316 ETH_ADDR_ARGS(nd_key->nd_tll));
1318 ds_put_format(ds, "/"ETH_ADDR_FMT,
1319 ETH_ADDR_ARGS(nd_mask->nd_tll));
1325 case OVS_KEY_ATTR_UNSPEC:
1326 case __OVS_KEY_ATTR_MAX:
1328 format_generic_odp_key(a, ds);
1330 ds_put_char(ds, '/');
1331 format_generic_odp_key(ma, ds);
1335 ds_put_char(ds, ')');
1338 static struct nlattr *
1339 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1341 const struct nlattr *a;
1343 int type = nl_attr_type(key);
1344 int size = nl_attr_get_size(key);
1346 if (odp_flow_key_attr_len(type) >=0) {
1347 memset(nl_msg_put_unspec_uninit(ofp, type, size), 0, size);
1351 nested_mask = nl_msg_start_nested(ofp, type);
1352 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1353 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1355 nl_msg_end_nested(ofp, nested_mask);
1361 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1362 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1363 * 'mask_len' bytes of 'mask' which apply to 'key'. */
1365 odp_flow_format(const struct nlattr *key, size_t key_len,
1366 const struct nlattr *mask, size_t mask_len,
1367 struct ds *ds, bool verbose)
1370 const struct nlattr *a;
1372 bool has_ethtype_key = false;
1373 const struct nlattr *ma = NULL;
1375 bool first_field = true;
1377 ofpbuf_init(&ofp, 100);
1378 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1379 bool is_nested_attr;
1380 bool is_wildcard = false;
1381 int attr_type = nl_attr_type(a);
1383 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1384 has_ethtype_key = true;
1387 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1389 if (mask && mask_len) {
1390 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1391 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1394 if (verbose || !is_wildcard || is_nested_attr) {
1395 if (is_wildcard && !ma) {
1396 ma = generate_all_wildcard_mask(&ofp, a);
1399 ds_put_char(ds, ',');
1401 format_odp_key_attr(a, ma, ds, verbose);
1402 first_field = false;
1406 ofpbuf_uninit(&ofp);
1411 if (left == key_len) {
1412 ds_put_cstr(ds, "<empty>");
1414 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1415 for (i = 0; i < left; i++) {
1416 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1418 ds_put_char(ds, ')');
1420 if (!has_ethtype_key) {
1421 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1423 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1424 ntohs(nl_attr_get_be16(ma)));
1428 ds_put_cstr(ds, "<empty>");
1432 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1433 * OVS_KEY_ATTR_* attributes in 'key'. */
1435 odp_flow_key_format(const struct nlattr *key,
1436 size_t key_len, struct ds *ds)
1438 odp_flow_format(key, key_len, NULL, 0, ds, true);
1442 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1443 const uint8_t *nd_tll, struct ofpbuf *key)
1446 memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1450 memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1453 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1457 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1458 const uint8_t *nd_tll, struct ofpbuf *key)
1460 struct ovs_key_nd nd_key;
1462 memset(&nd_key, 0, sizeof nd_key);
1464 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1468 put_nd(&nd_key, nd_sll, nd_tll, key);
1473 put_nd_mask(int n, const char *nd_target_s,
1474 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1476 struct ovs_key_nd nd_mask;
1478 memset(&nd_mask, 0xff, sizeof nd_mask);
1480 if (strlen(nd_target_s) != 0 &&
1481 inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1485 put_nd(&nd_mask, nd_sll, nd_tll, mask);
1490 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1492 if (!strcasecmp(s, "no")) {
1493 *type = OVS_FRAG_TYPE_NONE;
1494 } else if (!strcasecmp(s, "first")) {
1495 *type = OVS_FRAG_TYPE_FIRST;
1496 } else if (!strcasecmp(s, "later")) {
1497 *type = OVS_FRAG_TYPE_LATER;
1505 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1507 return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1508 (mpls_tc << MPLS_TC_SHIFT) |
1509 (mpls_ttl << MPLS_TTL_SHIFT) |
1510 (mpls_bos << MPLS_BOS_SHIFT)));
1514 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1515 struct ofpbuf *key, struct ofpbuf *mask)
1517 /* Many of the sscanf calls in this function use oversized destination
1518 * fields because some sscanf() implementations truncate the range of %i
1519 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1520 * value of 0x7fff. The other alternatives are to allow only a single
1521 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1524 * The tun_id parser has to use an alternative approach because there is no
1525 * type larger than 64 bits. */
1528 unsigned long long int priority;
1529 unsigned long long int priority_mask;
1532 if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority,
1533 &priority_mask, &n) > 0 && n > 0) {
1534 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1535 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1537 } else if (sscanf(s, "skb_priority(%lli)%n",
1538 &priority, &n) > 0 && n > 0) {
1539 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1541 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1548 unsigned long long int mark;
1549 unsigned long long int mark_mask;
1552 if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark,
1553 &mark_mask, &n) > 0 && n > 0) {
1554 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1555 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1557 } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
1558 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1560 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1568 int tos, tos_mask, ttl, ttl_mask;
1569 struct flow_tnl tun_key, tun_key_mask;
1570 unsigned long long tun_id_mask;
1573 if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1574 "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1575 "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n",
1576 tun_id_s, &tun_id_mask,
1577 IP_SCAN_ARGS(&tun_key.ip_src),
1578 IP_SCAN_ARGS(&tun_key_mask.ip_src),
1579 IP_SCAN_ARGS(&tun_key.ip_dst),
1580 IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1581 &tos, &tos_mask, &ttl, &ttl_mask,
1586 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1587 tun_key_mask.tun_id = htonll(tun_id_mask);
1588 tun_key.ip_tos = tos;
1589 tun_key_mask.ip_tos = tos_mask;
1590 tun_key.ip_ttl = ttl;
1591 tun_key_mask.ip_ttl = ttl_mask;
1592 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1593 tun_key.flags = flags;
1594 tun_key_mask.flags = UINT16_MAX;
1604 tun_key_to_attr(key, &tun_key);
1606 tun_key_to_attr(mask, &tun_key_mask);
1609 } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1610 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1611 ",tos=%i,ttl=%i,flags%n", tun_id_s,
1612 IP_SCAN_ARGS(&tun_key.ip_src),
1613 IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1618 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1619 tun_key.ip_tos = tos;
1620 tun_key.ip_ttl = ttl;
1621 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1622 tun_key.flags = flags;
1632 tun_key_to_attr(key, &tun_key);
1635 memset(&tun_key, 0xff, sizeof tun_key);
1636 tun_key_to_attr(mask, &tun_key);
1643 unsigned long long int in_port;
1644 unsigned long long int in_port_mask;
1647 if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port,
1648 &in_port_mask, &n) > 0 && n > 0) {
1649 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1650 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1652 } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1653 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1655 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1662 if (port_names && !strncmp(s, "in_port(", 8)) {
1664 const struct simap_node *node;
1668 name_len = strcspn(s, ")");
1669 node = simap_find_len(port_names, name, name_len);
1671 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1674 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1676 return 8 + name_len + 1;
1681 struct ovs_key_ethernet eth_key;
1682 struct ovs_key_ethernet eth_key_mask;
1685 if (mask && sscanf(s,
1686 "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1687 "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1688 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1689 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1690 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1691 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) {
1693 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1694 ð_key, sizeof eth_key);
1695 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1696 ð_key_mask, sizeof eth_key_mask);
1698 } else if (sscanf(s,
1699 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1700 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1701 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1702 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1703 ð_key, sizeof eth_key);
1706 memset(ð_key, 0xff, sizeof eth_key);
1707 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1708 ð_key, sizeof eth_key);
1715 uint16_t vid, vid_mask;
1720 if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n",
1721 &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) {
1722 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1723 htons((vid << VLAN_VID_SHIFT) |
1724 (pcp << VLAN_PCP_SHIFT) |
1726 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1727 htons((vid_mask << VLAN_VID_SHIFT) |
1728 (pcp_mask << VLAN_PCP_SHIFT) |
1729 (1 << VLAN_CFI_SHIFT)));
1731 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
1732 &vid, &pcp, &n) > 0 && n > 0)) {
1733 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1734 htons((vid << VLAN_VID_SHIFT) |
1735 (pcp << VLAN_PCP_SHIFT) |
1738 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1741 } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n",
1742 &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) {
1743 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1744 htons((vid << VLAN_VID_SHIFT) |
1745 (pcp << VLAN_PCP_SHIFT) |
1746 (cfi ? VLAN_CFI : 0)));
1747 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1748 htons((vid_mask << VLAN_VID_SHIFT) |
1749 (pcp_mask << VLAN_PCP_SHIFT) |
1750 (cfi_mask << VLAN_CFI_SHIFT)));
1752 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1753 &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
1754 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1755 htons((vid << VLAN_VID_SHIFT) |
1756 (pcp << VLAN_PCP_SHIFT) |
1757 (cfi ? VLAN_CFI : 0)));
1759 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1770 if (mask && sscanf(s, "eth_type(%i/%i)%n",
1771 ð_type, ð_type_mask, &n) > 0 && n > 0) {
1772 if (eth_type != 0) {
1773 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1775 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1777 } else if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
1778 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1780 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
1787 int label, tc, ttl, bos;
1788 int label_mask, tc_mask, ttl_mask, bos_mask;
1791 if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1792 &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1793 struct ovs_key_mpls *mpls, *mpls_mask;
1795 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1797 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1799 mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1801 mpls_mask->mpls_lse = mpls_lse_from_components(
1802 label_mask, tc_mask, ttl_mask, bos_mask);
1804 } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1805 &label, &tc, &ttl, &bos, &n) > 0 &&
1807 struct ovs_key_mpls *mpls;
1809 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1811 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1813 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1815 mpls->mpls_lse = OVS_BE32_MAX;
1823 ovs_be32 ipv4_src, ipv4_src_mask;
1824 ovs_be32 ipv4_dst, ipv4_dst_mask;
1825 int ipv4_proto, ipv4_proto_mask;
1826 int ipv4_tos, ipv4_tos_mask;
1827 int ipv4_ttl, ipv4_ttl_mask;
1830 enum ovs_frag_type ipv4_frag;
1833 if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1834 "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1835 "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1836 "frag=%7[a-z]/%i)%n",
1837 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1838 IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1839 &ipv4_proto, &ipv4_proto_mask,
1840 &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1841 frag, &ipv4_frag_mask, &n) > 0
1843 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1844 struct ovs_key_ipv4 ipv4_key;
1845 struct ovs_key_ipv4 ipv4_mask;
1847 ipv4_key.ipv4_src = ipv4_src;
1848 ipv4_key.ipv4_dst = ipv4_dst;
1849 ipv4_key.ipv4_proto = ipv4_proto;
1850 ipv4_key.ipv4_tos = ipv4_tos;
1851 ipv4_key.ipv4_ttl = ipv4_ttl;
1852 ipv4_key.ipv4_frag = ipv4_frag;
1853 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1854 &ipv4_key, sizeof ipv4_key);
1856 ipv4_mask.ipv4_src = ipv4_src_mask;
1857 ipv4_mask.ipv4_dst = ipv4_dst_mask;
1858 ipv4_mask.ipv4_proto = ipv4_proto_mask;
1859 ipv4_mask.ipv4_tos = ipv4_tos_mask;
1860 ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1861 ipv4_mask.ipv4_frag = ipv4_frag_mask;
1862 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1863 &ipv4_mask, sizeof ipv4_mask);
1865 } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1866 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1867 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1868 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1870 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1871 struct ovs_key_ipv4 ipv4_key;
1873 ipv4_key.ipv4_src = ipv4_src;
1874 ipv4_key.ipv4_dst = ipv4_dst;
1875 ipv4_key.ipv4_proto = ipv4_proto;
1876 ipv4_key.ipv4_tos = ipv4_tos;
1877 ipv4_key.ipv4_ttl = ipv4_ttl;
1878 ipv4_key.ipv4_frag = ipv4_frag;
1879 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1880 &ipv4_key, sizeof ipv4_key);
1883 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1884 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1885 &ipv4_key, sizeof ipv4_key);
1892 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1893 char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1894 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1895 char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1896 int ipv6_label, ipv6_label_mask;
1897 int ipv6_proto, ipv6_proto_mask;
1898 int ipv6_tclass, ipv6_tclass_mask;
1899 int ipv6_hlimit, ipv6_hlimit_mask;
1901 enum ovs_frag_type ipv6_frag;
1905 if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1906 IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1907 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1908 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1909 ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1910 &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1911 &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1912 &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1913 &ipv6_frag_mask, &n) > 0
1915 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1916 struct ovs_key_ipv6 ipv6_key;
1917 struct ovs_key_ipv6 ipv6_mask;
1919 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1920 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1921 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1922 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1926 ipv6_key.ipv6_label = htonl(ipv6_label);
1927 ipv6_key.ipv6_proto = ipv6_proto;
1928 ipv6_key.ipv6_tclass = ipv6_tclass;
1929 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1930 ipv6_key.ipv6_frag = ipv6_frag;
1931 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1932 &ipv6_key, sizeof ipv6_key);
1934 ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1935 ipv6_mask.ipv6_proto = ipv6_proto_mask;
1936 ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1937 ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1938 ipv6_mask.ipv6_frag = ipv6_frag_mask;
1939 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1940 &ipv6_mask, sizeof ipv6_mask);
1942 } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1943 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1944 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1945 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1947 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1948 struct ovs_key_ipv6 ipv6_key;
1950 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1951 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1954 ipv6_key.ipv6_label = htonl(ipv6_label);
1955 ipv6_key.ipv6_proto = ipv6_proto;
1956 ipv6_key.ipv6_tclass = ipv6_tclass;
1957 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1958 ipv6_key.ipv6_frag = ipv6_frag;
1959 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1960 &ipv6_key, sizeof ipv6_key);
1963 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1964 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1965 &ipv6_key, sizeof ipv6_key);
1978 if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
1979 &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
1981 struct ovs_key_tcp tcp_key;
1982 struct ovs_key_tcp tcp_mask;
1984 tcp_key.tcp_src = htons(tcp_src);
1985 tcp_key.tcp_dst = htons(tcp_dst);
1986 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1988 tcp_mask.tcp_src = htons(tcp_src_mask);
1989 tcp_mask.tcp_dst = htons(tcp_dst_mask);
1990 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1991 &tcp_mask, sizeof tcp_mask);
1993 } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1995 struct ovs_key_tcp tcp_key;
1997 tcp_key.tcp_src = htons(tcp_src);
1998 tcp_key.tcp_dst = htons(tcp_dst);
1999 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2002 memset(&tcp_key, 0xff, sizeof tcp_key);
2003 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2004 &tcp_key, sizeof tcp_key);
2017 if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
2018 &udp_src, &udp_src_mask,
2019 &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
2020 struct ovs_key_udp udp_key;
2021 struct ovs_key_udp udp_mask;
2023 udp_key.udp_src = htons(udp_src);
2024 udp_key.udp_dst = htons(udp_dst);
2025 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2027 udp_mask.udp_src = htons(udp_src_mask);
2028 udp_mask.udp_dst = htons(udp_dst_mask);
2029 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2030 &udp_mask, sizeof udp_mask);
2033 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
2035 struct ovs_key_udp udp_key;
2037 udp_key.udp_src = htons(udp_src);
2038 udp_key.udp_dst = htons(udp_dst);
2039 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2042 memset(&udp_key, 0xff, sizeof udp_key);
2043 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2056 if (mask && sscanf(s, "sctp(src=%i/%i,dst=%i/%i)%n",
2057 &sctp_src, &sctp_src_mask,
2058 &sctp_dst, &sctp_dst_mask, &n) > 0 && n > 0) {
2059 struct ovs_key_sctp sctp_key;
2060 struct ovs_key_sctp sctp_mask;
2062 sctp_key.sctp_src = htons(sctp_src);
2063 sctp_key.sctp_dst = htons(sctp_dst);
2064 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2066 sctp_mask.sctp_src = htons(sctp_src_mask);
2067 sctp_mask.sctp_dst = htons(sctp_dst_mask);
2068 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP,
2069 &sctp_mask, sizeof sctp_mask);
2072 if (sscanf(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n) > 0
2074 struct ovs_key_sctp sctp_key;
2076 sctp_key.sctp_src = htons(sctp_src);
2077 sctp_key.sctp_dst = htons(sctp_dst);
2078 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2081 memset(&sctp_key, 0xff, sizeof sctp_key);
2082 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2095 if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
2096 &icmp_type, &icmp_type_mask,
2097 &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
2098 struct ovs_key_icmp icmp_key;
2099 struct ovs_key_icmp icmp_mask;
2101 icmp_key.icmp_type = icmp_type;
2102 icmp_key.icmp_code = icmp_code;
2103 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2104 &icmp_key, sizeof icmp_key);
2106 icmp_mask.icmp_type = icmp_type_mask;
2107 icmp_mask.icmp_code = icmp_code_mask;
2108 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2109 &icmp_mask, sizeof icmp_mask);
2111 } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
2112 &icmp_type, &icmp_code, &n) > 0
2114 struct ovs_key_icmp icmp_key;
2116 icmp_key.icmp_type = icmp_type;
2117 icmp_key.icmp_code = icmp_code;
2118 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2119 &icmp_key, sizeof icmp_key);
2121 memset(&icmp_key, 0xff, sizeof icmp_key);
2122 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2130 struct ovs_key_icmpv6 icmpv6_key;
2131 struct ovs_key_icmpv6 icmpv6_mask;
2132 int icmpv6_type_mask;
2133 int icmpv6_code_mask;
2136 if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
2137 &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
2138 &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
2140 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2141 &icmpv6_key, sizeof icmpv6_key);
2143 icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2144 icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2145 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2146 sizeof icmpv6_mask);
2148 } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2149 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2151 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2152 &icmpv6_key, sizeof icmpv6_key);
2155 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2156 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2164 ovs_be32 arp_sip, arp_sip_mask;
2165 ovs_be32 arp_tip, arp_tip_mask;
2166 int arp_op, arp_op_mask;
2167 uint8_t arp_sha[ETH_ADDR_LEN];
2168 uint8_t arp_sha_mask[ETH_ADDR_LEN];
2169 uint8_t arp_tha[ETH_ADDR_LEN];
2170 uint8_t arp_tha_mask[ETH_ADDR_LEN];
2173 if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2174 "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2175 "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2176 "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2177 IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2178 IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2179 &arp_op, &arp_op_mask,
2180 ETH_ADDR_SCAN_ARGS(arp_sha),
2181 ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2182 ETH_ADDR_SCAN_ARGS(arp_tha),
2183 ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2184 struct ovs_key_arp arp_key;
2185 struct ovs_key_arp arp_mask;
2187 memset(&arp_key, 0, sizeof arp_key);
2188 arp_key.arp_sip = arp_sip;
2189 arp_key.arp_tip = arp_tip;
2190 arp_key.arp_op = htons(arp_op);
2191 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2192 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2193 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2195 arp_mask.arp_sip = arp_sip_mask;
2196 arp_mask.arp_tip = arp_tip_mask;
2197 arp_mask.arp_op = htons(arp_op_mask);
2198 memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2199 memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2200 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2201 &arp_mask, sizeof arp_mask);
2203 } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2204 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2205 IP_SCAN_ARGS(&arp_sip),
2206 IP_SCAN_ARGS(&arp_tip),
2208 ETH_ADDR_SCAN_ARGS(arp_sha),
2209 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2210 struct ovs_key_arp arp_key;
2212 memset(&arp_key, 0, sizeof arp_key);
2213 arp_key.arp_sip = arp_sip;
2214 arp_key.arp_tip = arp_tip;
2215 arp_key.arp_op = htons(arp_op);
2216 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2217 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2218 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2221 memset(&arp_key, 0xff, sizeof arp_key);
2222 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2223 &arp_key, sizeof arp_key);
2230 char nd_target_s[IPV6_SCAN_LEN + 1];
2231 char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2232 uint8_t nd_sll[ETH_ADDR_LEN];
2233 uint8_t nd_sll_mask[ETH_ADDR_LEN];
2234 uint8_t nd_tll[ETH_ADDR_LEN];
2235 uint8_t nd_tll_mask[ETH_ADDR_LEN];
2238 nd_target_mask_s[0] = 0;
2239 memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2240 memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2242 if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2243 nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2244 put_nd_key(n, nd_target_s, NULL, NULL, key);
2245 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2246 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2247 nd_target_s, &n) > 0 && n > 0) {
2248 put_nd_key(n, nd_target_s, NULL, NULL, key);
2250 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2252 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2253 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2254 nd_target_s, nd_target_mask_s,
2255 ETH_ADDR_SCAN_ARGS(nd_sll),
2256 ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2257 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2258 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2259 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2260 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2262 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2264 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2266 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2267 ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2268 nd_target_s, nd_target_mask_s,
2269 ETH_ADDR_SCAN_ARGS(nd_tll),
2270 ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2271 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2272 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2273 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2274 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2276 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2278 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2280 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2281 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2282 "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2283 nd_target_s, nd_target_mask_s,
2284 ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2285 ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2288 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2289 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2290 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2291 "tll="ETH_ADDR_SCAN_FMT")%n",
2292 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2293 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2295 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2297 put_nd_mask(n, nd_target_mask_s,
2298 nd_sll_mask, nd_tll_mask, mask);
2307 if (!strncmp(s, "encap(", 6)) {
2308 const char *start = s;
2309 size_t encap, encap_mask = 0;
2311 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2313 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2320 s += strspn(s, ", \t\r\n");
2323 } else if (*s == ')') {
2327 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2335 nl_msg_end_nested(key, encap);
2337 nl_msg_end_nested(mask, encap_mask);
2346 /* Parses the string representation of a datapath flow key, in the
2347 * format output by odp_flow_key_format(). Returns 0 if successful,
2348 * otherwise a positive errno value. On success, the flow key is
2349 * appended to 'key' as a series of Netlink attributes. On failure, no
2350 * data is appended to 'key'. Either way, 'key''s data might be
2353 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2354 * to a port number. (Port names may be used instead of port numbers in
2357 * On success, the attributes appended to 'key' are individually syntactically
2358 * valid, but they may not be valid as a sequence. 'key' might, for example,
2359 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2361 odp_flow_from_string(const char *s, const struct simap *port_names,
2362 struct ofpbuf *key, struct ofpbuf *mask)
2364 const size_t old_size = key->size;
2368 s += strspn(s, delimiters);
2373 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2375 key->size = old_size;
2385 ovs_to_odp_frag(uint8_t nw_frag)
2387 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2388 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2389 : OVS_FRAG_TYPE_LATER);
2393 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2395 uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2397 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2398 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2404 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2405 const struct flow *flow, odp_port_t odp_in_port)
2408 struct ovs_key_ethernet *eth_key;
2411 /* We assume that if 'data' and 'flow' are not the same, we should
2412 * treat 'data' as a mask. */
2413 is_mask = (data != flow);
2415 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2417 if (flow->tunnel.ip_dst || is_mask) {
2418 tun_key_to_attr(buf, &data->tunnel);
2421 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2423 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2424 * is not the magical value "ODPP_NONE". */
2425 if (is_mask || odp_in_port != ODPP_NONE) {
2426 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2429 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2431 memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2432 memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2434 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2436 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2438 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2440 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2441 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2442 if (flow->vlan_tci == htons(0)) {
2449 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2450 /* For backwards compatibility with kernels that don't support
2451 * wildcarding, the following convention is used to encode the
2452 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2455 * -------- -------- -------
2456 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2457 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2458 * <none> 0xffff Any non-Ethernet II frame (except valid
2459 * 802.3 SNAP packet with valid eth_type).
2462 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2467 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2469 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2470 struct ovs_key_ipv4 *ipv4_key;
2472 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2474 ipv4_key->ipv4_src = data->nw_src;
2475 ipv4_key->ipv4_dst = data->nw_dst;
2476 ipv4_key->ipv4_proto = data->nw_proto;
2477 ipv4_key->ipv4_tos = data->nw_tos;
2478 ipv4_key->ipv4_ttl = data->nw_ttl;
2479 ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2480 : ovs_to_odp_frag(data->nw_frag);
2481 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2482 struct ovs_key_ipv6 *ipv6_key;
2484 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2486 memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2487 memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2488 ipv6_key->ipv6_label = data->ipv6_label;
2489 ipv6_key->ipv6_proto = data->nw_proto;
2490 ipv6_key->ipv6_tclass = data->nw_tos;
2491 ipv6_key->ipv6_hlimit = data->nw_ttl;
2492 ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2493 : ovs_to_odp_frag(data->nw_frag);
2494 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2495 flow->dl_type == htons(ETH_TYPE_RARP)) {
2496 struct ovs_key_arp *arp_key;
2498 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2500 memset(arp_key, 0, sizeof *arp_key);
2501 arp_key->arp_sip = data->nw_src;
2502 arp_key->arp_tip = data->nw_dst;
2503 arp_key->arp_op = htons(data->nw_proto);
2504 memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2505 memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2508 if (flow->mpls_depth) {
2509 struct ovs_key_mpls *mpls_key;
2511 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2513 mpls_key->mpls_lse = data->mpls_lse;
2516 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2517 if (flow->nw_proto == IPPROTO_TCP) {
2518 struct ovs_key_tcp *tcp_key;
2520 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2522 tcp_key->tcp_src = data->tp_src;
2523 tcp_key->tcp_dst = data->tp_dst;
2524 } else if (flow->nw_proto == IPPROTO_UDP) {
2525 struct ovs_key_udp *udp_key;
2527 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2529 udp_key->udp_src = data->tp_src;
2530 udp_key->udp_dst = data->tp_dst;
2531 } else if (flow->nw_proto == IPPROTO_SCTP) {
2532 struct ovs_key_sctp *sctp_key;
2534 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2536 sctp_key->sctp_src = data->tp_src;
2537 sctp_key->sctp_dst = data->tp_dst;
2538 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2539 && flow->nw_proto == IPPROTO_ICMP) {
2540 struct ovs_key_icmp *icmp_key;
2542 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2544 icmp_key->icmp_type = ntohs(data->tp_src);
2545 icmp_key->icmp_code = ntohs(data->tp_dst);
2546 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2547 && flow->nw_proto == IPPROTO_ICMPV6) {
2548 struct ovs_key_icmpv6 *icmpv6_key;
2550 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2551 sizeof *icmpv6_key);
2552 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2553 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2555 if (flow->tp_dst == htons(0) &&
2556 (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2557 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) &&
2558 (!is_mask || (data->tp_src == htons(0xffff) &&
2559 data->tp_dst == htons(0xffff)))) {
2561 struct ovs_key_nd *nd_key;
2563 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2565 memcpy(nd_key->nd_target, &data->nd_target,
2566 sizeof nd_key->nd_target);
2567 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2568 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2575 nl_msg_end_nested(buf, encap);
2579 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2580 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2581 * number rather than a datapath port number). Instead, if 'odp_in_port'
2582 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2585 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2586 * capable of being expanded to allow for that much space. */
2588 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2589 odp_port_t odp_in_port)
2591 odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2594 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2595 * 'buf'. 'flow' is used as a template to determine how to interpret
2596 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2597 * it doesn't indicate whether the other fields should be interpreted as
2598 * ARP, IPv4, IPv6, etc.
2600 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2601 * capable of being expanded to allow for that much space. */
2603 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2604 const struct flow *flow, uint32_t odp_in_port_mask)
2606 odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask));
2610 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2612 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2613 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2614 key_len / sizeof(uint32_t), 0);
2618 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2619 uint64_t attrs, int out_of_range_attr,
2620 const struct nlattr *key, size_t key_len)
2625 if (VLOG_DROP_DBG(rl)) {
2630 for (i = 0; i < 64; i++) {
2631 if (attrs & (UINT64_C(1) << i)) {
2632 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2634 ds_put_format(&s, " %s",
2635 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2638 if (out_of_range_attr) {
2639 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2642 ds_put_cstr(&s, ": ");
2643 odp_flow_key_format(key, key_len, &s);
2645 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2650 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2652 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2654 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2655 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2659 if (odp_frag != OVS_FRAG_TYPE_NONE) {
2660 flow->nw_frag |= FLOW_NW_FRAG_ANY;
2661 if (odp_frag == OVS_FRAG_TYPE_LATER) {
2662 flow->nw_frag |= FLOW_NW_FRAG_LATER;
2669 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2670 const struct nlattr *attrs[], uint64_t *present_attrsp,
2671 int *out_of_range_attrp)
2673 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2674 const struct nlattr *nla;
2675 uint64_t present_attrs;
2678 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2680 *out_of_range_attrp = 0;
2681 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2682 uint16_t type = nl_attr_type(nla);
2683 size_t len = nl_attr_get_size(nla);
2684 int expected_len = odp_flow_key_attr_len(type);
2686 if (len != expected_len && expected_len >= 0) {
2687 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2689 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2690 "length %d", ovs_key_attr_to_string(type, namebuf,
2696 if (type > OVS_KEY_ATTR_MAX) {
2697 *out_of_range_attrp = type;
2699 if (present_attrs & (UINT64_C(1) << type)) {
2700 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2702 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2703 ovs_key_attr_to_string(type,
2704 namebuf, sizeof namebuf));
2708 present_attrs |= UINT64_C(1) << type;
2713 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2717 *present_attrsp = present_attrs;
2721 static enum odp_key_fitness
2722 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2723 uint64_t expected_attrs,
2724 const struct nlattr *key, size_t key_len)
2726 uint64_t missing_attrs;
2727 uint64_t extra_attrs;
2729 missing_attrs = expected_attrs & ~present_attrs;
2730 if (missing_attrs) {
2731 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2732 log_odp_key_attributes(&rl, "expected but not present",
2733 missing_attrs, 0, key, key_len);
2734 return ODP_FIT_TOO_LITTLE;
2737 extra_attrs = present_attrs & ~expected_attrs;
2738 if (extra_attrs || out_of_range_attr) {
2739 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2740 log_odp_key_attributes(&rl, "present but not expected",
2741 extra_attrs, out_of_range_attr, key, key_len);
2742 return ODP_FIT_TOO_MUCH;
2745 return ODP_FIT_PERFECT;
2749 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2750 uint64_t present_attrs, uint64_t *expected_attrs,
2751 struct flow *flow, const struct flow *src_flow)
2753 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2754 bool is_mask = flow != src_flow;
2756 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2757 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2758 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2759 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2760 ntohs(flow->dl_type));
2763 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
2764 flow->dl_type != htons(0xffff)) {
2767 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2770 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2771 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
2772 /* See comments in odp_flow_key_from_flow__(). */
2773 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
2780 static enum odp_key_fitness
2781 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2782 uint64_t present_attrs, int out_of_range_attr,
2783 uint64_t expected_attrs, struct flow *flow,
2784 const struct nlattr *key, size_t key_len,
2785 const struct flow *src_flow)
2787 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2788 bool is_mask = src_flow != flow;
2789 const void *check_start = NULL;
2790 size_t check_len = 0;
2791 enum ovs_key_attr expected_bit = 0xff;
2793 if (eth_type_mpls(src_flow->dl_type)) {
2795 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2797 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2798 return ODP_FIT_TOO_LITTLE;
2800 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2802 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
2803 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2805 if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) {
2806 return ODP_FIT_ERROR;
2808 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2809 if (flow->mpls_lse) {
2810 /* XXX Is this needed? */
2811 flow->mpls_depth = 0xffff;
2815 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
2817 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2819 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2820 const struct ovs_key_ipv4 *ipv4_key;
2822 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2823 flow->nw_src = ipv4_key->ipv4_src;
2824 flow->nw_dst = ipv4_key->ipv4_dst;
2825 flow->nw_proto = ipv4_key->ipv4_proto;
2826 flow->nw_tos = ipv4_key->ipv4_tos;
2827 flow->nw_ttl = ipv4_key->ipv4_ttl;
2829 flow->nw_frag = ipv4_key->ipv4_frag;
2830 check_start = ipv4_key;
2831 check_len = sizeof *ipv4_key;
2832 expected_bit = OVS_KEY_ATTR_IPV4;
2833 } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2834 return ODP_FIT_ERROR;
2837 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
2839 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2841 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2842 const struct ovs_key_ipv6 *ipv6_key;
2844 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2845 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2846 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2847 flow->ipv6_label = ipv6_key->ipv6_label;
2848 flow->nw_proto = ipv6_key->ipv6_proto;
2849 flow->nw_tos = ipv6_key->ipv6_tclass;
2850 flow->nw_ttl = ipv6_key->ipv6_hlimit;
2852 flow->nw_frag = ipv6_key->ipv6_frag;
2853 check_start = ipv6_key;
2854 check_len = sizeof *ipv6_key;
2855 expected_bit = OVS_KEY_ATTR_IPV6;
2856 } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2857 return ODP_FIT_ERROR;
2860 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
2861 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
2863 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2865 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2866 const struct ovs_key_arp *arp_key;
2868 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2869 flow->nw_src = arp_key->arp_sip;
2870 flow->nw_dst = arp_key->arp_tip;
2871 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
2872 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2873 "key", ntohs(arp_key->arp_op));
2874 return ODP_FIT_ERROR;
2876 flow->nw_proto = ntohs(arp_key->arp_op);
2877 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2878 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2881 check_start = arp_key;
2882 check_len = sizeof *arp_key;
2883 expected_bit = OVS_KEY_ATTR_ARP;
2890 if (!is_all_zeros(check_start, check_len) &&
2891 flow->dl_type != htons(0xffff)) {
2892 return ODP_FIT_ERROR;
2894 expected_attrs |= UINT64_C(1) << expected_bit;
2898 expected_bit = OVS_KEY_ATTR_UNSPEC;
2899 if (src_flow->nw_proto == IPPROTO_TCP
2900 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2901 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2902 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2904 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2906 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2907 const struct ovs_key_tcp *tcp_key;
2909 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2910 flow->tp_src = tcp_key->tcp_src;
2911 flow->tp_dst = tcp_key->tcp_dst;
2912 expected_bit = OVS_KEY_ATTR_TCP;
2914 } else if (src_flow->nw_proto == IPPROTO_UDP
2915 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2916 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2917 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2919 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2921 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2922 const struct ovs_key_udp *udp_key;
2924 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2925 flow->tp_src = udp_key->udp_src;
2926 flow->tp_dst = udp_key->udp_dst;
2927 expected_bit = OVS_KEY_ATTR_UDP;
2929 } else if (flow->nw_proto == IPPROTO_SCTP
2930 && (flow->dl_type == htons(ETH_TYPE_IP) ||
2931 flow->dl_type == htons(ETH_TYPE_IPV6))
2932 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2934 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
2936 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
2937 const struct ovs_key_sctp *sctp_key;
2939 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
2940 flow->tp_src = sctp_key->sctp_src;
2941 flow->tp_dst = sctp_key->sctp_dst;
2942 expected_bit = OVS_KEY_ATTR_SCTP;
2944 } else if (src_flow->nw_proto == IPPROTO_ICMP
2945 && src_flow->dl_type == htons(ETH_TYPE_IP)
2946 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2948 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
2950 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
2951 const struct ovs_key_icmp *icmp_key;
2953 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
2954 flow->tp_src = htons(icmp_key->icmp_type);
2955 flow->tp_dst = htons(icmp_key->icmp_code);
2956 expected_bit = OVS_KEY_ATTR_ICMP;
2958 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
2959 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
2960 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2962 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
2964 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
2965 const struct ovs_key_icmpv6 *icmpv6_key;
2967 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
2968 flow->tp_src = htons(icmpv6_key->icmpv6_type);
2969 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
2970 expected_bit = OVS_KEY_ATTR_ICMPV6;
2971 if (src_flow->tp_dst == htons(0) &&
2972 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2973 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
2975 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2977 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
2978 const struct ovs_key_nd *nd_key;
2980 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
2981 memcpy(&flow->nd_target, nd_key->nd_target,
2982 sizeof flow->nd_target);
2983 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
2984 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
2986 if (!is_all_zeros((const uint8_t *) nd_key,
2988 (flow->tp_src != htons(0xffff) ||
2989 flow->tp_dst != htons(0xffff))) {
2990 return ODP_FIT_ERROR;
2992 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2999 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3000 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3001 return ODP_FIT_ERROR;
3003 expected_attrs |= UINT64_C(1) << expected_bit;
3008 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3012 /* Parse 802.1Q header then encapsulated L3 attributes. */
3013 static enum odp_key_fitness
3014 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3015 uint64_t present_attrs, int out_of_range_attr,
3016 uint64_t expected_attrs, struct flow *flow,
3017 const struct nlattr *key, size_t key_len,
3018 const struct flow *src_flow)
3020 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3021 bool is_mask = src_flow != flow;
3023 const struct nlattr *encap
3024 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3025 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3026 enum odp_key_fitness encap_fitness;
3027 enum odp_key_fitness fitness;
3030 /* Calculate fitness of outer attributes. */
3032 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3033 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3035 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3036 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3038 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3039 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3042 fitness = check_expectations(present_attrs, out_of_range_attr,
3043 expected_attrs, key, key_len);
3045 /* Get the VLAN TCI value. */
3046 if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3047 return ODP_FIT_TOO_LITTLE;
3049 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3051 if (tci == htons(0)) {
3052 /* Corner case for a truncated 802.1Q header. */
3053 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3054 return ODP_FIT_TOO_MUCH;
3057 } else if (!(tci & htons(VLAN_CFI))) {
3058 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3059 "but CFI bit is not set", ntohs(tci));
3060 return ODP_FIT_ERROR;
3064 * Remove the TPID from dl_type since it's not the real Ethertype. */
3065 flow->dl_type = htons(0);
3066 flow->vlan_tci = tci;
3069 if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3072 /* Now parse the encapsulated attributes. */
3073 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3074 attrs, &present_attrs, &out_of_range_attr)) {
3075 return ODP_FIT_ERROR;
3079 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3080 return ODP_FIT_ERROR;
3082 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3083 expected_attrs, flow, key, key_len,
3086 /* The overall fitness is the worse of the outer and inner attributes. */
3087 return MAX(fitness, encap_fitness);
3090 static enum odp_key_fitness
3091 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3092 struct flow *flow, const struct flow *src_flow)
3094 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3095 uint64_t expected_attrs;
3096 uint64_t present_attrs;
3097 int out_of_range_attr;
3098 bool is_mask = src_flow != flow;
3100 memset(flow, 0, sizeof *flow);
3102 /* Parse attributes. */
3103 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3104 &out_of_range_attr)) {
3105 return ODP_FIT_ERROR;
3110 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3111 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3112 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3115 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3116 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3117 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3120 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3121 enum odp_key_fitness res;
3123 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3124 if (res == ODP_FIT_ERROR) {
3125 return ODP_FIT_ERROR;
3126 } else if (res == ODP_FIT_PERFECT) {
3127 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3131 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3132 flow->in_port.odp_port
3133 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3134 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3135 } else if (!is_mask) {
3136 flow->in_port.odp_port = ODPP_NONE;
3139 /* Ethernet header. */
3140 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3141 const struct ovs_key_ethernet *eth_key;
3143 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3144 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3145 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3147 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3151 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3154 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3155 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3157 return ODP_FIT_ERROR;
3160 if ((is_mask && (src_flow->vlan_tci & htons(VLAN_CFI))) ||
3161 (!is_mask && src_flow->dl_type == htons(ETH_TYPE_VLAN))) {
3162 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3163 expected_attrs, flow, key, key_len, src_flow);
3166 flow->vlan_tci = htons(0xffff);
3167 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3168 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3169 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3172 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3173 expected_attrs, flow, key, key_len, src_flow);
3176 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3177 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3178 * 'key' fits our expectations for what a flow key should contain.
3180 * The 'in_port' will be the datapath's understanding of the port. The
3181 * caller will need to translate with odp_port_to_ofp_port() if the
3182 * OpenFlow port is needed.
3184 * This function doesn't take the packet itself as an argument because none of
3185 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3186 * it is always possible to infer which additional attribute(s) should appear
3187 * by looking at the attributes for lower-level protocols, e.g. if the network
3188 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3189 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3190 * must be absent. */
3191 enum odp_key_fitness
3192 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3195 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3198 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3199 * structure in 'mask'. 'flow' must be a previously translated flow
3200 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3201 * 'key' fits our expectations for what a flow key should contain. */
3202 enum odp_key_fitness
3203 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3204 struct flow *mask, const struct flow *flow)
3206 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3209 /* Returns 'fitness' as a string, for use in debug messages. */
3211 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3214 case ODP_FIT_PERFECT:
3216 case ODP_FIT_TOO_MUCH:
3218 case ODP_FIT_TOO_LITTLE:
3219 return "too_little";
3227 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3228 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3229 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3230 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3231 * null, then the return value is not meaningful.) */
3233 odp_put_userspace_action(uint32_t pid,
3234 const void *userdata, size_t userdata_size,
3235 struct ofpbuf *odp_actions)
3237 size_t userdata_ofs;
3240 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3241 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3243 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3244 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3245 userdata, userdata_size);
3249 nl_msg_end_nested(odp_actions, offset);
3251 return userdata_ofs;
3255 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3256 struct ofpbuf *odp_actions)
3258 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3259 tun_key_to_attr(odp_actions, tunnel);
3260 nl_msg_end_nested(odp_actions, offset);
3263 /* The commit_odp_actions() function and its helpers. */
3266 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3267 const void *key, size_t key_size)
3269 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3270 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3271 nl_msg_end_nested(odp_actions, offset);
3275 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3276 struct ofpbuf *odp_actions)
3278 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3282 /* If any of the flow key data that ODP actions can modify are different in
3283 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3284 * 'odp_actions' that change the flow tunneling information in key from
3285 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3286 * same way. In other words, operates the same as commit_odp_actions(), but
3287 * only on tunneling information. */
3289 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3290 struct ofpbuf *odp_actions)
3292 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3293 if (flow->tunnel.ip_dst) {
3294 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3297 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3298 odp_put_tunnel_action(&base->tunnel, odp_actions);
3303 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3304 struct ofpbuf *odp_actions,
3305 struct flow_wildcards *wc)
3307 struct ovs_key_ethernet eth_key;
3309 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3310 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3314 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3315 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3317 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3318 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3320 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3321 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3323 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3324 ð_key, sizeof(eth_key));
3328 commit_vlan_action(const struct flow *flow, struct flow *base,
3329 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3331 if (base->vlan_tci == flow->vlan_tci) {
3335 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3337 if (base->vlan_tci & htons(VLAN_CFI)) {
3338 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3341 if (flow->vlan_tci & htons(VLAN_CFI)) {
3342 struct ovs_action_push_vlan vlan;
3344 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3345 vlan.vlan_tci = flow->vlan_tci;
3346 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3347 &vlan, sizeof vlan);
3349 base->vlan_tci = flow->vlan_tci;
3353 commit_mpls_action(const struct flow *flow, struct flow *base,
3354 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3356 if (flow->mpls_lse == base->mpls_lse &&
3357 flow->mpls_depth == base->mpls_depth) {
3361 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3363 if (flow->mpls_depth < base->mpls_depth) {
3364 if (base->mpls_depth - flow->mpls_depth > 1) {
3365 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3366 VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
3367 " a single mpls_pop action");
3370 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3371 } else if (flow->mpls_depth > base->mpls_depth) {
3372 struct ovs_action_push_mpls *mpls;
3374 if (flow->mpls_depth - base->mpls_depth > 1) {
3375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3376 VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
3377 " a single mpls_push action");
3380 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3382 memset(mpls, 0, sizeof *mpls);
3383 mpls->mpls_ethertype = flow->dl_type;
3384 mpls->mpls_lse = flow->mpls_lse;
3386 struct ovs_key_mpls mpls_key;
3388 mpls_key.mpls_lse = flow->mpls_lse;
3389 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3390 &mpls_key, sizeof(mpls_key));
3393 base->dl_type = flow->dl_type;
3394 base->mpls_lse = flow->mpls_lse;
3395 base->mpls_depth = flow->mpls_depth;
3399 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3400 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3402 struct ovs_key_ipv4 ipv4_key;
3404 if (base->nw_src == flow->nw_src &&
3405 base->nw_dst == flow->nw_dst &&
3406 base->nw_tos == flow->nw_tos &&
3407 base->nw_ttl == flow->nw_ttl &&
3408 base->nw_frag == flow->nw_frag) {
3412 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3413 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3414 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3415 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3416 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3417 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3419 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3420 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3421 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3422 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3423 ipv4_key.ipv4_proto = base->nw_proto;
3424 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3426 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3427 &ipv4_key, sizeof(ipv4_key));
3431 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3432 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3434 struct ovs_key_ipv6 ipv6_key;
3436 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3437 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3438 base->ipv6_label == flow->ipv6_label &&
3439 base->nw_tos == flow->nw_tos &&
3440 base->nw_ttl == flow->nw_ttl &&
3441 base->nw_frag == flow->nw_frag) {
3445 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3446 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3447 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3448 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3449 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3450 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3451 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3453 base->ipv6_src = flow->ipv6_src;
3454 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3455 base->ipv6_dst = flow->ipv6_dst;
3456 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3458 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3459 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3460 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3461 ipv6_key.ipv6_proto = base->nw_proto;
3462 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3464 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3465 &ipv6_key, sizeof(ipv6_key));
3469 commit_set_nw_action(const struct flow *flow, struct flow *base,
3470 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3472 /* Check if flow really have an IP header. */
3473 if (!flow->nw_proto) {
3477 if (base->dl_type == htons(ETH_TYPE_IP)) {
3478 commit_set_ipv4_action(flow, base, odp_actions, wc);
3479 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
3480 commit_set_ipv6_action(flow, base, odp_actions, wc);
3485 commit_set_port_action(const struct flow *flow, struct flow *base,
3486 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3488 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3492 if (base->tp_src == flow->tp_src &&
3493 base->tp_dst == flow->tp_dst) {
3497 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3498 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3500 if (flow->nw_proto == IPPROTO_TCP) {
3501 struct ovs_key_tcp port_key;
3503 port_key.tcp_src = base->tp_src = flow->tp_src;
3504 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3506 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3507 &port_key, sizeof(port_key));
3509 } else if (flow->nw_proto == IPPROTO_UDP) {
3510 struct ovs_key_udp port_key;
3512 port_key.udp_src = base->tp_src = flow->tp_src;
3513 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3515 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3516 &port_key, sizeof(port_key));
3517 } else if (flow->nw_proto == IPPROTO_SCTP) {
3518 struct ovs_key_sctp port_key;
3520 port_key.sctp_src = base->tp_src = flow->tp_src;
3521 port_key.sctp_dst = base->tp_dst = flow->tp_dst;
3523 commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
3524 &port_key, sizeof(port_key));
3529 commit_set_priority_action(const struct flow *flow, struct flow *base,
3530 struct ofpbuf *odp_actions,
3531 struct flow_wildcards *wc)
3533 if (base->skb_priority == flow->skb_priority) {
3537 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3538 base->skb_priority = flow->skb_priority;
3540 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3541 &base->skb_priority, sizeof(base->skb_priority));
3545 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
3546 struct ofpbuf *odp_actions,
3547 struct flow_wildcards *wc)
3549 if (base->pkt_mark == flow->pkt_mark) {
3553 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
3554 base->pkt_mark = flow->pkt_mark;
3556 odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
3558 /* If any of the flow key data that ODP actions can modify are different in
3559 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3560 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3561 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3562 * in addition to this function if needed. Sets fields in 'wc' that are
3563 * used as part of the action. */
3565 commit_odp_actions(const struct flow *flow, struct flow *base,
3566 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3568 commit_set_ether_addr_action(flow, base, odp_actions, wc);
3569 commit_vlan_action(flow, base, odp_actions, wc);
3570 commit_set_nw_action(flow, base, odp_actions, wc);
3571 commit_set_port_action(flow, base, odp_actions, wc);
3572 /* Committing MPLS actions should occur after committing nw and port
3573 * actions. This is because committing MPLS actions may alter a packet so
3574 * that it is no longer IP and thus nw and port actions are no longer valid.
3576 commit_mpls_action(flow, base, odp_actions, wc);
3577 commit_set_priority_action(flow, base, odp_actions, wc);
3578 commit_set_pkt_mark_action(flow, base, odp_actions, wc);