2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
77 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
78 case OVS_ACTION_ATTR_SET
: return -2;
79 case OVS_ACTION_ATTR_SAMPLE
: return -2;
81 case OVS_ACTION_ATTR_UNSPEC
:
82 case __OVS_ACTION_ATTR_MAX
:
90 ovs_key_attr_to_string(enum ovs_key_attr attr
)
92 static char unknown_attr
[3 + INT_STRLEN(unsigned int) + 1];
95 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
96 case OVS_KEY_ATTR_ENCAP
: return "encap";
97 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
98 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
99 case OVS_KEY_ATTR_TUN_ID
: return "tun_id";
100 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
101 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
102 case OVS_KEY_ATTR_ETHERNET
: return "eth";
103 case OVS_KEY_ATTR_VLAN
: return "vlan";
104 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
105 case OVS_KEY_ATTR_IPV4
: return "ipv4";
106 case OVS_KEY_ATTR_IPV6
: return "ipv6";
107 case OVS_KEY_ATTR_TCP
: return "tcp";
108 case OVS_KEY_ATTR_UDP
: return "udp";
109 case OVS_KEY_ATTR_ICMP
: return "icmp";
110 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
111 case OVS_KEY_ATTR_ARP
: return "arp";
112 case OVS_KEY_ATTR_ND
: return "nd";
113 case OVS_KEY_ATTR_MPLS
: return "mpls";
115 case __OVS_KEY_ATTR_MAX
:
117 snprintf(unknown_attr
, sizeof unknown_attr
, "key%u",
118 (unsigned int) attr
);
124 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
126 size_t len
= nl_attr_get_size(a
);
128 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
130 const uint8_t *unspec
;
133 unspec
= nl_attr_get(a
);
134 for (i
= 0; i
< len
; i
++) {
135 ds_put_char(ds
, i
? ' ': '(');
136 ds_put_format(ds
, "%02x", unspec
[i
]);
138 ds_put_char(ds
, ')');
143 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
145 static const struct nl_policy ovs_sample_policy
[] = {
146 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
147 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
149 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
151 const struct nlattr
*nla_acts
;
154 ds_put_cstr(ds
, "sample");
156 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
157 ds_put_cstr(ds
, "(error)");
161 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
164 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
166 ds_put_cstr(ds
, "actions(");
167 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
168 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
169 format_odp_actions(ds
, nla_acts
, len
);
170 ds_put_format(ds
, "))");
174 slow_path_reason_to_string(uint32_t data
)
176 enum slow_path_reason bit
= (enum slow_path_reason
) data
;
187 case SLOW_CONTROLLER
:
197 parse_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
208 while (s
[n
] != ')') {
209 unsigned long long int flags
;
213 if (sscanf(&s
[n
], "%lli%n", &flags
, &n0
) > 0 && n0
> 0) {
214 n
+= n0
+ (s
[n
+ n0
] == ',');
219 for (bit
= 1; bit
; bit
<<= 1) {
220 const char *name
= bit_to_string(bit
);
228 if (!strncmp(s
+ n
, name
, len
) &&
229 (s
[n
+ len
] == ',' || s
[n
+ len
] == ')')) {
231 n
+= len
+ (s
[n
+ len
] == ',');
247 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
249 static const struct nl_policy ovs_userspace_policy
[] = {
250 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
251 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_U64
, .optional
= true },
253 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
255 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
256 ds_put_cstr(ds
, "userspace(error)");
260 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
261 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
263 if (a
[OVS_USERSPACE_ATTR_USERDATA
]) {
264 uint64_t userdata
= nl_attr_get_u64(a
[OVS_USERSPACE_ATTR_USERDATA
]);
265 union user_action_cookie cookie
;
267 memcpy(&cookie
, &userdata
, sizeof cookie
);
269 switch (cookie
.type
) {
270 case USER_ACTION_COOKIE_SFLOW
:
271 ds_put_format(ds
, ",sFlow("
272 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
273 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
274 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
275 cookie
.sflow
.output
);
278 case USER_ACTION_COOKIE_SLOW_PATH
:
279 ds_put_cstr(ds
, ",slow_path(");
280 format_flags(ds
, slow_path_reason_to_string
,
281 cookie
.slow_path
.reason
, ',');
282 ds_put_format(ds
, ")");
285 case USER_ACTION_COOKIE_UNSPEC
:
287 ds_put_format(ds
, ",userdata=0x%"PRIx64
, userdata
);
292 ds_put_char(ds
, ')');
296 format_vlan_tci(struct ds
*ds
, ovs_be16 vlan_tci
)
298 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d",
299 vlan_tci_to_vid(vlan_tci
),
300 vlan_tci_to_pcp(vlan_tci
));
301 if (!(vlan_tci
& htons(VLAN_CFI
))) {
302 ds_put_cstr(ds
, ",cfi=0");
307 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
309 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
310 mpls_lse_to_label(mpls_lse
),
311 mpls_lse_to_tc(mpls_lse
),
312 mpls_lse_to_ttl(mpls_lse
),
313 mpls_lse_to_bos(mpls_lse
));
317 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
320 enum ovs_action_attr type
= nl_attr_type(a
);
321 const struct ovs_action_push_vlan
*vlan
;
323 expected_len
= odp_action_len(nl_attr_type(a
));
324 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
325 ds_put_format(ds
, "bad length %zu, expected %d for: ",
326 nl_attr_get_size(a
), expected_len
);
327 format_generic_odp_action(ds
, a
);
332 case OVS_ACTION_ATTR_OUTPUT
:
333 ds_put_format(ds
, "%"PRIu16
, nl_attr_get_u32(a
));
335 case OVS_ACTION_ATTR_USERSPACE
:
336 format_odp_userspace_action(ds
, a
);
338 case OVS_ACTION_ATTR_SET
:
339 ds_put_cstr(ds
, "set(");
340 format_odp_key_attr(nl_attr_get(a
), ds
);
341 ds_put_cstr(ds
, ")");
343 case OVS_ACTION_ATTR_PUSH_VLAN
:
344 vlan
= nl_attr_get(a
);
345 ds_put_cstr(ds
, "push_vlan(");
346 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
347 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
349 format_vlan_tci(ds
, vlan
->vlan_tci
);
350 ds_put_char(ds
, ')');
352 case OVS_ACTION_ATTR_POP_VLAN
:
353 ds_put_cstr(ds
, "pop_vlan");
355 case OVS_ACTION_ATTR_PUSH_MPLS
: {
356 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
357 ds_put_cstr(ds
, "push_mpls(");
358 format_mpls_lse(ds
, mpls
->mpls_lse
);
359 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
362 case OVS_ACTION_ATTR_POP_MPLS
: {
363 ovs_be16 ethertype
= nl_attr_get_be16(a
);
364 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
367 case OVS_ACTION_ATTR_SAMPLE
:
368 format_odp_sample_action(ds
, a
);
370 case OVS_ACTION_ATTR_UNSPEC
:
371 case __OVS_ACTION_ATTR_MAX
:
373 format_generic_odp_action(ds
, a
);
379 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
383 const struct nlattr
*a
;
386 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
388 ds_put_char(ds
, ',');
390 format_odp_action(ds
, a
);
395 if (left
== actions_len
) {
396 ds_put_cstr(ds
, "<empty>");
398 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
399 for (i
= 0; i
< left
; i
++) {
400 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
402 ds_put_char(ds
, ')');
405 ds_put_cstr(ds
, "drop");
410 parse_odp_action(const char *s
, const struct simap
*port_names
,
411 struct ofpbuf
*actions
)
413 /* Many of the sscanf calls in this function use oversized destination
414 * fields because some sscanf() implementations truncate the range of %i
415 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
416 * value of 0x7fff. The other alternatives are to allow only a single
417 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
420 * The tun_id parser has to use an alternative approach because there is no
421 * type larger than 64 bits. */
424 unsigned long long int port
;
427 if (sscanf(s
, "%lli%n", &port
, &n
) > 0 && n
> 0) {
428 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
434 int len
= strcspn(s
, delimiters
);
435 struct simap_node
*node
;
437 node
= simap_find_len(port_names
, s
, len
);
439 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
445 unsigned long long int pid
;
446 unsigned long long int output
;
451 if (sscanf(s
, "userspace(pid=%lli)%n", &pid
, &n
) > 0 && n
> 0) {
452 odp_put_userspace_action(pid
, NULL
, actions
);
454 } else if (sscanf(s
, "userspace(pid=%lli,sFlow(vid=%i,"
455 "pcp=%i,output=%lli))%n",
456 &pid
, &vid
, &pcp
, &output
, &n
) > 0 && n
> 0) {
457 union user_action_cookie cookie
;
460 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
465 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
466 cookie
.sflow
.vlan_tci
= htons(tci
);
467 cookie
.sflow
.output
= output
;
468 odp_put_userspace_action(pid
, &cookie
, actions
);
470 } else if (sscanf(s
, "userspace(pid=%lli,slow_path%n", &pid
, &n
) > 0
472 union user_action_cookie cookie
;
475 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
476 cookie
.slow_path
.unused
= 0;
477 cookie
.slow_path
.reason
= 0;
479 res
= parse_flags(&s
[n
], slow_path_reason_to_string
,
480 &cookie
.slow_path
.reason
);
490 odp_put_userspace_action(pid
, &cookie
, actions
);
492 } else if (sscanf(s
, "userspace(pid=%lli,userdata="
493 "%31[x0123456789abcdefABCDEF])%n", &pid
, userdata_s
,
495 union user_action_cookie cookie
;
498 userdata
= strtoull(userdata_s
, NULL
, 0);
499 memcpy(&cookie
, &userdata
, sizeof cookie
);
500 odp_put_userspace_action(pid
, &cookie
, actions
);
505 if (!strncmp(s
, "set(", 4)) {
509 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
510 retval
= parse_odp_key_attr(s
+ 4, port_names
, actions
);
514 if (s
[retval
+ 4] != ')') {
517 nl_msg_end_nested(actions
, start_ofs
);
522 struct ovs_action_push_vlan push
;
523 int tpid
= ETH_TYPE_VLAN
;
528 if ((sscanf(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
) > 0
530 || (sscanf(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
531 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)
532 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
533 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)
534 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
535 &tpid
, &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
536 push
.vlan_tpid
= htons(tpid
);
537 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
538 | (pcp
<< VLAN_PCP_SHIFT
)
539 | (cfi
? VLAN_CFI
: 0));
540 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
547 if (!strncmp(s
, "pop_vlan", 8)) {
548 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
556 if (sscanf(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
) > 0
557 && percentage
>= 0. && percentage
<= 100.0
559 size_t sample_ofs
, actions_ofs
;
562 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
563 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
564 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
565 (probability
<= 0 ? 0
566 : probability
>= UINT32_MAX
? UINT32_MAX
569 actions_ofs
= nl_msg_start_nested(actions
,
570 OVS_SAMPLE_ATTR_ACTIONS
);
574 n
+= strspn(s
+ n
, delimiters
);
579 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
585 nl_msg_end_nested(actions
, actions_ofs
);
586 nl_msg_end_nested(actions
, sample_ofs
);
588 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
595 /* Parses the string representation of datapath actions, in the format output
596 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
597 * value. On success, the ODP actions are appended to 'actions' as a series of
598 * Netlink attributes. On failure, no data is appended to 'actions'. Either
599 * way, 'actions''s data might be reallocated. */
601 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
602 struct ofpbuf
*actions
)
606 if (!strcasecmp(s
, "drop")) {
610 old_size
= actions
->size
;
614 s
+= strspn(s
, delimiters
);
619 retval
= parse_odp_action(s
, port_names
, actions
);
620 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
621 actions
->size
= old_size
;
630 /* Returns the correct length of the payload for a flow key attribute of the
631 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
632 * is variable length. */
634 odp_flow_key_attr_len(uint16_t type
)
636 if (type
> OVS_KEY_ATTR_MAX
) {
640 switch ((enum ovs_key_attr
) type
) {
641 case OVS_KEY_ATTR_ENCAP
: return -2;
642 case OVS_KEY_ATTR_PRIORITY
: return 4;
643 case OVS_KEY_ATTR_SKB_MARK
: return 4;
644 case OVS_KEY_ATTR_TUN_ID
: return 8;
645 case OVS_KEY_ATTR_TUNNEL
: return -2;
646 case OVS_KEY_ATTR_IN_PORT
: return 4;
647 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
648 case OVS_KEY_ATTR_VLAN
: return sizeof(ovs_be16
);
649 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
650 case OVS_KEY_ATTR_MPLS
: return sizeof(struct ovs_key_mpls
);
651 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
652 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
653 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
654 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
655 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
656 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
657 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
658 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
660 case OVS_KEY_ATTR_UNSPEC
:
661 case __OVS_KEY_ATTR_MAX
:
669 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
671 size_t len
= nl_attr_get_size(a
);
673 const uint8_t *unspec
;
676 unspec
= nl_attr_get(a
);
677 for (i
= 0; i
< len
; i
++) {
678 ds_put_char(ds
, i
? ' ': '(');
679 ds_put_format(ds
, "%02x", unspec
[i
]);
681 ds_put_char(ds
, ')');
686 ovs_frag_type_to_string(enum ovs_frag_type type
)
689 case OVS_FRAG_TYPE_NONE
:
691 case OVS_FRAG_TYPE_FIRST
:
693 case OVS_FRAG_TYPE_LATER
:
695 case __OVS_FRAG_TYPE_MAX
:
702 tunnel_key_attr_len(int type
)
705 case OVS_TUNNEL_KEY_ATTR_ID
: return 8;
706 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
: return 4;
707 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
: return 4;
708 case OVS_TUNNEL_KEY_ATTR_TOS
: return 1;
709 case OVS_TUNNEL_KEY_ATTR_TTL
: return 1;
710 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
: return 0;
711 case OVS_TUNNEL_KEY_ATTR_CSUM
: return 0;
712 case __OVS_TUNNEL_KEY_ATTR_MAX
:
718 static enum odp_key_fitness
719 tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
722 const struct nlattr
*a
;
724 bool unknown
= false;
726 NL_NESTED_FOR_EACH(a
, left
, attr
) {
727 uint16_t type
= nl_attr_type(a
);
728 size_t len
= nl_attr_get_size(a
);
729 int expected_len
= tunnel_key_attr_len(type
);
731 if (len
!= expected_len
&& expected_len
>= 0) {
732 return ODP_FIT_ERROR
;
736 case OVS_TUNNEL_KEY_ATTR_ID
:
737 tun
->tun_id
= nl_attr_get_be64(a
);
738 tun
->flags
|= FLOW_TNL_F_KEY
;
740 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
741 tun
->ip_src
= nl_attr_get_be32(a
);
743 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
744 tun
->ip_dst
= nl_attr_get_be32(a
);
746 case OVS_TUNNEL_KEY_ATTR_TOS
:
747 tun
->ip_tos
= nl_attr_get_u8(a
);
749 case OVS_TUNNEL_KEY_ATTR_TTL
:
750 tun
->ip_ttl
= nl_attr_get_u8(a
);
753 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
754 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
756 case OVS_TUNNEL_KEY_ATTR_CSUM
:
757 tun
->flags
|= FLOW_TNL_F_CSUM
;
760 /* Allow this to show up as unexpected, if there are unknown
761 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
768 return ODP_FIT_ERROR
;
771 return ODP_FIT_TOO_MUCH
;
773 return ODP_FIT_PERFECT
;
777 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
)
781 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
783 if (tun_key
->flags
& FLOW_TNL_F_KEY
) {
784 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
786 if (tun_key
->ip_src
) {
787 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
789 if (tun_key
->ip_dst
) {
790 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
792 if (tun_key
->ip_tos
) {
793 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
795 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
796 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
797 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
799 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
800 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
803 nl_msg_end_nested(a
, tun_key_ofs
);
807 format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
)
809 const struct ovs_key_ethernet
*eth_key
;
810 const struct ovs_key_ipv4
*ipv4_key
;
811 const struct ovs_key_ipv6
*ipv6_key
;
812 const struct ovs_key_tcp
*tcp_key
;
813 const struct ovs_key_udp
*udp_key
;
814 const struct ovs_key_icmp
*icmp_key
;
815 const struct ovs_key_icmpv6
*icmpv6_key
;
816 const struct ovs_key_arp
*arp_key
;
817 const struct ovs_key_nd
*nd_key
;
818 struct flow_tnl tun_key
;
819 enum ovs_key_attr attr
= nl_attr_type(a
);
822 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
));
823 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
824 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
825 ds_put_format(ds
, "(bad length %zu, expected %d)",
827 odp_flow_key_attr_len(nl_attr_type(a
)));
828 format_generic_odp_key(a
, ds
);
833 case OVS_KEY_ATTR_ENCAP
:
834 ds_put_cstr(ds
, "(");
835 if (nl_attr_get_size(a
)) {
836 odp_flow_key_format(nl_attr_get(a
), nl_attr_get_size(a
), ds
);
838 ds_put_char(ds
, ')');
841 case OVS_KEY_ATTR_PRIORITY
:
842 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
845 case OVS_KEY_ATTR_SKB_MARK
:
846 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
849 case OVS_KEY_ATTR_TUN_ID
:
850 ds_put_format(ds
, "(%#"PRIx64
")", ntohll(nl_attr_get_be64(a
)));
853 case OVS_KEY_ATTR_TUNNEL
:
854 memset(&tun_key
, 0, sizeof tun_key
);
855 if (tun_key_from_attr(a
, &tun_key
) == ODP_FIT_ERROR
) {
856 ds_put_format(ds
, "(error)");
858 ds_put_format(ds
, "(tun_id=0x%"PRIx64
",src="IP_FMT
",dst="IP_FMT
","
859 "tos=0x%"PRIx8
",ttl=%"PRIu8
",flags(",
860 ntohll(tun_key
.tun_id
),
861 IP_ARGS(tun_key
.ip_src
),
862 IP_ARGS(tun_key
.ip_dst
),
863 tun_key
.ip_tos
, tun_key
.ip_ttl
);
865 format_flags(ds
, flow_tun_flag_to_string
,
866 (uint32_t) tun_key
.flags
, ',');
867 ds_put_format(ds
, "))");
871 case OVS_KEY_ATTR_IN_PORT
:
872 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
875 case OVS_KEY_ATTR_ETHERNET
:
876 eth_key
= nl_attr_get(a
);
877 ds_put_format(ds
, "(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
878 ETH_ADDR_ARGS(eth_key
->eth_src
),
879 ETH_ADDR_ARGS(eth_key
->eth_dst
));
882 case OVS_KEY_ATTR_VLAN
:
883 ds_put_char(ds
, '(');
884 format_vlan_tci(ds
, nl_attr_get_be16(a
));
885 ds_put_char(ds
, ')');
888 case OVS_KEY_ATTR_MPLS
: {
889 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
890 ds_put_char(ds
, '(');
891 format_mpls_lse(ds
, mpls_key
->mpls_top_lse
);
892 ds_put_char(ds
, ')');
896 case OVS_KEY_ATTR_ETHERTYPE
:
897 ds_put_format(ds
, "(0x%04"PRIx16
")",
898 ntohs(nl_attr_get_be16(a
)));
901 case OVS_KEY_ATTR_IPV4
:
902 ipv4_key
= nl_attr_get(a
);
903 ds_put_format(ds
, "(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
904 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s)",
905 IP_ARGS(ipv4_key
->ipv4_src
),
906 IP_ARGS(ipv4_key
->ipv4_dst
),
907 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
909 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
912 case OVS_KEY_ATTR_IPV6
: {
913 char src_str
[INET6_ADDRSTRLEN
];
914 char dst_str
[INET6_ADDRSTRLEN
];
916 ipv6_key
= nl_attr_get(a
);
917 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
918 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
920 ds_put_format(ds
, "(src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
921 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s)",
922 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
923 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
924 ipv6_key
->ipv6_hlimit
,
925 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
929 case OVS_KEY_ATTR_TCP
:
930 tcp_key
= nl_attr_get(a
);
931 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
932 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
935 case OVS_KEY_ATTR_UDP
:
936 udp_key
= nl_attr_get(a
);
937 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
938 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
941 case OVS_KEY_ATTR_ICMP
:
942 icmp_key
= nl_attr_get(a
);
943 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
944 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
947 case OVS_KEY_ATTR_ICMPV6
:
948 icmpv6_key
= nl_attr_get(a
);
949 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
950 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
953 case OVS_KEY_ATTR_ARP
:
954 arp_key
= nl_attr_get(a
);
955 ds_put_format(ds
, "(sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
956 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
")",
957 IP_ARGS(arp_key
->arp_sip
), IP_ARGS(arp_key
->arp_tip
),
958 ntohs(arp_key
->arp_op
), ETH_ADDR_ARGS(arp_key
->arp_sha
),
959 ETH_ADDR_ARGS(arp_key
->arp_tha
));
962 case OVS_KEY_ATTR_ND
: {
963 char target
[INET6_ADDRSTRLEN
];
965 nd_key
= nl_attr_get(a
);
966 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
968 ds_put_format(ds
, "(target=%s", target
);
969 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
970 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
971 ETH_ADDR_ARGS(nd_key
->nd_sll
));
973 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
974 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
975 ETH_ADDR_ARGS(nd_key
->nd_tll
));
977 ds_put_char(ds
, ')');
981 case OVS_KEY_ATTR_UNSPEC
:
982 case __OVS_KEY_ATTR_MAX
:
984 format_generic_odp_key(a
, ds
);
989 /* Appends to 'ds' a string representation of the 'key_len' bytes of
990 * OVS_KEY_ATTR_* attributes in 'key'. */
992 odp_flow_key_format(const struct nlattr
*key
, size_t key_len
, struct ds
*ds
)
995 const struct nlattr
*a
;
998 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
1000 ds_put_char(ds
, ',');
1002 format_odp_key_attr(a
, ds
);
1007 if (left
== key_len
) {
1008 ds_put_cstr(ds
, "<empty>");
1010 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1011 for (i
= 0; i
< left
; i
++) {
1012 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1014 ds_put_char(ds
, ')');
1017 ds_put_cstr(ds
, "<empty>");
1022 put_nd_key(int n
, const char *nd_target_s
,
1023 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
1025 struct ovs_key_nd nd_key
;
1027 memset(&nd_key
, 0, sizeof nd_key
);
1028 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
1032 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
1035 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
1037 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
1042 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
1044 if (!strcasecmp(s
, "no")) {
1045 *type
= OVS_FRAG_TYPE_NONE
;
1046 } else if (!strcasecmp(s
, "first")) {
1047 *type
= OVS_FRAG_TYPE_FIRST
;
1048 } else if (!strcasecmp(s
, "later")) {
1049 *type
= OVS_FRAG_TYPE_LATER
;
1057 mpls_lse_from_components(int mpls_label
, int mpls_tc
, int mpls_ttl
, int mpls_bos
)
1059 return (htonl((mpls_label
<< MPLS_LABEL_SHIFT
) |
1060 (mpls_tc
<< MPLS_TC_SHIFT
) |
1061 (mpls_ttl
<< MPLS_TTL_SHIFT
) |
1062 (mpls_bos
<< MPLS_BOS_SHIFT
)));
1066 parse_odp_key_attr(const char *s
, const struct simap
*port_names
,
1069 /* Many of the sscanf calls in this function use oversized destination
1070 * fields because some sscanf() implementations truncate the range of %i
1071 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1072 * value of 0x7fff. The other alternatives are to allow only a single
1073 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1076 * The tun_id parser has to use an alternative approach because there is no
1077 * type larger than 64 bits. */
1080 unsigned long long int priority
;
1083 if (sscanf(s
, "skb_priority(%llx)%n", &priority
, &n
) > 0 && n
> 0) {
1084 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1090 unsigned long long int mark
;
1093 if (sscanf(s
, "skb_mark(%llx)%n", &mark
, &n
) > 0 && n
> 0) {
1094 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1103 if (sscanf(s
, "tun_id(%31[x0123456789abcdefABCDEF])%n",
1104 tun_id_s
, &n
) > 0 && n
> 0) {
1105 uint64_t tun_id
= strtoull(tun_id_s
, NULL
, 0);
1106 nl_msg_put_be64(key
, OVS_KEY_ATTR_TUN_ID
, htonll(tun_id
));
1114 struct flow_tnl tun_key
;
1117 if (sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1118 "src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
1119 ",tos=%i,ttl=%i,flags%n", tun_id_s
,
1120 IP_SCAN_ARGS(&tun_key
.ip_src
),
1121 IP_SCAN_ARGS(&tun_key
.ip_dst
), &tos
, &ttl
,
1126 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1127 tun_key
.ip_tos
= tos
;
1128 tun_key
.ip_ttl
= ttl
;
1129 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1130 tun_key
.flags
= (uint16_t) flags
;
1140 tun_key_to_attr(key
, &tun_key
);
1146 unsigned long long int in_port
;
1149 if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
1150 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1155 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
1157 const struct simap_node
*node
;
1161 name_len
= strcspn(s
, ")");
1162 node
= simap_find_len(port_names
, name
, name_len
);
1164 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, node
->data
);
1165 return 8 + name_len
+ 1;
1170 struct ovs_key_ethernet eth_key
;
1174 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
1175 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1176 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
1177 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1178 ð_key
, sizeof eth_key
);
1189 if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n", &vid
, &pcp
, &n
) > 0
1191 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1192 htons((vid
<< VLAN_VID_SHIFT
) |
1193 (pcp
<< VLAN_PCP_SHIFT
) |
1196 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
1197 &vid
, &pcp
, &cfi
, &n
) > 0
1199 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1200 htons((vid
<< VLAN_VID_SHIFT
) |
1201 (pcp
<< VLAN_PCP_SHIFT
) |
1202 (cfi
? VLAN_CFI
: 0)));
1211 if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
1212 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1218 int label
, tc
, ttl
, bos
;
1221 if (sscanf(s
, "mpls(label=%"SCNi32
",tc=%i,ttl=%i,bos=%i)%n",
1222 &label
, &tc
, &ttl
, &bos
, &n
) > 0 &&
1224 struct ovs_key_mpls
*mpls
;
1226 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1228 mpls
->mpls_top_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1240 enum ovs_frag_type ipv4_frag
;
1243 if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
1244 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1245 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
1246 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
1248 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1249 struct ovs_key_ipv4 ipv4_key
;
1251 ipv4_key
.ipv4_src
= ipv4_src
;
1252 ipv4_key
.ipv4_dst
= ipv4_dst
;
1253 ipv4_key
.ipv4_proto
= ipv4_proto
;
1254 ipv4_key
.ipv4_tos
= ipv4_tos
;
1255 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1256 ipv4_key
.ipv4_frag
= ipv4_frag
;
1257 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1258 &ipv4_key
, sizeof ipv4_key
);
1264 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
1265 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
1271 enum ovs_frag_type ipv6_frag
;
1274 if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
1275 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1276 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
1277 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
1279 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1280 struct ovs_key_ipv6 ipv6_key
;
1282 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1283 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
1286 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1287 ipv6_key
.ipv6_proto
= ipv6_proto
;
1288 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1289 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1290 ipv6_key
.ipv6_frag
= ipv6_frag
;
1291 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1292 &ipv6_key
, sizeof ipv6_key
);
1302 if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
1304 struct ovs_key_tcp tcp_key
;
1306 tcp_key
.tcp_src
= htons(tcp_src
);
1307 tcp_key
.tcp_dst
= htons(tcp_dst
);
1308 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1318 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
1320 struct ovs_key_udp udp_key
;
1322 udp_key
.udp_src
= htons(udp_src
);
1323 udp_key
.udp_dst
= htons(udp_dst
);
1324 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1334 if (sscanf(s
, "icmp(type=%i,code=%i)%n",
1335 &icmp_type
, &icmp_code
, &n
) > 0
1337 struct ovs_key_icmp icmp_key
;
1339 icmp_key
.icmp_type
= icmp_type
;
1340 icmp_key
.icmp_code
= icmp_code
;
1341 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1342 &icmp_key
, sizeof icmp_key
);
1348 struct ovs_key_icmpv6 icmpv6_key
;
1351 if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
1352 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
1354 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
1355 &icmpv6_key
, sizeof icmpv6_key
);
1364 uint8_t arp_sha
[ETH_ADDR_LEN
];
1365 uint8_t arp_tha
[ETH_ADDR_LEN
];
1368 if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
1369 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
1370 IP_SCAN_ARGS(&arp_sip
),
1371 IP_SCAN_ARGS(&arp_tip
),
1373 ETH_ADDR_SCAN_ARGS(arp_sha
),
1374 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
1375 struct ovs_key_arp arp_key
;
1377 memset(&arp_key
, 0, sizeof arp_key
);
1378 arp_key
.arp_sip
= arp_sip
;
1379 arp_key
.arp_tip
= arp_tip
;
1380 arp_key
.arp_op
= htons(arp_op
);
1381 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
1382 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
1383 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
1389 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
1390 uint8_t nd_sll
[ETH_ADDR_LEN
];
1391 uint8_t nd_tll
[ETH_ADDR_LEN
];
1394 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
1395 nd_target_s
, &n
) > 0 && n
> 0) {
1396 return put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
1398 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
1399 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
1401 return put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
1403 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
1404 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1406 return put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
1408 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
1409 "tll="ETH_ADDR_SCAN_FMT
")%n",
1410 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
1411 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1413 return put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
1417 if (!strncmp(s
, "encap(", 6)) {
1418 const char *start
= s
;
1421 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
1427 s
+= strspn(s
, ", \t\r\n");
1430 } else if (*s
== ')') {
1434 retval
= parse_odp_key_attr(s
, port_names
, key
);
1442 nl_msg_end_nested(key
, encap
);
1450 /* Parses the string representation of a datapath flow key, in the
1451 * format output by odp_flow_key_format(). Returns 0 if successful,
1452 * otherwise a positive errno value. On success, the flow key is
1453 * appended to 'key' as a series of Netlink attributes. On failure, no
1454 * data is appended to 'key'. Either way, 'key''s data might be
1457 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1458 * to a port number. (Port names may be used instead of port numbers in
1461 * On success, the attributes appended to 'key' are individually syntactically
1462 * valid, but they may not be valid as a sequence. 'key' might, for example,
1463 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1465 odp_flow_key_from_string(const char *s
, const struct simap
*port_names
,
1468 const size_t old_size
= key
->size
;
1472 s
+= strspn(s
, delimiters
);
1477 retval
= parse_odp_key_attr(s
, port_names
, key
);
1479 key
->size
= old_size
;
1489 ovs_to_odp_frag(uint8_t nw_frag
)
1491 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
1492 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
1493 : OVS_FRAG_TYPE_LATER
);
1496 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1497 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1498 * number rather than a datapath port number). Instead, if 'odp_in_port'
1499 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1502 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1503 * capable of being expanded to allow for that much space. */
1505 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
,
1506 uint32_t odp_in_port
)
1508 struct ovs_key_ethernet
*eth_key
;
1511 if (flow
->skb_priority
) {
1512 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, flow
->skb_priority
);
1515 if (flow
->tunnel
.ip_dst
) {
1516 tun_key_to_attr(buf
, &flow
->tunnel
);
1517 } else if (flow
->tunnel
.tun_id
!= htonll(0)) {
1518 nl_msg_put_be64(buf
, OVS_KEY_ATTR_TUN_ID
, flow
->tunnel
.tun_id
);
1521 if (flow
->skb_mark
) {
1522 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, flow
->skb_mark
);
1525 if (odp_in_port
!= OVSP_NONE
) {
1526 nl_msg_put_u32(buf
, OVS_KEY_ATTR_IN_PORT
, odp_in_port
);
1529 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
1531 memcpy(eth_key
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1532 memcpy(eth_key
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1534 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1535 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
1536 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, flow
->vlan_tci
);
1537 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
1538 if (flow
->vlan_tci
== htons(0)) {
1545 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
1549 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, flow
->dl_type
);
1551 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1552 struct ovs_key_ipv4
*ipv4_key
;
1554 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
1556 ipv4_key
->ipv4_src
= flow
->nw_src
;
1557 ipv4_key
->ipv4_dst
= flow
->nw_dst
;
1558 ipv4_key
->ipv4_proto
= flow
->nw_proto
;
1559 ipv4_key
->ipv4_tos
= flow
->nw_tos
;
1560 ipv4_key
->ipv4_ttl
= flow
->nw_ttl
;
1561 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1562 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1563 struct ovs_key_ipv6
*ipv6_key
;
1565 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
1567 memcpy(ipv6_key
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
1568 memcpy(ipv6_key
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
1569 ipv6_key
->ipv6_label
= flow
->ipv6_label
;
1570 ipv6_key
->ipv6_proto
= flow
->nw_proto
;
1571 ipv6_key
->ipv6_tclass
= flow
->nw_tos
;
1572 ipv6_key
->ipv6_hlimit
= flow
->nw_ttl
;
1573 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1574 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
1575 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
1576 struct ovs_key_arp
*arp_key
;
1578 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
1580 memset(arp_key
, 0, sizeof *arp_key
);
1581 arp_key
->arp_sip
= flow
->nw_src
;
1582 arp_key
->arp_tip
= flow
->nw_dst
;
1583 arp_key
->arp_op
= htons(flow
->nw_proto
);
1584 memcpy(arp_key
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
1585 memcpy(arp_key
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
1588 if (flow
->mpls_depth
) {
1589 struct ovs_key_mpls
*mpls_key
;
1591 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
1593 mpls_key
->mpls_top_lse
= flow
->mpls_lse
;
1596 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1597 if (flow
->nw_proto
== IPPROTO_TCP
) {
1598 struct ovs_key_tcp
*tcp_key
;
1600 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
1602 tcp_key
->tcp_src
= flow
->tp_src
;
1603 tcp_key
->tcp_dst
= flow
->tp_dst
;
1604 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
1605 struct ovs_key_udp
*udp_key
;
1607 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
1609 udp_key
->udp_src
= flow
->tp_src
;
1610 udp_key
->udp_dst
= flow
->tp_dst
;
1611 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
1612 && flow
->nw_proto
== IPPROTO_ICMP
) {
1613 struct ovs_key_icmp
*icmp_key
;
1615 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
1617 icmp_key
->icmp_type
= ntohs(flow
->tp_src
);
1618 icmp_key
->icmp_code
= ntohs(flow
->tp_dst
);
1619 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1620 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
1621 struct ovs_key_icmpv6
*icmpv6_key
;
1623 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
1624 sizeof *icmpv6_key
);
1625 icmpv6_key
->icmpv6_type
= ntohs(flow
->tp_src
);
1626 icmpv6_key
->icmpv6_code
= ntohs(flow
->tp_dst
);
1628 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
1629 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
1630 struct ovs_key_nd
*nd_key
;
1632 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
1634 memcpy(nd_key
->nd_target
, &flow
->nd_target
,
1635 sizeof nd_key
->nd_target
);
1636 memcpy(nd_key
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
1637 memcpy(nd_key
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
1644 nl_msg_end_nested(buf
, encap
);
1649 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
1651 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
1652 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
1656 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
1657 uint64_t attrs
, int out_of_range_attr
,
1658 const struct nlattr
*key
, size_t key_len
)
1663 if (VLOG_DROP_DBG(rl
)) {
1668 for (i
= 0; i
< 64; i
++) {
1669 if (attrs
& (UINT64_C(1) << i
)) {
1670 ds_put_format(&s
, " %s", ovs_key_attr_to_string(i
));
1673 if (out_of_range_attr
) {
1674 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
1677 ds_put_cstr(&s
, ": ");
1678 odp_flow_key_format(key
, key_len
, &s
);
1680 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
1685 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
1687 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1689 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
1690 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
1694 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
1695 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
1696 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
1697 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
1704 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
1705 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
1706 int *out_of_range_attrp
)
1708 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1709 const struct nlattr
*nla
;
1710 uint64_t present_attrs
;
1714 *out_of_range_attrp
= 0;
1715 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
1716 uint16_t type
= nl_attr_type(nla
);
1717 size_t len
= nl_attr_get_size(nla
);
1718 int expected_len
= odp_flow_key_attr_len(type
);
1720 if (len
!= expected_len
&& expected_len
>= 0) {
1721 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
1722 "length %d", ovs_key_attr_to_string(type
),
1727 if (type
>= CHAR_BIT
* sizeof present_attrs
) {
1728 *out_of_range_attrp
= type
;
1730 if (present_attrs
& (UINT64_C(1) << type
)) {
1731 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
1732 ovs_key_attr_to_string(type
));
1736 present_attrs
|= UINT64_C(1) << type
;
1741 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
1745 *present_attrsp
= present_attrs
;
1749 static enum odp_key_fitness
1750 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
1751 uint64_t expected_attrs
,
1752 const struct nlattr
*key
, size_t key_len
)
1754 uint64_t missing_attrs
;
1755 uint64_t extra_attrs
;
1757 missing_attrs
= expected_attrs
& ~present_attrs
;
1758 if (missing_attrs
) {
1759 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1760 log_odp_key_attributes(&rl
, "expected but not present",
1761 missing_attrs
, 0, key
, key_len
);
1762 return ODP_FIT_TOO_LITTLE
;
1765 extra_attrs
= present_attrs
& ~expected_attrs
;
1766 if (extra_attrs
|| out_of_range_attr
) {
1767 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1768 log_odp_key_attributes(&rl
, "present but not expected",
1769 extra_attrs
, out_of_range_attr
, key
, key_len
);
1770 return ODP_FIT_TOO_MUCH
;
1773 return ODP_FIT_PERFECT
;
1777 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1778 uint64_t present_attrs
, uint64_t *expected_attrs
,
1781 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1783 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
1784 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
1785 if (ntohs(flow
->dl_type
) < 1536) {
1786 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
1787 ntohs(flow
->dl_type
));
1790 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
1792 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
1797 static enum odp_key_fitness
1798 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1799 uint64_t present_attrs
, int out_of_range_attr
,
1800 uint64_t expected_attrs
, struct flow
*flow
,
1801 const struct nlattr
*key
, size_t key_len
)
1803 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1806 /* Parse MPLS label stack entry */
1807 if (eth_type_mpls(flow
->dl_type
)) {
1808 /* Calculate fitness of outer attributes. */
1809 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
1811 /* Get the MPLS LSE value. */
1812 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
))) {
1813 return ODP_FIT_TOO_LITTLE
;
1815 flow
->mpls_lse
= nl_attr_get_be32(attrs
[OVS_KEY_ATTR_MPLS
]);
1818 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1819 flow
->encap_dl_type
= htons(ETH_TYPE_IP
);
1820 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1821 flow
->encap_dl_type
= htons(ETH_TYPE_IPV6
);
1822 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1823 flow
->encap_dl_type
= htons(ETH_TYPE_ARP
);
1827 dl_type
= flow_innermost_dl_type(flow
);
1829 if (dl_type
== htons(ETH_TYPE_IP
)) {
1830 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
1831 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1832 const struct ovs_key_ipv4
*ipv4_key
;
1834 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
1835 flow
->nw_src
= ipv4_key
->ipv4_src
;
1836 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
1837 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
1838 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
1839 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
1840 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
1841 return ODP_FIT_ERROR
;
1844 } else if (dl_type
== htons(ETH_TYPE_IPV6
)) {
1845 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
1846 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1847 const struct ovs_key_ipv6
*ipv6_key
;
1849 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
1850 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
1851 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
1852 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
1853 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
1854 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
1855 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
1856 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
1857 return ODP_FIT_ERROR
;
1860 } else if (dl_type
== htons(ETH_TYPE_ARP
) ||
1861 dl_type
== htons(ETH_TYPE_RARP
)) {
1862 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
1863 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1864 const struct ovs_key_arp
*arp_key
;
1866 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
1867 flow
->nw_src
= arp_key
->arp_sip
;
1868 flow
->nw_dst
= arp_key
->arp_tip
;
1869 if (arp_key
->arp_op
& htons(0xff00)) {
1870 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
1871 "key", ntohs(arp_key
->arp_op
));
1872 return ODP_FIT_ERROR
;
1874 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
1875 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
1876 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
1880 if (flow
->nw_proto
== IPPROTO_TCP
1881 && (dl_type
== htons(ETH_TYPE_IP
) ||
1882 dl_type
== htons(ETH_TYPE_IPV6
))
1883 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1884 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
1885 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
1886 const struct ovs_key_tcp
*tcp_key
;
1888 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
1889 flow
->tp_src
= tcp_key
->tcp_src
;
1890 flow
->tp_dst
= tcp_key
->tcp_dst
;
1892 } else if (flow
->nw_proto
== IPPROTO_UDP
1893 && (dl_type
== htons(ETH_TYPE_IP
) ||
1894 dl_type
== htons(ETH_TYPE_IPV6
))
1895 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1896 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
1897 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
1898 const struct ovs_key_udp
*udp_key
;
1900 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
1901 flow
->tp_src
= udp_key
->udp_src
;
1902 flow
->tp_dst
= udp_key
->udp_dst
;
1904 } else if (flow
->nw_proto
== IPPROTO_ICMP
1905 && dl_type
== htons(ETH_TYPE_IP
)
1906 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1907 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
1908 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
1909 const struct ovs_key_icmp
*icmp_key
;
1911 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
1912 flow
->tp_src
= htons(icmp_key
->icmp_type
);
1913 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
1915 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
1916 && dl_type
== htons(ETH_TYPE_IPV6
)
1917 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1918 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
1919 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
1920 const struct ovs_key_icmpv6
*icmpv6_key
;
1922 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
1923 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
1924 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
1926 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
1927 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
1928 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
1929 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
1930 const struct ovs_key_nd
*nd_key
;
1932 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
1933 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
1934 sizeof flow
->nd_target
);
1935 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
1936 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
1942 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
1946 /* Parse 802.1Q header then encapsulated L3 attributes. */
1947 static enum odp_key_fitness
1948 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1949 uint64_t present_attrs
, int out_of_range_attr
,
1950 uint64_t expected_attrs
, struct flow
*flow
,
1951 const struct nlattr
*key
, size_t key_len
)
1953 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1955 const struct nlattr
*encap
1956 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
1957 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
1958 enum odp_key_fitness encap_fitness
;
1959 enum odp_key_fitness fitness
;
1962 /* Calulate fitness of outer attributes. */
1963 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
1964 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
1965 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
1966 expected_attrs
, key
, key_len
);
1968 /* Get the VLAN TCI value. */
1969 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
1970 return ODP_FIT_TOO_LITTLE
;
1972 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
1973 if (tci
== htons(0)) {
1974 /* Corner case for a truncated 802.1Q header. */
1975 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
1976 return ODP_FIT_TOO_MUCH
;
1979 } else if (!(tci
& htons(VLAN_CFI
))) {
1980 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
1981 "but CFI bit is not set", ntohs(tci
));
1982 return ODP_FIT_ERROR
;
1986 * Remove the TPID from dl_type since it's not the real Ethertype. */
1987 flow
->vlan_tci
= tci
;
1988 flow
->dl_type
= htons(0);
1990 /* Now parse the encapsulated attributes. */
1991 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
1992 attrs
, &present_attrs
, &out_of_range_attr
)) {
1993 return ODP_FIT_ERROR
;
1997 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
1998 return ODP_FIT_ERROR
;
2000 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2001 expected_attrs
, flow
, key
, key_len
);
2003 /* The overall fitness is the worse of the outer and inner attributes. */
2004 return MAX(fitness
, encap_fitness
);
2007 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2008 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2009 * 'key' fits our expectations for what a flow key should contain.
2011 * The 'in_port' will be the datapath's understanding of the port. The
2012 * caller will need to translate with odp_port_to_ofp_port() if the
2013 * OpenFlow port is needed.
2015 * This function doesn't take the packet itself as an argument because none of
2016 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2017 * it is always possible to infer which additional attribute(s) should appear
2018 * by looking at the attributes for lower-level protocols, e.g. if the network
2019 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2020 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2021 * must be absent. */
2022 enum odp_key_fitness
2023 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
2026 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
2027 uint64_t expected_attrs
;
2028 uint64_t present_attrs
;
2029 int out_of_range_attr
;
2031 memset(flow
, 0, sizeof *flow
);
2033 /* Parse attributes. */
2034 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
2035 &out_of_range_attr
)) {
2036 return ODP_FIT_ERROR
;
2041 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
2042 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
2043 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
2046 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
2047 flow
->skb_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
2048 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
2051 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID
)) {
2052 flow
->tunnel
.tun_id
= nl_attr_get_be64(attrs
[OVS_KEY_ATTR_TUN_ID
]);
2053 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID
;
2056 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
2057 enum odp_key_fitness res
;
2059 res
= tun_key_from_attr(attrs
[OVS_KEY_ATTR_TUNNEL
], &flow
->tunnel
);
2060 if (res
== ODP_FIT_ERROR
) {
2061 return ODP_FIT_ERROR
;
2062 } else if (res
== ODP_FIT_PERFECT
) {
2063 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
2067 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
2068 flow
->in_port
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_IN_PORT
]);
2069 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
2071 flow
->in_port
= OVSP_NONE
;
2074 /* Ethernet header. */
2075 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
2076 const struct ovs_key_ethernet
*eth_key
;
2078 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
2079 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
2080 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
2082 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
2084 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2085 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2086 return ODP_FIT_ERROR
;
2089 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2090 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
2091 expected_attrs
, flow
, key
, key_len
);
2093 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2094 expected_attrs
, flow
, key
, key_len
);
2097 /* Returns 'fitness' as a string, for use in debug messages. */
2099 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
2102 case ODP_FIT_PERFECT
:
2104 case ODP_FIT_TOO_MUCH
:
2106 case ODP_FIT_TOO_LITTLE
:
2107 return "too_little";
2115 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2116 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
2117 * contents contains 'cookie' and returns the offset within 'odp_actions' of
2118 * the start of the cookie. (If 'cookie' is null, then the return value is not
2121 odp_put_userspace_action(uint32_t pid
, const union user_action_cookie
*cookie
,
2122 struct ofpbuf
*odp_actions
)
2126 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
2127 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
2129 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
2130 cookie
, sizeof *cookie
);
2132 nl_msg_end_nested(odp_actions
, offset
);
2134 return cookie
? odp_actions
->size
- NLA_ALIGN(sizeof *cookie
) : 0;
2138 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
2139 struct ofpbuf
*odp_actions
)
2141 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2142 tun_key_to_attr(odp_actions
, tunnel
);
2143 nl_msg_end_nested(odp_actions
, offset
);
2146 /* The commit_odp_actions() function and its helpers. */
2149 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
2150 const void *key
, size_t key_size
)
2152 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2153 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
2154 nl_msg_end_nested(odp_actions
, offset
);
2157 /* If any of the flow key data that ODP actions can modify are different in
2158 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2159 * 'odp_actions' that change the flow tunneling information in key from
2160 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2161 * same way. In other words, operates the same as commit_odp_actions(), but
2162 * only on tunneling information. */
2164 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
2165 struct ofpbuf
*odp_actions
)
2167 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
2170 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
2172 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2173 if (flow
->tunnel
.ip_dst
) {
2174 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
2176 commit_set_action(odp_actions
, OVS_KEY_ATTR_TUN_ID
,
2177 &base
->tunnel
.tun_id
, sizeof base
->tunnel
.tun_id
);
2182 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
2183 struct ofpbuf
*odp_actions
)
2185 struct ovs_key_ethernet eth_key
;
2187 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
2188 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
2192 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
2193 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
2195 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
2196 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
2198 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
2199 ð_key
, sizeof(eth_key
));
2203 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
2204 struct ofpbuf
*odp_actions
)
2206 if (base
->vlan_tci
== flow
->vlan_tci
) {
2210 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
2211 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2214 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
2215 struct ovs_action_push_vlan vlan
;
2217 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
2218 vlan
.vlan_tci
= flow
->vlan_tci
;
2219 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2220 &vlan
, sizeof vlan
);
2222 base
->vlan_tci
= flow
->vlan_tci
;
2226 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
2227 struct ofpbuf
*odp_actions
)
2229 if (flow
->mpls_lse
== base
->mpls_lse
&&
2230 flow
->mpls_depth
== base
->mpls_depth
) {
2234 if (flow
->mpls_depth
< base
->mpls_depth
) {
2235 if (base
->mpls_depth
- flow
->mpls_depth
> 1) {
2236 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2237 VLOG_WARN_RL(&rl
, "Multiple mpls_pop actions reduced to "
2238 " a single mpls_pop action");
2241 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, flow
->dl_type
);
2242 } else if (flow
->mpls_depth
> base
->mpls_depth
) {
2243 struct ovs_action_push_mpls
*mpls
;
2245 if (flow
->mpls_depth
- base
->mpls_depth
> 1) {
2246 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2247 VLOG_WARN_RL(&rl
, "Multiple mpls_push actions reduced to "
2248 " a single mpls_push action");
2251 mpls
= nl_msg_put_unspec_uninit(odp_actions
, OVS_ACTION_ATTR_PUSH_MPLS
,
2253 memset(mpls
, 0, sizeof *mpls
);
2254 mpls
->mpls_ethertype
= flow
->dl_type
;
2255 mpls
->mpls_lse
= flow
->mpls_lse
;
2257 struct ovs_key_mpls mpls_key
;
2259 mpls_key
.mpls_top_lse
= flow
->mpls_lse
;
2260 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
2261 &mpls_key
, sizeof(mpls_key
));
2264 base
->dl_type
= flow
->dl_type
;
2265 base
->mpls_lse
= flow
->mpls_lse
;
2266 base
->mpls_depth
= flow
->mpls_depth
;
2270 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
2271 struct ofpbuf
*odp_actions
)
2273 struct ovs_key_ipv4 ipv4_key
;
2275 if (base
->nw_src
== flow
->nw_src
&&
2276 base
->nw_dst
== flow
->nw_dst
&&
2277 base
->nw_tos
== flow
->nw_tos
&&
2278 base
->nw_ttl
== flow
->nw_ttl
&&
2279 base
->nw_frag
== flow
->nw_frag
) {
2283 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
2284 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
2285 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
2286 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
2287 ipv4_key
.ipv4_proto
= base
->nw_proto
;
2288 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
2290 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
2291 &ipv4_key
, sizeof(ipv4_key
));
2295 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
2296 struct ofpbuf
*odp_actions
)
2298 struct ovs_key_ipv6 ipv6_key
;
2300 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
2301 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
2302 base
->ipv6_label
== flow
->ipv6_label
&&
2303 base
->nw_tos
== flow
->nw_tos
&&
2304 base
->nw_ttl
== flow
->nw_ttl
&&
2305 base
->nw_frag
== flow
->nw_frag
) {
2309 base
->ipv6_src
= flow
->ipv6_src
;
2310 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
2311 base
->ipv6_dst
= flow
->ipv6_dst
;
2312 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
2314 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
2315 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
2316 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
2317 ipv6_key
.ipv6_proto
= base
->nw_proto
;
2318 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
2320 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
2321 &ipv6_key
, sizeof(ipv6_key
));
2325 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
2326 struct ofpbuf
*odp_actions
)
2328 /* Check if flow really have an IP header. */
2329 if (!flow
->nw_proto
) {
2333 if (base
->dl_type
== htons(ETH_TYPE_IP
)) {
2334 commit_set_ipv4_action(flow
, base
, odp_actions
);
2335 } else if (base
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2336 commit_set_ipv6_action(flow
, base
, odp_actions
);
2341 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
2342 struct ofpbuf
*odp_actions
)
2344 if (!base
->tp_src
&& !base
->tp_dst
) {
2348 if (base
->tp_src
== flow
->tp_src
&&
2349 base
->tp_dst
== flow
->tp_dst
) {
2353 if (flow
->nw_proto
== IPPROTO_TCP
) {
2354 struct ovs_key_tcp port_key
;
2356 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
2357 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
2359 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
2360 &port_key
, sizeof(port_key
));
2362 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
2363 struct ovs_key_udp port_key
;
2365 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
2366 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
2368 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
2369 &port_key
, sizeof(port_key
));
2374 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
2375 struct ofpbuf
*odp_actions
)
2377 if (base
->skb_priority
== flow
->skb_priority
) {
2380 base
->skb_priority
= flow
->skb_priority
;
2382 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
2383 &base
->skb_priority
, sizeof(base
->skb_priority
));
2387 commit_set_skb_mark_action(const struct flow
*flow
, struct flow
*base
,
2388 struct ofpbuf
*odp_actions
)
2390 if (base
->skb_mark
== flow
->skb_mark
) {
2393 base
->skb_mark
= flow
->skb_mark
;
2395 commit_set_action(odp_actions
, OVS_KEY_ATTR_SKB_MARK
,
2396 &base
->skb_mark
, sizeof(base
->skb_mark
));
2398 /* If any of the flow key data that ODP actions can modify are different in
2399 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2400 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
2401 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
2402 * in addition to this function if needed. */
2404 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
2405 struct ofpbuf
*odp_actions
)
2407 commit_set_ether_addr_action(flow
, base
, odp_actions
);
2408 commit_vlan_action(flow
, base
, odp_actions
);
2409 commit_mpls_action(flow
, base
, odp_actions
);
2410 commit_set_nw_action(flow
, base
, odp_actions
);
2411 commit_set_port_action(flow
, base
, odp_actions
);
2412 commit_set_priority_action(flow
, base
, odp_actions
);
2413 commit_set_skb_mark_action(flow
, base
, odp_actions
);