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.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
29 #include "dynamic-string.h"
39 VLOG_DEFINE_THIS_MODULE(odp_util);
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42 * Since this is fairly non-specific for the OVS userspace components,
43 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44 * interactions with the datapath.
47 /* The set of characters that may separate one action or one key attribute
49 static const char *delimiters = ", \t\r\n";
51 static int parse_odp_key_attr(const char *, const struct simap *port_names,
53 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
55 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
58 * - For an action whose argument has a fixed length, returned that
59 * nonnegative length in bytes.
61 * - For an action with a variable-length argument, returns -2.
63 * - For an invalid 'type', returns -1. */
65 odp_action_len(uint16_t type)
67 if (type > OVS_ACTION_ATTR_MAX) {
71 switch ((enum ovs_action_attr) type) {
72 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
73 case OVS_ACTION_ATTR_USERSPACE: return -2;
74 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
75 case OVS_ACTION_ATTR_POP_VLAN: return 0;
76 case OVS_ACTION_ATTR_SET: return -2;
77 case OVS_ACTION_ATTR_SAMPLE: return -2;
79 case OVS_ACTION_ATTR_UNSPEC:
80 case __OVS_ACTION_ATTR_MAX:
88 ovs_key_attr_to_string(enum ovs_key_attr attr)
90 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
93 case OVS_KEY_ATTR_UNSPEC: return "unspec";
94 case OVS_KEY_ATTR_ENCAP: return "encap";
95 case OVS_KEY_ATTR_PRIORITY: return "priority";
96 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
97 case OVS_KEY_ATTR_TUN_ID: return "tun_id";
98 case OVS_KEY_ATTR_IPV4_TUNNEL: return "ipv4_tunnel";
99 case OVS_KEY_ATTR_IN_PORT: return "in_port";
100 case OVS_KEY_ATTR_ETHERNET: return "eth";
101 case OVS_KEY_ATTR_VLAN: return "vlan";
102 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
103 case OVS_KEY_ATTR_IPV4: return "ipv4";
104 case OVS_KEY_ATTR_IPV6: return "ipv6";
105 case OVS_KEY_ATTR_TCP: return "tcp";
106 case OVS_KEY_ATTR_UDP: return "udp";
107 case OVS_KEY_ATTR_ICMP: return "icmp";
108 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
109 case OVS_KEY_ATTR_ARP: return "arp";
110 case OVS_KEY_ATTR_ND: return "nd";
112 case __OVS_KEY_ATTR_MAX:
114 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
115 (unsigned int) attr);
121 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
123 size_t len = nl_attr_get_size(a);
125 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
127 const uint8_t *unspec;
130 unspec = nl_attr_get(a);
131 for (i = 0; i < len; i++) {
132 ds_put_char(ds, i ? ' ': '(');
133 ds_put_format(ds, "%02x", unspec[i]);
135 ds_put_char(ds, ')');
140 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
142 static const struct nl_policy ovs_sample_policy[] = {
143 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
144 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
146 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
148 const struct nlattr *nla_acts;
151 ds_put_cstr(ds, "sample");
153 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
154 ds_put_cstr(ds, "(error)");
158 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
161 ds_put_format(ds, "(sample=%.1f%%,", percentage);
163 ds_put_cstr(ds, "actions(");
164 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
165 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
166 format_odp_actions(ds, nla_acts, len);
167 ds_put_format(ds, "))");
171 slow_path_reason_to_string(uint32_t data)
173 enum slow_path_reason bit = (enum slow_path_reason) data;
184 case SLOW_CONTROLLER:
194 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
205 while (s[n] != ')') {
206 unsigned long long int flags;
210 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
211 n += n0 + (s[n + n0] == ',');
216 for (bit = 1; bit; bit <<= 1) {
217 const char *name = bit_to_string(bit);
225 if (!strncmp(s + n, name, len) &&
226 (s[n + len] == ',' || s[n + len] == ')')) {
228 n += len + (s[n + len] == ',');
244 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
246 static const struct nl_policy ovs_userspace_policy[] = {
247 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
248 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
250 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
252 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
253 ds_put_cstr(ds, "userspace(error)");
257 ds_put_format(ds, "userspace(pid=%"PRIu32,
258 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
260 if (a[OVS_USERSPACE_ATTR_USERDATA]) {
261 uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
262 union user_action_cookie cookie;
264 memcpy(&cookie, &userdata, sizeof cookie);
266 switch (cookie.type) {
267 case USER_ACTION_COOKIE_SFLOW:
268 ds_put_format(ds, ",sFlow("
269 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
270 vlan_tci_to_vid(cookie.sflow.vlan_tci),
271 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
272 cookie.sflow.output);
275 case USER_ACTION_COOKIE_SLOW_PATH:
276 ds_put_cstr(ds, ",slow_path(");
277 format_flags(ds, slow_path_reason_to_string,
278 cookie.slow_path.reason, ',');
279 ds_put_format(ds, ")");
282 case USER_ACTION_COOKIE_UNSPEC:
284 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
289 ds_put_char(ds, ')');
293 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
295 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
296 vlan_tci_to_vid(vlan_tci),
297 vlan_tci_to_pcp(vlan_tci));
298 if (!(vlan_tci & htons(VLAN_CFI))) {
299 ds_put_cstr(ds, ",cfi=0");
304 format_odp_action(struct ds *ds, const struct nlattr *a)
307 enum ovs_action_attr type = nl_attr_type(a);
308 const struct ovs_action_push_vlan *vlan;
310 expected_len = odp_action_len(nl_attr_type(a));
311 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
312 ds_put_format(ds, "bad length %zu, expected %d for: ",
313 nl_attr_get_size(a), expected_len);
314 format_generic_odp_action(ds, a);
319 case OVS_ACTION_ATTR_OUTPUT:
320 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
322 case OVS_ACTION_ATTR_USERSPACE:
323 format_odp_userspace_action(ds, a);
325 case OVS_ACTION_ATTR_SET:
326 ds_put_cstr(ds, "set(");
327 format_odp_key_attr(nl_attr_get(a), ds);
328 ds_put_cstr(ds, ")");
330 case OVS_ACTION_ATTR_PUSH_VLAN:
331 vlan = nl_attr_get(a);
332 ds_put_cstr(ds, "push_vlan(");
333 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
334 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
336 format_vlan_tci(ds, vlan->vlan_tci);
337 ds_put_char(ds, ')');
339 case OVS_ACTION_ATTR_POP_VLAN:
340 ds_put_cstr(ds, "pop_vlan");
342 case OVS_ACTION_ATTR_SAMPLE:
343 format_odp_sample_action(ds, a);
345 case OVS_ACTION_ATTR_UNSPEC:
346 case __OVS_ACTION_ATTR_MAX:
348 format_generic_odp_action(ds, a);
354 format_odp_actions(struct ds *ds, const struct nlattr *actions,
358 const struct nlattr *a;
361 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
363 ds_put_char(ds, ',');
365 format_odp_action(ds, a);
370 if (left == actions_len) {
371 ds_put_cstr(ds, "<empty>");
373 ds_put_format(ds, ",***%u leftover bytes*** (", left);
374 for (i = 0; i < left; i++) {
375 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
377 ds_put_char(ds, ')');
380 ds_put_cstr(ds, "drop");
385 parse_odp_action(const char *s, const struct simap *port_names,
386 struct ofpbuf *actions)
388 /* Many of the sscanf calls in this function use oversized destination
389 * fields because some sscanf() implementations truncate the range of %i
390 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
391 * value of 0x7fff. The other alternatives are to allow only a single
392 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
395 * The tun_id parser has to use an alternative approach because there is no
396 * type larger than 64 bits. */
399 unsigned long long int port;
402 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
403 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
409 int len = strcspn(s, delimiters);
410 struct simap_node *node;
412 node = simap_find_len(port_names, s, len);
414 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
420 unsigned long long int pid;
421 unsigned long long int output;
426 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
427 odp_put_userspace_action(pid, NULL, actions);
429 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
430 "pcp=%i,output=%lli))%n",
431 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
432 union user_action_cookie cookie;
435 tci = vid | (pcp << VLAN_PCP_SHIFT);
440 cookie.type = USER_ACTION_COOKIE_SFLOW;
441 cookie.sflow.vlan_tci = htons(tci);
442 cookie.sflow.output = output;
443 odp_put_userspace_action(pid, &cookie, actions);
445 } else if (sscanf(s, "userspace(pid=%lli,slow_path%n", &pid, &n) > 0
447 union user_action_cookie cookie;
450 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
451 cookie.slow_path.unused = 0;
452 cookie.slow_path.reason = 0;
454 res = parse_flags(&s[n], slow_path_reason_to_string,
455 &cookie.slow_path.reason);
465 odp_put_userspace_action(pid, &cookie, actions);
467 } else if (sscanf(s, "userspace(pid=%lli,userdata="
468 "%31[x0123456789abcdefABCDEF])%n", &pid, userdata_s,
470 union user_action_cookie cookie;
473 userdata = strtoull(userdata_s, NULL, 0);
474 memcpy(&cookie, &userdata, sizeof cookie);
475 odp_put_userspace_action(pid, &cookie, actions);
480 if (!strncmp(s, "set(", 4)) {
484 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
485 retval = parse_odp_key_attr(s + 4, port_names, actions);
489 if (s[retval + 4] != ')') {
492 nl_msg_end_nested(actions, start_ofs);
497 struct ovs_action_push_vlan push;
498 int tpid = ETH_TYPE_VLAN;
503 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
505 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
506 &vid, &pcp, &cfi, &n) > 0 && n > 0)
507 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
508 &tpid, &vid, &pcp, &n) > 0 && n > 0)
509 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
510 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
511 push.vlan_tpid = htons(tpid);
512 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
513 | (pcp << VLAN_PCP_SHIFT)
514 | (cfi ? VLAN_CFI : 0));
515 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
522 if (!strncmp(s, "pop_vlan", 8)) {
523 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
531 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
532 && percentage >= 0. && percentage <= 100.0
534 size_t sample_ofs, actions_ofs;
537 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
538 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
539 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
540 (probability <= 0 ? 0
541 : probability >= UINT32_MAX ? UINT32_MAX
544 actions_ofs = nl_msg_start_nested(actions,
545 OVS_SAMPLE_ATTR_ACTIONS);
549 n += strspn(s + n, delimiters);
554 retval = parse_odp_action(s + n, port_names, actions);
560 nl_msg_end_nested(actions, actions_ofs);
561 nl_msg_end_nested(actions, sample_ofs);
563 return s[n + 1] == ')' ? n + 2 : -EINVAL;
570 /* Parses the string representation of datapath actions, in the format output
571 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
572 * value. On success, the ODP actions are appended to 'actions' as a series of
573 * Netlink attributes. On failure, no data is appended to 'actions'. Either
574 * way, 'actions''s data might be reallocated. */
576 odp_actions_from_string(const char *s, const struct simap *port_names,
577 struct ofpbuf *actions)
581 if (!strcasecmp(s, "drop")) {
585 old_size = actions->size;
589 s += strspn(s, delimiters);
594 retval = parse_odp_action(s, port_names, actions);
595 if (retval < 0 || !strchr(delimiters, s[retval])) {
596 actions->size = old_size;
605 /* Returns the correct length of the payload for a flow key attribute of the
606 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
607 * is variable length. */
609 odp_flow_key_attr_len(uint16_t type)
611 if (type > OVS_KEY_ATTR_MAX) {
615 switch ((enum ovs_key_attr) type) {
616 case OVS_KEY_ATTR_ENCAP: return -2;
617 case OVS_KEY_ATTR_PRIORITY: return 4;
618 case OVS_KEY_ATTR_SKB_MARK: return 4;
619 case OVS_KEY_ATTR_TUN_ID: return 8;
620 case OVS_KEY_ATTR_IPV4_TUNNEL: return sizeof(struct ovs_key_ipv4_tunnel);
621 case OVS_KEY_ATTR_IN_PORT: return 4;
622 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
623 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
624 case OVS_KEY_ATTR_ETHERTYPE: return 2;
625 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
626 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
627 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
628 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
629 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
630 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
631 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
632 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
634 case OVS_KEY_ATTR_UNSPEC:
635 case __OVS_KEY_ATTR_MAX:
643 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
645 size_t len = nl_attr_get_size(a);
647 const uint8_t *unspec;
650 unspec = nl_attr_get(a);
651 for (i = 0; i < len; i++) {
652 ds_put_char(ds, i ? ' ': '(');
653 ds_put_format(ds, "%02x", unspec[i]);
655 ds_put_char(ds, ')');
660 ovs_frag_type_to_string(enum ovs_frag_type type)
663 case OVS_FRAG_TYPE_NONE:
665 case OVS_FRAG_TYPE_FIRST:
667 case OVS_FRAG_TYPE_LATER:
669 case __OVS_FRAG_TYPE_MAX:
676 odp_tun_flag_to_string(uint32_t flags)
679 case OVS_TNL_F_DONT_FRAGMENT:
691 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
693 const struct ovs_key_ethernet *eth_key;
694 const struct ovs_key_ipv4 *ipv4_key;
695 const struct ovs_key_ipv6 *ipv6_key;
696 const struct ovs_key_tcp *tcp_key;
697 const struct ovs_key_udp *udp_key;
698 const struct ovs_key_icmp *icmp_key;
699 const struct ovs_key_icmpv6 *icmpv6_key;
700 const struct ovs_key_arp *arp_key;
701 const struct ovs_key_nd *nd_key;
702 const struct ovs_key_ipv4_tunnel *ipv4_tun_key;
703 enum ovs_key_attr attr = nl_attr_type(a);
706 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
707 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
708 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
709 ds_put_format(ds, "(bad length %zu, expected %d)",
711 odp_flow_key_attr_len(nl_attr_type(a)));
712 format_generic_odp_key(a, ds);
717 case OVS_KEY_ATTR_ENCAP:
718 ds_put_cstr(ds, "(");
719 if (nl_attr_get_size(a)) {
720 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
722 ds_put_char(ds, ')');
725 case OVS_KEY_ATTR_PRIORITY:
726 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
729 case OVS_KEY_ATTR_SKB_MARK:
730 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
733 case OVS_KEY_ATTR_TUN_ID:
734 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
737 case OVS_KEY_ATTR_IPV4_TUNNEL:
738 ipv4_tun_key = nl_attr_get(a);
739 ds_put_format(ds, "(tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
740 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
741 ntohll(ipv4_tun_key->tun_id),
742 IP_ARGS(&ipv4_tun_key->ipv4_src),
743 IP_ARGS(&ipv4_tun_key->ipv4_dst),
744 ipv4_tun_key->ipv4_tos, ipv4_tun_key->ipv4_ttl);
746 format_flags(ds, odp_tun_flag_to_string,
747 ipv4_tun_key->tun_flags, ',');
748 ds_put_format(ds, "))");
751 case OVS_KEY_ATTR_IN_PORT:
752 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
755 case OVS_KEY_ATTR_ETHERNET:
756 eth_key = nl_attr_get(a);
757 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
758 ETH_ADDR_ARGS(eth_key->eth_src),
759 ETH_ADDR_ARGS(eth_key->eth_dst));
762 case OVS_KEY_ATTR_VLAN:
763 ds_put_char(ds, '(');
764 format_vlan_tci(ds, nl_attr_get_be16(a));
765 ds_put_char(ds, ')');
768 case OVS_KEY_ATTR_ETHERTYPE:
769 ds_put_format(ds, "(0x%04"PRIx16")",
770 ntohs(nl_attr_get_be16(a)));
773 case OVS_KEY_ATTR_IPV4:
774 ipv4_key = nl_attr_get(a);
775 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
776 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
777 IP_ARGS(&ipv4_key->ipv4_src),
778 IP_ARGS(&ipv4_key->ipv4_dst),
779 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
781 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
784 case OVS_KEY_ATTR_IPV6: {
785 char src_str[INET6_ADDRSTRLEN];
786 char dst_str[INET6_ADDRSTRLEN];
788 ipv6_key = nl_attr_get(a);
789 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
790 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
792 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
793 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
794 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
795 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
796 ipv6_key->ipv6_hlimit,
797 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
801 case OVS_KEY_ATTR_TCP:
802 tcp_key = nl_attr_get(a);
803 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
804 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
807 case OVS_KEY_ATTR_UDP:
808 udp_key = nl_attr_get(a);
809 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
810 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
813 case OVS_KEY_ATTR_ICMP:
814 icmp_key = nl_attr_get(a);
815 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
816 icmp_key->icmp_type, icmp_key->icmp_code);
819 case OVS_KEY_ATTR_ICMPV6:
820 icmpv6_key = nl_attr_get(a);
821 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
822 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
825 case OVS_KEY_ATTR_ARP:
826 arp_key = nl_attr_get(a);
827 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
828 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
829 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
830 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
831 ETH_ADDR_ARGS(arp_key->arp_tha));
834 case OVS_KEY_ATTR_ND: {
835 char target[INET6_ADDRSTRLEN];
837 nd_key = nl_attr_get(a);
838 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
840 ds_put_format(ds, "(target=%s", target);
841 if (!eth_addr_is_zero(nd_key->nd_sll)) {
842 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
843 ETH_ADDR_ARGS(nd_key->nd_sll));
845 if (!eth_addr_is_zero(nd_key->nd_tll)) {
846 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
847 ETH_ADDR_ARGS(nd_key->nd_tll));
849 ds_put_char(ds, ')');
853 case OVS_KEY_ATTR_UNSPEC:
854 case __OVS_KEY_ATTR_MAX:
856 format_generic_odp_key(a, ds);
861 /* Appends to 'ds' a string representation of the 'key_len' bytes of
862 * OVS_KEY_ATTR_* attributes in 'key'. */
864 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
867 const struct nlattr *a;
870 NL_ATTR_FOR_EACH (a, left, key, key_len) {
872 ds_put_char(ds, ',');
874 format_odp_key_attr(a, ds);
879 if (left == key_len) {
880 ds_put_cstr(ds, "<empty>");
882 ds_put_format(ds, ",***%u leftover bytes*** (", left);
883 for (i = 0; i < left; i++) {
884 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
886 ds_put_char(ds, ')');
889 ds_put_cstr(ds, "<empty>");
894 put_nd_key(int n, const char *nd_target_s,
895 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
897 struct ovs_key_nd nd_key;
899 memset(&nd_key, 0, sizeof nd_key);
900 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
904 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
907 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
909 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
914 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
916 if (!strcasecmp(s, "no")) {
917 *type = OVS_FRAG_TYPE_NONE;
918 } else if (!strcasecmp(s, "first")) {
919 *type = OVS_FRAG_TYPE_FIRST;
920 } else if (!strcasecmp(s, "later")) {
921 *type = OVS_FRAG_TYPE_LATER;
929 parse_odp_key_attr(const char *s, const struct simap *port_names,
932 /* Many of the sscanf calls in this function use oversized destination
933 * fields because some sscanf() implementations truncate the range of %i
934 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
935 * value of 0x7fff. The other alternatives are to allow only a single
936 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
939 * The tun_id parser has to use an alternative approach because there is no
940 * type larger than 64 bits. */
943 unsigned long long int priority;
946 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
947 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
953 unsigned long long int mark;
956 if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
957 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
966 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
967 tun_id_s, &n) > 0 && n > 0) {
968 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
969 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
977 struct ovs_key_ipv4_tunnel tun_key;
980 if (sscanf(s, "ipv4_tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
981 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
982 ",tos=%i,ttl=%i,flags%n", tun_id_s,
983 IP_SCAN_ARGS(&tun_key.ipv4_src),
984 IP_SCAN_ARGS(&tun_key.ipv4_dst), &tos, &ttl,
988 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
989 tun_key.ipv4_tos = tos;
990 tun_key.ipv4_ttl = ttl;
992 res = parse_flags(&s[n], odp_tun_flag_to_string,
1003 memset(&tun_key.pad, 0, sizeof tun_key.pad);
1004 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key,
1011 unsigned long long int in_port;
1014 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1015 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1020 if (port_names && !strncmp(s, "in_port(", 8)) {
1022 const struct simap_node *node;
1026 name_len = strcspn(s, ")");
1027 node = simap_find_len(port_names, name, name_len);
1029 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1030 return 8 + name_len + 1;
1035 struct ovs_key_ethernet eth_key;
1039 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1040 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1041 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1042 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1043 ð_key, sizeof eth_key);
1054 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
1056 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1057 htons((vid << VLAN_VID_SHIFT) |
1058 (pcp << VLAN_PCP_SHIFT) |
1061 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1062 &vid, &pcp, &cfi, &n) > 0
1064 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1065 htons((vid << VLAN_VID_SHIFT) |
1066 (pcp << VLAN_PCP_SHIFT) |
1067 (cfi ? VLAN_CFI : 0)));
1076 if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
1077 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1089 enum ovs_frag_type ipv4_frag;
1092 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1093 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1094 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1095 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1097 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1098 struct ovs_key_ipv4 ipv4_key;
1100 ipv4_key.ipv4_src = ipv4_src;
1101 ipv4_key.ipv4_dst = ipv4_dst;
1102 ipv4_key.ipv4_proto = ipv4_proto;
1103 ipv4_key.ipv4_tos = ipv4_tos;
1104 ipv4_key.ipv4_ttl = ipv4_ttl;
1105 ipv4_key.ipv4_frag = ipv4_frag;
1106 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1107 &ipv4_key, sizeof ipv4_key);
1113 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1114 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1120 enum ovs_frag_type ipv6_frag;
1123 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1124 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1125 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1126 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1128 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1129 struct ovs_key_ipv6 ipv6_key;
1131 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1132 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1135 ipv6_key.ipv6_label = htonl(ipv6_label);
1136 ipv6_key.ipv6_proto = ipv6_proto;
1137 ipv6_key.ipv6_tclass = ipv6_tclass;
1138 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1139 ipv6_key.ipv6_frag = ipv6_frag;
1140 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1141 &ipv6_key, sizeof ipv6_key);
1151 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1153 struct ovs_key_tcp tcp_key;
1155 tcp_key.tcp_src = htons(tcp_src);
1156 tcp_key.tcp_dst = htons(tcp_dst);
1157 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1167 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
1169 struct ovs_key_udp udp_key;
1171 udp_key.udp_src = htons(udp_src);
1172 udp_key.udp_dst = htons(udp_dst);
1173 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
1183 if (sscanf(s, "icmp(type=%i,code=%i)%n",
1184 &icmp_type, &icmp_code, &n) > 0
1186 struct ovs_key_icmp icmp_key;
1188 icmp_key.icmp_type = icmp_type;
1189 icmp_key.icmp_code = icmp_code;
1190 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
1191 &icmp_key, sizeof icmp_key);
1197 struct ovs_key_icmpv6 icmpv6_key;
1200 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
1201 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
1203 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
1204 &icmpv6_key, sizeof icmpv6_key);
1213 uint8_t arp_sha[ETH_ADDR_LEN];
1214 uint8_t arp_tha[ETH_ADDR_LEN];
1217 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
1218 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
1219 IP_SCAN_ARGS(&arp_sip),
1220 IP_SCAN_ARGS(&arp_tip),
1222 ETH_ADDR_SCAN_ARGS(arp_sha),
1223 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
1224 struct ovs_key_arp arp_key;
1226 memset(&arp_key, 0, sizeof arp_key);
1227 arp_key.arp_sip = arp_sip;
1228 arp_key.arp_tip = arp_tip;
1229 arp_key.arp_op = htons(arp_op);
1230 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
1231 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
1232 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
1238 char nd_target_s[IPV6_SCAN_LEN + 1];
1239 uint8_t nd_sll[ETH_ADDR_LEN];
1240 uint8_t nd_tll[ETH_ADDR_LEN];
1243 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
1244 nd_target_s, &n) > 0 && n > 0) {
1245 return put_nd_key(n, nd_target_s, NULL, NULL, key);
1247 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
1248 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
1250 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
1252 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
1253 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1255 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
1257 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
1258 "tll="ETH_ADDR_SCAN_FMT")%n",
1259 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
1260 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
1262 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
1266 if (!strncmp(s, "encap(", 6)) {
1267 const char *start = s;
1270 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
1276 s += strspn(s, ", \t\r\n");
1279 } else if (*s == ')') {
1283 retval = parse_odp_key_attr(s, port_names, key);
1291 nl_msg_end_nested(key, encap);
1299 /* Parses the string representation of a datapath flow key, in the
1300 * format output by odp_flow_key_format(). Returns 0 if successful,
1301 * otherwise a positive errno value. On success, the flow key is
1302 * appended to 'key' as a series of Netlink attributes. On failure, no
1303 * data is appended to 'key'. Either way, 'key''s data might be
1306 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1307 * to a port number. (Port names may be used instead of port numbers in
1310 * On success, the attributes appended to 'key' are individually syntactically
1311 * valid, but they may not be valid as a sequence. 'key' might, for example,
1312 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1314 odp_flow_key_from_string(const char *s, const struct simap *port_names,
1317 const size_t old_size = key->size;
1321 s += strspn(s, delimiters);
1326 retval = parse_odp_key_attr(s, port_names, key);
1328 key->size = old_size;
1338 ovs_to_odp_frag(uint8_t nw_frag)
1340 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
1341 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
1342 : OVS_FRAG_TYPE_LATER);
1345 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1346 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1347 * number rather than a datapath port number). Instead, if 'odp_in_port'
1348 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1351 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1352 * capable of being expanded to allow for that much space. */
1354 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
1355 uint32_t odp_in_port)
1357 struct ovs_key_ethernet *eth_key;
1360 if (flow->skb_priority) {
1361 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->skb_priority);
1364 if (flow->tunnel.tun_id != htonll(0)) {
1365 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tunnel.tun_id);
1368 if (flow->skb_mark) {
1369 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, flow->skb_mark);
1372 if (odp_in_port != OVSP_NONE) {
1373 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
1376 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
1378 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
1379 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
1381 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
1382 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
1383 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
1384 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
1385 if (flow->vlan_tci == htons(0)) {
1392 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
1396 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
1398 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1399 struct ovs_key_ipv4 *ipv4_key;
1401 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
1403 ipv4_key->ipv4_src = flow->nw_src;
1404 ipv4_key->ipv4_dst = flow->nw_dst;
1405 ipv4_key->ipv4_proto = flow->nw_proto;
1406 ipv4_key->ipv4_tos = flow->nw_tos;
1407 ipv4_key->ipv4_ttl = flow->nw_ttl;
1408 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
1409 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1410 struct ovs_key_ipv6 *ipv6_key;
1412 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
1414 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1415 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1416 ipv6_key->ipv6_label = flow->ipv6_label;
1417 ipv6_key->ipv6_proto = flow->nw_proto;
1418 ipv6_key->ipv6_tclass = flow->nw_tos;
1419 ipv6_key->ipv6_hlimit = flow->nw_ttl;
1420 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1421 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1422 flow->dl_type == htons(ETH_TYPE_RARP)) {
1423 struct ovs_key_arp *arp_key;
1425 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1427 memset(arp_key, 0, sizeof *arp_key);
1428 arp_key->arp_sip = flow->nw_src;
1429 arp_key->arp_tip = flow->nw_dst;
1430 arp_key->arp_op = htons(flow->nw_proto);
1431 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1432 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1435 if ((flow->dl_type == htons(ETH_TYPE_IP)
1436 || flow->dl_type == htons(ETH_TYPE_IPV6))
1437 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1439 if (flow->nw_proto == IPPROTO_TCP) {
1440 struct ovs_key_tcp *tcp_key;
1442 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1444 tcp_key->tcp_src = flow->tp_src;
1445 tcp_key->tcp_dst = flow->tp_dst;
1446 } else if (flow->nw_proto == IPPROTO_UDP) {
1447 struct ovs_key_udp *udp_key;
1449 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1451 udp_key->udp_src = flow->tp_src;
1452 udp_key->udp_dst = flow->tp_dst;
1453 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1454 && flow->nw_proto == IPPROTO_ICMP) {
1455 struct ovs_key_icmp *icmp_key;
1457 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1459 icmp_key->icmp_type = ntohs(flow->tp_src);
1460 icmp_key->icmp_code = ntohs(flow->tp_dst);
1461 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1462 && flow->nw_proto == IPPROTO_ICMPV6) {
1463 struct ovs_key_icmpv6 *icmpv6_key;
1465 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1466 sizeof *icmpv6_key);
1467 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1468 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1470 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1471 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1472 struct ovs_key_nd *nd_key;
1474 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1476 memcpy(nd_key->nd_target, &flow->nd_target,
1477 sizeof nd_key->nd_target);
1478 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1479 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1486 nl_msg_end_nested(buf, encap);
1491 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
1493 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
1494 return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
1498 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1499 uint64_t attrs, int out_of_range_attr,
1500 const struct nlattr *key, size_t key_len)
1505 if (VLOG_DROP_DBG(rl)) {
1510 for (i = 0; i < 64; i++) {
1511 if (attrs & (UINT64_C(1) << i)) {
1512 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1515 if (out_of_range_attr) {
1516 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
1519 ds_put_cstr(&s, ": ");
1520 odp_flow_key_format(key, key_len, &s);
1522 VLOG_DBG("%s:%s", title, ds_cstr(&s));
1527 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1529 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1531 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1532 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
1536 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1537 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1538 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1539 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1546 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1547 const struct nlattr *attrs[], uint64_t *present_attrsp,
1548 int *out_of_range_attrp)
1550 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1551 const struct nlattr *nla;
1552 uint64_t present_attrs;
1556 *out_of_range_attrp = 0;
1557 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1558 uint16_t type = nl_attr_type(nla);
1559 size_t len = nl_attr_get_size(nla);
1560 int expected_len = odp_flow_key_attr_len(type);
1562 if (len != expected_len && expected_len >= 0) {
1563 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1564 "length %d", ovs_key_attr_to_string(type),
1569 if (type >= CHAR_BIT * sizeof present_attrs) {
1570 *out_of_range_attrp = type;
1572 if (present_attrs & (UINT64_C(1) << type)) {
1573 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1574 ovs_key_attr_to_string(type));
1578 present_attrs |= UINT64_C(1) << type;
1583 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1587 *present_attrsp = present_attrs;
1591 static enum odp_key_fitness
1592 check_expectations(uint64_t present_attrs, int out_of_range_attr,
1593 uint64_t expected_attrs,
1594 const struct nlattr *key, size_t key_len)
1596 uint64_t missing_attrs;
1597 uint64_t extra_attrs;
1599 missing_attrs = expected_attrs & ~present_attrs;
1600 if (missing_attrs) {
1601 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1602 log_odp_key_attributes(&rl, "expected but not present",
1603 missing_attrs, 0, key, key_len);
1604 return ODP_FIT_TOO_LITTLE;
1607 extra_attrs = present_attrs & ~expected_attrs;
1608 if (extra_attrs || out_of_range_attr) {
1609 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
1610 log_odp_key_attributes(&rl, "present but not expected",
1611 extra_attrs, out_of_range_attr, key, key_len);
1612 return ODP_FIT_TOO_MUCH;
1615 return ODP_FIT_PERFECT;
1619 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1620 uint64_t present_attrs, uint64_t *expected_attrs,
1623 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1625 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1626 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1627 if (ntohs(flow->dl_type) < 1536) {
1628 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1629 ntohs(flow->dl_type));
1632 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1634 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1639 static enum odp_key_fitness
1640 parse_l3_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1641 uint64_t present_attrs, int out_of_range_attr,
1642 uint64_t expected_attrs, struct flow *flow,
1643 const struct nlattr *key, size_t key_len)
1645 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1647 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1648 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1649 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1650 const struct ovs_key_ipv4 *ipv4_key;
1652 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1653 flow->nw_src = ipv4_key->ipv4_src;
1654 flow->nw_dst = ipv4_key->ipv4_dst;
1655 flow->nw_proto = ipv4_key->ipv4_proto;
1656 flow->nw_tos = ipv4_key->ipv4_tos;
1657 flow->nw_ttl = ipv4_key->ipv4_ttl;
1658 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1659 return ODP_FIT_ERROR;
1662 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1663 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1664 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1665 const struct ovs_key_ipv6 *ipv6_key;
1667 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1668 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1669 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1670 flow->ipv6_label = ipv6_key->ipv6_label;
1671 flow->nw_proto = ipv6_key->ipv6_proto;
1672 flow->nw_tos = ipv6_key->ipv6_tclass;
1673 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1674 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1675 return ODP_FIT_ERROR;
1678 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1679 flow->dl_type == htons(ETH_TYPE_RARP)) {
1680 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1681 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1682 const struct ovs_key_arp *arp_key;
1684 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1685 flow->nw_src = arp_key->arp_sip;
1686 flow->nw_dst = arp_key->arp_tip;
1687 if (arp_key->arp_op & htons(0xff00)) {
1688 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1689 "key", ntohs(arp_key->arp_op));
1690 return ODP_FIT_ERROR;
1692 flow->nw_proto = ntohs(arp_key->arp_op);
1693 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1694 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1698 if (flow->nw_proto == IPPROTO_TCP
1699 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1700 flow->dl_type == htons(ETH_TYPE_IPV6))
1701 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1702 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1703 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1704 const struct ovs_key_tcp *tcp_key;
1706 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1707 flow->tp_src = tcp_key->tcp_src;
1708 flow->tp_dst = tcp_key->tcp_dst;
1710 } else if (flow->nw_proto == IPPROTO_UDP
1711 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1712 flow->dl_type == htons(ETH_TYPE_IPV6))
1713 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1714 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1715 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1716 const struct ovs_key_udp *udp_key;
1718 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1719 flow->tp_src = udp_key->udp_src;
1720 flow->tp_dst = udp_key->udp_dst;
1722 } else if (flow->nw_proto == IPPROTO_ICMP
1723 && flow->dl_type == htons(ETH_TYPE_IP)
1724 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1725 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1726 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1727 const struct ovs_key_icmp *icmp_key;
1729 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1730 flow->tp_src = htons(icmp_key->icmp_type);
1731 flow->tp_dst = htons(icmp_key->icmp_code);
1733 } else if (flow->nw_proto == IPPROTO_ICMPV6
1734 && flow->dl_type == htons(ETH_TYPE_IPV6)
1735 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1736 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1737 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1738 const struct ovs_key_icmpv6 *icmpv6_key;
1740 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1741 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1742 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1744 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1745 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1746 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1747 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1748 const struct ovs_key_nd *nd_key;
1750 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1751 memcpy(&flow->nd_target, nd_key->nd_target,
1752 sizeof flow->nd_target);
1753 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1754 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1760 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
1764 /* Parse 802.1Q header then encapsulated L3 attributes. */
1765 static enum odp_key_fitness
1766 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
1767 uint64_t present_attrs, int out_of_range_attr,
1768 uint64_t expected_attrs, struct flow *flow,
1769 const struct nlattr *key, size_t key_len)
1771 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1773 const struct nlattr *encap
1774 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
1775 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
1776 enum odp_key_fitness encap_fitness;
1777 enum odp_key_fitness fitness;
1780 /* Calulate fitness of outer attributes. */
1781 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1782 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1783 fitness = check_expectations(present_attrs, out_of_range_attr,
1784 expected_attrs, key, key_len);
1786 /* Get the VLAN TCI value. */
1787 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
1788 return ODP_FIT_TOO_LITTLE;
1790 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1791 if (tci == htons(0)) {
1792 /* Corner case for a truncated 802.1Q header. */
1793 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
1794 return ODP_FIT_TOO_MUCH;
1797 } else if (!(tci & htons(VLAN_CFI))) {
1798 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
1799 "but CFI bit is not set", ntohs(tci));
1800 return ODP_FIT_ERROR;
1804 * Remove the TPID from dl_type since it's not the real Ethertype. */
1805 flow->vlan_tci = tci;
1806 flow->dl_type = htons(0);
1808 /* Now parse the encapsulated attributes. */
1809 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
1810 attrs, &present_attrs, &out_of_range_attr)) {
1811 return ODP_FIT_ERROR;
1815 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1816 return ODP_FIT_ERROR;
1818 encap_fitness = parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1819 expected_attrs, flow, key, key_len);
1821 /* The overall fitness is the worse of the outer and inner attributes. */
1822 return MAX(fitness, encap_fitness);
1825 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1826 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1827 * 'key' fits our expectations for what a flow key should contain.
1829 * The 'in_port' will be the datapath's understanding of the port. The
1830 * caller will need to translate with odp_port_to_ofp_port() if the
1831 * OpenFlow port is needed.
1833 * This function doesn't take the packet itself as an argument because none of
1834 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
1835 * it is always possible to infer which additional attribute(s) should appear
1836 * by looking at the attributes for lower-level protocols, e.g. if the network
1837 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1838 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1839 * must be absent. */
1840 enum odp_key_fitness
1841 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1844 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1845 uint64_t expected_attrs;
1846 uint64_t present_attrs;
1847 int out_of_range_attr;
1849 memset(flow, 0, sizeof *flow);
1851 /* Parse attributes. */
1852 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
1853 &out_of_range_attr)) {
1854 return ODP_FIT_ERROR;
1859 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1860 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1861 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1864 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
1865 flow->skb_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
1866 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
1869 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1870 flow->tunnel.tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1871 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1874 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1875 flow->in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1876 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1878 flow->in_port = OVSP_NONE;
1881 /* Ethernet header. */
1882 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1883 const struct ovs_key_ethernet *eth_key;
1885 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1886 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1887 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1889 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1891 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1892 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow)) {
1893 return ODP_FIT_ERROR;
1896 if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
1897 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
1898 expected_attrs, flow, key, key_len);
1900 return parse_l3_onward(attrs, present_attrs, out_of_range_attr,
1901 expected_attrs, flow, key, key_len);
1904 /* Returns 'fitness' as a string, for use in debug messages. */
1906 odp_key_fitness_to_string(enum odp_key_fitness fitness)
1909 case ODP_FIT_PERFECT:
1911 case ODP_FIT_TOO_MUCH:
1913 case ODP_FIT_TOO_LITTLE:
1914 return "too_little";
1922 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1923 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1924 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1925 * the start of the cookie. (If 'cookie' is null, then the return value is not
1928 odp_put_userspace_action(uint32_t pid, const union user_action_cookie *cookie,
1929 struct ofpbuf *odp_actions)
1933 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1934 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1936 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1937 cookie, sizeof *cookie);
1939 nl_msg_end_nested(odp_actions, offset);
1941 return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;
1944 /* The commit_odp_actions() function and its helpers. */
1947 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
1948 const void *key, size_t key_size)
1950 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
1951 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
1952 nl_msg_end_nested(odp_actions, offset);
1956 commit_set_tun_id_action(const struct flow *flow, struct flow *base,
1957 struct ofpbuf *odp_actions)
1959 if (base->tunnel.tun_id == flow->tunnel.tun_id) {
1962 base->tunnel.tun_id = flow->tunnel.tun_id;
1964 commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID,
1965 &base->tunnel.tun_id, sizeof(base->tunnel.tun_id));
1969 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
1970 struct ofpbuf *odp_actions)
1972 struct ovs_key_ethernet eth_key;
1974 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
1975 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
1979 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
1980 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
1982 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
1983 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
1985 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
1986 ð_key, sizeof(eth_key));
1990 commit_vlan_action(const struct flow *flow, struct flow *base,
1991 struct ofpbuf *odp_actions)
1993 if (base->vlan_tci == flow->vlan_tci) {
1997 if (base->vlan_tci & htons(VLAN_CFI)) {
1998 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
2001 if (flow->vlan_tci & htons(VLAN_CFI)) {
2002 struct ovs_action_push_vlan vlan;
2004 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
2005 vlan.vlan_tci = flow->vlan_tci;
2006 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
2007 &vlan, sizeof vlan);
2009 base->vlan_tci = flow->vlan_tci;
2013 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
2014 struct ofpbuf *odp_actions)
2016 struct ovs_key_ipv4 ipv4_key;
2018 if (base->nw_src == flow->nw_src &&
2019 base->nw_dst == flow->nw_dst &&
2020 base->nw_tos == flow->nw_tos &&
2021 base->nw_ttl == flow->nw_ttl &&
2022 base->nw_frag == flow->nw_frag) {
2026 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
2027 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
2028 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
2029 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
2030 ipv4_key.ipv4_proto = base->nw_proto;
2031 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
2033 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
2034 &ipv4_key, sizeof(ipv4_key));
2038 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
2039 struct ofpbuf *odp_actions)
2041 struct ovs_key_ipv6 ipv6_key;
2043 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
2044 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
2045 base->ipv6_label == flow->ipv6_label &&
2046 base->nw_tos == flow->nw_tos &&
2047 base->nw_ttl == flow->nw_ttl &&
2048 base->nw_frag == flow->nw_frag) {
2052 base->ipv6_src = flow->ipv6_src;
2053 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
2054 base->ipv6_dst = flow->ipv6_dst;
2055 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
2057 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
2058 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
2059 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
2060 ipv6_key.ipv6_proto = base->nw_proto;
2061 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
2063 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
2064 &ipv6_key, sizeof(ipv6_key));
2068 commit_set_nw_action(const struct flow *flow, struct flow *base,
2069 struct ofpbuf *odp_actions)
2071 /* Check if flow really have an IP header. */
2072 if (!flow->nw_proto) {
2076 if (base->dl_type == htons(ETH_TYPE_IP)) {
2077 commit_set_ipv4_action(flow, base, odp_actions);
2078 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
2079 commit_set_ipv6_action(flow, base, odp_actions);
2084 commit_set_port_action(const struct flow *flow, struct flow *base,
2085 struct ofpbuf *odp_actions)
2087 if (!base->tp_src && !base->tp_dst) {
2091 if (base->tp_src == flow->tp_src &&
2092 base->tp_dst == flow->tp_dst) {
2096 if (flow->nw_proto == IPPROTO_TCP) {
2097 struct ovs_key_tcp port_key;
2099 port_key.tcp_src = base->tp_src = flow->tp_src;
2100 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
2102 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
2103 &port_key, sizeof(port_key));
2105 } else if (flow->nw_proto == IPPROTO_UDP) {
2106 struct ovs_key_udp port_key;
2108 port_key.udp_src = base->tp_src = flow->tp_src;
2109 port_key.udp_dst = base->tp_dst = flow->tp_dst;
2111 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
2112 &port_key, sizeof(port_key));
2117 commit_set_priority_action(const struct flow *flow, struct flow *base,
2118 struct ofpbuf *odp_actions)
2120 if (base->skb_priority == flow->skb_priority) {
2123 base->skb_priority = flow->skb_priority;
2125 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
2126 &base->skb_priority, sizeof(base->skb_priority));
2130 commit_set_skb_mark_action(const struct flow *flow, struct flow *base,
2131 struct ofpbuf *odp_actions)
2133 if (base->skb_mark == flow->skb_mark) {
2136 base->skb_mark = flow->skb_mark;
2138 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK,
2139 &base->skb_mark, sizeof(base->skb_mark));
2141 /* If any of the flow key data that ODP actions can modify are different in
2142 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2143 * key from 'base' into 'flow', and then changes 'base' the same way. */
2145 commit_odp_actions(const struct flow *flow, struct flow *base,
2146 struct ofpbuf *odp_actions)
2148 commit_set_tun_id_action(flow, base, odp_actions);
2149 commit_set_ether_addr_action(flow, base, odp_actions);
2150 commit_vlan_action(flow, base, odp_actions);
2151 commit_set_nw_action(flow, base, odp_actions);
2152 commit_set_port_action(flow, base, odp_actions);
2153 commit_set_priority_action(flow, base, odp_actions);
2154 commit_set_skb_mark_action(flow, base, odp_actions);