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, htons(UINT16_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, htons(UINT16_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,
1788 int label, tc, ttl, bos;
1789 int label_mask, tc_mask, ttl_mask, bos_mask;
1792 if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1793 &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1794 struct ovs_key_mpls *mpls, *mpls_mask;
1796 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1798 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1800 mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1802 mpls_mask->mpls_lse = mpls_lse_from_components(
1803 label_mask, tc_mask, ttl_mask, bos_mask);
1805 } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1806 &label, &tc, &ttl, &bos, &n) > 0 &&
1808 struct ovs_key_mpls *mpls;
1810 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1812 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1814 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1816 mpls->mpls_lse = htonl(UINT32_MAX);
1824 ovs_be32 ipv4_src, ipv4_src_mask;
1825 ovs_be32 ipv4_dst, ipv4_dst_mask;
1826 int ipv4_proto, ipv4_proto_mask;
1827 int ipv4_tos, ipv4_tos_mask;
1828 int ipv4_ttl, ipv4_ttl_mask;
1831 enum ovs_frag_type ipv4_frag;
1834 if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1835 "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1836 "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1837 "frag=%7[a-z]/%i)%n",
1838 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1839 IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1840 &ipv4_proto, &ipv4_proto_mask,
1841 &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1842 frag, &ipv4_frag_mask, &n) > 0
1844 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1845 struct ovs_key_ipv4 ipv4_key;
1846 struct ovs_key_ipv4 ipv4_mask;
1848 ipv4_key.ipv4_src = ipv4_src;
1849 ipv4_key.ipv4_dst = ipv4_dst;
1850 ipv4_key.ipv4_proto = ipv4_proto;
1851 ipv4_key.ipv4_tos = ipv4_tos;
1852 ipv4_key.ipv4_ttl = ipv4_ttl;
1853 ipv4_key.ipv4_frag = ipv4_frag;
1854 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1855 &ipv4_key, sizeof ipv4_key);
1857 ipv4_mask.ipv4_src = ipv4_src_mask;
1858 ipv4_mask.ipv4_dst = ipv4_dst_mask;
1859 ipv4_mask.ipv4_proto = ipv4_proto_mask;
1860 ipv4_mask.ipv4_tos = ipv4_tos_mask;
1861 ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1862 ipv4_mask.ipv4_frag = ipv4_frag_mask;
1863 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1864 &ipv4_mask, sizeof ipv4_mask);
1866 } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1867 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1868 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1869 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1871 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1872 struct ovs_key_ipv4 ipv4_key;
1874 ipv4_key.ipv4_src = ipv4_src;
1875 ipv4_key.ipv4_dst = ipv4_dst;
1876 ipv4_key.ipv4_proto = ipv4_proto;
1877 ipv4_key.ipv4_tos = ipv4_tos;
1878 ipv4_key.ipv4_ttl = ipv4_ttl;
1879 ipv4_key.ipv4_frag = ipv4_frag;
1880 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1881 &ipv4_key, sizeof ipv4_key);
1884 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1885 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1886 &ipv4_key, sizeof ipv4_key);
1893 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1894 char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1895 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1896 char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1897 int ipv6_label, ipv6_label_mask;
1898 int ipv6_proto, ipv6_proto_mask;
1899 int ipv6_tclass, ipv6_tclass_mask;
1900 int ipv6_hlimit, ipv6_hlimit_mask;
1902 enum ovs_frag_type ipv6_frag;
1906 if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1907 IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1908 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1909 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1910 ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1911 &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1912 &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1913 &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1914 &ipv6_frag_mask, &n) > 0
1916 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1917 struct ovs_key_ipv6 ipv6_key;
1918 struct ovs_key_ipv6 ipv6_mask;
1920 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1921 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1922 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1923 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1927 ipv6_key.ipv6_label = htonl(ipv6_label);
1928 ipv6_key.ipv6_proto = ipv6_proto;
1929 ipv6_key.ipv6_tclass = ipv6_tclass;
1930 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1931 ipv6_key.ipv6_frag = ipv6_frag;
1932 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1933 &ipv6_key, sizeof ipv6_key);
1935 ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1936 ipv6_mask.ipv6_proto = ipv6_proto_mask;
1937 ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1938 ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1939 ipv6_mask.ipv6_frag = ipv6_frag_mask;
1940 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1941 &ipv6_mask, sizeof ipv6_mask);
1943 } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1944 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1945 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1946 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1948 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1949 struct ovs_key_ipv6 ipv6_key;
1951 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1952 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1955 ipv6_key.ipv6_label = htonl(ipv6_label);
1956 ipv6_key.ipv6_proto = ipv6_proto;
1957 ipv6_key.ipv6_tclass = ipv6_tclass;
1958 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1959 ipv6_key.ipv6_frag = ipv6_frag;
1960 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1961 &ipv6_key, sizeof ipv6_key);
1964 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1965 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1966 &ipv6_key, sizeof ipv6_key);
1979 if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
1980 &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
1982 struct ovs_key_tcp tcp_key;
1983 struct ovs_key_tcp tcp_mask;
1985 tcp_key.tcp_src = htons(tcp_src);
1986 tcp_key.tcp_dst = htons(tcp_dst);
1987 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1989 tcp_mask.tcp_src = htons(tcp_src_mask);
1990 tcp_mask.tcp_dst = htons(tcp_dst_mask);
1991 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1992 &tcp_mask, sizeof tcp_mask);
1994 } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1996 struct ovs_key_tcp tcp_key;
1998 tcp_key.tcp_src = htons(tcp_src);
1999 tcp_key.tcp_dst = htons(tcp_dst);
2000 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2003 memset(&tcp_key, 0xff, sizeof tcp_key);
2004 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2005 &tcp_key, sizeof tcp_key);
2018 if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
2019 &udp_src, &udp_src_mask,
2020 &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
2021 struct ovs_key_udp udp_key;
2022 struct ovs_key_udp udp_mask;
2024 udp_key.udp_src = htons(udp_src);
2025 udp_key.udp_dst = htons(udp_dst);
2026 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2028 udp_mask.udp_src = htons(udp_src_mask);
2029 udp_mask.udp_dst = htons(udp_dst_mask);
2030 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2031 &udp_mask, sizeof udp_mask);
2034 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
2036 struct ovs_key_udp udp_key;
2038 udp_key.udp_src = htons(udp_src);
2039 udp_key.udp_dst = htons(udp_dst);
2040 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2043 memset(&udp_key, 0xff, sizeof udp_key);
2044 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2057 if (mask && sscanf(s, "sctp(src=%i/%i,dst=%i/%i)%n",
2058 &sctp_src, &sctp_src_mask,
2059 &sctp_dst, &sctp_dst_mask, &n) > 0 && n > 0) {
2060 struct ovs_key_sctp sctp_key;
2061 struct ovs_key_sctp sctp_mask;
2063 sctp_key.sctp_src = htons(sctp_src);
2064 sctp_key.sctp_dst = htons(sctp_dst);
2065 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2067 sctp_mask.sctp_src = htons(sctp_src_mask);
2068 sctp_mask.sctp_dst = htons(sctp_dst_mask);
2069 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP,
2070 &sctp_mask, sizeof sctp_mask);
2073 if (sscanf(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n) > 0
2075 struct ovs_key_sctp sctp_key;
2077 sctp_key.sctp_src = htons(sctp_src);
2078 sctp_key.sctp_dst = htons(sctp_dst);
2079 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2082 memset(&sctp_key, 0xff, sizeof sctp_key);
2083 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2096 if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
2097 &icmp_type, &icmp_type_mask,
2098 &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
2099 struct ovs_key_icmp icmp_key;
2100 struct ovs_key_icmp icmp_mask;
2102 icmp_key.icmp_type = icmp_type;
2103 icmp_key.icmp_code = icmp_code;
2104 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2105 &icmp_key, sizeof icmp_key);
2107 icmp_mask.icmp_type = icmp_type_mask;
2108 icmp_mask.icmp_code = icmp_code_mask;
2109 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2110 &icmp_mask, sizeof icmp_mask);
2112 } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
2113 &icmp_type, &icmp_code, &n) > 0
2115 struct ovs_key_icmp icmp_key;
2117 icmp_key.icmp_type = icmp_type;
2118 icmp_key.icmp_code = icmp_code;
2119 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2120 &icmp_key, sizeof icmp_key);
2122 memset(&icmp_key, 0xff, sizeof icmp_key);
2123 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2131 struct ovs_key_icmpv6 icmpv6_key;
2132 struct ovs_key_icmpv6 icmpv6_mask;
2133 int icmpv6_type_mask;
2134 int icmpv6_code_mask;
2137 if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
2138 &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
2139 &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
2141 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2142 &icmpv6_key, sizeof icmpv6_key);
2144 icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2145 icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2146 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2147 sizeof icmpv6_mask);
2149 } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2150 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2152 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2153 &icmpv6_key, sizeof icmpv6_key);
2156 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2157 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2165 ovs_be32 arp_sip, arp_sip_mask;
2166 ovs_be32 arp_tip, arp_tip_mask;
2167 int arp_op, arp_op_mask;
2168 uint8_t arp_sha[ETH_ADDR_LEN];
2169 uint8_t arp_sha_mask[ETH_ADDR_LEN];
2170 uint8_t arp_tha[ETH_ADDR_LEN];
2171 uint8_t arp_tha_mask[ETH_ADDR_LEN];
2174 if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2175 "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2176 "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2177 "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2178 IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2179 IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2180 &arp_op, &arp_op_mask,
2181 ETH_ADDR_SCAN_ARGS(arp_sha),
2182 ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2183 ETH_ADDR_SCAN_ARGS(arp_tha),
2184 ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2185 struct ovs_key_arp arp_key;
2186 struct ovs_key_arp arp_mask;
2188 memset(&arp_key, 0, sizeof arp_key);
2189 arp_key.arp_sip = arp_sip;
2190 arp_key.arp_tip = arp_tip;
2191 arp_key.arp_op = htons(arp_op);
2192 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2193 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2194 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2196 arp_mask.arp_sip = arp_sip_mask;
2197 arp_mask.arp_tip = arp_tip_mask;
2198 arp_mask.arp_op = htons(arp_op_mask);
2199 memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2200 memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2201 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2202 &arp_mask, sizeof arp_mask);
2204 } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2205 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2206 IP_SCAN_ARGS(&arp_sip),
2207 IP_SCAN_ARGS(&arp_tip),
2209 ETH_ADDR_SCAN_ARGS(arp_sha),
2210 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2211 struct ovs_key_arp arp_key;
2213 memset(&arp_key, 0, sizeof arp_key);
2214 arp_key.arp_sip = arp_sip;
2215 arp_key.arp_tip = arp_tip;
2216 arp_key.arp_op = htons(arp_op);
2217 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2218 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2219 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2222 memset(&arp_key, 0xff, sizeof arp_key);
2223 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2224 &arp_key, sizeof arp_key);
2231 char nd_target_s[IPV6_SCAN_LEN + 1];
2232 char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2233 uint8_t nd_sll[ETH_ADDR_LEN];
2234 uint8_t nd_sll_mask[ETH_ADDR_LEN];
2235 uint8_t nd_tll[ETH_ADDR_LEN];
2236 uint8_t nd_tll_mask[ETH_ADDR_LEN];
2239 nd_target_mask_s[0] = 0;
2240 memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2241 memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2243 if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2244 nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2245 put_nd_key(n, nd_target_s, NULL, NULL, key);
2246 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2247 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2248 nd_target_s, &n) > 0 && n > 0) {
2249 put_nd_key(n, nd_target_s, NULL, NULL, key);
2251 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2253 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2254 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2255 nd_target_s, nd_target_mask_s,
2256 ETH_ADDR_SCAN_ARGS(nd_sll),
2257 ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2258 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2259 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2260 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2261 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2263 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2265 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2267 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2268 ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2269 nd_target_s, nd_target_mask_s,
2270 ETH_ADDR_SCAN_ARGS(nd_tll),
2271 ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2272 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2273 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2274 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2275 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2277 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2279 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2281 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2282 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2283 "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2284 nd_target_s, nd_target_mask_s,
2285 ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2286 ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2289 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2290 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2291 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2292 "tll="ETH_ADDR_SCAN_FMT")%n",
2293 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2294 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2296 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2298 put_nd_mask(n, nd_target_mask_s,
2299 nd_sll_mask, nd_tll_mask, mask);
2308 if (!strncmp(s, "encap(", 6)) {
2309 const char *start = s;
2310 size_t encap, encap_mask = 0;
2312 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2314 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2321 s += strspn(s, ", \t\r\n");
2324 } else if (*s == ')') {
2328 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2336 nl_msg_end_nested(key, encap);
2338 nl_msg_end_nested(mask, encap_mask);
2347 /* Parses the string representation of a datapath flow key, in the
2348 * format output by odp_flow_key_format(). Returns 0 if successful,
2349 * otherwise a positive errno value. On success, the flow key is
2350 * appended to 'key' as a series of Netlink attributes. On failure, no
2351 * data is appended to 'key'. Either way, 'key''s data might be
2354 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2355 * to a port number. (Port names may be used instead of port numbers in
2358 * On success, the attributes appended to 'key' are individually syntactically
2359 * valid, but they may not be valid as a sequence. 'key' might, for example,
2360 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2362 odp_flow_from_string(const char *s, const struct simap *port_names,
2363 struct ofpbuf *key, struct ofpbuf *mask)
2365 const size_t old_size = key->size;
2369 s += strspn(s, delimiters);
2374 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2376 key->size = old_size;
2386 ovs_to_odp_frag(uint8_t nw_frag)
2388 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2389 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2390 : OVS_FRAG_TYPE_LATER);
2394 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2396 uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2398 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2399 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2405 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2406 const struct flow *flow, odp_port_t odp_in_port)
2409 struct ovs_key_ethernet *eth_key;
2412 /* We assume that if 'data' and 'flow' are not the same, we should
2413 * treat 'data' as a mask. */
2414 is_mask = (data != flow);
2416 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2418 if (flow->tunnel.ip_dst || is_mask) {
2419 tun_key_to_attr(buf, &data->tunnel);
2422 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2424 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2425 * is not the magical value "ODPP_NONE". */
2426 if (is_mask || odp_in_port != ODPP_NONE) {
2427 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2430 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2432 memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2433 memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2435 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2437 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2439 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2441 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2442 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2443 if (flow->vlan_tci == htons(0)) {
2450 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2451 /* For backwards compatibility with kernels that don't support
2452 * wildcarding, the following convention is used to encode the
2453 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2456 * -------- -------- -------
2457 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2458 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2459 * <none> 0xffff Any non-Ethernet II frame (except valid
2460 * 802.3 SNAP packet with valid eth_type).
2463 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(UINT16_MAX));
2468 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2470 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2471 struct ovs_key_ipv4 *ipv4_key;
2473 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2475 ipv4_key->ipv4_src = data->nw_src;
2476 ipv4_key->ipv4_dst = data->nw_dst;
2477 ipv4_key->ipv4_proto = data->nw_proto;
2478 ipv4_key->ipv4_tos = data->nw_tos;
2479 ipv4_key->ipv4_ttl = data->nw_ttl;
2480 ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2481 : ovs_to_odp_frag(data->nw_frag);
2482 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2483 struct ovs_key_ipv6 *ipv6_key;
2485 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2487 memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2488 memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2489 ipv6_key->ipv6_label = data->ipv6_label;
2490 ipv6_key->ipv6_proto = data->nw_proto;
2491 ipv6_key->ipv6_tclass = data->nw_tos;
2492 ipv6_key->ipv6_hlimit = data->nw_ttl;
2493 ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2494 : ovs_to_odp_frag(data->nw_frag);
2495 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2496 flow->dl_type == htons(ETH_TYPE_RARP)) {
2497 struct ovs_key_arp *arp_key;
2499 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2501 memset(arp_key, 0, sizeof *arp_key);
2502 arp_key->arp_sip = data->nw_src;
2503 arp_key->arp_tip = data->nw_dst;
2504 arp_key->arp_op = htons(data->nw_proto);
2505 memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2506 memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2509 if (flow->mpls_depth) {
2510 struct ovs_key_mpls *mpls_key;
2512 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2514 mpls_key->mpls_lse = data->mpls_lse;
2517 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2518 if (flow->nw_proto == IPPROTO_TCP) {
2519 struct ovs_key_tcp *tcp_key;
2521 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2523 tcp_key->tcp_src = data->tp_src;
2524 tcp_key->tcp_dst = data->tp_dst;
2525 } else if (flow->nw_proto == IPPROTO_UDP) {
2526 struct ovs_key_udp *udp_key;
2528 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2530 udp_key->udp_src = data->tp_src;
2531 udp_key->udp_dst = data->tp_dst;
2532 } else if (flow->nw_proto == IPPROTO_SCTP) {
2533 struct ovs_key_sctp *sctp_key;
2535 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2537 sctp_key->sctp_src = data->tp_src;
2538 sctp_key->sctp_dst = data->tp_dst;
2539 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2540 && flow->nw_proto == IPPROTO_ICMP) {
2541 struct ovs_key_icmp *icmp_key;
2543 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2545 icmp_key->icmp_type = ntohs(data->tp_src);
2546 icmp_key->icmp_code = ntohs(data->tp_dst);
2547 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2548 && flow->nw_proto == IPPROTO_ICMPV6) {
2549 struct ovs_key_icmpv6 *icmpv6_key;
2551 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2552 sizeof *icmpv6_key);
2553 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2554 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2556 if (flow->tp_dst == htons(0) &&
2557 (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2558 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) &&
2559 (!is_mask || (data->tp_src == htons(0xffff) &&
2560 data->tp_dst == htons(0xffff)))) {
2562 struct ovs_key_nd *nd_key;
2564 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2566 memcpy(nd_key->nd_target, &data->nd_target,
2567 sizeof nd_key->nd_target);
2568 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2569 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2576 nl_msg_end_nested(buf, encap);
2580 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2581 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2582 * number rather than a datapath port number). Instead, if 'odp_in_port'
2583 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2586 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2587 * capable of being expanded to allow for that much space. */
2589 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2590 odp_port_t odp_in_port)
2592 odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2595 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2596 * 'buf'. 'flow' is used as a template to determine how to interpret
2597 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2598 * it doesn't indicate whether the other fields should be interpreted as
2599 * ARP, IPv4, IPv6, etc.
2601 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2602 * capable of being expanded to allow for that much space. */
2604 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2605 const struct flow *flow, uint32_t odp_in_port_mask)
2607 odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask));
2611 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2613 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2614 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2615 key_len / sizeof(uint32_t), 0);
2619 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2620 uint64_t attrs, int out_of_range_attr,
2621 const struct nlattr *key, size_t key_len)
2626 if (VLOG_DROP_DBG(rl)) {
2631 for (i = 0; i < 64; i++) {
2632 if (attrs & (UINT64_C(1) << i)) {
2633 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2635 ds_put_format(&s, " %s",
2636 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2639 if (out_of_range_attr) {
2640 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2643 ds_put_cstr(&s, ": ");
2644 odp_flow_key_format(key, key_len, &s);
2646 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2651 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2653 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2655 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2656 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2660 if (odp_frag != OVS_FRAG_TYPE_NONE) {
2661 flow->nw_frag |= FLOW_NW_FRAG_ANY;
2662 if (odp_frag == OVS_FRAG_TYPE_LATER) {
2663 flow->nw_frag |= FLOW_NW_FRAG_LATER;
2670 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2671 const struct nlattr *attrs[], uint64_t *present_attrsp,
2672 int *out_of_range_attrp)
2674 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2675 const struct nlattr *nla;
2676 uint64_t present_attrs;
2679 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2681 *out_of_range_attrp = 0;
2682 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2683 uint16_t type = nl_attr_type(nla);
2684 size_t len = nl_attr_get_size(nla);
2685 int expected_len = odp_flow_key_attr_len(type);
2687 if (len != expected_len && expected_len >= 0) {
2688 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2690 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2691 "length %d", ovs_key_attr_to_string(type, namebuf,
2697 if (type > OVS_KEY_ATTR_MAX) {
2698 *out_of_range_attrp = type;
2700 if (present_attrs & (UINT64_C(1) << type)) {
2701 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2703 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2704 ovs_key_attr_to_string(type,
2705 namebuf, sizeof namebuf));
2709 present_attrs |= UINT64_C(1) << type;
2714 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2718 *present_attrsp = present_attrs;
2722 static enum odp_key_fitness
2723 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2724 uint64_t expected_attrs,
2725 const struct nlattr *key, size_t key_len)
2727 uint64_t missing_attrs;
2728 uint64_t extra_attrs;
2730 missing_attrs = expected_attrs & ~present_attrs;
2731 if (missing_attrs) {
2732 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2733 log_odp_key_attributes(&rl, "expected but not present",
2734 missing_attrs, 0, key, key_len);
2735 return ODP_FIT_TOO_LITTLE;
2738 extra_attrs = present_attrs & ~expected_attrs;
2739 if (extra_attrs || out_of_range_attr) {
2740 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2741 log_odp_key_attributes(&rl, "present but not expected",
2742 extra_attrs, out_of_range_attr, key, key_len);
2743 return ODP_FIT_TOO_MUCH;
2746 return ODP_FIT_PERFECT;
2750 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2751 uint64_t present_attrs, uint64_t *expected_attrs,
2752 struct flow *flow, const struct flow *src_flow)
2754 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2755 bool is_mask = flow != src_flow;
2757 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2758 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2759 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2760 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2761 ntohs(flow->dl_type));
2764 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
2765 flow->dl_type != htons(0xffff)) {
2768 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2771 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2772 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
2773 /* See comments in odp_flow_key_from_flow__(). */
2774 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
2781 static enum odp_key_fitness
2782 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2783 uint64_t present_attrs, int out_of_range_attr,
2784 uint64_t expected_attrs, struct flow *flow,
2785 const struct nlattr *key, size_t key_len,
2786 const struct flow *src_flow)
2788 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2789 bool is_mask = src_flow != flow;
2790 const void *check_start = NULL;
2791 size_t check_len = 0;
2792 enum ovs_key_attr expected_bit = 0xff;
2794 if (eth_type_mpls(src_flow->dl_type)) {
2796 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2798 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2799 return ODP_FIT_TOO_LITTLE;
2801 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2803 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
2804 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2806 if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) {
2807 return ODP_FIT_ERROR;
2809 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2810 if (flow->mpls_lse) {
2811 /* XXX Is this needed? */
2812 flow->mpls_depth = 0xffff;
2816 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
2818 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2820 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2821 const struct ovs_key_ipv4 *ipv4_key;
2823 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2824 flow->nw_src = ipv4_key->ipv4_src;
2825 flow->nw_dst = ipv4_key->ipv4_dst;
2826 flow->nw_proto = ipv4_key->ipv4_proto;
2827 flow->nw_tos = ipv4_key->ipv4_tos;
2828 flow->nw_ttl = ipv4_key->ipv4_ttl;
2830 flow->nw_frag = ipv4_key->ipv4_frag;
2831 check_start = ipv4_key;
2832 check_len = sizeof *ipv4_key;
2833 expected_bit = OVS_KEY_ATTR_IPV4;
2834 } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2835 return ODP_FIT_ERROR;
2838 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
2840 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2842 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2843 const struct ovs_key_ipv6 *ipv6_key;
2845 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2846 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2847 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2848 flow->ipv6_label = ipv6_key->ipv6_label;
2849 flow->nw_proto = ipv6_key->ipv6_proto;
2850 flow->nw_tos = ipv6_key->ipv6_tclass;
2851 flow->nw_ttl = ipv6_key->ipv6_hlimit;
2853 flow->nw_frag = ipv6_key->ipv6_frag;
2854 check_start = ipv6_key;
2855 check_len = sizeof *ipv6_key;
2856 expected_bit = OVS_KEY_ATTR_IPV6;
2857 } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2858 return ODP_FIT_ERROR;
2861 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
2862 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
2864 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2866 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2867 const struct ovs_key_arp *arp_key;
2869 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2870 flow->nw_src = arp_key->arp_sip;
2871 flow->nw_dst = arp_key->arp_tip;
2872 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
2873 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2874 "key", ntohs(arp_key->arp_op));
2875 return ODP_FIT_ERROR;
2877 flow->nw_proto = ntohs(arp_key->arp_op);
2878 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2879 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2882 check_start = arp_key;
2883 check_len = sizeof *arp_key;
2884 expected_bit = OVS_KEY_ATTR_ARP;
2891 if (!is_all_zeros(check_start, check_len) &&
2892 flow->dl_type != htons(0xffff)) {
2893 return ODP_FIT_ERROR;
2895 expected_attrs |= UINT64_C(1) << expected_bit;
2899 expected_bit = OVS_KEY_ATTR_UNSPEC;
2900 if (src_flow->nw_proto == IPPROTO_TCP
2901 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2902 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2903 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2905 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2907 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2908 const struct ovs_key_tcp *tcp_key;
2910 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2911 flow->tp_src = tcp_key->tcp_src;
2912 flow->tp_dst = tcp_key->tcp_dst;
2913 expected_bit = OVS_KEY_ATTR_TCP;
2915 } else if (src_flow->nw_proto == IPPROTO_UDP
2916 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2917 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2918 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2920 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2922 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2923 const struct ovs_key_udp *udp_key;
2925 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2926 flow->tp_src = udp_key->udp_src;
2927 flow->tp_dst = udp_key->udp_dst;
2928 expected_bit = OVS_KEY_ATTR_UDP;
2930 } else if (flow->nw_proto == IPPROTO_SCTP
2931 && (flow->dl_type == htons(ETH_TYPE_IP) ||
2932 flow->dl_type == htons(ETH_TYPE_IPV6))
2933 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2935 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
2937 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
2938 const struct ovs_key_sctp *sctp_key;
2940 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
2941 flow->tp_src = sctp_key->sctp_src;
2942 flow->tp_dst = sctp_key->sctp_dst;
2943 expected_bit = OVS_KEY_ATTR_SCTP;
2945 } else if (src_flow->nw_proto == IPPROTO_ICMP
2946 && src_flow->dl_type == htons(ETH_TYPE_IP)
2947 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2949 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
2951 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
2952 const struct ovs_key_icmp *icmp_key;
2954 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
2955 flow->tp_src = htons(icmp_key->icmp_type);
2956 flow->tp_dst = htons(icmp_key->icmp_code);
2957 expected_bit = OVS_KEY_ATTR_ICMP;
2959 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
2960 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
2961 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2963 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
2965 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
2966 const struct ovs_key_icmpv6 *icmpv6_key;
2968 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
2969 flow->tp_src = htons(icmpv6_key->icmpv6_type);
2970 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
2971 expected_bit = OVS_KEY_ATTR_ICMPV6;
2972 if (src_flow->tp_dst == htons(0) &&
2973 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2974 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
2976 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2978 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
2979 const struct ovs_key_nd *nd_key;
2981 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
2982 memcpy(&flow->nd_target, nd_key->nd_target,
2983 sizeof flow->nd_target);
2984 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
2985 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
2987 if (!is_all_zeros((const uint8_t *) nd_key,
2989 (flow->tp_src != htons(0xffff) ||
2990 flow->tp_dst != htons(0xffff))) {
2991 return ODP_FIT_ERROR;
2993 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
3000 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3001 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3002 return ODP_FIT_ERROR;
3004 expected_attrs |= UINT64_C(1) << expected_bit;
3009 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3013 /* Parse 802.1Q header then encapsulated L3 attributes. */
3014 static enum odp_key_fitness
3015 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3016 uint64_t present_attrs, int out_of_range_attr,
3017 uint64_t expected_attrs, struct flow *flow,
3018 const struct nlattr *key, size_t key_len,
3019 const struct flow *src_flow)
3021 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3022 bool is_mask = src_flow != flow;
3024 const struct nlattr *encap
3025 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3026 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3027 enum odp_key_fitness encap_fitness;
3028 enum odp_key_fitness fitness;
3031 /* Calculate fitness of outer attributes. */
3033 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3034 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3036 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3037 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3039 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3040 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3043 fitness = check_expectations(present_attrs, out_of_range_attr,
3044 expected_attrs, key, key_len);
3046 /* Get the VLAN TCI value. */
3047 if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3048 return ODP_FIT_TOO_LITTLE;
3050 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3052 if (tci == htons(0)) {
3053 /* Corner case for a truncated 802.1Q header. */
3054 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3055 return ODP_FIT_TOO_MUCH;
3058 } else if (!(tci & htons(VLAN_CFI))) {
3059 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3060 "but CFI bit is not set", ntohs(tci));
3061 return ODP_FIT_ERROR;
3065 * Remove the TPID from dl_type since it's not the real Ethertype. */
3066 flow->dl_type = htons(0);
3067 flow->vlan_tci = tci;
3070 if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3073 /* Now parse the encapsulated attributes. */
3074 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3075 attrs, &present_attrs, &out_of_range_attr)) {
3076 return ODP_FIT_ERROR;
3080 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3081 return ODP_FIT_ERROR;
3083 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3084 expected_attrs, flow, key, key_len,
3087 /* The overall fitness is the worse of the outer and inner attributes. */
3088 return MAX(fitness, encap_fitness);
3091 static enum odp_key_fitness
3092 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3093 struct flow *flow, const struct flow *src_flow)
3095 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3096 uint64_t expected_attrs;
3097 uint64_t present_attrs;
3098 int out_of_range_attr;
3099 bool is_mask = src_flow != flow;
3101 memset(flow, 0, sizeof *flow);
3103 /* Parse attributes. */
3104 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3105 &out_of_range_attr)) {
3106 return ODP_FIT_ERROR;
3111 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3112 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3113 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3116 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3117 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3118 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3121 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3122 enum odp_key_fitness res;
3124 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3125 if (res == ODP_FIT_ERROR) {
3126 return ODP_FIT_ERROR;
3127 } else if (res == ODP_FIT_PERFECT) {
3128 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3132 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3133 flow->in_port.odp_port
3134 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3135 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3136 } else if (!is_mask) {
3137 flow->in_port.odp_port = ODPP_NONE;
3140 /* Ethernet header. */
3141 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3142 const struct ovs_key_ethernet *eth_key;
3144 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3145 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3146 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3148 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3152 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3155 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3156 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3158 return ODP_FIT_ERROR;
3161 if ((is_mask && (src_flow->vlan_tci & htons(VLAN_CFI))) ||
3162 (!is_mask && src_flow->dl_type == htons(ETH_TYPE_VLAN))) {
3163 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3164 expected_attrs, flow, key, key_len, src_flow);
3167 flow->vlan_tci = htons(0xffff);
3168 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3169 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3170 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3173 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3174 expected_attrs, flow, key, key_len, src_flow);
3177 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3178 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3179 * 'key' fits our expectations for what a flow key should contain.
3181 * The 'in_port' will be the datapath's understanding of the port. The
3182 * caller will need to translate with odp_port_to_ofp_port() if the
3183 * OpenFlow port is needed.
3185 * This function doesn't take the packet itself as an argument because none of
3186 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3187 * it is always possible to infer which additional attribute(s) should appear
3188 * by looking at the attributes for lower-level protocols, e.g. if the network
3189 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3190 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3191 * must be absent. */
3192 enum odp_key_fitness
3193 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3196 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3199 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3200 * structure in 'mask'. 'flow' must be a previously translated flow
3201 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3202 * 'key' fits our expectations for what a flow key should contain. */
3203 enum odp_key_fitness
3204 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3205 struct flow *mask, const struct flow *flow)
3207 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3210 /* Returns 'fitness' as a string, for use in debug messages. */
3212 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3215 case ODP_FIT_PERFECT:
3217 case ODP_FIT_TOO_MUCH:
3219 case ODP_FIT_TOO_LITTLE:
3220 return "too_little";
3228 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3229 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3230 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3231 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3232 * null, then the return value is not meaningful.) */
3234 odp_put_userspace_action(uint32_t pid,
3235 const void *userdata, size_t userdata_size,
3236 struct ofpbuf *odp_actions)
3238 size_t userdata_ofs;
3241 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3242 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3244 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3245 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3246 userdata, userdata_size);
3250 nl_msg_end_nested(odp_actions, offset);
3252 return userdata_ofs;
3256 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3257 struct ofpbuf *odp_actions)
3259 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3260 tun_key_to_attr(odp_actions, tunnel);
3261 nl_msg_end_nested(odp_actions, offset);
3264 /* The commit_odp_actions() function and its helpers. */
3267 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3268 const void *key, size_t key_size)
3270 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3271 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3272 nl_msg_end_nested(odp_actions, offset);
3276 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3277 struct ofpbuf *odp_actions)
3279 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3283 /* If any of the flow key data that ODP actions can modify are different in
3284 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3285 * 'odp_actions' that change the flow tunneling information in key from
3286 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3287 * same way. In other words, operates the same as commit_odp_actions(), but
3288 * only on tunneling information. */
3290 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3291 struct ofpbuf *odp_actions)
3293 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3294 if (flow->tunnel.ip_dst) {
3295 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3298 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3299 odp_put_tunnel_action(&base->tunnel, odp_actions);
3304 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3305 struct ofpbuf *odp_actions,
3306 struct flow_wildcards *wc)
3308 struct ovs_key_ethernet eth_key;
3310 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3311 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3315 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3316 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3318 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3319 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3321 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3322 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3324 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3325 ð_key, sizeof(eth_key));
3329 commit_vlan_action(const struct flow *flow, struct flow *base,
3330 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3332 if (base->vlan_tci == flow->vlan_tci) {
3336 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3338 if (base->vlan_tci & htons(VLAN_CFI)) {
3339 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3342 if (flow->vlan_tci & htons(VLAN_CFI)) {
3343 struct ovs_action_push_vlan vlan;
3345 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3346 vlan.vlan_tci = flow->vlan_tci;
3347 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3348 &vlan, sizeof vlan);
3350 base->vlan_tci = flow->vlan_tci;
3354 commit_mpls_action(const struct flow *flow, struct flow *base,
3355 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3357 if (flow->mpls_lse == base->mpls_lse &&
3358 flow->mpls_depth == base->mpls_depth) {
3362 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3364 if (flow->mpls_depth < base->mpls_depth) {
3365 if (base->mpls_depth - flow->mpls_depth > 1) {
3366 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3367 VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
3368 " a single mpls_pop action");
3371 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3372 } else if (flow->mpls_depth > base->mpls_depth) {
3373 struct ovs_action_push_mpls *mpls;
3375 if (flow->mpls_depth - base->mpls_depth > 1) {
3376 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3377 VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
3378 " a single mpls_push action");
3381 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3383 memset(mpls, 0, sizeof *mpls);
3384 mpls->mpls_ethertype = flow->dl_type;
3385 mpls->mpls_lse = flow->mpls_lse;
3387 struct ovs_key_mpls mpls_key;
3389 mpls_key.mpls_lse = flow->mpls_lse;
3390 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3391 &mpls_key, sizeof(mpls_key));
3394 base->dl_type = flow->dl_type;
3395 base->mpls_lse = flow->mpls_lse;
3396 base->mpls_depth = flow->mpls_depth;
3400 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3401 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3403 struct ovs_key_ipv4 ipv4_key;
3405 if (base->nw_src == flow->nw_src &&
3406 base->nw_dst == flow->nw_dst &&
3407 base->nw_tos == flow->nw_tos &&
3408 base->nw_ttl == flow->nw_ttl &&
3409 base->nw_frag == flow->nw_frag) {
3413 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3414 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3415 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3416 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3417 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3418 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3420 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3421 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3422 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3423 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3424 ipv4_key.ipv4_proto = base->nw_proto;
3425 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3427 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3428 &ipv4_key, sizeof(ipv4_key));
3432 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3433 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3435 struct ovs_key_ipv6 ipv6_key;
3437 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3438 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3439 base->ipv6_label == flow->ipv6_label &&
3440 base->nw_tos == flow->nw_tos &&
3441 base->nw_ttl == flow->nw_ttl &&
3442 base->nw_frag == flow->nw_frag) {
3446 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3447 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3448 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3449 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3450 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3451 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3452 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3454 base->ipv6_src = flow->ipv6_src;
3455 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3456 base->ipv6_dst = flow->ipv6_dst;
3457 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3459 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3460 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3461 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3462 ipv6_key.ipv6_proto = base->nw_proto;
3463 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3465 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3466 &ipv6_key, sizeof(ipv6_key));
3470 commit_set_nw_action(const struct flow *flow, struct flow *base,
3471 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3473 /* Check if flow really have an IP header. */
3474 if (!flow->nw_proto) {
3478 if (base->dl_type == htons(ETH_TYPE_IP)) {
3479 commit_set_ipv4_action(flow, base, odp_actions, wc);
3480 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
3481 commit_set_ipv6_action(flow, base, odp_actions, wc);
3486 commit_set_port_action(const struct flow *flow, struct flow *base,
3487 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3489 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3493 if (base->tp_src == flow->tp_src &&
3494 base->tp_dst == flow->tp_dst) {
3498 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3499 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3501 if (flow->nw_proto == IPPROTO_TCP) {
3502 struct ovs_key_tcp port_key;
3504 port_key.tcp_src = base->tp_src = flow->tp_src;
3505 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3507 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3508 &port_key, sizeof(port_key));
3510 } else if (flow->nw_proto == IPPROTO_UDP) {
3511 struct ovs_key_udp port_key;
3513 port_key.udp_src = base->tp_src = flow->tp_src;
3514 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3516 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3517 &port_key, sizeof(port_key));
3518 } else if (flow->nw_proto == IPPROTO_SCTP) {
3519 struct ovs_key_sctp port_key;
3521 port_key.sctp_src = base->tp_src = flow->tp_src;
3522 port_key.sctp_dst = base->tp_dst = flow->tp_dst;
3524 commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
3525 &port_key, sizeof(port_key));
3530 commit_set_priority_action(const struct flow *flow, struct flow *base,
3531 struct ofpbuf *odp_actions,
3532 struct flow_wildcards *wc)
3534 if (base->skb_priority == flow->skb_priority) {
3538 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3539 base->skb_priority = flow->skb_priority;
3541 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3542 &base->skb_priority, sizeof(base->skb_priority));
3546 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
3547 struct ofpbuf *odp_actions,
3548 struct flow_wildcards *wc)
3550 if (base->pkt_mark == flow->pkt_mark) {
3554 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
3555 base->pkt_mark = flow->pkt_mark;
3557 odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
3559 /* If any of the flow key data that ODP actions can modify are different in
3560 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3561 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3562 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3563 * in addition to this function if needed. Sets fields in 'wc' that are
3564 * used as part of the action. */
3566 commit_odp_actions(const struct flow *flow, struct flow *base,
3567 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3569 commit_set_ether_addr_action(flow, base, odp_actions, wc);
3570 commit_vlan_action(flow, base, odp_actions, wc);
3571 commit_set_nw_action(flow, base, odp_actions, wc);
3572 commit_set_port_action(flow, base, odp_actions, wc);
3573 /* Committing MPLS actions should occur after committing nw and port
3574 * actions. This is because committing MPLS actions may alter a packet so
3575 * that it is no longer IP and thus nw and port actions are no longer valid.
3577 commit_mpls_action(flow, base, odp_actions, wc);
3578 commit_set_priority_action(flow, base, odp_actions, wc);
3579 commit_set_pkt_mark_action(flow, base, odp_actions, wc);