2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
29 #include "dynamic-string.h"
33 #include "openvswitch/tunnel.h"
40 VLOG_DEFINE_THIS_MODULE(odp_util
);
42 /* The interface between userspace and kernel uses an "OVS_*" prefix.
43 * Since this is fairly non-specific for the OVS userspace components,
44 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
45 * interactions with the datapath.
48 /* The set of characters that may separate one action or one key attribute
50 static const char *delimiters
= ", \t\r\n";
52 static int parse_odp_key_attr(const char *, const struct shash
*port_names
,
54 static void format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
);
56 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
59 * - For an action whose argument has a fixed length, returned that
60 * nonnegative length in bytes.
62 * - For an action with a variable-length argument, returns -2.
64 * - For an invalid 'type', returns -1. */
66 odp_action_len(uint16_t type
)
68 if (type
> OVS_ACTION_ATTR_MAX
) {
72 switch ((enum ovs_action_attr
) type
) {
73 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
74 case OVS_ACTION_ATTR_USERSPACE
: return -2;
75 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
76 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
77 case OVS_ACTION_ATTR_SET
: return -2;
78 case OVS_ACTION_ATTR_SAMPLE
: return -2;
80 case OVS_ACTION_ATTR_UNSPEC
:
81 case __OVS_ACTION_ATTR_MAX
:
89 ovs_key_attr_to_string(enum ovs_key_attr attr
)
91 static char unknown_attr
[3 + INT_STRLEN(unsigned int) + 1];
94 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
95 case OVS_KEY_ATTR_ENCAP
: return "encap";
96 case OVS_KEY_ATTR_PRIORITY
: return "priority";
97 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
98 case OVS_KEY_ATTR_ETHERNET
: return "eth";
99 case OVS_KEY_ATTR_VLAN
: return "vlan";
100 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
101 case OVS_KEY_ATTR_IPV4
: return "ipv4";
102 case OVS_KEY_ATTR_IPV6
: return "ipv6";
103 case OVS_KEY_ATTR_TCP
: return "tcp";
104 case OVS_KEY_ATTR_UDP
: return "udp";
105 case OVS_KEY_ATTR_ICMP
: return "icmp";
106 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
107 case OVS_KEY_ATTR_ARP
: return "arp";
108 case OVS_KEY_ATTR_ND
: return "nd";
109 case OVS_KEY_ATTR_TUN_ID
: return "tun_id";
111 case __OVS_KEY_ATTR_MAX
:
113 snprintf(unknown_attr
, sizeof unknown_attr
, "key%u",
114 (unsigned int) attr
);
120 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
122 size_t len
= nl_attr_get_size(a
);
124 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
126 const uint8_t *unspec
;
129 unspec
= nl_attr_get(a
);
130 for (i
= 0; i
< len
; i
++) {
131 ds_put_char(ds
, i
? ' ': '(');
132 ds_put_format(ds
, "%02x", unspec
[i
]);
134 ds_put_char(ds
, ')');
139 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
141 static const struct nl_policy ovs_sample_policy
[] = {
142 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
143 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
145 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
147 const struct nlattr
*nla_acts
;
150 ds_put_cstr(ds
, "sample");
152 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
153 ds_put_cstr(ds
, "(error)");
157 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
160 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
162 ds_put_cstr(ds
, "actions(");
163 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
164 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
165 format_odp_actions(ds
, nla_acts
, len
);
166 ds_put_format(ds
, "))");
170 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
172 static const struct nl_policy ovs_userspace_policy
[] = {
173 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
174 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_U64
, .optional
= true },
176 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
178 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
179 ds_put_cstr(ds
, "userspace(error)");
183 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
184 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
186 if (a
[OVS_USERSPACE_ATTR_USERDATA
]) {
187 uint64_t userdata
= nl_attr_get_u64(a
[OVS_USERSPACE_ATTR_USERDATA
]);
188 struct user_action_cookie cookie
;
190 memcpy(&cookie
, &userdata
, sizeof cookie
);
192 switch (cookie
.type
) {
193 case USER_ACTION_COOKIE_SFLOW
:
194 ds_put_format(ds
, ",sFlow,n_output=%"PRIu8
","
195 "vid=%"PRIu16
",pcp=%"PRIu8
",ifindex=%"PRIu32
,
196 cookie
.n_output
, vlan_tci_to_vid(cookie
.vlan_tci
),
197 vlan_tci_to_pcp(cookie
.vlan_tci
), cookie
.data
);
200 case USER_ACTION_COOKIE_UNSPEC
:
202 ds_put_format(ds
, ",userdata=0x%"PRIx64
, userdata
);
206 ds_put_char(ds
, ')');
210 format_vlan_tci(struct ds
*ds
, ovs_be16 vlan_tci
)
212 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d",
213 vlan_tci_to_vid(vlan_tci
),
214 vlan_tci_to_pcp(vlan_tci
));
215 if (!(vlan_tci
& htons(VLAN_CFI
))) {
216 ds_put_cstr(ds
, ",cfi=0");
221 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
224 enum ovs_action_attr type
= nl_attr_type(a
);
225 const struct ovs_action_push_vlan
*vlan
;
227 expected_len
= odp_action_len(nl_attr_type(a
));
228 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
229 ds_put_format(ds
, "bad length %zu, expected %d for: ",
230 nl_attr_get_size(a
), expected_len
);
231 format_generic_odp_action(ds
, a
);
236 case OVS_ACTION_ATTR_OUTPUT
:
237 ds_put_format(ds
, "%"PRIu16
, nl_attr_get_u32(a
));
239 case OVS_ACTION_ATTR_USERSPACE
:
240 format_odp_userspace_action(ds
, a
);
242 case OVS_ACTION_ATTR_SET
:
243 ds_put_cstr(ds
, "set(");
244 format_odp_key_attr(nl_attr_get(a
), ds
);
245 ds_put_cstr(ds
, ")");
247 case OVS_ACTION_ATTR_PUSH_VLAN
:
248 vlan
= nl_attr_get(a
);
249 ds_put_cstr(ds
, "push_vlan(");
250 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
251 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
253 format_vlan_tci(ds
, vlan
->vlan_tci
);
254 ds_put_char(ds
, ')');
256 case OVS_ACTION_ATTR_POP_VLAN
:
257 ds_put_cstr(ds
, "pop_vlan");
259 case OVS_ACTION_ATTR_SAMPLE
:
260 format_odp_sample_action(ds
, a
);
262 case OVS_ACTION_ATTR_UNSPEC
:
263 case __OVS_ACTION_ATTR_MAX
:
265 format_generic_odp_action(ds
, a
);
271 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
275 const struct nlattr
*a
;
278 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
280 ds_put_char(ds
, ',');
282 format_odp_action(ds
, a
);
287 if (left
== actions_len
) {
288 ds_put_cstr(ds
, "<empty>");
290 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
291 for (i
= 0; i
< left
; i
++) {
292 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
294 ds_put_char(ds
, ')');
297 ds_put_cstr(ds
, "drop");
302 parse_odp_action(const char *s
, const struct shash
*port_names
,
303 struct ofpbuf
*actions
)
305 /* Many of the sscanf calls in this function use oversized destination
306 * fields because some sscanf() implementations truncate the range of %i
307 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
308 * value of 0x7fff. The other alternatives are to allow only a single
309 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
312 * The tun_id parser has to use an alternative approach because there is no
313 * type larger than 64 bits. */
316 unsigned long long int port
;
319 if (sscanf(s
, "%lli%n", &port
, &n
) > 0 && n
> 0) {
320 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
326 int len
= strcspn(s
, delimiters
);
327 struct shash_node
*node
;
329 node
= shash_find_len(port_names
, s
, len
);
331 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
,
332 (uintptr_t) node
->data
);
338 unsigned long long int pid
;
339 unsigned long long int ifindex
;
345 if (sscanf(s
, "userspace(pid=%lli)%n", &pid
, &n
) > 0 && n
> 0) {
346 odp_put_userspace_action(pid
, NULL
, actions
);
348 } else if (sscanf(s
, "userspace(pid=%lli,sFlow,n_output=%i,vid=%i,"
349 "pcp=%i,ifindex=%lli)%n", &pid
, &n_output
,
350 &vid
, &pcp
, &ifindex
, &n
) > 0 && n
> 0) {
351 struct user_action_cookie cookie
;
354 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
359 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
360 cookie
.n_output
= n_output
;
361 cookie
.vlan_tci
= htons(tci
);
362 cookie
.data
= ifindex
;
363 odp_put_userspace_action(pid
, &cookie
, actions
);
365 } else if (sscanf(s
, "userspace(pid=%lli,userdata="
366 "%31[x0123456789abcdefABCDEF])%n", &pid
, userdata_s
,
368 struct user_action_cookie cookie
;
371 userdata
= strtoull(userdata_s
, NULL
, 0);
372 memcpy(&cookie
, &userdata
, sizeof cookie
);
373 odp_put_userspace_action(pid
, &cookie
, actions
);
378 if (!strncmp(s
, "set(", 4)) {
382 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
383 retval
= parse_odp_key_attr(s
+ 4, port_names
, actions
);
387 if (s
[retval
+ 4] != ')') {
390 nl_msg_end_nested(actions
, start_ofs
);
395 struct ovs_action_push_vlan push
;
396 int tpid
= ETH_TYPE_VLAN
;
401 if ((sscanf(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
) > 0
403 || (sscanf(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
404 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)
405 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
406 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)
407 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
408 &tpid
, &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
409 push
.vlan_tpid
= htons(tpid
);
410 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
411 | (pcp
<< VLAN_PCP_SHIFT
)
412 | (cfi
? VLAN_CFI
: 0));
413 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
420 if (!strncmp(s
, "pop_vlan", 8)) {
421 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
429 if (sscanf(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
) > 0
430 && percentage
>= 0. && percentage
<= 100.0
432 size_t sample_ofs
, actions_ofs
;
435 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
436 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
437 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
438 (probability
<= 0 ? 0
439 : probability
>= UINT32_MAX
? UINT32_MAX
442 actions_ofs
= nl_msg_start_nested(actions
,
443 OVS_SAMPLE_ATTR_ACTIONS
);
447 s
+= strspn(s
, delimiters
);
452 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
459 nl_msg_end_nested(actions
, actions_ofs
);
460 nl_msg_end_nested(actions
, sample_ofs
);
462 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
469 /* Parses the string representation of datapath actions, in the format output
470 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
471 * value. On success, the ODP actions are appended to 'actions' as a series of
472 * Netlink attributes. On failure, no data is appended to 'actions'. Either
473 * way, 'actions''s data might be reallocated. */
475 odp_actions_from_string(const char *s
, const struct shash
*port_names
,
476 struct ofpbuf
*actions
)
480 if (!strcasecmp(s
, "drop")) {
484 old_size
= actions
->size
;
488 s
+= strspn(s
, delimiters
);
493 retval
= parse_odp_action(s
, port_names
, actions
);
494 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
495 actions
->size
= old_size
;
504 /* Returns the correct length of the payload for a flow key attribute of the
505 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
506 * is variable length. */
508 odp_flow_key_attr_len(uint16_t type
)
510 if (type
> OVS_KEY_ATTR_MAX
) {
514 switch ((enum ovs_key_attr
) type
) {
515 case OVS_KEY_ATTR_ENCAP
: return -2;
516 case OVS_KEY_ATTR_PRIORITY
: return 4;
517 case OVS_KEY_ATTR_TUN_ID
: return 8;
518 case OVS_KEY_ATTR_IN_PORT
: return 4;
519 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
520 case OVS_KEY_ATTR_VLAN
: return sizeof(ovs_be16
);
521 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
522 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
523 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
524 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
525 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
526 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
527 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
528 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
529 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
531 case OVS_KEY_ATTR_UNSPEC
:
532 case __OVS_KEY_ATTR_MAX
:
540 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
542 size_t len
= nl_attr_get_size(a
);
544 const uint8_t *unspec
;
547 unspec
= nl_attr_get(a
);
548 for (i
= 0; i
< len
; i
++) {
549 ds_put_char(ds
, i
? ' ': '(');
550 ds_put_format(ds
, "%02x", unspec
[i
]);
552 ds_put_char(ds
, ')');
557 ovs_frag_type_to_string(enum ovs_frag_type type
)
560 case OVS_FRAG_TYPE_NONE
:
562 case OVS_FRAG_TYPE_FIRST
:
564 case OVS_FRAG_TYPE_LATER
:
566 case __OVS_FRAG_TYPE_MAX
:
573 format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
)
575 const struct ovs_key_ethernet
*eth_key
;
576 const struct ovs_key_ipv4
*ipv4_key
;
577 const struct ovs_key_ipv6
*ipv6_key
;
578 const struct ovs_key_tcp
*tcp_key
;
579 const struct ovs_key_udp
*udp_key
;
580 const struct ovs_key_icmp
*icmp_key
;
581 const struct ovs_key_icmpv6
*icmpv6_key
;
582 const struct ovs_key_arp
*arp_key
;
583 const struct ovs_key_nd
*nd_key
;
584 enum ovs_key_attr attr
= nl_attr_type(a
);
587 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
));
588 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
589 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
590 ds_put_format(ds
, "(bad length %zu, expected %d)",
592 odp_flow_key_attr_len(nl_attr_type(a
)));
593 format_generic_odp_key(a
, ds
);
598 case OVS_KEY_ATTR_ENCAP
:
599 ds_put_cstr(ds
, "(");
600 if (nl_attr_get_size(a
)) {
601 odp_flow_key_format(nl_attr_get(a
), nl_attr_get_size(a
), ds
);
603 ds_put_char(ds
, ')');
606 case OVS_KEY_ATTR_PRIORITY
:
607 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
610 case OVS_KEY_ATTR_TUN_ID
:
611 ds_put_format(ds
, "(%#"PRIx64
")", ntohll(nl_attr_get_be64(a
)));
614 case OVS_KEY_ATTR_IN_PORT
:
615 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
618 case OVS_KEY_ATTR_ETHERNET
:
619 eth_key
= nl_attr_get(a
);
620 ds_put_format(ds
, "(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
621 ETH_ADDR_ARGS(eth_key
->eth_src
),
622 ETH_ADDR_ARGS(eth_key
->eth_dst
));
625 case OVS_KEY_ATTR_VLAN
:
626 ds_put_char(ds
, '(');
627 format_vlan_tci(ds
, nl_attr_get_be16(a
));
628 ds_put_char(ds
, ')');
631 case OVS_KEY_ATTR_ETHERTYPE
:
632 ds_put_format(ds
, "(0x%04"PRIx16
")",
633 ntohs(nl_attr_get_be16(a
)));
636 case OVS_KEY_ATTR_IPV4
:
637 ipv4_key
= nl_attr_get(a
);
638 ds_put_format(ds
, "(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
639 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s)",
640 IP_ARGS(&ipv4_key
->ipv4_src
),
641 IP_ARGS(&ipv4_key
->ipv4_dst
),
642 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
644 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
647 case OVS_KEY_ATTR_IPV6
: {
648 char src_str
[INET6_ADDRSTRLEN
];
649 char dst_str
[INET6_ADDRSTRLEN
];
651 ipv6_key
= nl_attr_get(a
);
652 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
653 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
655 ds_put_format(ds
, "(src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
656 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s)",
657 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
658 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
659 ipv6_key
->ipv6_hlimit
,
660 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
664 case OVS_KEY_ATTR_TCP
:
665 tcp_key
= nl_attr_get(a
);
666 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
667 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
670 case OVS_KEY_ATTR_UDP
:
671 udp_key
= nl_attr_get(a
);
672 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
673 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
676 case OVS_KEY_ATTR_ICMP
:
677 icmp_key
= nl_attr_get(a
);
678 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
679 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
682 case OVS_KEY_ATTR_ICMPV6
:
683 icmpv6_key
= nl_attr_get(a
);
684 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
685 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
688 case OVS_KEY_ATTR_ARP
:
689 arp_key
= nl_attr_get(a
);
690 ds_put_format(ds
, "(sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
691 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
")",
692 IP_ARGS(&arp_key
->arp_sip
), IP_ARGS(&arp_key
->arp_tip
),
693 ntohs(arp_key
->arp_op
), ETH_ADDR_ARGS(arp_key
->arp_sha
),
694 ETH_ADDR_ARGS(arp_key
->arp_tha
));
697 case OVS_KEY_ATTR_ND
: {
698 char target
[INET6_ADDRSTRLEN
];
700 nd_key
= nl_attr_get(a
);
701 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
703 ds_put_format(ds
, "(target=%s", target
);
704 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
705 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
706 ETH_ADDR_ARGS(nd_key
->nd_sll
));
708 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
709 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
710 ETH_ADDR_ARGS(nd_key
->nd_tll
));
712 ds_put_char(ds
, ')');
716 case OVS_KEY_ATTR_UNSPEC
:
717 case __OVS_KEY_ATTR_MAX
:
719 format_generic_odp_key(a
, ds
);
724 /* Appends to 'ds' a string representation of the 'key_len' bytes of
725 * OVS_KEY_ATTR_* attributes in 'key'. */
727 odp_flow_key_format(const struct nlattr
*key
, size_t key_len
, struct ds
*ds
)
730 const struct nlattr
*a
;
733 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
735 ds_put_char(ds
, ',');
737 format_odp_key_attr(a
, ds
);
742 if (left
== key_len
) {
743 ds_put_cstr(ds
, "<empty>");
745 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
746 for (i
= 0; i
< left
; i
++) {
747 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
749 ds_put_char(ds
, ')');
752 ds_put_cstr(ds
, "<empty>");
757 put_nd_key(int n
, const char *nd_target_s
,
758 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
760 struct ovs_key_nd nd_key
;
762 memset(&nd_key
, 0, sizeof nd_key
);
763 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
767 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
770 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
772 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
777 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
779 if (!strcasecmp(s
, "no")) {
780 *type
= OVS_FRAG_TYPE_NONE
;
781 } else if (!strcasecmp(s
, "first")) {
782 *type
= OVS_FRAG_TYPE_FIRST
;
783 } else if (!strcasecmp(s
, "later")) {
784 *type
= OVS_FRAG_TYPE_LATER
;
792 parse_odp_key_attr(const char *s
, const struct shash
*port_names
,
795 /* Many of the sscanf calls in this function use oversized destination
796 * fields because some sscanf() implementations truncate the range of %i
797 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
798 * value of 0x7fff. The other alternatives are to allow only a single
799 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
802 * The tun_id parser has to use an alternative approach because there is no
803 * type larger than 64 bits. */
806 unsigned long long int priority
;
809 if (sscanf(s
, "priority(%lli)%n", &priority
, &n
) > 0 && n
> 0) {
810 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
819 if (sscanf(s
, "tun_id(%31[x0123456789abcdefABCDEF])%n",
820 tun_id_s
, &n
) > 0 && n
> 0) {
821 uint64_t tun_id
= strtoull(tun_id_s
, NULL
, 0);
822 nl_msg_put_be64(key
, OVS_KEY_ATTR_TUN_ID
, htonll(tun_id
));
828 unsigned long long int in_port
;
831 if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
832 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
837 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
839 const struct shash_node
*node
;
843 name_len
= strcspn(s
, ")");
844 node
= shash_find_len(port_names
, name
, name_len
);
846 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, (uintptr_t) node
->data
);
847 return 8 + name_len
+ 1;
852 struct ovs_key_ethernet eth_key
;
856 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
857 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
858 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
859 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
860 ð_key
, sizeof eth_key
);
871 if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n", &vid
, &pcp
, &n
) > 0
873 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
874 htons((vid
<< VLAN_VID_SHIFT
) |
875 (pcp
<< VLAN_PCP_SHIFT
) |
878 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
879 &vid
, &pcp
, &cfi
, &n
) > 0
881 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
882 htons((vid
<< VLAN_VID_SHIFT
) |
883 (pcp
<< VLAN_PCP_SHIFT
) |
884 (cfi
? VLAN_CFI
: 0)));
893 if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
894 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
906 enum ovs_frag_type ipv4_frag
;
909 if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
910 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
911 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
912 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
914 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
915 struct ovs_key_ipv4 ipv4_key
;
917 ipv4_key
.ipv4_src
= ipv4_src
;
918 ipv4_key
.ipv4_dst
= ipv4_dst
;
919 ipv4_key
.ipv4_proto
= ipv4_proto
;
920 ipv4_key
.ipv4_tos
= ipv4_tos
;
921 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
922 ipv4_key
.ipv4_frag
= ipv4_frag
;
923 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
924 &ipv4_key
, sizeof ipv4_key
);
930 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
931 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
937 enum ovs_frag_type ipv6_frag
;
940 if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
941 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
942 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
943 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
945 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
946 struct ovs_key_ipv6 ipv6_key
;
948 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
949 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
952 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
953 ipv6_key
.ipv6_proto
= ipv6_proto
;
954 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
955 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
956 ipv6_key
.ipv6_frag
= ipv6_frag
;
957 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
958 &ipv6_key
, sizeof ipv6_key
);
968 if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
970 struct ovs_key_tcp tcp_key
;
972 tcp_key
.tcp_src
= htons(tcp_src
);
973 tcp_key
.tcp_dst
= htons(tcp_dst
);
974 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
984 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
986 struct ovs_key_udp udp_key
;
988 udp_key
.udp_src
= htons(udp_src
);
989 udp_key
.udp_dst
= htons(udp_dst
);
990 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1000 if (sscanf(s
, "icmp(type=%i,code=%i)%n",
1001 &icmp_type
, &icmp_code
, &n
) > 0
1003 struct ovs_key_icmp icmp_key
;
1005 icmp_key
.icmp_type
= icmp_type
;
1006 icmp_key
.icmp_code
= icmp_code
;
1007 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1008 &icmp_key
, sizeof icmp_key
);
1014 struct ovs_key_icmpv6 icmpv6_key
;
1017 if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
1018 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
1020 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
1021 &icmpv6_key
, sizeof icmpv6_key
);
1030 uint8_t arp_sha
[ETH_ADDR_LEN
];
1031 uint8_t arp_tha
[ETH_ADDR_LEN
];
1034 if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
1035 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
1036 IP_SCAN_ARGS(&arp_sip
),
1037 IP_SCAN_ARGS(&arp_tip
),
1039 ETH_ADDR_SCAN_ARGS(arp_sha
),
1040 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
1041 struct ovs_key_arp arp_key
;
1043 memset(&arp_key
, 0, sizeof arp_key
);
1044 arp_key
.arp_sip
= arp_sip
;
1045 arp_key
.arp_tip
= arp_tip
;
1046 arp_key
.arp_op
= htons(arp_op
);
1047 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
1048 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
1049 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
1055 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
1056 uint8_t nd_sll
[ETH_ADDR_LEN
];
1057 uint8_t nd_tll
[ETH_ADDR_LEN
];
1060 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
1061 nd_target_s
, &n
) > 0 && n
> 0) {
1062 return put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
1064 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
1065 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
1067 return put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
1069 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
1070 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1072 return put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
1074 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
1075 "tll="ETH_ADDR_SCAN_FMT
")%n",
1076 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
1077 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1079 return put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
1083 if (!strncmp(s
, "encap(", 6)) {
1084 const char *start
= s
;
1087 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
1093 s
+= strspn(s
, ", \t\r\n");
1096 } else if (*s
== ')') {
1100 retval
= parse_odp_key_attr(s
, port_names
, key
);
1108 nl_msg_end_nested(key
, encap
);
1116 /* Parses the string representation of a datapath flow key, in the
1117 * format output by odp_flow_key_format(). Returns 0 if successful,
1118 * otherwise a positive errno value. On success, the flow key is
1119 * appended to 'key' as a series of Netlink attributes. On failure, no
1120 * data is appended to 'key'. Either way, 'key''s data might be
1123 * If 'port_names' is nonnull, it points to an shash that maps from a port name
1124 * to a port number cast to void *. (Port names may be used instead of port
1125 * numbers in in_port.)
1127 * On success, the attributes appended to 'key' are individually syntactically
1128 * valid, but they may not be valid as a sequence. 'key' might, for example,
1129 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1131 odp_flow_key_from_string(const char *s
, const struct shash
*port_names
,
1134 const size_t old_size
= key
->size
;
1138 s
+= strspn(s
, delimiters
);
1143 retval
= parse_odp_key_attr(s
, port_names
, key
);
1145 key
->size
= old_size
;
1155 ovs_to_odp_frag(uint8_t nw_frag
)
1157 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
1158 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
1159 : OVS_FRAG_TYPE_LATER
);
1162 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
1164 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
)
1166 struct ovs_key_ethernet
*eth_key
;
1169 if (flow
->skb_priority
) {
1170 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, flow
->skb_priority
);
1173 if (flow
->tun_id
!= htonll(0)) {
1174 nl_msg_put_be64(buf
, OVS_KEY_ATTR_TUN_ID
, flow
->tun_id
);
1177 if (flow
->in_port
!= OFPP_NONE
&& flow
->in_port
!= OFPP_CONTROLLER
) {
1178 nl_msg_put_u32(buf
, OVS_KEY_ATTR_IN_PORT
,
1179 ofp_port_to_odp_port(flow
->in_port
));
1182 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
1184 memcpy(eth_key
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1185 memcpy(eth_key
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1187 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1188 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
1189 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, flow
->vlan_tci
);
1190 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
1191 if (flow
->vlan_tci
== htons(0)) {
1198 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
1202 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, flow
->dl_type
);
1204 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1205 struct ovs_key_ipv4
*ipv4_key
;
1207 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
1209 ipv4_key
->ipv4_src
= flow
->nw_src
;
1210 ipv4_key
->ipv4_dst
= flow
->nw_dst
;
1211 ipv4_key
->ipv4_proto
= flow
->nw_proto
;
1212 ipv4_key
->ipv4_tos
= flow
->nw_tos
;
1213 ipv4_key
->ipv4_ttl
= flow
->nw_ttl
;
1214 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1215 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1216 struct ovs_key_ipv6
*ipv6_key
;
1218 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
1220 memcpy(ipv6_key
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
1221 memcpy(ipv6_key
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
1222 ipv6_key
->ipv6_label
= flow
->ipv6_label
;
1223 ipv6_key
->ipv6_proto
= flow
->nw_proto
;
1224 ipv6_key
->ipv6_tclass
= flow
->nw_tos
;
1225 ipv6_key
->ipv6_hlimit
= flow
->nw_ttl
;
1226 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1227 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
1228 struct ovs_key_arp
*arp_key
;
1230 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
1232 memset(arp_key
, 0, sizeof *arp_key
);
1233 arp_key
->arp_sip
= flow
->nw_src
;
1234 arp_key
->arp_tip
= flow
->nw_dst
;
1235 arp_key
->arp_op
= htons(flow
->nw_proto
);
1236 memcpy(arp_key
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
1237 memcpy(arp_key
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
1240 if ((flow
->dl_type
== htons(ETH_TYPE_IP
)
1241 || flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1242 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1244 if (flow
->nw_proto
== IPPROTO_TCP
) {
1245 struct ovs_key_tcp
*tcp_key
;
1247 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
1249 tcp_key
->tcp_src
= flow
->tp_src
;
1250 tcp_key
->tcp_dst
= flow
->tp_dst
;
1251 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
1252 struct ovs_key_udp
*udp_key
;
1254 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
1256 udp_key
->udp_src
= flow
->tp_src
;
1257 udp_key
->udp_dst
= flow
->tp_dst
;
1258 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
1259 && flow
->nw_proto
== IPPROTO_ICMP
) {
1260 struct ovs_key_icmp
*icmp_key
;
1262 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
1264 icmp_key
->icmp_type
= ntohs(flow
->tp_src
);
1265 icmp_key
->icmp_code
= ntohs(flow
->tp_dst
);
1266 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1267 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
1268 struct ovs_key_icmpv6
*icmpv6_key
;
1270 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
1271 sizeof *icmpv6_key
);
1272 icmpv6_key
->icmpv6_type
= ntohs(flow
->tp_src
);
1273 icmpv6_key
->icmpv6_code
= ntohs(flow
->tp_dst
);
1275 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
1276 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
1277 struct ovs_key_nd
*nd_key
;
1279 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
1281 memcpy(nd_key
->nd_target
, &flow
->nd_target
,
1282 sizeof nd_key
->nd_target
);
1283 memcpy(nd_key
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
1284 memcpy(nd_key
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
1291 nl_msg_end_nested(buf
, encap
);
1296 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
1298 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
1299 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
1303 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
1304 uint64_t attrs
, int out_of_range_attr
,
1305 const struct nlattr
*key
, size_t key_len
)
1310 if (VLOG_DROP_DBG(rl
)) {
1315 for (i
= 0; i
< 64; i
++) {
1316 if (attrs
& (UINT64_C(1) << i
)) {
1317 ds_put_format(&s
, " %s", ovs_key_attr_to_string(i
));
1320 if (out_of_range_attr
) {
1321 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
1324 ds_put_cstr(&s
, ": ");
1325 odp_flow_key_format(key
, key_len
, &s
);
1327 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
1332 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
1334 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1336 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
1337 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
1341 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
1342 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
1343 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
1344 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
1351 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
1352 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
1353 int *out_of_range_attrp
)
1355 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1356 const struct nlattr
*nla
;
1357 uint64_t present_attrs
;
1361 *out_of_range_attrp
= 0;
1362 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
1363 uint16_t type
= nl_attr_type(nla
);
1364 size_t len
= nl_attr_get_size(nla
);
1365 int expected_len
= odp_flow_key_attr_len(type
);
1367 if (len
!= expected_len
&& expected_len
>= 0) {
1368 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
1369 "length %d", ovs_key_attr_to_string(type
),
1374 if (type
>= CHAR_BIT
* sizeof present_attrs
) {
1375 *out_of_range_attrp
= type
;
1377 if (present_attrs
& (UINT64_C(1) << type
)) {
1378 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
1379 ovs_key_attr_to_string(type
));
1383 present_attrs
|= UINT64_C(1) << type
;
1388 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
1392 *present_attrsp
= present_attrs
;
1396 static enum odp_key_fitness
1397 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
1398 uint64_t expected_attrs
,
1399 const struct nlattr
*key
, size_t key_len
)
1401 uint64_t missing_attrs
;
1402 uint64_t extra_attrs
;
1404 missing_attrs
= expected_attrs
& ~present_attrs
;
1405 if (missing_attrs
) {
1406 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1407 log_odp_key_attributes(&rl
, "expected but not present",
1408 missing_attrs
, 0, key
, key_len
);
1409 return ODP_FIT_TOO_LITTLE
;
1412 extra_attrs
= present_attrs
& ~expected_attrs
;
1413 if (extra_attrs
|| out_of_range_attr
) {
1414 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1415 log_odp_key_attributes(&rl
, "present but not expected",
1416 extra_attrs
, out_of_range_attr
, key
, key_len
);
1417 return ODP_FIT_TOO_MUCH
;
1420 return ODP_FIT_PERFECT
;
1424 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1425 uint64_t present_attrs
, uint64_t *expected_attrs
,
1428 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1430 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
1431 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
1432 if (ntohs(flow
->dl_type
) < 1536) {
1433 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
1434 ntohs(flow
->dl_type
));
1437 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
1439 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
1444 static enum odp_key_fitness
1445 parse_l3_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1446 uint64_t present_attrs
, int out_of_range_attr
,
1447 uint64_t expected_attrs
, struct flow
*flow
,
1448 const struct nlattr
*key
, size_t key_len
)
1450 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1452 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1453 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
1454 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1455 const struct ovs_key_ipv4
*ipv4_key
;
1457 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
1458 flow
->nw_src
= ipv4_key
->ipv4_src
;
1459 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
1460 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
1461 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
1462 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
1463 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
1464 return ODP_FIT_ERROR
;
1467 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1468 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
1469 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1470 const struct ovs_key_ipv6
*ipv6_key
;
1472 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
1473 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
1474 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
1475 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
1476 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
1477 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
1478 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
1479 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
1480 return ODP_FIT_ERROR
;
1483 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
1484 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
1485 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1486 const struct ovs_key_arp
*arp_key
;
1488 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
1489 flow
->nw_src
= arp_key
->arp_sip
;
1490 flow
->nw_dst
= arp_key
->arp_tip
;
1491 if (arp_key
->arp_op
& htons(0xff00)) {
1492 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
1493 "key", ntohs(arp_key
->arp_op
));
1494 return ODP_FIT_ERROR
;
1496 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
1497 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
1498 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
1502 if (flow
->nw_proto
== IPPROTO_TCP
1503 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1504 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1505 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1506 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
1507 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
1508 const struct ovs_key_tcp
*tcp_key
;
1510 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
1511 flow
->tp_src
= tcp_key
->tcp_src
;
1512 flow
->tp_dst
= tcp_key
->tcp_dst
;
1514 } else if (flow
->nw_proto
== IPPROTO_UDP
1515 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1516 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1517 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1518 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
1519 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
1520 const struct ovs_key_udp
*udp_key
;
1522 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
1523 flow
->tp_src
= udp_key
->udp_src
;
1524 flow
->tp_dst
= udp_key
->udp_dst
;
1526 } else if (flow
->nw_proto
== IPPROTO_ICMP
1527 && flow
->dl_type
== htons(ETH_TYPE_IP
)
1528 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1529 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
1530 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
1531 const struct ovs_key_icmp
*icmp_key
;
1533 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
1534 flow
->tp_src
= htons(icmp_key
->icmp_type
);
1535 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
1537 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
1538 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1539 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1540 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
1541 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
1542 const struct ovs_key_icmpv6
*icmpv6_key
;
1544 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
1545 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
1546 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
1548 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
1549 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
1550 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
1551 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
1552 const struct ovs_key_nd
*nd_key
;
1554 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
1555 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
1556 sizeof flow
->nd_target
);
1557 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
1558 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
1564 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
1568 /* Parse 802.1Q header then encapsulated L3 attributes. */
1569 static enum odp_key_fitness
1570 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1571 uint64_t present_attrs
, int out_of_range_attr
,
1572 uint64_t expected_attrs
, struct flow
*flow
,
1573 const struct nlattr
*key
, size_t key_len
)
1575 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1577 const struct nlattr
*encap
1578 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
1579 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
1580 enum odp_key_fitness encap_fitness
;
1581 enum odp_key_fitness fitness
;
1584 /* Calulate fitness of outer attributes. */
1585 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
1586 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
1587 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
1588 expected_attrs
, key
, key_len
);
1590 /* Get the VLAN TCI value. */
1591 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
1592 return ODP_FIT_TOO_LITTLE
;
1594 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
1595 if (tci
== htons(0)) {
1596 /* Corner case for a truncated 802.1Q header. */
1597 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
1598 return ODP_FIT_TOO_MUCH
;
1601 } else if (!(tci
& htons(VLAN_CFI
))) {
1602 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
1603 "but CFI bit is not set", ntohs(tci
));
1604 return ODP_FIT_ERROR
;
1608 * Remove the TPID from dl_type since it's not the real Ethertype. */
1609 flow
->vlan_tci
= tci
;
1610 flow
->dl_type
= htons(0);
1612 /* Now parse the encapsulated attributes. */
1613 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
1614 attrs
, &present_attrs
, &out_of_range_attr
)) {
1615 return ODP_FIT_ERROR
;
1619 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
1620 return ODP_FIT_ERROR
;
1622 encap_fitness
= parse_l3_onward(attrs
, present_attrs
, out_of_range_attr
,
1623 expected_attrs
, flow
, key
, key_len
);
1625 /* The overall fitness is the worse of the outer and inner attributes. */
1626 return MAX(fitness
, encap_fitness
);
1629 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1630 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
1631 * 'key' fits our expectations for what a flow key should contain.
1633 * This function doesn't take the packet itself as an argument because none of
1634 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
1635 * it is always possible to infer which additional attribute(s) should appear
1636 * by looking at the attributes for lower-level protocols, e.g. if the network
1637 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
1638 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
1639 * must be absent. */
1640 enum odp_key_fitness
1641 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
1644 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1645 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
1646 uint64_t expected_attrs
;
1647 uint64_t present_attrs
;
1648 int out_of_range_attr
;
1650 memset(flow
, 0, sizeof *flow
);
1652 /* Parse attributes. */
1653 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
1654 &out_of_range_attr
)) {
1655 return ODP_FIT_ERROR
;
1660 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
1661 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
1662 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
1665 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID
)) {
1666 flow
->tun_id
= nl_attr_get_be64(attrs
[OVS_KEY_ATTR_TUN_ID
]);
1667 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID
;
1670 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
1671 uint32_t in_port
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_IN_PORT
]);
1672 if (in_port
>= UINT16_MAX
|| in_port
>= OFPP_MAX
) {
1673 VLOG_ERR_RL(&rl
, "in_port %"PRIu32
" out of supported range",
1675 return ODP_FIT_ERROR
;
1677 flow
->in_port
= odp_port_to_ofp_port(in_port
);
1678 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
1680 flow
->in_port
= OFPP_NONE
;
1683 /* Ethernet header. */
1684 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
1685 const struct ovs_key_ethernet
*eth_key
;
1687 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
1688 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
1689 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
1691 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
1693 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
1694 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
1695 return ODP_FIT_ERROR
;
1698 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1699 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
1700 expected_attrs
, flow
, key
, key_len
);
1702 return parse_l3_onward(attrs
, present_attrs
, out_of_range_attr
,
1703 expected_attrs
, flow
, key
, key_len
);
1706 /* Returns 'fitness' as a string, for use in debug messages. */
1708 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
1711 case ODP_FIT_PERFECT
:
1713 case ODP_FIT_TOO_MUCH
:
1715 case ODP_FIT_TOO_LITTLE
:
1716 return "too_little";
1724 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1725 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1726 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1727 * the start of the cookie. (If 'cookie' is null, then the return value is not
1730 odp_put_userspace_action(uint32_t pid
, const struct user_action_cookie
*cookie
,
1731 struct ofpbuf
*odp_actions
)
1735 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
1736 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
1738 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
1739 cookie
, sizeof *cookie
);
1741 nl_msg_end_nested(odp_actions
, offset
);
1743 return cookie
? odp_actions
->size
- NLA_ALIGN(sizeof *cookie
) : 0;
1746 /* The commit_odp_actions() function and its helpers. */
1749 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
1750 const void *key
, size_t key_size
)
1752 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
1753 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
1754 nl_msg_end_nested(odp_actions
, offset
);
1758 commit_set_tun_id_action(const struct flow
*flow
, struct flow
*base
,
1759 struct ofpbuf
*odp_actions
)
1761 if (base
->tun_id
== flow
->tun_id
) {
1764 base
->tun_id
= flow
->tun_id
;
1766 commit_set_action(odp_actions
, OVS_KEY_ATTR_TUN_ID
,
1767 &base
->tun_id
, sizeof(base
->tun_id
));
1771 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
1772 struct ofpbuf
*odp_actions
)
1774 struct ovs_key_ethernet eth_key
;
1776 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
1777 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
1781 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1782 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1784 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
1785 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
1787 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
1788 ð_key
, sizeof(eth_key
));
1792 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
1793 struct ofpbuf
*odp_actions
)
1795 if (base
->vlan_tci
== flow
->vlan_tci
) {
1799 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
1800 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
1803 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
1804 struct ovs_action_push_vlan vlan
;
1806 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
1807 vlan
.vlan_tci
= flow
->vlan_tci
;
1808 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1809 &vlan
, sizeof vlan
);
1811 base
->vlan_tci
= flow
->vlan_tci
;
1815 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
1816 struct ofpbuf
*odp_actions
)
1818 struct ovs_key_ipv4 ipv4_key
;
1820 if (base
->nw_src
== flow
->nw_src
&&
1821 base
->nw_dst
== flow
->nw_dst
&&
1822 base
->nw_tos
== flow
->nw_tos
&&
1823 base
->nw_ttl
== flow
->nw_ttl
&&
1824 base
->nw_frag
== flow
->nw_frag
) {
1828 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
1829 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
1830 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
1831 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
1832 ipv4_key
.ipv4_proto
= base
->nw_proto
;
1833 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
1835 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
1836 &ipv4_key
, sizeof(ipv4_key
));
1840 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
1841 struct ofpbuf
*odp_actions
)
1843 struct ovs_key_ipv6 ipv6_key
;
1845 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
1846 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
1847 base
->ipv6_label
== flow
->ipv6_label
&&
1848 base
->nw_tos
== flow
->nw_tos
&&
1849 base
->nw_ttl
== flow
->nw_ttl
&&
1850 base
->nw_frag
== flow
->nw_frag
) {
1854 base
->ipv6_src
= flow
->ipv6_src
;
1855 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
1856 base
->ipv6_dst
= flow
->ipv6_dst
;
1857 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
1859 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
1860 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
1861 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
1862 ipv6_key
.ipv6_proto
= base
->nw_proto
;
1863 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
1865 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
1866 &ipv6_key
, sizeof(ipv6_key
));
1870 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
1871 struct ofpbuf
*odp_actions
)
1873 /* Check if flow really have an IP header. */
1874 if (!flow
->nw_proto
) {
1878 if (base
->dl_type
== htons(ETH_TYPE_IP
)) {
1879 commit_set_ipv4_action(flow
, base
, odp_actions
);
1880 } else if (base
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1881 commit_set_ipv6_action(flow
, base
, odp_actions
);
1886 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
1887 struct ofpbuf
*odp_actions
)
1889 if (!base
->tp_src
|| !base
->tp_dst
) {
1893 if (base
->tp_src
== flow
->tp_src
&&
1894 base
->tp_dst
== flow
->tp_dst
) {
1898 if (flow
->nw_proto
== IPPROTO_TCP
) {
1899 struct ovs_key_tcp port_key
;
1901 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
1902 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
1904 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
1905 &port_key
, sizeof(port_key
));
1907 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
1908 struct ovs_key_udp port_key
;
1910 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
1911 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
1913 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
1914 &port_key
, sizeof(port_key
));
1919 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
1920 struct ofpbuf
*odp_actions
)
1922 if (base
->skb_priority
== flow
->skb_priority
) {
1925 base
->skb_priority
= flow
->skb_priority
;
1927 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
1928 &base
->skb_priority
, sizeof(base
->skb_priority
));
1931 /* If any of the flow key data that ODP actions can modify are different in
1932 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
1933 * key from 'base' into 'flow', and then changes 'base' the same way. */
1935 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
1936 struct ofpbuf
*odp_actions
)
1938 commit_set_tun_id_action(flow
, base
, odp_actions
);
1939 commit_set_ether_addr_action(flow
, base
, odp_actions
);
1940 commit_vlan_action(flow
, base
, odp_actions
);
1941 commit_set_nw_action(flow
, base
, odp_actions
);
1942 commit_set_port_action(flow
, base
, odp_actions
);
1943 commit_set_priority_action(flow
, base
, odp_actions
);