2 * Copyright (c) 2009, 2010, 2011, 2012 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.
17 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
29 #include "dynamic-string.h"
33 #include "openvswitch/tunnel.h"
40 VLOG_DEFINE_THIS_MODULE(odp_util);
42 /* The interface between userspace and kernel uses an "OVS_*" prefix.
43 * Since this is fairly non-specific for the OVS userspace components,
44 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
45 * interactions with the datapath.
48 /* The set of characters that may separate one action or one key attribute
50 static const char *delimiters = ", \t\r\n";
52 static int parse_odp_key_attr(const char *, const struct shash *port_names,
54 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
56 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
59 * - For an action whose argument has a fixed length, returned that
60 * nonnegative length in bytes.
62 * - For an action with a variable-length argument, returns -2.
64 * - For an invalid 'type', returns -1. */
66 odp_action_len(uint16_t type)
68 if (type > OVS_ACTION_ATTR_MAX) {
72 switch ((enum ovs_action_attr) type) {
73 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
74 case OVS_ACTION_ATTR_USERSPACE: return -2;
75 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
76 case OVS_ACTION_ATTR_POP_VLAN: return 0;
77 case OVS_ACTION_ATTR_SET: return -2;
78 case OVS_ACTION_ATTR_SAMPLE: return -2;
80 case OVS_ACTION_ATTR_UNSPEC:
81 case __OVS_ACTION_ATTR_MAX:
89 ovs_key_attr_to_string(enum ovs_key_attr attr)
91 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
94 case OVS_KEY_ATTR_UNSPEC: return "unspec";
95 case OVS_KEY_ATTR_ENCAP: return "encap";
96 case OVS_KEY_ATTR_PRIORITY: return "priority";
97 case OVS_KEY_ATTR_IN_PORT: return "in_port";
98 case OVS_KEY_ATTR_ETHERNET: return "eth";
99 case OVS_KEY_ATTR_VLAN: return "vlan";
100 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
101 case OVS_KEY_ATTR_IPV4: return "ipv4";
102 case OVS_KEY_ATTR_IPV6: return "ipv6";
103 case OVS_KEY_ATTR_TCP: return "tcp";
104 case OVS_KEY_ATTR_UDP: return "udp";
105 case OVS_KEY_ATTR_ICMP: return "icmp";
106 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
107 case OVS_KEY_ATTR_ARP: return "arp";
108 case OVS_KEY_ATTR_ND: return "nd";
109 case OVS_KEY_ATTR_TUN_ID: return "tun_id";
111 case __OVS_KEY_ATTR_MAX:
113 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
114 (unsigned int) attr);
120 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
122 size_t len = nl_attr_get_size(a);
124 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
126 const uint8_t *unspec;
129 unspec = nl_attr_get(a);
130 for (i = 0; i < len; i++) {
131 ds_put_char(ds, i ? ' ': '(');
132 ds_put_format(ds, "%02x", unspec[i]);
134 ds_put_char(ds, ')');
139 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
141 static const struct nl_policy ovs_sample_policy[] = {
142 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
143 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
145 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
147 const struct nlattr *nla_acts;
150 ds_put_cstr(ds, "sample");
152 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
153 ds_put_cstr(ds, "(error)");
157 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
160 ds_put_format(ds, "(sample=%.1f%%,", percentage);
162 ds_put_cstr(ds, "actions(");
163 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
164 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
165 format_odp_actions(ds, nla_acts, len);
166 ds_put_format(ds, "))");
170 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
172 static const struct nl_policy ovs_userspace_policy[] = {
173 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
174 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
176 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
178 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
179 ds_put_cstr(ds, "userspace(error)");
183 ds_put_format(ds, "userspace(pid=%"PRIu32,
184 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
186 if (a[OVS_USERSPACE_ATTR_USERDATA]) {
187 uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
188 struct user_action_cookie cookie;
190 memcpy(&cookie, &userdata, sizeof cookie);
192 if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
193 ds_put_format(ds, ",sFlow,n_output=%"PRIu8","
194 "vid=%"PRIu16",pcp=%"PRIu8",ifindex=%"PRIu32,
195 cookie.n_output, vlan_tci_to_vid(cookie.vlan_tci),
196 vlan_tci_to_pcp(cookie.vlan_tci), cookie.data);
198 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
202 ds_put_char(ds, ')');
206 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
208 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
209 vlan_tci_to_vid(vlan_tci),
210 vlan_tci_to_pcp(vlan_tci));
211 if (!(vlan_tci & htons(VLAN_CFI))) {
212 ds_put_cstr(ds, ",cfi=0");
217 format_odp_action(struct ds *ds, const struct nlattr *a)
220 enum ovs_action_attr type = nl_attr_type(a);
221 const struct ovs_action_push_vlan *vlan;
223 expected_len = odp_action_len(nl_attr_type(a));
224 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
225 ds_put_format(ds, "bad length %zu, expected %d for: ",
226 nl_attr_get_size(a), expected_len);
227 format_generic_odp_action(ds, a);
232 case OVS_ACTION_ATTR_OUTPUT:
233 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
235 case OVS_ACTION_ATTR_USERSPACE:
236 format_odp_userspace_action(ds, a);
238 case OVS_ACTION_ATTR_SET:
239 ds_put_cstr(ds, "set(");
240 format_odp_key_attr(nl_attr_get(a), ds);
241 ds_put_cstr(ds, ")");
243 case OVS_ACTION_ATTR_PUSH_VLAN:
244 vlan = nl_attr_get(a);
245 ds_put_cstr(ds, "push_vlan(");
246 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
247 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
249 format_vlan_tci(ds, vlan->vlan_tci);
250 ds_put_char(ds, ')');
252 case OVS_ACTION_ATTR_POP_VLAN:
253 ds_put_cstr(ds, "pop_vlan");
255 case OVS_ACTION_ATTR_SAMPLE:
256 format_odp_sample_action(ds, a);
258 case OVS_ACTION_ATTR_UNSPEC:
259 case __OVS_ACTION_ATTR_MAX:
261 format_generic_odp_action(ds, a);
267 format_odp_actions(struct ds *ds, const struct nlattr *actions,
271 const struct nlattr *a;
274 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
276 ds_put_char(ds, ',');
278 format_odp_action(ds, a);
283 if (left == actions_len) {
284 ds_put_cstr(ds, "<empty>");
286 ds_put_format(ds, ",***%u leftover bytes*** (", left);
287 for (i = 0; i < left; i++) {
288 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
290 ds_put_char(ds, ')');
293 ds_put_cstr(ds, "drop");
298 parse_odp_action(const char *s, const struct shash *port_names,
299 struct ofpbuf *actions)
301 /* Many of the sscanf calls in this function use oversized destination
302 * fields because some sscanf() implementations truncate the range of %i
303 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
304 * value of 0x7fff. The other alternatives are to allow only a single
305 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
308 * The tun_id parser has to use an alternative approach because there is no
309 * type larger than 64 bits. */
312 unsigned long long int port;
315 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
316 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
322 int len = strcspn(s, delimiters);
323 struct shash_node *node;
325 node = shash_find_len(port_names, s, len);
327 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT,
328 (uintptr_t) node->data);
334 unsigned long long int pid;
335 unsigned long long int ifindex;
341 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
342 odp_put_userspace_action(pid, NULL, actions);
344 } else if (sscanf(s, "userspace(pid=%lli,sFlow,n_output=%i,vid=%i,"
345 "pcp=%i,ifindex=%lli)%n", &pid, &n_output,
346 &vid, &pcp, &ifindex, &n) > 0 && n > 0) {
347 struct user_action_cookie cookie;
350 tci = vid | (pcp << VLAN_PCP_SHIFT);
355 cookie.type = USER_ACTION_COOKIE_SFLOW;
356 cookie.n_output = n_output;
357 cookie.vlan_tci = htons(tci);
358 cookie.data = ifindex;
359 odp_put_userspace_action(pid, &cookie, actions);
361 } else if (sscanf(s, "userspace(pid=%lli,userdata="
362 "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
364 struct user_action_cookie cookie;
367 userdata = strtoull(userdata_s, NULL, 0);
368 memcpy(&cookie, &userdata, sizeof cookie);
369 odp_put_userspace_action(pid, &cookie, actions);
374 if (!strncmp(s, "set(", 4)) {
378 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
379 retval = parse_odp_key_attr(s + 4, port_names, actions);
383 if (s[retval + 4] != ')') {
386 nl_msg_end_nested(actions, start_ofs);
391 struct ovs_action_push_vlan push;
392 int tpid = ETH_TYPE_VLAN;
397 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
399 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
400 &vid, &pcp, &cfi, &n) > 0 && n > 0)
401 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
402 &tpid, &vid, &pcp, &n) > 0 && n > 0)
403 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
404 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
405 push.vlan_tpid = htons(tpid);
406 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
407 | (pcp << VLAN_PCP_SHIFT)
408 | (cfi ? VLAN_CFI : 0));
409 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
416 if (!strncmp(s, "pop_vlan", 8)) {
417 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
425 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
426 && percentage >= 0. && percentage <= 100.0
428 size_t sample_ofs, actions_ofs;
431 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
432 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
433 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
434 (probability <= 0 ? 0
435 : probability >= UINT32_MAX ? UINT32_MAX
438 actions_ofs = nl_msg_start_nested(actions,
439 OVS_SAMPLE_ATTR_ACTIONS);
443 s += strspn(s, delimiters);
448 retval = parse_odp_action(s + n, port_names, actions);
455 nl_msg_end_nested(actions, actions_ofs);
456 nl_msg_end_nested(actions, sample_ofs);
458 return s[n + 1] == ')' ? n + 2 : -EINVAL;
465 /* Parses the string representation of datapath actions, in the format output
466 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
467 * value. On success, the ODP actions are appended to 'actions' as a series of
468 * Netlink attributes. On failure, no data is appended to 'actions'. Either
469 * way, 'actions''s data might be reallocated. */
471 odp_actions_from_string(const char *s, const struct shash *port_names,
472 struct ofpbuf *actions)
476 if (!strcasecmp(s, "drop")) {
480 old_size = actions->size;
484 s += strspn(s, delimiters);
489 retval = parse_odp_action(s, port_names, actions);
490 if (retval < 0 || !strchr(delimiters, s[retval])) {
491 actions->size = old_size;
500 /* Returns the correct length of the payload for a flow key attribute of the
501 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
502 * is variable length. */
504 odp_flow_key_attr_len(uint16_t type)
506 if (type > OVS_KEY_ATTR_MAX) {
510 switch ((enum ovs_key_attr) type) {
511 case OVS_KEY_ATTR_ENCAP: return -2;
512 case OVS_KEY_ATTR_PRIORITY: return 4;
513 case OVS_KEY_ATTR_TUN_ID: return 8;
514 case OVS_KEY_ATTR_IN_PORT: return 4;
515 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
516 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
517 case OVS_KEY_ATTR_ETHERTYPE: return 2;
518 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
519 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
520 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
521 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
522 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
523 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
524 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
525 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
527 case OVS_KEY_ATTR_UNSPEC:
528 case __OVS_KEY_ATTR_MAX:
536 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
538 size_t len = nl_attr_get_size(a);
540 const uint8_t *unspec;
543 unspec = nl_attr_get(a);
544 for (i = 0; i < len; i++) {
545 ds_put_char(ds, i ? ' ': '(');
546 ds_put_format(ds, "%02x", unspec[i]);
548 ds_put_char(ds, ')');
553 ovs_frag_type_to_string(enum ovs_frag_type type)
556 case OVS_FRAG_TYPE_NONE:
558 case OVS_FRAG_TYPE_FIRST:
560 case OVS_FRAG_TYPE_LATER:
562 case __OVS_FRAG_TYPE_MAX:
569 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
571 const struct ovs_key_ethernet *eth_key;
572 const struct ovs_key_ipv4 *ipv4_key;
573 const struct ovs_key_ipv6 *ipv6_key;
574 const struct ovs_key_tcp *tcp_key;
575 const struct ovs_key_udp *udp_key;
576 const struct ovs_key_icmp *icmp_key;
577 const struct ovs_key_icmpv6 *icmpv6_key;
578 const struct ovs_key_arp *arp_key;
579 const struct ovs_key_nd *nd_key;
580 enum ovs_key_attr attr = nl_attr_type(a);
583 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
584 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
585 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
586 ds_put_format(ds, "(bad length %zu, expected %d)",
588 odp_flow_key_attr_len(nl_attr_type(a)));
589 format_generic_odp_key(a, ds);
594 case OVS_KEY_ATTR_ENCAP:
595 ds_put_cstr(ds, "(");
596 if (nl_attr_get_size(a)) {
597 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
599 ds_put_char(ds, ')');
602 case OVS_KEY_ATTR_PRIORITY:
603 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
606 case OVS_KEY_ATTR_TUN_ID:
607 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
610 case OVS_KEY_ATTR_IN_PORT:
611 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
614 case OVS_KEY_ATTR_ETHERNET:
615 eth_key = nl_attr_get(a);
616 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
617 ETH_ADDR_ARGS(eth_key->eth_src),
618 ETH_ADDR_ARGS(eth_key->eth_dst));
621 case OVS_KEY_ATTR_VLAN:
622 ds_put_char(ds, '(');
623 format_vlan_tci(ds, nl_attr_get_be16(a));
624 ds_put_char(ds, ')');
627 case OVS_KEY_ATTR_ETHERTYPE:
628 ds_put_format(ds, "(0x%04"PRIx16")",
629 ntohs(nl_attr_get_be16(a)));
632 case OVS_KEY_ATTR_IPV4:
633 ipv4_key = nl_attr_get(a);
634 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
635 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
636 IP_ARGS(&ipv4_key->ipv4_src),
637 IP_ARGS(&ipv4_key->ipv4_dst),
638 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
640 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
643 case OVS_KEY_ATTR_IPV6: {
644 char src_str[INET6_ADDRSTRLEN];
645 char dst_str[INET6_ADDRSTRLEN];
647 ipv6_key = nl_attr_get(a);
648 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
649 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
651 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
652 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
653 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
654 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
655 ipv6_key->ipv6_hlimit,
656 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
660 case OVS_KEY_ATTR_TCP:
661 tcp_key = nl_attr_get(a);
662 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
663 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
666 case OVS_KEY_ATTR_UDP:
667 udp_key = nl_attr_get(a);
668 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
669 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
672 case OVS_KEY_ATTR_ICMP:
673 icmp_key = nl_attr_get(a);
674 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
675 icmp_key->icmp_type, icmp_key->icmp_code);
678 case OVS_KEY_ATTR_ICMPV6:
679 icmpv6_key = nl_attr_get(a);
680 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
681 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
684 case OVS_KEY_ATTR_ARP:
685 arp_key = nl_attr_get(a);
686 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
687 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
688 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
689 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
690 ETH_ADDR_ARGS(arp_key->arp_tha));
693 case OVS_KEY_ATTR_ND: {
694 char target[INET6_ADDRSTRLEN];
696 nd_key = nl_attr_get(a);
697 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
699 ds_put_format(ds, "(target=%s", target);
700 if (!eth_addr_is_zero(nd_key->nd_sll)) {
701 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
702 ETH_ADDR_ARGS(nd_key->nd_sll));
704 if (!eth_addr_is_zero(nd_key->nd_tll)) {
705 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
706 ETH_ADDR_ARGS(nd_key->nd_tll));
708 ds_put_char(ds, ')');
712 case OVS_KEY_ATTR_UNSPEC:
713 case __OVS_KEY_ATTR_MAX:
715 format_generic_odp_key(a, ds);
720 /* Appends to 'ds' a string representation of the 'key_len' bytes of
721 * OVS_KEY_ATTR_* attributes in 'key'. */
723 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
726 const struct nlattr *a;
729 NL_ATTR_FOR_EACH (a, left, key, key_len) {
731 ds_put_char(ds, ',');
733 format_odp_key_attr(a, ds);
738 if (left == key_len) {
739 ds_put_cstr(ds, "<empty>");
741 ds_put_format(ds, ",***%u leftover bytes*** (", left);
742 for (i = 0; i < left; i++) {
743 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
745 ds_put_char(ds, ')');
748 ds_put_cstr(ds, "<empty>");
753 put_nd_key(int n, const char *nd_target_s,
754 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
756 struct ovs_key_nd nd_key;
758 memset(&nd_key, 0, sizeof nd_key);
759 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
763 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
766 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
768 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
773 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
775 if (!strcasecmp(s, "no")) {
776 *type = OVS_FRAG_TYPE_NONE;
777 } else if (!strcasecmp(s, "first")) {
778 *type = OVS_FRAG_TYPE_FIRST;
779 } else if (!strcasecmp(s, "later")) {
780 *type = OVS_FRAG_TYPE_LATER;
788 parse_odp_key_attr(const char *s, const struct shash *port_names,
791 /* Many of the sscanf calls in this function use oversized destination
792 * fields because some sscanf() implementations truncate the range of %i
793 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
794 * value of 0x7fff. The other alternatives are to allow only a single
795 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
798 * The tun_id parser has to use an alternative approach because there is no
799 * type larger than 64 bits. */
802 unsigned long long int priority;
805 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
806 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
815 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
816 tun_id_s, &n) > 0 && n > 0) {
817 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
818 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
824 unsigned long long int in_port;
827 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
828 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
833 if (port_names && !strncmp(s, "in_port(", 8)) {
835 const struct shash_node *node;
839 name_len = strcspn(s, ")");
840 node = shash_find_len(port_names, name, name_len);
842 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, (uintptr_t) node->data);
843 return 8 + name_len + 1;
848 struct ovs_key_ethernet eth_key;
852 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
853 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
854 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
855 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
856 ð_key, sizeof eth_key);
867 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
869 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
870 htons((vid << VLAN_VID_SHIFT) |
871 (pcp << VLAN_PCP_SHIFT) |
874 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
875 &vid, &pcp, &cfi, &n) > 0
877 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
878 htons((vid << VLAN_VID_SHIFT) |
879 (pcp << VLAN_PCP_SHIFT) |
880 (cfi ? VLAN_CFI : 0)));
889 if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
890 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
902 enum ovs_frag_type ipv4_frag;
905 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
906 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
907 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
908 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
910 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
911 struct ovs_key_ipv4 ipv4_key;
913 ipv4_key.ipv4_src = ipv4_src;
914 ipv4_key.ipv4_dst = ipv4_dst;
915 ipv4_key.ipv4_proto = ipv4_proto;
916 ipv4_key.ipv4_tos = ipv4_tos;
917 ipv4_key.ipv4_ttl = ipv4_ttl;
918 ipv4_key.ipv4_frag = ipv4_frag;
919 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
920 &ipv4_key, sizeof ipv4_key);
926 char ipv6_src_s[IPV6_SCAN_LEN + 1];
927 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
933 enum ovs_frag_type ipv6_frag;
936 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
937 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
938 ipv6_src_s, ipv6_dst_s, &ipv6_label,
939 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
941 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
942 struct ovs_key_ipv6 ipv6_key;
944 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
945 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
948 ipv6_key.ipv6_label = htonl(ipv6_label);
949 ipv6_key.ipv6_proto = ipv6_proto;
950 ipv6_key.ipv6_tclass = ipv6_tclass;
951 ipv6_key.ipv6_hlimit = ipv6_hlimit;
952 ipv6_key.ipv6_frag = ipv6_frag;
953 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
954 &ipv6_key, sizeof ipv6_key);
964 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
966 struct ovs_key_tcp tcp_key;
968 tcp_key.tcp_src = htons(tcp_src);
969 tcp_key.tcp_dst = htons(tcp_dst);
970 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
980 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
982 struct ovs_key_udp udp_key;
984 udp_key.udp_src = htons(udp_src);
985 udp_key.udp_dst = htons(udp_dst);
986 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
996 if (sscanf(s, "icmp(type=%i,code=%i)%n",
997 &icmp_type, &icmp_code, &n) > 0
999 struct ovs_key_icmp icmp_key;
1001 icmp_key.icmp_type = icmp_type;
1002 icmp_key.icmp_code = icmp_code;
1003 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1004 &icmp_key, sizeof icmp_key);
1010 struct ovs_key_icmpv6 icmpv6_key;
1013 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1014 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1016 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1017 &icmpv6_key, sizeof icmpv6_key);
1026 uint8_t arp_sha[ETH_ADDR_LEN];
1027 uint8_t arp_tha[ETH_ADDR_LEN];
1030 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1031 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1032 IP_SCAN_ARGS(&arp_sip),
1033 IP_SCAN_ARGS(&arp_tip),
1035 ETH_ADDR_SCAN_ARGS(arp_sha),
1036 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1037 struct ovs_key_arp arp_key;
1039 memset(&arp_key, 0, sizeof arp_key);
1040 arp_key.arp_sip = arp_sip;
1041 arp_key.arp_tip = arp_tip;
1042 arp_key.arp_op = htons(arp_op);
1043 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1044 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1045 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1051 char nd_target_s[IPV6_SCAN_LEN + 1];
1052 uint8_t nd_sll[ETH_ADDR_LEN];
1053 uint8_t nd_tll[ETH_ADDR_LEN];
1056 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1057 nd_target_s, &n) > 0 && n > 0) {
1058 return put_nd_key(n, nd_target_s, NULL, NULL, key);
1060 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1061 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1063 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1065 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1066 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1068 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1070 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1071 "tll="ETH_ADDR_SCAN_FMT")%n",
1072 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1073 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1075 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1079 if (!strncmp(s, "encap(", 6)) {
1080 const char *start = s;
1083 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1089 s += strspn(s, ", \t\r\n");
1092 } else if (*s == ')') {
1096 retval = parse_odp_key_attr(s, port_names, key);
1104 nl_msg_end_nested(key, encap);
1112 /* Parses the string representation of a datapath flow key, in the
1113 * format output by odp_flow_key_format(). Returns 0 if successful,
1114 * otherwise a positive errno value. On success, the flow key is
1115 * appended to 'key' as a series of Netlink attributes. On failure, no
1116 * data is appended to 'key'. Either way, 'key''s data might be
1119 * If 'port_names' is nonnull, it points to an shash that maps from a port name
1120 * to a port number cast to void *. (Port names may be used instead of port
1121 * numbers in in_port.)
1123 * On success, the attributes appended to 'key' are individually syntactically
1124 * valid, but they may not be valid as a sequence. 'key' might, for example,
1125 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1127 odp_flow_key_from_string(const char *s, const struct shash *port_names,
1130 const size_t old_size = key->size;
1134 s += strspn(s, delimiters);
1139 retval = parse_odp_key_attr(s, port_names, key);
1141 key->size = old_size;
1151 ovs_to_odp_frag(uint8_t nw_frag)
1153 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1154 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1155 : OVS_FRAG_TYPE_LATER);
1158 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
1160 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
1162 struct ovs_key_ethernet *eth_key;
1165 if (flow->skb_priority) {
1166 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1169 if (flow->tun_id != htonll(0)) {
1170 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tun_id);
1173 if (flow->in_port != OFPP_NONE) {
1174 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
1175 ofp_port_to_odp_port(flow->in_port));
1178 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1180 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1181 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1183 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1184 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1185 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1186 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1187 if (flow->vlan_tci == htons(0)) {
1194 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1198 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1200 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1201 struct ovs_key_ipv4 *ipv4_key;
1203 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1205 ipv4_key->ipv4_src = flow->nw_src;
1206 ipv4_key->ipv4_dst = flow->nw_dst;
1207 ipv4_key->ipv4_proto = flow->nw_proto;
1208 ipv4_key->ipv4_tos = flow->nw_tos;
1209 ipv4_key->ipv4_ttl = flow->nw_ttl;
1210 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1211 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1212 struct ovs_key_ipv6 *ipv6_key;
1214 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1216 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1217 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1218 ipv6_key->ipv6_label = flow->ipv6_label;
1219 ipv6_key->ipv6_proto = flow->nw_proto;
1220 ipv6_key->ipv6_tclass = flow->nw_tos;
1221 ipv6_key->ipv6_hlimit = flow->nw_ttl;
1222 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1223 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1224 struct ovs_key_arp *arp_key;
1226 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1228 memset(arp_key, 0, sizeof *arp_key);
1229 arp_key->arp_sip = flow->nw_src;
1230 arp_key->arp_tip = flow->nw_dst;
1231 arp_key->arp_op = htons(flow->nw_proto);
1232 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1233 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1236 if ((flow->dl_type == htons(ETH_TYPE_IP)
1237 || flow->dl_type == htons(ETH_TYPE_IPV6))
1238 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1240 if (flow->nw_proto == IPPROTO_TCP) {
1241 struct ovs_key_tcp *tcp_key;
1243 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1245 tcp_key->tcp_src = flow->tp_src;
1246 tcp_key->tcp_dst = flow->tp_dst;
1247 } else if (flow->nw_proto == IPPROTO_UDP) {
1248 struct ovs_key_udp *udp_key;
1250 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1252 udp_key->udp_src = flow->tp_src;
1253 udp_key->udp_dst = flow->tp_dst;
1254 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1255 && flow->nw_proto == IPPROTO_ICMP) {
1256 struct ovs_key_icmp *icmp_key;
1258 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1260 icmp_key->icmp_type = ntohs(flow->tp_src);
1261 icmp_key->icmp_code = ntohs(flow->tp_dst);
1262 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1263 && flow->nw_proto == IPPROTO_ICMPV6) {
1264 struct ovs_key_icmpv6 *icmpv6_key;
1266 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1267 sizeof *icmpv6_key);
1268 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1269 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1271 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1272 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1273 struct ovs_key_nd *nd_key;
1275 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1277 memcpy(nd_key->nd_target, &flow->nd_target,
1278 sizeof nd_key->nd_target);
1279 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1280 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1287 nl_msg_end_nested(buf, encap);
1292 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1294 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1295 return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1299 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1300 uint64_t attrs, int out_of_range_attr,
1301 const struct nlattr *key, size_t key_len)
1306 if (VLOG_DROP_DBG(rl)) {
1311 for (i = 0; i < 64; i++) {
1312 if (attrs & (UINT64_C(1) << i)) {
1313 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1316 if (out_of_range_attr) {
1317 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1320 ds_put_cstr(&s, ": ");
1321 odp_flow_key_format(key, key_len, &s);
1323 VLOG_DBG("%s:%s", title, ds_cstr(&s));
1328 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1330 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1332 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1333 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1337 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1338 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1339 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1340 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1347 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1348 const struct nlattr *attrs[], uint64_t *present_attrsp,
1349 int *out_of_range_attrp)
1351 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1352 const struct nlattr *nla;
1353 uint64_t present_attrs;
1357 *out_of_range_attrp = 0;
1358 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1359 uint16_t type = nl_attr_type(nla);
1360 size_t len = nl_attr_get_size(nla);
1361 int expected_len = odp_flow_key_attr_len(type);
1363 if (len != expected_len && expected_len >= 0) {
1364 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1365 "length %d", ovs_key_attr_to_string(type),
1370 if (type >= CHAR_BIT * sizeof present_attrs) {
1371 *out_of_range_attrp = type;
1373 if (present_attrs & (UINT64_C(1) << type)) {
1374 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1375 ovs_key_attr_to_string(type));
1379 present_attrs |= UINT64_C(1) << type;
1384 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1388 *present_attrsp = present_attrs;
1392 static enum odp_key_fitness
1393 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1394 uint64_t expected_attrs,
1395 const struct nlattr *key, size_t key_len)
1397 uint64_t missing_attrs;
1398 uint64_t extra_attrs;
1400 missing_attrs = expected_attrs & ~present_attrs;
1401 if (missing_attrs) {
1402 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1403 log_odp_key_attributes(&rl, "expected but not present",
1404 missing_attrs, 0, key, key_len);
1405 return ODP_FIT_TOO_LITTLE;
1408 extra_attrs = present_attrs & ~expected_attrs;
1409 if (extra_attrs || out_of_range_attr) {
1410 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1411 log_odp_key_attributes(&rl, "present but not expected",
1412 extra_attrs, out_of_range_attr, key, key_len);
1413 return ODP_FIT_TOO_MUCH;
1416 return ODP_FIT_PERFECT;
1420 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1421 uint64_t present_attrs, uint64_t *expected_attrs,
1424 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1426 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1427 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1428 if (ntohs(flow->dl_type) < 1536) {
1429 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1430 ntohs(flow->dl_type));
1433 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1435 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1440 static enum odp_key_fitness
1441 parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1442 uint64_t present_attrs, int out_of_range_attr,
1443 uint64_t expected_attrs, struct flow *flow,
1444 const struct nlattr *key, size_t key_len)
1446 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1448 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1449 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1450 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1451 const struct ovs_key_ipv4 *ipv4_key;
1453 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1454 flow->nw_src = ipv4_key->ipv4_src;
1455 flow->nw_dst = ipv4_key->ipv4_dst;
1456 flow->nw_proto = ipv4_key->ipv4_proto;
1457 flow->nw_tos = ipv4_key->ipv4_tos;
1458 flow->nw_ttl = ipv4_key->ipv4_ttl;
1459 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1460 return ODP_FIT_ERROR;
1463 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1464 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1465 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1466 const struct ovs_key_ipv6 *ipv6_key;
1468 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1469 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1470 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1471 flow->ipv6_label = ipv6_key->ipv6_label;
1472 flow->nw_proto = ipv6_key->ipv6_proto;
1473 flow->nw_tos = ipv6_key->ipv6_tclass;
1474 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1475 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1476 return ODP_FIT_ERROR;
1479 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1480 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1481 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1482 const struct ovs_key_arp *arp_key;
1484 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1485 flow->nw_src = arp_key->arp_sip;
1486 flow->nw_dst = arp_key->arp_tip;
1487 if (arp_key->arp_op & htons(0xff00)) {
1488 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1489 "key", ntohs(arp_key->arp_op));
1490 return ODP_FIT_ERROR;
1492 flow->nw_proto = ntohs(arp_key->arp_op);
1493 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1494 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1498 if (flow->nw_proto == IPPROTO_TCP
1499 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1500 flow->dl_type == htons(ETH_TYPE_IPV6))
1501 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1502 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1503 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1504 const struct ovs_key_tcp *tcp_key;
1506 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1507 flow->tp_src = tcp_key->tcp_src;
1508 flow->tp_dst = tcp_key->tcp_dst;
1510 } else if (flow->nw_proto == IPPROTO_UDP
1511 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1512 flow->dl_type == htons(ETH_TYPE_IPV6))
1513 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1514 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1515 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1516 const struct ovs_key_udp *udp_key;
1518 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1519 flow->tp_src = udp_key->udp_src;
1520 flow->tp_dst = udp_key->udp_dst;
1522 } else if (flow->nw_proto == IPPROTO_ICMP
1523 && flow->dl_type == htons(ETH_TYPE_IP)
1524 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1525 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1526 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1527 const struct ovs_key_icmp *icmp_key;
1529 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1530 flow->tp_src = htons(icmp_key->icmp_type);
1531 flow->tp_dst = htons(icmp_key->icmp_code);
1533 } else if (flow->nw_proto == IPPROTO_ICMPV6
1534 && flow->dl_type == htons(ETH_TYPE_IPV6)
1535 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1536 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1537 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1538 const struct ovs_key_icmpv6 *icmpv6_key;
1540 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1541 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1542 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1544 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1545 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1546 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1547 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1548 const struct ovs_key_nd *nd_key;
1550 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1551 memcpy(&flow->nd_target, nd_key->nd_target,
1552 sizeof flow->nd_target);
1553 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1554 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1560 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1564 /* Parse 802.1Q header then encapsulated L3 attributes. */
1565 static enum odp_key_fitness
1566 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1567 uint64_t present_attrs, int out_of_range_attr,
1568 uint64_t expected_attrs, struct flow *flow,
1569 const struct nlattr *key, size_t key_len)
1571 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1573 const struct nlattr *encap
1574 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1575 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1576 enum odp_key_fitness encap_fitness;
1577 enum odp_key_fitness fitness;
1580 /* Calulate fitness of outer attributes. */
1581 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1582 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1583 fitness = check_expectations(present_attrs, out_of_range_attr,
1584 expected_attrs, key, key_len);
1586 /* Get the VLAN TCI value. */
1587 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1588 return ODP_FIT_TOO_LITTLE;
1590 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1591 if (tci == htons(0)) {
1592 /* Corner case for a truncated 802.1Q header. */
1593 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1594 return ODP_FIT_TOO_MUCH;
1597 } else if (!(tci & htons(VLAN_CFI))) {
1598 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1599 "but CFI bit is not set", ntohs(tci));
1600 return ODP_FIT_ERROR;
1604 * Remove the TPID from dl_type since it's not the real Ethertype. */
1605 flow->vlan_tci = tci;
1606 flow->dl_type = htons(0);
1608 /* Now parse the encapsulated attributes. */
1609 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1610 attrs, &present_attrs, &out_of_range_attr)) {
1611 return ODP_FIT_ERROR;
1615 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1616 return ODP_FIT_ERROR;
1618 encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1619 expected_attrs, flow, key, key_len);
1621 /* The overall fitness is the worse of the outer and inner attributes. */
1622 return MAX(fitness, encap_fitness);
1625 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1626 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1627 * 'key' fits our expectations for what a flow key should contain.
1629 * This function doesn't take the packet itself as an argument because none of
1630 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
1631 * it is always possible to infer which additional attribute(s) should appear
1632 * by looking at the attributes for lower-level protocols, e.g. if the network
1633 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1634 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1635 * must be absent. */
1636 enum odp_key_fitness
1637 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1640 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1641 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1642 uint64_t expected_attrs;
1643 uint64_t present_attrs;
1644 int out_of_range_attr;
1646 memset(flow, 0, sizeof *flow);
1648 /* Parse attributes. */
1649 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1650 &out_of_range_attr)) {
1651 return ODP_FIT_ERROR;
1656 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1657 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1658 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1661 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1662 flow->tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1663 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1666 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1667 uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1668 if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1669 VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1671 return ODP_FIT_ERROR;
1673 flow->in_port = odp_port_to_ofp_port(in_port);
1674 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1676 flow->in_port = OFPP_NONE;
1679 /* Ethernet header. */
1680 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1681 const struct ovs_key_ethernet *eth_key;
1683 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1684 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1685 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1687 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1689 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1690 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1691 return ODP_FIT_ERROR;
1694 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1695 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1696 expected_attrs, flow, key, key_len);
1698 return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1699 expected_attrs, flow, key, key_len);
1702 /* Returns 'fitness' as a string, for use in debug messages. */
1704 odp_key_fitness_to_string(enum odp_key_fitness fitness)
1707 case ODP_FIT_PERFECT:
1709 case ODP_FIT_TOO_MUCH:
1711 case ODP_FIT_TOO_LITTLE:
1712 return "too_little";
1720 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1721 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1722 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1723 * the start of the cookie. (If 'cookie' is null, then the return value is not
1726 odp_put_userspace_action(uint32_t pid, const struct user_action_cookie *cookie,
1727 struct ofpbuf *odp_actions)
1731 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1732 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1734 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1735 cookie, sizeof *cookie);
1737 nl_msg_end_nested(odp_actions, offset);
1739 return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
1742 /* The commit_odp_actions() function and its helpers. */
1745 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
1746 const void *key, size_t key_size)
1748 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
1749 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
1750 nl_msg_end_nested(odp_actions, offset);
1754 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
1755 struct ofpbuf *odp_actions)
1757 if (base->tun_id == flow->tun_id) {
1760 base->tun_id = flow->tun_id;
1762 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
1763 &base->tun_id, sizeof(base->tun_id));
1767 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
1768 struct ofpbuf *odp_actions)
1770 struct ovs_key_ethernet eth_key;
1772 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
1773 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
1777 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
1778 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
1780 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
1781 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
1783 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
1784 ð_key, sizeof(eth_key));
1788 commit_vlan_action(const struct flow *flow, struct flow *base,
1789 struct ofpbuf *odp_actions)
1791 if (base->vlan_tci == flow->vlan_tci) {
1795 if (base->vlan_tci & htons(VLAN_CFI)) {
1796 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
1799 if (flow->vlan_tci & htons(VLAN_CFI)) {
1800 struct ovs_action_push_vlan vlan;
1802 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
1803 vlan.vlan_tci = flow->vlan_tci;
1804 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
1805 &vlan, sizeof vlan);
1807 base->vlan_tci = flow->vlan_tci;
1811 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
1812 struct ofpbuf *odp_actions)
1814 struct ovs_key_ipv4 ipv4_key;
1816 if (base->nw_src == flow->nw_src &&
1817 base->nw_dst == flow->nw_dst &&
1818 base->nw_tos == flow->nw_tos &&
1819 base->nw_ttl == flow->nw_ttl &&
1820 base->nw_frag == flow->nw_frag) {
1824 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
1825 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
1826 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
1827 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
1828 ipv4_key.ipv4_proto = base->nw_proto;
1829 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
1831 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
1832 &ipv4_key, sizeof(ipv4_key));
1836 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
1837 struct ofpbuf *odp_actions)
1839 struct ovs_key_ipv6 ipv6_key;
1841 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
1842 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
1843 base->ipv6_label == flow->ipv6_label &&
1844 base->nw_tos == flow->nw_tos &&
1845 base->nw_ttl == flow->nw_ttl &&
1846 base->nw_frag == flow->nw_frag) {
1850 base->ipv6_src = flow->ipv6_src;
1851 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
1852 base->ipv6_dst = flow->ipv6_dst;
1853 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
1855 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
1856 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
1857 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
1858 ipv6_key.ipv6_proto = base->nw_proto;
1859 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
1861 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
1862 &ipv6_key, sizeof(ipv6_key));
1866 commit_set_nw_action(const struct flow *flow, struct flow *base,
1867 struct ofpbuf *odp_actions)
1869 /* Check if flow really have an IP header. */
1870 if (!flow->nw_proto) {
1874 if (base->dl_type == htons(ETH_TYPE_IP)) {
1875 commit_set_ipv4_action(flow, base, odp_actions);
1876 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
1877 commit_set_ipv6_action(flow, base, odp_actions);
1882 commit_set_port_action(const struct flow *flow, struct flow *base,
1883 struct ofpbuf *odp_actions)
1885 if (!base->tp_src || !base->tp_dst) {
1889 if (base->tp_src == flow->tp_src &&
1890 base->tp_dst == flow->tp_dst) {
1894 if (flow->nw_proto == IPPROTO_TCP) {
1895 struct ovs_key_tcp port_key;
1897 port_key.tcp_src = base->tp_src = flow->tp_src;
1898 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
1900 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
1901 &port_key, sizeof(port_key));
1903 } else if (flow->nw_proto == IPPROTO_UDP) {
1904 struct ovs_key_udp port_key;
1906 port_key.udp_src = base->tp_src = flow->tp_src;
1907 port_key.udp_dst = base->tp_dst = flow->tp_dst;
1909 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
1910 &port_key, sizeof(port_key));
1915 commit_set_priority_action(const struct flow *flow, struct flow *base,
1916 struct ofpbuf *odp_actions)
1918 if (base->skb_priority == flow->skb_priority) {
1921 base->skb_priority = flow->skb_priority;
1923 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
1924 &base->skb_priority, sizeof(base->skb_priority));
1927 /* If any of the flow key data that ODP actions can modify are different in
1928 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
1929 * key from 'base' into 'flow', and then changes 'base' the same way. */
1931 commit_odp_actions(const struct flow *flow, struct flow *base,
1932 struct ofpbuf *odp_actions)
1934 commit_set_tun_id_action(flow, base, odp_actions);
1935 commit_set_ether_addr_action(flow, base, odp_actions);
1936 commit_vlan_action(flow, base, odp_actions);
1937 commit_set_nw_action(flow, base, odp_actions);
1938 commit_set_port_action(flow, base, odp_actions);
1939 commit_set_priority_action(flow, base, odp_actions);