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 switch (cookie.type) {
193 case USER_ACTION_COOKIE_SFLOW:
194 ds_put_format(ds, ",sFlow,"
195 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32,
196 vlan_tci_to_vid(cookie.vlan_tci),
197 vlan_tci_to_pcp(cookie.vlan_tci), cookie.output);
200 case USER_ACTION_COOKIE_UNSPEC:
202 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
207 ds_put_char(ds, ')');
211 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
213 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
214 vlan_tci_to_vid(vlan_tci),
215 vlan_tci_to_pcp(vlan_tci));
216 if (!(vlan_tci & htons(VLAN_CFI))) {
217 ds_put_cstr(ds, ",cfi=0");
222 format_odp_action(struct ds *ds, const struct nlattr *a)
225 enum ovs_action_attr type = nl_attr_type(a);
226 const struct ovs_action_push_vlan *vlan;
228 expected_len = odp_action_len(nl_attr_type(a));
229 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
230 ds_put_format(ds, "bad length %zu, expected %d for: ",
231 nl_attr_get_size(a), expected_len);
232 format_generic_odp_action(ds, a);
237 case OVS_ACTION_ATTR_OUTPUT:
238 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
240 case OVS_ACTION_ATTR_USERSPACE:
241 format_odp_userspace_action(ds, a);
243 case OVS_ACTION_ATTR_SET:
244 ds_put_cstr(ds, "set(");
245 format_odp_key_attr(nl_attr_get(a), ds);
246 ds_put_cstr(ds, ")");
248 case OVS_ACTION_ATTR_PUSH_VLAN:
249 vlan = nl_attr_get(a);
250 ds_put_cstr(ds, "push_vlan(");
251 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
252 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
254 format_vlan_tci(ds, vlan->vlan_tci);
255 ds_put_char(ds, ')');
257 case OVS_ACTION_ATTR_POP_VLAN:
258 ds_put_cstr(ds, "pop_vlan");
260 case OVS_ACTION_ATTR_SAMPLE:
261 format_odp_sample_action(ds, a);
263 case OVS_ACTION_ATTR_UNSPEC:
264 case __OVS_ACTION_ATTR_MAX:
266 format_generic_odp_action(ds, a);
272 format_odp_actions(struct ds *ds, const struct nlattr *actions,
276 const struct nlattr *a;
279 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
281 ds_put_char(ds, ',');
283 format_odp_action(ds, a);
288 if (left == actions_len) {
289 ds_put_cstr(ds, "<empty>");
291 ds_put_format(ds, ",***%u leftover bytes*** (", left);
292 for (i = 0; i < left; i++) {
293 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
295 ds_put_char(ds, ')');
298 ds_put_cstr(ds, "drop");
303 parse_odp_action(const char *s, const struct shash *port_names,
304 struct ofpbuf *actions)
306 /* Many of the sscanf calls in this function use oversized destination
307 * fields because some sscanf() implementations truncate the range of %i
308 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
309 * value of 0x7fff. The other alternatives are to allow only a single
310 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
313 * The tun_id parser has to use an alternative approach because there is no
314 * type larger than 64 bits. */
317 unsigned long long int port;
320 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
321 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
327 int len = strcspn(s, delimiters);
328 struct shash_node *node;
330 node = shash_find_len(port_names, s, len);
332 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT,
333 (uintptr_t) node->data);
339 unsigned long long int pid;
340 unsigned long long int output;
345 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
346 odp_put_userspace_action(pid, NULL, actions);
348 } else if (sscanf(s, "userspace(pid=%lli,sFlow,vid=%i,"
349 "pcp=%i,output=%lli)%n",
350 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
351 struct user_action_cookie cookie;
354 tci = vid | (pcp << VLAN_PCP_SHIFT);
359 cookie.type = USER_ACTION_COOKIE_SFLOW;
360 cookie.vlan_tci = htons(tci);
361 cookie.output = output;
362 odp_put_userspace_action(pid, &cookie, actions);
364 } else if (sscanf(s, "userspace(pid=%lli,userdata="
365 "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
367 struct user_action_cookie cookie;
370 userdata = strtoull(userdata_s, NULL, 0);
371 memcpy(&cookie, &userdata, sizeof cookie);
372 odp_put_userspace_action(pid, &cookie, actions);
377 if (!strncmp(s, "set(", 4)) {
381 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
382 retval = parse_odp_key_attr(s + 4, port_names, actions);
386 if (s[retval + 4] != ')') {
389 nl_msg_end_nested(actions, start_ofs);
394 struct ovs_action_push_vlan push;
395 int tpid = ETH_TYPE_VLAN;
400 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
402 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
403 &vid, &pcp, &cfi, &n) > 0 && n > 0)
404 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
405 &tpid, &vid, &pcp, &n) > 0 && n > 0)
406 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
407 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
408 push.vlan_tpid = htons(tpid);
409 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
410 | (pcp << VLAN_PCP_SHIFT)
411 | (cfi ? VLAN_CFI : 0));
412 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
419 if (!strncmp(s, "pop_vlan", 8)) {
420 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
428 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
429 && percentage >= 0. && percentage <= 100.0
431 size_t sample_ofs, actions_ofs;
434 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
435 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
436 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
437 (probability <= 0 ? 0
438 : probability >= UINT32_MAX ? UINT32_MAX
441 actions_ofs = nl_msg_start_nested(actions,
442 OVS_SAMPLE_ATTR_ACTIONS);
446 s += strspn(s, delimiters);
451 retval = parse_odp_action(s + n, port_names, actions);
458 nl_msg_end_nested(actions, actions_ofs);
459 nl_msg_end_nested(actions, sample_ofs);
461 return s[n + 1] == ')' ? n + 2 : -EINVAL;
468 /* Parses the string representation of datapath actions, in the format output
469 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
470 * value. On success, the ODP actions are appended to 'actions' as a series of
471 * Netlink attributes. On failure, no data is appended to 'actions'. Either
472 * way, 'actions''s data might be reallocated. */
474 odp_actions_from_string(const char *s, const struct shash *port_names,
475 struct ofpbuf *actions)
479 if (!strcasecmp(s, "drop")) {
483 old_size = actions->size;
487 s += strspn(s, delimiters);
492 retval = parse_odp_action(s, port_names, actions);
493 if (retval < 0 || !strchr(delimiters, s[retval])) {
494 actions->size = old_size;
503 /* Returns the correct length of the payload for a flow key attribute of the
504 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
505 * is variable length. */
507 odp_flow_key_attr_len(uint16_t type)
509 if (type > OVS_KEY_ATTR_MAX) {
513 switch ((enum ovs_key_attr) type) {
514 case OVS_KEY_ATTR_ENCAP: return -2;
515 case OVS_KEY_ATTR_PRIORITY: return 4;
516 case OVS_KEY_ATTR_TUN_ID: return 8;
517 case OVS_KEY_ATTR_IN_PORT: return 4;
518 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
519 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
520 case OVS_KEY_ATTR_ETHERTYPE: return 2;
521 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
522 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
523 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
524 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
525 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
526 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
527 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
528 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
530 case OVS_KEY_ATTR_UNSPEC:
531 case __OVS_KEY_ATTR_MAX:
539 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
541 size_t len = nl_attr_get_size(a);
543 const uint8_t *unspec;
546 unspec = nl_attr_get(a);
547 for (i = 0; i < len; i++) {
548 ds_put_char(ds, i ? ' ': '(');
549 ds_put_format(ds, "%02x", unspec[i]);
551 ds_put_char(ds, ')');
556 ovs_frag_type_to_string(enum ovs_frag_type type)
559 case OVS_FRAG_TYPE_NONE:
561 case OVS_FRAG_TYPE_FIRST:
563 case OVS_FRAG_TYPE_LATER:
565 case __OVS_FRAG_TYPE_MAX:
572 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
574 const struct ovs_key_ethernet *eth_key;
575 const struct ovs_key_ipv4 *ipv4_key;
576 const struct ovs_key_ipv6 *ipv6_key;
577 const struct ovs_key_tcp *tcp_key;
578 const struct ovs_key_udp *udp_key;
579 const struct ovs_key_icmp *icmp_key;
580 const struct ovs_key_icmpv6 *icmpv6_key;
581 const struct ovs_key_arp *arp_key;
582 const struct ovs_key_nd *nd_key;
583 enum ovs_key_attr attr = nl_attr_type(a);
586 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
587 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
588 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
589 ds_put_format(ds, "(bad length %zu, expected %d)",
591 odp_flow_key_attr_len(nl_attr_type(a)));
592 format_generic_odp_key(a, ds);
597 case OVS_KEY_ATTR_ENCAP:
598 ds_put_cstr(ds, "(");
599 if (nl_attr_get_size(a)) {
600 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
602 ds_put_char(ds, ')');
605 case OVS_KEY_ATTR_PRIORITY:
606 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
609 case OVS_KEY_ATTR_TUN_ID:
610 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
613 case OVS_KEY_ATTR_IN_PORT:
614 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
617 case OVS_KEY_ATTR_ETHERNET:
618 eth_key = nl_attr_get(a);
619 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
620 ETH_ADDR_ARGS(eth_key->eth_src),
621 ETH_ADDR_ARGS(eth_key->eth_dst));
624 case OVS_KEY_ATTR_VLAN:
625 ds_put_char(ds, '(');
626 format_vlan_tci(ds, nl_attr_get_be16(a));
627 ds_put_char(ds, ')');
630 case OVS_KEY_ATTR_ETHERTYPE:
631 ds_put_format(ds, "(0x%04"PRIx16")",
632 ntohs(nl_attr_get_be16(a)));
635 case OVS_KEY_ATTR_IPV4:
636 ipv4_key = nl_attr_get(a);
637 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
638 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
639 IP_ARGS(&ipv4_key->ipv4_src),
640 IP_ARGS(&ipv4_key->ipv4_dst),
641 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
643 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
646 case OVS_KEY_ATTR_IPV6: {
647 char src_str[INET6_ADDRSTRLEN];
648 char dst_str[INET6_ADDRSTRLEN];
650 ipv6_key = nl_attr_get(a);
651 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
652 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
654 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
655 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
656 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
657 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
658 ipv6_key->ipv6_hlimit,
659 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
663 case OVS_KEY_ATTR_TCP:
664 tcp_key = nl_attr_get(a);
665 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
666 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
669 case OVS_KEY_ATTR_UDP:
670 udp_key = nl_attr_get(a);
671 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
672 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
675 case OVS_KEY_ATTR_ICMP:
676 icmp_key = nl_attr_get(a);
677 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
678 icmp_key->icmp_type, icmp_key->icmp_code);
681 case OVS_KEY_ATTR_ICMPV6:
682 icmpv6_key = nl_attr_get(a);
683 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
684 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
687 case OVS_KEY_ATTR_ARP:
688 arp_key = nl_attr_get(a);
689 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
690 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
691 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
692 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
693 ETH_ADDR_ARGS(arp_key->arp_tha));
696 case OVS_KEY_ATTR_ND: {
697 char target[INET6_ADDRSTRLEN];
699 nd_key = nl_attr_get(a);
700 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
702 ds_put_format(ds, "(target=%s", target);
703 if (!eth_addr_is_zero(nd_key->nd_sll)) {
704 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
705 ETH_ADDR_ARGS(nd_key->nd_sll));
707 if (!eth_addr_is_zero(nd_key->nd_tll)) {
708 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
709 ETH_ADDR_ARGS(nd_key->nd_tll));
711 ds_put_char(ds, ')');
715 case OVS_KEY_ATTR_UNSPEC:
716 case __OVS_KEY_ATTR_MAX:
718 format_generic_odp_key(a, ds);
723 /* Appends to 'ds' a string representation of the 'key_len' bytes of
724 * OVS_KEY_ATTR_* attributes in 'key'. */
726 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
729 const struct nlattr *a;
732 NL_ATTR_FOR_EACH (a, left, key, key_len) {
734 ds_put_char(ds, ',');
736 format_odp_key_attr(a, ds);
741 if (left == key_len) {
742 ds_put_cstr(ds, "<empty>");
744 ds_put_format(ds, ",***%u leftover bytes*** (", left);
745 for (i = 0; i < left; i++) {
746 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
748 ds_put_char(ds, ')');
751 ds_put_cstr(ds, "<empty>");
756 put_nd_key(int n, const char *nd_target_s,
757 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
759 struct ovs_key_nd nd_key;
761 memset(&nd_key, 0, sizeof nd_key);
762 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
766 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
769 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
771 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
776 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
778 if (!strcasecmp(s, "no")) {
779 *type = OVS_FRAG_TYPE_NONE;
780 } else if (!strcasecmp(s, "first")) {
781 *type = OVS_FRAG_TYPE_FIRST;
782 } else if (!strcasecmp(s, "later")) {
783 *type = OVS_FRAG_TYPE_LATER;
791 parse_odp_key_attr(const char *s, const struct shash *port_names,
794 /* Many of the sscanf calls in this function use oversized destination
795 * fields because some sscanf() implementations truncate the range of %i
796 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
797 * value of 0x7fff. The other alternatives are to allow only a single
798 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
801 * The tun_id parser has to use an alternative approach because there is no
802 * type larger than 64 bits. */
805 unsigned long long int priority;
808 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
809 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
818 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
819 tun_id_s, &n) > 0 && n > 0) {
820 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
821 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
827 unsigned long long int in_port;
830 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
831 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
836 if (port_names && !strncmp(s, "in_port(", 8)) {
838 const struct shash_node *node;
842 name_len = strcspn(s, ")");
843 node = shash_find_len(port_names, name, name_len);
845 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, (uintptr_t) node->data);
846 return 8 + name_len + 1;
851 struct ovs_key_ethernet eth_key;
855 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
856 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
857 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
858 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
859 ð_key, sizeof eth_key);
870 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
872 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
873 htons((vid << VLAN_VID_SHIFT) |
874 (pcp << VLAN_PCP_SHIFT) |
877 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
878 &vid, &pcp, &cfi, &n) > 0
880 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
881 htons((vid << VLAN_VID_SHIFT) |
882 (pcp << VLAN_PCP_SHIFT) |
883 (cfi ? VLAN_CFI : 0)));
892 if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
893 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
905 enum ovs_frag_type ipv4_frag;
908 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
909 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
910 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
911 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
913 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
914 struct ovs_key_ipv4 ipv4_key;
916 ipv4_key.ipv4_src = ipv4_src;
917 ipv4_key.ipv4_dst = ipv4_dst;
918 ipv4_key.ipv4_proto = ipv4_proto;
919 ipv4_key.ipv4_tos = ipv4_tos;
920 ipv4_key.ipv4_ttl = ipv4_ttl;
921 ipv4_key.ipv4_frag = ipv4_frag;
922 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
923 &ipv4_key, sizeof ipv4_key);
929 char ipv6_src_s[IPV6_SCAN_LEN + 1];
930 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
936 enum ovs_frag_type ipv6_frag;
939 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
940 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
941 ipv6_src_s, ipv6_dst_s, &ipv6_label,
942 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
944 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
945 struct ovs_key_ipv6 ipv6_key;
947 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
948 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
951 ipv6_key.ipv6_label = htonl(ipv6_label);
952 ipv6_key.ipv6_proto = ipv6_proto;
953 ipv6_key.ipv6_tclass = ipv6_tclass;
954 ipv6_key.ipv6_hlimit = ipv6_hlimit;
955 ipv6_key.ipv6_frag = ipv6_frag;
956 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
957 &ipv6_key, sizeof ipv6_key);
967 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
969 struct ovs_key_tcp tcp_key;
971 tcp_key.tcp_src = htons(tcp_src);
972 tcp_key.tcp_dst = htons(tcp_dst);
973 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
983 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
985 struct ovs_key_udp udp_key;
987 udp_key.udp_src = htons(udp_src);
988 udp_key.udp_dst = htons(udp_dst);
989 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
999 if (sscanf(s, "icmp(type=%i,code=%i)%n",
1000 &icmp_type, &icmp_code, &n) > 0
1002 struct ovs_key_icmp icmp_key;
1004 icmp_key.icmp_type = icmp_type;
1005 icmp_key.icmp_code = icmp_code;
1006 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1007 &icmp_key, sizeof icmp_key);
1013 struct ovs_key_icmpv6 icmpv6_key;
1016 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1017 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1019 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1020 &icmpv6_key, sizeof icmpv6_key);
1029 uint8_t arp_sha[ETH_ADDR_LEN];
1030 uint8_t arp_tha[ETH_ADDR_LEN];
1033 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1034 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1035 IP_SCAN_ARGS(&arp_sip),
1036 IP_SCAN_ARGS(&arp_tip),
1038 ETH_ADDR_SCAN_ARGS(arp_sha),
1039 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1040 struct ovs_key_arp arp_key;
1042 memset(&arp_key, 0, sizeof arp_key);
1043 arp_key.arp_sip = arp_sip;
1044 arp_key.arp_tip = arp_tip;
1045 arp_key.arp_op = htons(arp_op);
1046 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1047 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1048 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1054 char nd_target_s[IPV6_SCAN_LEN + 1];
1055 uint8_t nd_sll[ETH_ADDR_LEN];
1056 uint8_t nd_tll[ETH_ADDR_LEN];
1059 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1060 nd_target_s, &n) > 0 && n > 0) {
1061 return put_nd_key(n, nd_target_s, NULL, NULL, key);
1063 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1064 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1066 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1068 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1069 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1071 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1073 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1074 "tll="ETH_ADDR_SCAN_FMT")%n",
1075 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1076 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1078 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1082 if (!strncmp(s, "encap(", 6)) {
1083 const char *start = s;
1086 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1092 s += strspn(s, ", \t\r\n");
1095 } else if (*s == ')') {
1099 retval = parse_odp_key_attr(s, port_names, key);
1107 nl_msg_end_nested(key, encap);
1115 /* Parses the string representation of a datapath flow key, in the
1116 * format output by odp_flow_key_format(). Returns 0 if successful,
1117 * otherwise a positive errno value. On success, the flow key is
1118 * appended to 'key' as a series of Netlink attributes. On failure, no
1119 * data is appended to 'key'. Either way, 'key''s data might be
1122 * If 'port_names' is nonnull, it points to an shash that maps from a port name
1123 * to a port number cast to void *. (Port names may be used instead of port
1124 * numbers in in_port.)
1126 * On success, the attributes appended to 'key' are individually syntactically
1127 * valid, but they may not be valid as a sequence. 'key' might, for example,
1128 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1130 odp_flow_key_from_string(const char *s, const struct shash *port_names,
1133 const size_t old_size = key->size;
1137 s += strspn(s, delimiters);
1142 retval = parse_odp_key_attr(s, port_names, key);
1144 key->size = old_size;
1154 ovs_to_odp_frag(uint8_t nw_frag)
1156 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1157 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1158 : OVS_FRAG_TYPE_LATER);
1161 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
1163 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
1165 struct ovs_key_ethernet *eth_key;
1168 if (flow->skb_priority) {
1169 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1172 if (flow->tun_id != htonll(0)) {
1173 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tun_id);
1176 if (flow->in_port != OFPP_NONE && flow->in_port != OFPP_CONTROLLER) {
1177 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
1178 ofp_port_to_odp_port(flow->in_port));
1181 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1183 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1184 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1186 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1187 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1188 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1189 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1190 if (flow->vlan_tci == htons(0)) {
1197 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1201 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1203 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1204 struct ovs_key_ipv4 *ipv4_key;
1206 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1208 ipv4_key->ipv4_src = flow->nw_src;
1209 ipv4_key->ipv4_dst = flow->nw_dst;
1210 ipv4_key->ipv4_proto = flow->nw_proto;
1211 ipv4_key->ipv4_tos = flow->nw_tos;
1212 ipv4_key->ipv4_ttl = flow->nw_ttl;
1213 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1214 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1215 struct ovs_key_ipv6 *ipv6_key;
1217 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1219 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1220 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1221 ipv6_key->ipv6_label = flow->ipv6_label;
1222 ipv6_key->ipv6_proto = flow->nw_proto;
1223 ipv6_key->ipv6_tclass = flow->nw_tos;
1224 ipv6_key->ipv6_hlimit = flow->nw_ttl;
1225 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1226 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1227 struct ovs_key_arp *arp_key;
1229 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1231 memset(arp_key, 0, sizeof *arp_key);
1232 arp_key->arp_sip = flow->nw_src;
1233 arp_key->arp_tip = flow->nw_dst;
1234 arp_key->arp_op = htons(flow->nw_proto);
1235 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1236 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1239 if ((flow->dl_type == htons(ETH_TYPE_IP)
1240 || flow->dl_type == htons(ETH_TYPE_IPV6))
1241 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1243 if (flow->nw_proto == IPPROTO_TCP) {
1244 struct ovs_key_tcp *tcp_key;
1246 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1248 tcp_key->tcp_src = flow->tp_src;
1249 tcp_key->tcp_dst = flow->tp_dst;
1250 } else if (flow->nw_proto == IPPROTO_UDP) {
1251 struct ovs_key_udp *udp_key;
1253 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1255 udp_key->udp_src = flow->tp_src;
1256 udp_key->udp_dst = flow->tp_dst;
1257 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1258 && flow->nw_proto == IPPROTO_ICMP) {
1259 struct ovs_key_icmp *icmp_key;
1261 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1263 icmp_key->icmp_type = ntohs(flow->tp_src);
1264 icmp_key->icmp_code = ntohs(flow->tp_dst);
1265 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1266 && flow->nw_proto == IPPROTO_ICMPV6) {
1267 struct ovs_key_icmpv6 *icmpv6_key;
1269 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1270 sizeof *icmpv6_key);
1271 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1272 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1274 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1275 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1276 struct ovs_key_nd *nd_key;
1278 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1280 memcpy(nd_key->nd_target, &flow->nd_target,
1281 sizeof nd_key->nd_target);
1282 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1283 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1290 nl_msg_end_nested(buf, encap);
1295 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1297 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1298 return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1302 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1303 uint64_t attrs, int out_of_range_attr,
1304 const struct nlattr *key, size_t key_len)
1309 if (VLOG_DROP_DBG(rl)) {
1314 for (i = 0; i < 64; i++) {
1315 if (attrs & (UINT64_C(1) << i)) {
1316 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1319 if (out_of_range_attr) {
1320 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1323 ds_put_cstr(&s, ": ");
1324 odp_flow_key_format(key, key_len, &s);
1326 VLOG_DBG("%s:%s", title, ds_cstr(&s));
1331 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1333 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1335 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1336 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1340 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1341 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1342 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1343 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1350 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1351 const struct nlattr *attrs[], uint64_t *present_attrsp,
1352 int *out_of_range_attrp)
1354 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1355 const struct nlattr *nla;
1356 uint64_t present_attrs;
1360 *out_of_range_attrp = 0;
1361 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1362 uint16_t type = nl_attr_type(nla);
1363 size_t len = nl_attr_get_size(nla);
1364 int expected_len = odp_flow_key_attr_len(type);
1366 if (len != expected_len && expected_len >= 0) {
1367 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1368 "length %d", ovs_key_attr_to_string(type),
1373 if (type >= CHAR_BIT * sizeof present_attrs) {
1374 *out_of_range_attrp = type;
1376 if (present_attrs & (UINT64_C(1) << type)) {
1377 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1378 ovs_key_attr_to_string(type));
1382 present_attrs |= UINT64_C(1) << type;
1387 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1391 *present_attrsp = present_attrs;
1395 static enum odp_key_fitness
1396 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1397 uint64_t expected_attrs,
1398 const struct nlattr *key, size_t key_len)
1400 uint64_t missing_attrs;
1401 uint64_t extra_attrs;
1403 missing_attrs = expected_attrs & ~present_attrs;
1404 if (missing_attrs) {
1405 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1406 log_odp_key_attributes(&rl, "expected but not present",
1407 missing_attrs, 0, key, key_len);
1408 return ODP_FIT_TOO_LITTLE;
1411 extra_attrs = present_attrs & ~expected_attrs;
1412 if (extra_attrs || out_of_range_attr) {
1413 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1414 log_odp_key_attributes(&rl, "present but not expected",
1415 extra_attrs, out_of_range_attr, key, key_len);
1416 return ODP_FIT_TOO_MUCH;
1419 return ODP_FIT_PERFECT;
1423 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1424 uint64_t present_attrs, uint64_t *expected_attrs,
1427 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1429 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1430 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1431 if (ntohs(flow->dl_type) < 1536) {
1432 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1433 ntohs(flow->dl_type));
1436 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1438 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1443 static enum odp_key_fitness
1444 parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1445 uint64_t present_attrs, int out_of_range_attr,
1446 uint64_t expected_attrs, struct flow *flow,
1447 const struct nlattr *key, size_t key_len)
1449 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1451 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1452 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1453 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1454 const struct ovs_key_ipv4 *ipv4_key;
1456 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1457 flow->nw_src = ipv4_key->ipv4_src;
1458 flow->nw_dst = ipv4_key->ipv4_dst;
1459 flow->nw_proto = ipv4_key->ipv4_proto;
1460 flow->nw_tos = ipv4_key->ipv4_tos;
1461 flow->nw_ttl = ipv4_key->ipv4_ttl;
1462 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1463 return ODP_FIT_ERROR;
1466 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1467 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1468 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1469 const struct ovs_key_ipv6 *ipv6_key;
1471 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1472 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1473 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1474 flow->ipv6_label = ipv6_key->ipv6_label;
1475 flow->nw_proto = ipv6_key->ipv6_proto;
1476 flow->nw_tos = ipv6_key->ipv6_tclass;
1477 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1478 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1479 return ODP_FIT_ERROR;
1482 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1483 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1484 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1485 const struct ovs_key_arp *arp_key;
1487 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1488 flow->nw_src = arp_key->arp_sip;
1489 flow->nw_dst = arp_key->arp_tip;
1490 if (arp_key->arp_op & htons(0xff00)) {
1491 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1492 "key", ntohs(arp_key->arp_op));
1493 return ODP_FIT_ERROR;
1495 flow->nw_proto = ntohs(arp_key->arp_op);
1496 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1497 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1501 if (flow->nw_proto == IPPROTO_TCP
1502 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1503 flow->dl_type == htons(ETH_TYPE_IPV6))
1504 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1505 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1506 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1507 const struct ovs_key_tcp *tcp_key;
1509 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1510 flow->tp_src = tcp_key->tcp_src;
1511 flow->tp_dst = tcp_key->tcp_dst;
1513 } else if (flow->nw_proto == IPPROTO_UDP
1514 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1515 flow->dl_type == htons(ETH_TYPE_IPV6))
1516 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1517 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1518 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1519 const struct ovs_key_udp *udp_key;
1521 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1522 flow->tp_src = udp_key->udp_src;
1523 flow->tp_dst = udp_key->udp_dst;
1525 } else if (flow->nw_proto == IPPROTO_ICMP
1526 && flow->dl_type == htons(ETH_TYPE_IP)
1527 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1528 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1529 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1530 const struct ovs_key_icmp *icmp_key;
1532 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1533 flow->tp_src = htons(icmp_key->icmp_type);
1534 flow->tp_dst = htons(icmp_key->icmp_code);
1536 } else if (flow->nw_proto == IPPROTO_ICMPV6
1537 && flow->dl_type == htons(ETH_TYPE_IPV6)
1538 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1539 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1540 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1541 const struct ovs_key_icmpv6 *icmpv6_key;
1543 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1544 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1545 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1547 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1548 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1549 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1550 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1551 const struct ovs_key_nd *nd_key;
1553 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1554 memcpy(&flow->nd_target, nd_key->nd_target,
1555 sizeof flow->nd_target);
1556 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1557 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1563 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1567 /* Parse 802.1Q header then encapsulated L3 attributes. */
1568 static enum odp_key_fitness
1569 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1570 uint64_t present_attrs, int out_of_range_attr,
1571 uint64_t expected_attrs, struct flow *flow,
1572 const struct nlattr *key, size_t key_len)
1574 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1576 const struct nlattr *encap
1577 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1578 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1579 enum odp_key_fitness encap_fitness;
1580 enum odp_key_fitness fitness;
1583 /* Calulate fitness of outer attributes. */
1584 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1585 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1586 fitness = check_expectations(present_attrs, out_of_range_attr,
1587 expected_attrs, key, key_len);
1589 /* Get the VLAN TCI value. */
1590 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1591 return ODP_FIT_TOO_LITTLE;
1593 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1594 if (tci == htons(0)) {
1595 /* Corner case for a truncated 802.1Q header. */
1596 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1597 return ODP_FIT_TOO_MUCH;
1600 } else if (!(tci & htons(VLAN_CFI))) {
1601 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1602 "but CFI bit is not set", ntohs(tci));
1603 return ODP_FIT_ERROR;
1607 * Remove the TPID from dl_type since it's not the real Ethertype. */
1608 flow->vlan_tci = tci;
1609 flow->dl_type = htons(0);
1611 /* Now parse the encapsulated attributes. */
1612 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1613 attrs, &present_attrs, &out_of_range_attr)) {
1614 return ODP_FIT_ERROR;
1618 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1619 return ODP_FIT_ERROR;
1621 encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1622 expected_attrs, flow, key, key_len);
1624 /* The overall fitness is the worse of the outer and inner attributes. */
1625 return MAX(fitness, encap_fitness);
1628 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1629 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1630 * 'key' fits our expectations for what a flow key should contain.
1632 * This function doesn't take the packet itself as an argument because none of
1633 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
1634 * it is always possible to infer which additional attribute(s) should appear
1635 * by looking at the attributes for lower-level protocols, e.g. if the network
1636 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1637 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1638 * must be absent. */
1639 enum odp_key_fitness
1640 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1643 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1644 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1645 uint64_t expected_attrs;
1646 uint64_t present_attrs;
1647 int out_of_range_attr;
1649 memset(flow, 0, sizeof *flow);
1651 /* Parse attributes. */
1652 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1653 &out_of_range_attr)) {
1654 return ODP_FIT_ERROR;
1659 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1660 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1661 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1664 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1665 flow->tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1666 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1669 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1670 uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1671 if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1672 VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1674 return ODP_FIT_ERROR;
1676 flow->in_port = odp_port_to_ofp_port(in_port);
1677 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1679 flow->in_port = OFPP_NONE;
1682 /* Ethernet header. */
1683 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1684 const struct ovs_key_ethernet *eth_key;
1686 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1687 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1688 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1690 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1692 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1693 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1694 return ODP_FIT_ERROR;
1697 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1698 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1699 expected_attrs, flow, key, key_len);
1701 return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1702 expected_attrs, flow, key, key_len);
1705 /* Returns 'fitness' as a string, for use in debug messages. */
1707 odp_key_fitness_to_string(enum odp_key_fitness fitness)
1710 case ODP_FIT_PERFECT:
1712 case ODP_FIT_TOO_MUCH:
1714 case ODP_FIT_TOO_LITTLE:
1715 return "too_little";
1723 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1724 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1725 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1726 * the start of the cookie. (If 'cookie' is null, then the return value is not
1729 odp_put_userspace_action(uint32_t pid, const struct user_action_cookie *cookie,
1730 struct ofpbuf *odp_actions)
1734 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1735 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1737 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1738 cookie, sizeof *cookie);
1740 nl_msg_end_nested(odp_actions, offset);
1742 return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
1745 /* The commit_odp_actions() function and its helpers. */
1748 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
1749 const void *key, size_t key_size)
1751 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
1752 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
1753 nl_msg_end_nested(odp_actions, offset);
1757 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
1758 struct ofpbuf *odp_actions)
1760 if (base->tun_id == flow->tun_id) {
1763 base->tun_id = flow->tun_id;
1765 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
1766 &base->tun_id, sizeof(base->tun_id));
1770 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
1771 struct ofpbuf *odp_actions)
1773 struct ovs_key_ethernet eth_key;
1775 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
1776 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
1780 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
1781 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
1783 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
1784 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
1786 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
1787 ð_key, sizeof(eth_key));
1791 commit_vlan_action(const struct flow *flow, struct flow *base,
1792 struct ofpbuf *odp_actions)
1794 if (base->vlan_tci == flow->vlan_tci) {
1798 if (base->vlan_tci & htons(VLAN_CFI)) {
1799 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
1802 if (flow->vlan_tci & htons(VLAN_CFI)) {
1803 struct ovs_action_push_vlan vlan;
1805 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
1806 vlan.vlan_tci = flow->vlan_tci;
1807 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
1808 &vlan, sizeof vlan);
1810 base->vlan_tci = flow->vlan_tci;
1814 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
1815 struct ofpbuf *odp_actions)
1817 struct ovs_key_ipv4 ipv4_key;
1819 if (base->nw_src == flow->nw_src &&
1820 base->nw_dst == flow->nw_dst &&
1821 base->nw_tos == flow->nw_tos &&
1822 base->nw_ttl == flow->nw_ttl &&
1823 base->nw_frag == flow->nw_frag) {
1827 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
1828 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
1829 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
1830 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
1831 ipv4_key.ipv4_proto = base->nw_proto;
1832 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
1834 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
1835 &ipv4_key, sizeof(ipv4_key));
1839 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
1840 struct ofpbuf *odp_actions)
1842 struct ovs_key_ipv6 ipv6_key;
1844 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
1845 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
1846 base->ipv6_label == flow->ipv6_label &&
1847 base->nw_tos == flow->nw_tos &&
1848 base->nw_ttl == flow->nw_ttl &&
1849 base->nw_frag == flow->nw_frag) {
1853 base->ipv6_src = flow->ipv6_src;
1854 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
1855 base->ipv6_dst = flow->ipv6_dst;
1856 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
1858 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
1859 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
1860 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
1861 ipv6_key.ipv6_proto = base->nw_proto;
1862 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
1864 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
1865 &ipv6_key, sizeof(ipv6_key));
1869 commit_set_nw_action(const struct flow *flow, struct flow *base,
1870 struct ofpbuf *odp_actions)
1872 /* Check if flow really have an IP header. */
1873 if (!flow->nw_proto) {
1877 if (base->dl_type == htons(ETH_TYPE_IP)) {
1878 commit_set_ipv4_action(flow, base, odp_actions);
1879 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
1880 commit_set_ipv6_action(flow, base, odp_actions);
1885 commit_set_port_action(const struct flow *flow, struct flow *base,
1886 struct ofpbuf *odp_actions)
1888 if (!base->tp_src || !base->tp_dst) {
1892 if (base->tp_src == flow->tp_src &&
1893 base->tp_dst == flow->tp_dst) {
1897 if (flow->nw_proto == IPPROTO_TCP) {
1898 struct ovs_key_tcp port_key;
1900 port_key.tcp_src = base->tp_src = flow->tp_src;
1901 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
1903 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
1904 &port_key, sizeof(port_key));
1906 } else if (flow->nw_proto == IPPROTO_UDP) {
1907 struct ovs_key_udp port_key;
1909 port_key.udp_src = base->tp_src = flow->tp_src;
1910 port_key.udp_dst = base->tp_dst = flow->tp_dst;
1912 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
1913 &port_key, sizeof(port_key));
1918 commit_set_priority_action(const struct flow *flow, struct flow *base,
1919 struct ofpbuf *odp_actions)
1921 if (base->skb_priority == flow->skb_priority) {
1924 base->skb_priority = flow->skb_priority;
1926 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
1927 &base->skb_priority, sizeof(base->skb_priority));
1930 /* If any of the flow key data that ODP actions can modify are different in
1931 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
1932 * key from 'base' into 'flow', and then changes 'base' the same way. */
1934 commit_odp_actions(const struct flow *flow, struct flow *base,
1935 struct ofpbuf *odp_actions)
1937 commit_set_tun_id_action(flow, base, odp_actions);
1938 commit_set_ether_addr_action(flow, base, odp_actions);
1939 commit_vlan_action(flow, base, odp_actions);
1940 commit_set_nw_action(flow, base, odp_actions);
1941 commit_set_port_action(flow, base, odp_actions);
1942 commit_set_priority_action(flow, base, odp_actions);