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_TUN_ID
: return "tun_id";
97 case OVS_KEY_ATTR_IPV4_TUNNEL
: return "ipv4_tunnel";
98 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
99 case OVS_KEY_ATTR_ETHERNET
: return "eth";
100 case OVS_KEY_ATTR_VLAN
: return "vlan";
101 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
102 case OVS_KEY_ATTR_IPV4
: return "ipv4";
103 case OVS_KEY_ATTR_IPV6
: return "ipv6";
104 case OVS_KEY_ATTR_TCP
: return "tcp";
105 case OVS_KEY_ATTR_UDP
: return "udp";
106 case OVS_KEY_ATTR_ICMP
: return "icmp";
107 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
108 case OVS_KEY_ATTR_ARP
: return "arp";
109 case OVS_KEY_ATTR_ND
: return "nd";
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 slow_path_reason_to_string(enum slow_path_reason bit
)
181 case SLOW_CONTROLLER
:
191 format_slow_path_reason(struct ds
*ds
, uint32_t slow
)
196 uint32_t bit
= rightmost_1bit(slow
);
199 s
= slow_path_reason_to_string(bit
);
201 ds_put_format(ds
, "%s,", s
);
210 ds_put_format(ds
, "0x%"PRIx32
",", bad
);
216 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
218 static const struct nl_policy ovs_userspace_policy
[] = {
219 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
220 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_U64
, .optional
= true },
222 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
224 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
225 ds_put_cstr(ds
, "userspace(error)");
229 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
230 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
232 if (a
[OVS_USERSPACE_ATTR_USERDATA
]) {
233 uint64_t userdata
= nl_attr_get_u64(a
[OVS_USERSPACE_ATTR_USERDATA
]);
234 union user_action_cookie cookie
;
236 memcpy(&cookie
, &userdata
, sizeof cookie
);
238 switch (cookie
.type
) {
239 case USER_ACTION_COOKIE_SFLOW
:
240 ds_put_format(ds
, ",sFlow("
241 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
242 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
243 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
244 cookie
.sflow
.output
);
247 case USER_ACTION_COOKIE_SLOW_PATH
:
248 ds_put_cstr(ds
, ",slow_path(");
249 if (cookie
.slow_path
.reason
) {
250 format_slow_path_reason(ds
, cookie
.slow_path
.reason
);
252 ds_put_char(ds
, ')');
255 case USER_ACTION_COOKIE_UNSPEC
:
257 ds_put_format(ds
, ",userdata=0x%"PRIx64
, userdata
);
262 ds_put_char(ds
, ')');
266 format_vlan_tci(struct ds
*ds
, ovs_be16 vlan_tci
)
268 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d",
269 vlan_tci_to_vid(vlan_tci
),
270 vlan_tci_to_pcp(vlan_tci
));
271 if (!(vlan_tci
& htons(VLAN_CFI
))) {
272 ds_put_cstr(ds
, ",cfi=0");
277 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
280 enum ovs_action_attr type
= nl_attr_type(a
);
281 const struct ovs_action_push_vlan
*vlan
;
283 expected_len
= odp_action_len(nl_attr_type(a
));
284 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
285 ds_put_format(ds
, "bad length %zu, expected %d for: ",
286 nl_attr_get_size(a
), expected_len
);
287 format_generic_odp_action(ds
, a
);
292 case OVS_ACTION_ATTR_OUTPUT
:
293 ds_put_format(ds
, "%"PRIu16
, nl_attr_get_u32(a
));
295 case OVS_ACTION_ATTR_USERSPACE
:
296 format_odp_userspace_action(ds
, a
);
298 case OVS_ACTION_ATTR_SET
:
299 ds_put_cstr(ds
, "set(");
300 format_odp_key_attr(nl_attr_get(a
), ds
);
301 ds_put_cstr(ds
, ")");
303 case OVS_ACTION_ATTR_PUSH_VLAN
:
304 vlan
= nl_attr_get(a
);
305 ds_put_cstr(ds
, "push_vlan(");
306 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
307 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
309 format_vlan_tci(ds
, vlan
->vlan_tci
);
310 ds_put_char(ds
, ')');
312 case OVS_ACTION_ATTR_POP_VLAN
:
313 ds_put_cstr(ds
, "pop_vlan");
315 case OVS_ACTION_ATTR_SAMPLE
:
316 format_odp_sample_action(ds
, a
);
318 case OVS_ACTION_ATTR_UNSPEC
:
319 case __OVS_ACTION_ATTR_MAX
:
321 format_generic_odp_action(ds
, a
);
327 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
331 const struct nlattr
*a
;
334 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
336 ds_put_char(ds
, ',');
338 format_odp_action(ds
, a
);
343 if (left
== actions_len
) {
344 ds_put_cstr(ds
, "<empty>");
346 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
347 for (i
= 0; i
< left
; i
++) {
348 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
350 ds_put_char(ds
, ')');
353 ds_put_cstr(ds
, "drop");
358 parse_odp_action(const char *s
, const struct simap
*port_names
,
359 struct ofpbuf
*actions
)
361 /* Many of the sscanf calls in this function use oversized destination
362 * fields because some sscanf() implementations truncate the range of %i
363 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
364 * value of 0x7fff. The other alternatives are to allow only a single
365 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
368 * The tun_id parser has to use an alternative approach because there is no
369 * type larger than 64 bits. */
372 unsigned long long int port
;
375 if (sscanf(s
, "%lli%n", &port
, &n
) > 0 && n
> 0) {
376 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
382 int len
= strcspn(s
, delimiters
);
383 struct simap_node
*node
;
385 node
= simap_find_len(port_names
, s
, len
);
387 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
393 unsigned long long int pid
;
394 unsigned long long int output
;
399 if (sscanf(s
, "userspace(pid=%lli)%n", &pid
, &n
) > 0 && n
> 0) {
400 odp_put_userspace_action(pid
, NULL
, actions
);
402 } else if (sscanf(s
, "userspace(pid=%lli,sFlow(vid=%i,"
403 "pcp=%i,output=%lli))%n",
404 &pid
, &vid
, &pcp
, &output
, &n
) > 0 && n
> 0) {
405 union user_action_cookie cookie
;
408 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
413 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
414 cookie
.sflow
.vlan_tci
= htons(tci
);
415 cookie
.sflow
.output
= output
;
416 odp_put_userspace_action(pid
, &cookie
, actions
);
418 } else if (sscanf(s
, "userspace(pid=%lli,slow_path(%n", &pid
, &n
) > 0
420 union user_action_cookie cookie
;
422 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
423 cookie
.slow_path
.unused
= 0;
424 cookie
.slow_path
.reason
= 0;
426 while (s
[n
] != ')') {
429 for (bit
= 1; bit
; bit
<<= 1) {
430 const char *reason
= slow_path_reason_to_string(bit
);
431 size_t len
= strlen(reason
);
434 && !strncmp(s
+ n
, reason
, len
)
435 && (s
[n
+ len
] == ',' || s
[n
+ len
] == ')'))
437 cookie
.slow_path
.reason
|= bit
;
438 n
+= len
+ (s
[n
+ len
] == ',');
447 if (s
[n
+ 1] != ')') {
452 odp_put_userspace_action(pid
, &cookie
, actions
);
454 } else if (sscanf(s
, "userspace(pid=%lli,userdata="
455 "%31[x0123456789abcdefABCDEF])%n", &pid
, userdata_s
,
457 union user_action_cookie cookie
;
460 userdata
= strtoull(userdata_s
, NULL
, 0);
461 memcpy(&cookie
, &userdata
, sizeof cookie
);
462 odp_put_userspace_action(pid
, &cookie
, actions
);
467 if (!strncmp(s
, "set(", 4)) {
471 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
472 retval
= parse_odp_key_attr(s
+ 4, port_names
, actions
);
476 if (s
[retval
+ 4] != ')') {
479 nl_msg_end_nested(actions
, start_ofs
);
484 struct ovs_action_push_vlan push
;
485 int tpid
= ETH_TYPE_VLAN
;
490 if ((sscanf(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
) > 0
492 || (sscanf(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
493 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)
494 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
495 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)
496 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
497 &tpid
, &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
498 push
.vlan_tpid
= htons(tpid
);
499 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
500 | (pcp
<< VLAN_PCP_SHIFT
)
501 | (cfi
? VLAN_CFI
: 0));
502 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
509 if (!strncmp(s
, "pop_vlan", 8)) {
510 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
518 if (sscanf(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
) > 0
519 && percentage
>= 0. && percentage
<= 100.0
521 size_t sample_ofs
, actions_ofs
;
524 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
525 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
526 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
527 (probability
<= 0 ? 0
528 : probability
>= UINT32_MAX
? UINT32_MAX
531 actions_ofs
= nl_msg_start_nested(actions
,
532 OVS_SAMPLE_ATTR_ACTIONS
);
536 n
+= strspn(s
+ n
, delimiters
);
541 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
547 nl_msg_end_nested(actions
, actions_ofs
);
548 nl_msg_end_nested(actions
, sample_ofs
);
550 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
557 /* Parses the string representation of datapath actions, in the format output
558 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
559 * value. On success, the ODP actions are appended to 'actions' as a series of
560 * Netlink attributes. On failure, no data is appended to 'actions'. Either
561 * way, 'actions''s data might be reallocated. */
563 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
564 struct ofpbuf
*actions
)
568 if (!strcasecmp(s
, "drop")) {
572 old_size
= actions
->size
;
576 s
+= strspn(s
, delimiters
);
581 retval
= parse_odp_action(s
, port_names
, actions
);
582 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
583 actions
->size
= old_size
;
592 /* Returns the correct length of the payload for a flow key attribute of the
593 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
594 * is variable length. */
596 odp_flow_key_attr_len(uint16_t type
)
598 if (type
> OVS_KEY_ATTR_MAX
) {
602 switch ((enum ovs_key_attr
) type
) {
603 case OVS_KEY_ATTR_ENCAP
: return -2;
604 case OVS_KEY_ATTR_PRIORITY
: return 4;
605 case OVS_KEY_ATTR_TUN_ID
: return 8;
606 case OVS_KEY_ATTR_IPV4_TUNNEL
: return sizeof(struct ovs_key_ipv4_tunnel
);
607 case OVS_KEY_ATTR_IN_PORT
: return 4;
608 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
609 case OVS_KEY_ATTR_VLAN
: return sizeof(ovs_be16
);
610 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
611 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
612 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
613 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
614 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
615 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
616 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
617 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
618 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
620 case OVS_KEY_ATTR_UNSPEC
:
621 case __OVS_KEY_ATTR_MAX
:
629 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
631 size_t len
= nl_attr_get_size(a
);
633 const uint8_t *unspec
;
636 unspec
= nl_attr_get(a
);
637 for (i
= 0; i
< len
; i
++) {
638 ds_put_char(ds
, i
? ' ': '(');
639 ds_put_format(ds
, "%02x", unspec
[i
]);
641 ds_put_char(ds
, ')');
646 ovs_frag_type_to_string(enum ovs_frag_type type
)
649 case OVS_FRAG_TYPE_NONE
:
651 case OVS_FRAG_TYPE_FIRST
:
653 case OVS_FRAG_TYPE_LATER
:
655 case __OVS_FRAG_TYPE_MAX
:
662 format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
)
664 const struct ovs_key_ethernet
*eth_key
;
665 const struct ovs_key_ipv4
*ipv4_key
;
666 const struct ovs_key_ipv6
*ipv6_key
;
667 const struct ovs_key_tcp
*tcp_key
;
668 const struct ovs_key_udp
*udp_key
;
669 const struct ovs_key_icmp
*icmp_key
;
670 const struct ovs_key_icmpv6
*icmpv6_key
;
671 const struct ovs_key_arp
*arp_key
;
672 const struct ovs_key_nd
*nd_key
;
673 const struct ovs_key_ipv4_tunnel
*ipv4_tun_key
;
674 enum ovs_key_attr attr
= nl_attr_type(a
);
677 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
));
678 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
679 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
680 ds_put_format(ds
, "(bad length %zu, expected %d)",
682 odp_flow_key_attr_len(nl_attr_type(a
)));
683 format_generic_odp_key(a
, ds
);
688 case OVS_KEY_ATTR_ENCAP
:
689 ds_put_cstr(ds
, "(");
690 if (nl_attr_get_size(a
)) {
691 odp_flow_key_format(nl_attr_get(a
), nl_attr_get_size(a
), ds
);
693 ds_put_char(ds
, ')');
696 case OVS_KEY_ATTR_PRIORITY
:
697 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
700 case OVS_KEY_ATTR_TUN_ID
:
701 ds_put_format(ds
, "(%#"PRIx64
")", ntohll(nl_attr_get_be64(a
)));
704 case OVS_KEY_ATTR_IPV4_TUNNEL
:
705 ipv4_tun_key
= nl_attr_get(a
);
706 ds_put_format(ds
, "(tun_id=0x%"PRIx64
",flags=0x%"PRIx32
707 ",src="IP_FMT
",dst="IP_FMT
",tos=0x%"PRIx8
",ttl=%"PRIu8
")",
708 ntohll(ipv4_tun_key
->tun_id
), ipv4_tun_key
->tun_flags
,
709 IP_ARGS(&ipv4_tun_key
->ipv4_src
),
710 IP_ARGS(&ipv4_tun_key
->ipv4_dst
),
711 ipv4_tun_key
->ipv4_tos
, ipv4_tun_key
->ipv4_ttl
);
714 case OVS_KEY_ATTR_IN_PORT
:
715 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
718 case OVS_KEY_ATTR_ETHERNET
:
719 eth_key
= nl_attr_get(a
);
720 ds_put_format(ds
, "(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
721 ETH_ADDR_ARGS(eth_key
->eth_src
),
722 ETH_ADDR_ARGS(eth_key
->eth_dst
));
725 case OVS_KEY_ATTR_VLAN
:
726 ds_put_char(ds
, '(');
727 format_vlan_tci(ds
, nl_attr_get_be16(a
));
728 ds_put_char(ds
, ')');
731 case OVS_KEY_ATTR_ETHERTYPE
:
732 ds_put_format(ds
, "(0x%04"PRIx16
")",
733 ntohs(nl_attr_get_be16(a
)));
736 case OVS_KEY_ATTR_IPV4
:
737 ipv4_key
= nl_attr_get(a
);
738 ds_put_format(ds
, "(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
739 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s)",
740 IP_ARGS(&ipv4_key
->ipv4_src
),
741 IP_ARGS(&ipv4_key
->ipv4_dst
),
742 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
744 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
747 case OVS_KEY_ATTR_IPV6
: {
748 char src_str
[INET6_ADDRSTRLEN
];
749 char dst_str
[INET6_ADDRSTRLEN
];
751 ipv6_key
= nl_attr_get(a
);
752 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
753 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
755 ds_put_format(ds
, "(src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
756 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s)",
757 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
758 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
759 ipv6_key
->ipv6_hlimit
,
760 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
764 case OVS_KEY_ATTR_TCP
:
765 tcp_key
= nl_attr_get(a
);
766 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
767 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
770 case OVS_KEY_ATTR_UDP
:
771 udp_key
= nl_attr_get(a
);
772 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
773 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
776 case OVS_KEY_ATTR_ICMP
:
777 icmp_key
= nl_attr_get(a
);
778 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
779 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
782 case OVS_KEY_ATTR_ICMPV6
:
783 icmpv6_key
= nl_attr_get(a
);
784 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
785 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
788 case OVS_KEY_ATTR_ARP
:
789 arp_key
= nl_attr_get(a
);
790 ds_put_format(ds
, "(sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
791 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
")",
792 IP_ARGS(&arp_key
->arp_sip
), IP_ARGS(&arp_key
->arp_tip
),
793 ntohs(arp_key
->arp_op
), ETH_ADDR_ARGS(arp_key
->arp_sha
),
794 ETH_ADDR_ARGS(arp_key
->arp_tha
));
797 case OVS_KEY_ATTR_ND
: {
798 char target
[INET6_ADDRSTRLEN
];
800 nd_key
= nl_attr_get(a
);
801 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
803 ds_put_format(ds
, "(target=%s", target
);
804 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
805 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
806 ETH_ADDR_ARGS(nd_key
->nd_sll
));
808 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
809 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
810 ETH_ADDR_ARGS(nd_key
->nd_tll
));
812 ds_put_char(ds
, ')');
816 case OVS_KEY_ATTR_UNSPEC
:
817 case __OVS_KEY_ATTR_MAX
:
819 format_generic_odp_key(a
, ds
);
824 /* Appends to 'ds' a string representation of the 'key_len' bytes of
825 * OVS_KEY_ATTR_* attributes in 'key'. */
827 odp_flow_key_format(const struct nlattr
*key
, size_t key_len
, struct ds
*ds
)
830 const struct nlattr
*a
;
833 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
835 ds_put_char(ds
, ',');
837 format_odp_key_attr(a
, ds
);
842 if (left
== key_len
) {
843 ds_put_cstr(ds
, "<empty>");
845 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
846 for (i
= 0; i
< left
; i
++) {
847 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
849 ds_put_char(ds
, ')');
852 ds_put_cstr(ds
, "<empty>");
857 put_nd_key(int n
, const char *nd_target_s
,
858 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
860 struct ovs_key_nd nd_key
;
862 memset(&nd_key
, 0, sizeof nd_key
);
863 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
867 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
870 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
872 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
877 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
879 if (!strcasecmp(s
, "no")) {
880 *type
= OVS_FRAG_TYPE_NONE
;
881 } else if (!strcasecmp(s
, "first")) {
882 *type
= OVS_FRAG_TYPE_FIRST
;
883 } else if (!strcasecmp(s
, "later")) {
884 *type
= OVS_FRAG_TYPE_LATER
;
892 parse_odp_key_attr(const char *s
, const struct simap
*port_names
,
895 /* Many of the sscanf calls in this function use oversized destination
896 * fields because some sscanf() implementations truncate the range of %i
897 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
898 * value of 0x7fff. The other alternatives are to allow only a single
899 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
902 * The tun_id parser has to use an alternative approach because there is no
903 * type larger than 64 bits. */
906 unsigned long long int priority
;
909 if (sscanf(s
, "priority(%lli)%n", &priority
, &n
) > 0 && n
> 0) {
910 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
919 if (sscanf(s
, "tun_id(%31[x0123456789abcdefABCDEF])%n",
920 tun_id_s
, &n
) > 0 && n
> 0) {
921 uint64_t tun_id
= strtoull(tun_id_s
, NULL
, 0);
922 nl_msg_put_be64(key
, OVS_KEY_ATTR_TUN_ID
, htonll(tun_id
));
928 unsigned long long int in_port
;
931 if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
932 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
937 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
939 const struct simap_node
*node
;
943 name_len
= strcspn(s
, ")");
944 node
= simap_find_len(port_names
, name
, name_len
);
946 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, node
->data
);
947 return 8 + name_len
+ 1;
952 struct ovs_key_ethernet eth_key
;
956 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
957 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
958 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
959 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
960 ð_key
, sizeof eth_key
);
971 if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n", &vid
, &pcp
, &n
) > 0
973 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
974 htons((vid
<< VLAN_VID_SHIFT
) |
975 (pcp
<< VLAN_PCP_SHIFT
) |
978 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
979 &vid
, &pcp
, &cfi
, &n
) > 0
981 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
982 htons((vid
<< VLAN_VID_SHIFT
) |
983 (pcp
<< VLAN_PCP_SHIFT
) |
984 (cfi
? VLAN_CFI
: 0)));
993 if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
994 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1006 enum ovs_frag_type ipv4_frag
;
1009 if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
1010 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1011 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
1012 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
1014 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1015 struct ovs_key_ipv4 ipv4_key
;
1017 ipv4_key
.ipv4_src
= ipv4_src
;
1018 ipv4_key
.ipv4_dst
= ipv4_dst
;
1019 ipv4_key
.ipv4_proto
= ipv4_proto
;
1020 ipv4_key
.ipv4_tos
= ipv4_tos
;
1021 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1022 ipv4_key
.ipv4_frag
= ipv4_frag
;
1023 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1024 &ipv4_key
, sizeof ipv4_key
);
1030 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
1031 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
1037 enum ovs_frag_type ipv6_frag
;
1040 if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
1041 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1042 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
1043 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
1045 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1046 struct ovs_key_ipv6 ipv6_key
;
1048 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1049 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
1052 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1053 ipv6_key
.ipv6_proto
= ipv6_proto
;
1054 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1055 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1056 ipv6_key
.ipv6_frag
= ipv6_frag
;
1057 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1058 &ipv6_key
, sizeof ipv6_key
);
1068 if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
1070 struct ovs_key_tcp tcp_key
;
1072 tcp_key
.tcp_src
= htons(tcp_src
);
1073 tcp_key
.tcp_dst
= htons(tcp_dst
);
1074 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1084 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
1086 struct ovs_key_udp udp_key
;
1088 udp_key
.udp_src
= htons(udp_src
);
1089 udp_key
.udp_dst
= htons(udp_dst
);
1090 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1100 if (sscanf(s
, "icmp(type=%i,code=%i)%n",
1101 &icmp_type
, &icmp_code
, &n
) > 0
1103 struct ovs_key_icmp icmp_key
;
1105 icmp_key
.icmp_type
= icmp_type
;
1106 icmp_key
.icmp_code
= icmp_code
;
1107 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1108 &icmp_key
, sizeof icmp_key
);
1114 struct ovs_key_icmpv6 icmpv6_key
;
1117 if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
1118 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
1120 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
1121 &icmpv6_key
, sizeof icmpv6_key
);
1130 uint8_t arp_sha
[ETH_ADDR_LEN
];
1131 uint8_t arp_tha
[ETH_ADDR_LEN
];
1134 if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
1135 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
1136 IP_SCAN_ARGS(&arp_sip
),
1137 IP_SCAN_ARGS(&arp_tip
),
1139 ETH_ADDR_SCAN_ARGS(arp_sha
),
1140 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
1141 struct ovs_key_arp arp_key
;
1143 memset(&arp_key
, 0, sizeof arp_key
);
1144 arp_key
.arp_sip
= arp_sip
;
1145 arp_key
.arp_tip
= arp_tip
;
1146 arp_key
.arp_op
= htons(arp_op
);
1147 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
1148 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
1149 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
1155 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
1156 uint8_t nd_sll
[ETH_ADDR_LEN
];
1157 uint8_t nd_tll
[ETH_ADDR_LEN
];
1160 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
1161 nd_target_s
, &n
) > 0 && n
> 0) {
1162 return put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
1164 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
1165 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
1167 return put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
1169 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
1170 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1172 return put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
1174 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
1175 "tll="ETH_ADDR_SCAN_FMT
")%n",
1176 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
1177 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1179 return put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
1183 if (!strncmp(s
, "encap(", 6)) {
1184 const char *start
= s
;
1187 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
1193 s
+= strspn(s
, ", \t\r\n");
1196 } else if (*s
== ')') {
1200 retval
= parse_odp_key_attr(s
, port_names
, key
);
1208 nl_msg_end_nested(key
, encap
);
1216 /* Parses the string representation of a datapath flow key, in the
1217 * format output by odp_flow_key_format(). Returns 0 if successful,
1218 * otherwise a positive errno value. On success, the flow key is
1219 * appended to 'key' as a series of Netlink attributes. On failure, no
1220 * data is appended to 'key'. Either way, 'key''s data might be
1223 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1224 * to a port number. (Port names may be used instead of port numbers in
1227 * On success, the attributes appended to 'key' are individually syntactically
1228 * valid, but they may not be valid as a sequence. 'key' might, for example,
1229 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1231 odp_flow_key_from_string(const char *s
, const struct simap
*port_names
,
1234 const size_t old_size
= key
->size
;
1238 s
+= strspn(s
, delimiters
);
1243 retval
= parse_odp_key_attr(s
, port_names
, key
);
1245 key
->size
= old_size
;
1255 ovs_to_odp_frag(uint8_t nw_frag
)
1257 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
1258 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
1259 : OVS_FRAG_TYPE_LATER
);
1262 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1264 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1265 * capable of being expanded to allow for that much space. */
1267 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
)
1269 struct ovs_key_ethernet
*eth_key
;
1272 if (flow
->skb_priority
) {
1273 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, flow
->skb_priority
);
1276 if (flow
->tunnel
.tun_id
!= htonll(0)) {
1277 nl_msg_put_be64(buf
, OVS_KEY_ATTR_TUN_ID
, flow
->tunnel
.tun_id
);
1280 if (flow
->in_port
!= OFPP_NONE
&& flow
->in_port
!= OFPP_CONTROLLER
) {
1281 nl_msg_put_u32(buf
, OVS_KEY_ATTR_IN_PORT
,
1282 ofp_port_to_odp_port(flow
->in_port
));
1285 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
1287 memcpy(eth_key
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1288 memcpy(eth_key
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1290 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1291 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
1292 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, flow
->vlan_tci
);
1293 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
1294 if (flow
->vlan_tci
== htons(0)) {
1301 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
1305 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, flow
->dl_type
);
1307 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1308 struct ovs_key_ipv4
*ipv4_key
;
1310 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
1312 ipv4_key
->ipv4_src
= flow
->nw_src
;
1313 ipv4_key
->ipv4_dst
= flow
->nw_dst
;
1314 ipv4_key
->ipv4_proto
= flow
->nw_proto
;
1315 ipv4_key
->ipv4_tos
= flow
->nw_tos
;
1316 ipv4_key
->ipv4_ttl
= flow
->nw_ttl
;
1317 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1318 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1319 struct ovs_key_ipv6
*ipv6_key
;
1321 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
1323 memcpy(ipv6_key
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
1324 memcpy(ipv6_key
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
1325 ipv6_key
->ipv6_label
= flow
->ipv6_label
;
1326 ipv6_key
->ipv6_proto
= flow
->nw_proto
;
1327 ipv6_key
->ipv6_tclass
= flow
->nw_tos
;
1328 ipv6_key
->ipv6_hlimit
= flow
->nw_ttl
;
1329 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1330 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
1331 struct ovs_key_arp
*arp_key
;
1333 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
1335 memset(arp_key
, 0, sizeof *arp_key
);
1336 arp_key
->arp_sip
= flow
->nw_src
;
1337 arp_key
->arp_tip
= flow
->nw_dst
;
1338 arp_key
->arp_op
= htons(flow
->nw_proto
);
1339 memcpy(arp_key
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
1340 memcpy(arp_key
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
1343 if ((flow
->dl_type
== htons(ETH_TYPE_IP
)
1344 || flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1345 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1347 if (flow
->nw_proto
== IPPROTO_TCP
) {
1348 struct ovs_key_tcp
*tcp_key
;
1350 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
1352 tcp_key
->tcp_src
= flow
->tp_src
;
1353 tcp_key
->tcp_dst
= flow
->tp_dst
;
1354 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
1355 struct ovs_key_udp
*udp_key
;
1357 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
1359 udp_key
->udp_src
= flow
->tp_src
;
1360 udp_key
->udp_dst
= flow
->tp_dst
;
1361 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
1362 && flow
->nw_proto
== IPPROTO_ICMP
) {
1363 struct ovs_key_icmp
*icmp_key
;
1365 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
1367 icmp_key
->icmp_type
= ntohs(flow
->tp_src
);
1368 icmp_key
->icmp_code
= ntohs(flow
->tp_dst
);
1369 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1370 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
1371 struct ovs_key_icmpv6
*icmpv6_key
;
1373 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
1374 sizeof *icmpv6_key
);
1375 icmpv6_key
->icmpv6_type
= ntohs(flow
->tp_src
);
1376 icmpv6_key
->icmpv6_code
= ntohs(flow
->tp_dst
);
1378 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
1379 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
1380 struct ovs_key_nd
*nd_key
;
1382 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
1384 memcpy(nd_key
->nd_target
, &flow
->nd_target
,
1385 sizeof nd_key
->nd_target
);
1386 memcpy(nd_key
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
1387 memcpy(nd_key
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
1394 nl_msg_end_nested(buf
, encap
);
1399 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
1401 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
1402 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
1406 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
1407 uint64_t attrs
, int out_of_range_attr
,
1408 const struct nlattr
*key
, size_t key_len
)
1413 if (VLOG_DROP_DBG(rl
)) {
1418 for (i
= 0; i
< 64; i
++) {
1419 if (attrs
& (UINT64_C(1) << i
)) {
1420 ds_put_format(&s
, " %s", ovs_key_attr_to_string(i
));
1423 if (out_of_range_attr
) {
1424 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
1427 ds_put_cstr(&s
, ": ");
1428 odp_flow_key_format(key
, key_len
, &s
);
1430 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
1435 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
1437 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1439 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
1440 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
1444 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
1445 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
1446 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
1447 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
1454 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
1455 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
1456 int *out_of_range_attrp
)
1458 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1459 const struct nlattr
*nla
;
1460 uint64_t present_attrs
;
1464 *out_of_range_attrp
= 0;
1465 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
1466 uint16_t type
= nl_attr_type(nla
);
1467 size_t len
= nl_attr_get_size(nla
);
1468 int expected_len
= odp_flow_key_attr_len(type
);
1470 if (len
!= expected_len
&& expected_len
>= 0) {
1471 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
1472 "length %d", ovs_key_attr_to_string(type
),
1477 if (type
>= CHAR_BIT
* sizeof present_attrs
) {
1478 *out_of_range_attrp
= type
;
1480 if (present_attrs
& (UINT64_C(1) << type
)) {
1481 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
1482 ovs_key_attr_to_string(type
));
1486 present_attrs
|= UINT64_C(1) << type
;
1491 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
1495 *present_attrsp
= present_attrs
;
1499 static enum odp_key_fitness
1500 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
1501 uint64_t expected_attrs
,
1502 const struct nlattr
*key
, size_t key_len
)
1504 uint64_t missing_attrs
;
1505 uint64_t extra_attrs
;
1507 missing_attrs
= expected_attrs
& ~present_attrs
;
1508 if (missing_attrs
) {
1509 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1510 log_odp_key_attributes(&rl
, "expected but not present",
1511 missing_attrs
, 0, key
, key_len
);
1512 return ODP_FIT_TOO_LITTLE
;
1515 extra_attrs
= present_attrs
& ~expected_attrs
;
1516 if (extra_attrs
|| out_of_range_attr
) {
1517 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1518 log_odp_key_attributes(&rl
, "present but not expected",
1519 extra_attrs
, out_of_range_attr
, key
, key_len
);
1520 return ODP_FIT_TOO_MUCH
;
1523 return ODP_FIT_PERFECT
;
1527 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1528 uint64_t present_attrs
, uint64_t *expected_attrs
,
1531 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1533 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
1534 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
1535 if (ntohs(flow
->dl_type
) < 1536) {
1536 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
1537 ntohs(flow
->dl_type
));
1540 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
1542 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
1547 static enum odp_key_fitness
1548 parse_l3_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1549 uint64_t present_attrs
, int out_of_range_attr
,
1550 uint64_t expected_attrs
, struct flow
*flow
,
1551 const struct nlattr
*key
, size_t key_len
)
1553 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1555 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1556 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
1557 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1558 const struct ovs_key_ipv4
*ipv4_key
;
1560 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
1561 flow
->nw_src
= ipv4_key
->ipv4_src
;
1562 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
1563 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
1564 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
1565 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
1566 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
1567 return ODP_FIT_ERROR
;
1570 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1571 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
1572 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1573 const struct ovs_key_ipv6
*ipv6_key
;
1575 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
1576 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
1577 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
1578 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
1579 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
1580 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
1581 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
1582 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
1583 return ODP_FIT_ERROR
;
1586 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
1587 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
1588 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1589 const struct ovs_key_arp
*arp_key
;
1591 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
1592 flow
->nw_src
= arp_key
->arp_sip
;
1593 flow
->nw_dst
= arp_key
->arp_tip
;
1594 if (arp_key
->arp_op
& htons(0xff00)) {
1595 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
1596 "key", ntohs(arp_key
->arp_op
));
1597 return ODP_FIT_ERROR
;
1599 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
1600 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
1601 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
1605 if (flow
->nw_proto
== IPPROTO_TCP
1606 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1607 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1608 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1609 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
1610 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
1611 const struct ovs_key_tcp
*tcp_key
;
1613 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
1614 flow
->tp_src
= tcp_key
->tcp_src
;
1615 flow
->tp_dst
= tcp_key
->tcp_dst
;
1617 } else if (flow
->nw_proto
== IPPROTO_UDP
1618 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1619 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1620 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1621 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
1622 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
1623 const struct ovs_key_udp
*udp_key
;
1625 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
1626 flow
->tp_src
= udp_key
->udp_src
;
1627 flow
->tp_dst
= udp_key
->udp_dst
;
1629 } else if (flow
->nw_proto
== IPPROTO_ICMP
1630 && flow
->dl_type
== htons(ETH_TYPE_IP
)
1631 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1632 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
1633 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
1634 const struct ovs_key_icmp
*icmp_key
;
1636 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
1637 flow
->tp_src
= htons(icmp_key
->icmp_type
);
1638 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
1640 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
1641 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1642 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1643 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
1644 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
1645 const struct ovs_key_icmpv6
*icmpv6_key
;
1647 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
1648 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
1649 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
1651 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
1652 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
1653 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
1654 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
1655 const struct ovs_key_nd
*nd_key
;
1657 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
1658 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
1659 sizeof flow
->nd_target
);
1660 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
1661 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
1667 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
1671 /* Parse 802.1Q header then encapsulated L3 attributes. */
1672 static enum odp_key_fitness
1673 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1674 uint64_t present_attrs
, int out_of_range_attr
,
1675 uint64_t expected_attrs
, struct flow
*flow
,
1676 const struct nlattr
*key
, size_t key_len
)
1678 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1680 const struct nlattr
*encap
1681 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
1682 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
1683 enum odp_key_fitness encap_fitness
;
1684 enum odp_key_fitness fitness
;
1687 /* Calulate fitness of outer attributes. */
1688 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
1689 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
1690 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
1691 expected_attrs
, key
, key_len
);
1693 /* Get the VLAN TCI value. */
1694 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
1695 return ODP_FIT_TOO_LITTLE
;
1697 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
1698 if (tci
== htons(0)) {
1699 /* Corner case for a truncated 802.1Q header. */
1700 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
1701 return ODP_FIT_TOO_MUCH
;
1704 } else if (!(tci
& htons(VLAN_CFI
))) {
1705 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
1706 "but CFI bit is not set", ntohs(tci
));
1707 return ODP_FIT_ERROR
;
1711 * Remove the TPID from dl_type since it's not the real Ethertype. */
1712 flow
->vlan_tci
= tci
;
1713 flow
->dl_type
= htons(0);
1715 /* Now parse the encapsulated attributes. */
1716 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
1717 attrs
, &present_attrs
, &out_of_range_attr
)) {
1718 return ODP_FIT_ERROR
;
1722 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
1723 return ODP_FIT_ERROR
;
1725 encap_fitness
= parse_l3_onward(attrs
, present_attrs
, out_of_range_attr
,
1726 expected_attrs
, flow
, key
, key_len
);
1728 /* The overall fitness is the worse of the outer and inner attributes. */
1729 return MAX(fitness
, encap_fitness
);
1732 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1733 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1734 * 'key' fits our expectations for what a flow key should contain.
1736 * This function doesn't take the packet itself as an argument because none of
1737 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
1738 * it is always possible to infer which additional attribute(s) should appear
1739 * by looking at the attributes for lower-level protocols, e.g. if the network
1740 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1741 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1742 * must be absent. */
1743 enum odp_key_fitness
1744 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
1747 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1748 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
1749 uint64_t expected_attrs
;
1750 uint64_t present_attrs
;
1751 int out_of_range_attr
;
1753 memset(flow
, 0, sizeof *flow
);
1755 /* Parse attributes. */
1756 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
1757 &out_of_range_attr
)) {
1758 return ODP_FIT_ERROR
;
1763 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
1764 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
1765 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
1768 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID
)) {
1769 flow
->tunnel
.tun_id
= nl_attr_get_be64(attrs
[OVS_KEY_ATTR_TUN_ID
]);
1770 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID
;
1773 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
1774 uint32_t in_port
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_IN_PORT
]);
1775 if (in_port
>= UINT16_MAX
|| in_port
>= OFPP_MAX
) {
1776 VLOG_ERR_RL(&rl
, "in_port %"PRIu32
" out of supported range",
1778 return ODP_FIT_ERROR
;
1780 flow
->in_port
= odp_port_to_ofp_port(in_port
);
1781 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
1783 flow
->in_port
= OFPP_NONE
;
1786 /* Ethernet header. */
1787 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
1788 const struct ovs_key_ethernet
*eth_key
;
1790 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
1791 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
1792 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
1794 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
1796 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1797 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
1798 return ODP_FIT_ERROR
;
1801 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1802 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
1803 expected_attrs
, flow
, key
, key_len
);
1805 return parse_l3_onward(attrs
, present_attrs
, out_of_range_attr
,
1806 expected_attrs
, flow
, key
, key_len
);
1809 /* Returns 'fitness' as a string, for use in debug messages. */
1811 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
1814 case ODP_FIT_PERFECT
:
1816 case ODP_FIT_TOO_MUCH
:
1818 case ODP_FIT_TOO_LITTLE
:
1819 return "too_little";
1827 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1828 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1829 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1830 * the start of the cookie. (If 'cookie' is null, then the return value is not
1833 odp_put_userspace_action(uint32_t pid
, const union user_action_cookie
*cookie
,
1834 struct ofpbuf
*odp_actions
)
1838 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
1839 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
1841 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
1842 cookie
, sizeof *cookie
);
1844 nl_msg_end_nested(odp_actions
, offset
);
1846 return cookie
? odp_actions
->size
- NLA_ALIGN(sizeof *cookie
) : 0;
1849 /* The commit_odp_actions() function and its helpers. */
1852 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
1853 const void *key
, size_t key_size
)
1855 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
1856 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
1857 nl_msg_end_nested(odp_actions
, offset
);
1861 commit_set_tun_id_action(const struct flow
*flow
, struct flow
*base
,
1862 struct ofpbuf
*odp_actions
)
1864 if (base
->tunnel
.tun_id
== flow
->tunnel
.tun_id
) {
1867 base
->tunnel
.tun_id
= flow
->tunnel
.tun_id
;
1869 commit_set_action(odp_actions
, OVS_KEY_ATTR_TUN_ID
,
1870 &base
->tunnel
.tun_id
, sizeof(base
->tunnel
.tun_id
));
1874 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
1875 struct ofpbuf
*odp_actions
)
1877 struct ovs_key_ethernet eth_key
;
1879 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
1880 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
1884 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1885 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1887 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
1888 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
1890 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
1891 ð_key
, sizeof(eth_key
));
1895 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
1896 struct ofpbuf
*odp_actions
)
1898 if (base
->vlan_tci
== flow
->vlan_tci
) {
1902 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
1903 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
1906 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
1907 struct ovs_action_push_vlan vlan
;
1909 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
1910 vlan
.vlan_tci
= flow
->vlan_tci
;
1911 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1912 &vlan
, sizeof vlan
);
1914 base
->vlan_tci
= flow
->vlan_tci
;
1918 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
1919 struct ofpbuf
*odp_actions
)
1921 struct ovs_key_ipv4 ipv4_key
;
1923 if (base
->nw_src
== flow
->nw_src
&&
1924 base
->nw_dst
== flow
->nw_dst
&&
1925 base
->nw_tos
== flow
->nw_tos
&&
1926 base
->nw_ttl
== flow
->nw_ttl
&&
1927 base
->nw_frag
== flow
->nw_frag
) {
1931 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
1932 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
1933 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
1934 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
1935 ipv4_key
.ipv4_proto
= base
->nw_proto
;
1936 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
1938 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
1939 &ipv4_key
, sizeof(ipv4_key
));
1943 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
1944 struct ofpbuf
*odp_actions
)
1946 struct ovs_key_ipv6 ipv6_key
;
1948 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
1949 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
1950 base
->ipv6_label
== flow
->ipv6_label
&&
1951 base
->nw_tos
== flow
->nw_tos
&&
1952 base
->nw_ttl
== flow
->nw_ttl
&&
1953 base
->nw_frag
== flow
->nw_frag
) {
1957 base
->ipv6_src
= flow
->ipv6_src
;
1958 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
1959 base
->ipv6_dst
= flow
->ipv6_dst
;
1960 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
1962 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
1963 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
1964 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
1965 ipv6_key
.ipv6_proto
= base
->nw_proto
;
1966 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
1968 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
1969 &ipv6_key
, sizeof(ipv6_key
));
1973 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
1974 struct ofpbuf
*odp_actions
)
1976 /* Check if flow really have an IP header. */
1977 if (!flow
->nw_proto
) {
1981 if (base
->dl_type
== htons(ETH_TYPE_IP
)) {
1982 commit_set_ipv4_action(flow
, base
, odp_actions
);
1983 } else if (base
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1984 commit_set_ipv6_action(flow
, base
, odp_actions
);
1989 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
1990 struct ofpbuf
*odp_actions
)
1992 if (!base
->tp_src
&& !base
->tp_dst
) {
1996 if (base
->tp_src
== flow
->tp_src
&&
1997 base
->tp_dst
== flow
->tp_dst
) {
2001 if (flow
->nw_proto
== IPPROTO_TCP
) {
2002 struct ovs_key_tcp port_key
;
2004 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
2005 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
2007 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
2008 &port_key
, sizeof(port_key
));
2010 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
2011 struct ovs_key_udp port_key
;
2013 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
2014 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
2016 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
2017 &port_key
, sizeof(port_key
));
2022 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
2023 struct ofpbuf
*odp_actions
)
2025 if (base
->skb_priority
== flow
->skb_priority
) {
2028 base
->skb_priority
= flow
->skb_priority
;
2030 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
2031 &base
->skb_priority
, sizeof(base
->skb_priority
));
2034 /* If any of the flow key data that ODP actions can modify are different in
2035 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2036 * key from 'base' into 'flow', and then changes 'base' the same way. */
2038 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
2039 struct ofpbuf
*odp_actions
)
2041 commit_set_tun_id_action(flow
, base
, odp_actions
);
2042 commit_set_ether_addr_action(flow
, base
, odp_actions
);
2043 commit_vlan_action(flow
, base
, odp_actions
);
2044 commit_set_nw_action(flow
, base
, odp_actions
);
2045 commit_set_port_action(flow
, base
, odp_actions
);
2046 commit_set_priority_action(flow
, base
, odp_actions
);