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_TUNNEL
: return "tunnel";
100 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
101 case OVS_KEY_ATTR_ETHERNET
: return "eth";
102 case OVS_KEY_ATTR_VLAN
: return "vlan";
103 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
104 case OVS_KEY_ATTR_IPV4
: return "ipv4";
105 case OVS_KEY_ATTR_IPV6
: return "ipv6";
106 case OVS_KEY_ATTR_TCP
: return "tcp";
107 case OVS_KEY_ATTR_UDP
: return "udp";
108 case OVS_KEY_ATTR_ICMP
: return "icmp";
109 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
110 case OVS_KEY_ATTR_ARP
: return "arp";
111 case OVS_KEY_ATTR_ND
: return "nd";
112 case OVS_KEY_ATTR_MPLS
: return "mpls";
114 case __OVS_KEY_ATTR_MAX
:
116 snprintf(unknown_attr
, sizeof unknown_attr
, "key%u",
117 (unsigned int) attr
);
123 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
125 size_t len
= nl_attr_get_size(a
);
127 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
129 const uint8_t *unspec
;
132 unspec
= nl_attr_get(a
);
133 for (i
= 0; i
< len
; i
++) {
134 ds_put_char(ds
, i
? ' ': '(');
135 ds_put_format(ds
, "%02x", unspec
[i
]);
137 ds_put_char(ds
, ')');
142 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
144 static const struct nl_policy ovs_sample_policy
[] = {
145 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
146 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
148 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
150 const struct nlattr
*nla_acts
;
153 ds_put_cstr(ds
, "sample");
155 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
156 ds_put_cstr(ds
, "(error)");
160 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
163 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
165 ds_put_cstr(ds
, "actions(");
166 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
167 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
168 format_odp_actions(ds
, nla_acts
, len
);
169 ds_put_format(ds
, "))");
173 slow_path_reason_to_string(enum slow_path_reason reason
)
184 case SLOW_CONTROLLER
:
192 static enum slow_path_reason
193 string_to_slow_path_reason(const char *string
)
195 enum slow_path_reason i
;
197 for (i
= 1; i
< __SLOW_MAX
; i
++) {
198 if (!strcmp(string
, slow_path_reason_to_string(i
))) {
207 parse_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
218 while (s
[n
] != ')') {
219 unsigned long long int flags
;
223 if (sscanf(&s
[n
], "%lli%n", &flags
, &n0
) > 0 && n0
> 0) {
224 n
+= n0
+ (s
[n
+ n0
] == ',');
229 for (bit
= 1; bit
; bit
<<= 1) {
230 const char *name
= bit_to_string(bit
);
238 if (!strncmp(s
+ n
, name
, len
) &&
239 (s
[n
+ len
] == ',' || s
[n
+ len
] == ')')) {
241 n
+= len
+ (s
[n
+ len
] == ',');
257 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
259 static const struct nl_policy ovs_userspace_policy
[] = {
260 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
261 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
264 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
265 const struct nlattr
*userdata_attr
;
267 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
268 ds_put_cstr(ds
, "userspace(error)");
272 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
273 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
275 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
278 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
279 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
280 bool userdata_unspec
= true;
281 union user_action_cookie cookie
;
283 if (userdata_len
>= sizeof cookie
.type
284 && userdata_len
<= sizeof cookie
) {
286 memset(&cookie
, 0, sizeof cookie
);
287 memcpy(&cookie
, userdata
, userdata_len
);
289 userdata_unspec
= false;
291 if (userdata_len
== sizeof cookie
.sflow
292 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
293 ds_put_format(ds
, ",sFlow("
294 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
295 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
296 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
297 cookie
.sflow
.output
);
298 } else if (userdata_len
== sizeof cookie
.slow_path
299 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
301 reason
= slow_path_reason_to_string(cookie
.slow_path
.reason
);
302 reason
= reason
? reason
: "";
303 ds_put_format(ds
, ",slow_path(%s)", reason
);
304 } else if (userdata_len
== sizeof cookie
.flow_sample
305 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
306 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
307 ",collector_set_id=%"PRIu32
308 ",obs_domain_id=%"PRIu32
309 ",obs_point_id=%"PRIu32
")",
310 cookie
.flow_sample
.probability
,
311 cookie
.flow_sample
.collector_set_id
,
312 cookie
.flow_sample
.obs_domain_id
,
313 cookie
.flow_sample
.obs_point_id
);
314 } else if (userdata_len
== sizeof cookie
.ipfix
315 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
316 ds_put_format(ds
, ",ipfix");
318 userdata_unspec
= true;
322 if (userdata_unspec
) {
324 ds_put_format(ds
, ",userdata(");
325 for (i
= 0; i
< userdata_len
; i
++) {
326 ds_put_format(ds
, "%02x", userdata
[i
]);
328 ds_put_char(ds
, ')');
332 ds_put_char(ds
, ')');
336 format_vlan_tci(struct ds
*ds
, ovs_be16 vlan_tci
)
338 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d",
339 vlan_tci_to_vid(vlan_tci
),
340 vlan_tci_to_pcp(vlan_tci
));
341 if (!(vlan_tci
& htons(VLAN_CFI
))) {
342 ds_put_cstr(ds
, ",cfi=0");
347 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
349 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
350 mpls_lse_to_label(mpls_lse
),
351 mpls_lse_to_tc(mpls_lse
),
352 mpls_lse_to_ttl(mpls_lse
),
353 mpls_lse_to_bos(mpls_lse
));
357 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
360 enum ovs_action_attr type
= nl_attr_type(a
);
361 const struct ovs_action_push_vlan
*vlan
;
363 expected_len
= odp_action_len(nl_attr_type(a
));
364 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
365 ds_put_format(ds
, "bad length %zu, expected %d for: ",
366 nl_attr_get_size(a
), expected_len
);
367 format_generic_odp_action(ds
, a
);
372 case OVS_ACTION_ATTR_OUTPUT
:
373 ds_put_format(ds
, "%"PRIu16
, nl_attr_get_u32(a
));
375 case OVS_ACTION_ATTR_USERSPACE
:
376 format_odp_userspace_action(ds
, a
);
378 case OVS_ACTION_ATTR_SET
:
379 ds_put_cstr(ds
, "set(");
380 format_odp_key_attr(nl_attr_get(a
), ds
);
381 ds_put_cstr(ds
, ")");
383 case OVS_ACTION_ATTR_PUSH_VLAN
:
384 vlan
= nl_attr_get(a
);
385 ds_put_cstr(ds
, "push_vlan(");
386 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
387 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
389 format_vlan_tci(ds
, vlan
->vlan_tci
);
390 ds_put_char(ds
, ')');
392 case OVS_ACTION_ATTR_POP_VLAN
:
393 ds_put_cstr(ds
, "pop_vlan");
395 case OVS_ACTION_ATTR_PUSH_MPLS
: {
396 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
397 ds_put_cstr(ds
, "push_mpls(");
398 format_mpls_lse(ds
, mpls
->mpls_lse
);
399 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
402 case OVS_ACTION_ATTR_POP_MPLS
: {
403 ovs_be16 ethertype
= nl_attr_get_be16(a
);
404 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
407 case OVS_ACTION_ATTR_SAMPLE
:
408 format_odp_sample_action(ds
, a
);
410 case OVS_ACTION_ATTR_UNSPEC
:
411 case __OVS_ACTION_ATTR_MAX
:
413 format_generic_odp_action(ds
, a
);
419 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
423 const struct nlattr
*a
;
426 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
428 ds_put_char(ds
, ',');
430 format_odp_action(ds
, a
);
435 if (left
== actions_len
) {
436 ds_put_cstr(ds
, "<empty>");
438 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
439 for (i
= 0; i
< left
; i
++) {
440 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
442 ds_put_char(ds
, ')');
445 ds_put_cstr(ds
, "drop");
450 parse_odp_action(const char *s
, const struct simap
*port_names
,
451 struct ofpbuf
*actions
)
453 /* Many of the sscanf calls in this function use oversized destination
454 * fields because some sscanf() implementations truncate the range of %i
455 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
456 * value of 0x7fff. The other alternatives are to allow only a single
457 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
460 * The tun_id parser has to use an alternative approach because there is no
461 * type larger than 64 bits. */
464 unsigned long long int port
;
467 if (sscanf(s
, "%lli%n", &port
, &n
) > 0 && n
> 0) {
468 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
474 int len
= strcspn(s
, delimiters
);
475 struct simap_node
*node
;
477 node
= simap_find_len(port_names
, s
, len
);
479 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
485 unsigned long long int pid
;
486 unsigned long long int output
;
487 unsigned long long int probability
;
488 unsigned long long int collector_set_id
;
489 unsigned long long int obs_domain_id
;
490 unsigned long long int obs_point_id
;
494 if (sscanf(s
, "userspace(pid=%lli)%n", &pid
, &n
) > 0 && n
> 0) {
495 odp_put_userspace_action(pid
, NULL
, 0, actions
);
497 } else if (sscanf(s
, "userspace(pid=%lli,sFlow(vid=%i,"
498 "pcp=%i,output=%lli))%n",
499 &pid
, &vid
, &pcp
, &output
, &n
) > 0 && n
> 0) {
500 union user_action_cookie cookie
;
503 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
508 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
509 cookie
.sflow
.vlan_tci
= htons(tci
);
510 cookie
.sflow
.output
= output
;
511 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.sflow
,
514 } else if (sscanf(s
, "userspace(pid=%lli,slow_path(%n", &pid
, &n
) > 0
516 union user_action_cookie cookie
;
519 if (s
[n
] == ')' && s
[n
+ 1] == ')') {
522 } else if (sscanf(s
+ n
, "%31[^)]))", reason
) > 0) {
523 n
+= strlen(reason
) + 2;
528 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
529 cookie
.slow_path
.unused
= 0;
530 cookie
.slow_path
.reason
= string_to_slow_path_reason(reason
);
532 if (reason
[0] && !cookie
.slow_path
.reason
) {
536 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.slow_path
,
539 } else if (sscanf(s
, "userspace(pid=%lli,flow_sample(probability=%lli,"
540 "collector_set_id=%lli,obs_domain_id=%lli,"
541 "obs_point_id=%lli))%n",
542 &pid
, &probability
, &collector_set_id
,
543 &obs_domain_id
, &obs_point_id
, &n
) > 0 && n
> 0) {
544 union user_action_cookie cookie
;
546 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
547 cookie
.flow_sample
.probability
= probability
;
548 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
549 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
550 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
551 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.flow_sample
,
554 } else if (sscanf(s
, "userspace(pid=%lli,ipfix)%n", &pid
, &n
) > 0
556 union user_action_cookie cookie
;
558 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
559 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.ipfix
,
562 } else if (sscanf(s
, "userspace(pid=%lli,userdata(%n", &pid
, &n
) > 0
567 ofpbuf_init(&buf
, 16);
568 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
569 if (end
[0] == ')' && end
[1] == ')') {
570 odp_put_userspace_action(pid
, buf
.data
, buf
.size
, actions
);
572 return (end
+ 2) - s
;
577 if (!strncmp(s
, "set(", 4)) {
581 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
582 retval
= parse_odp_key_attr(s
+ 4, port_names
, actions
);
586 if (s
[retval
+ 4] != ')') {
589 nl_msg_end_nested(actions
, start_ofs
);
594 struct ovs_action_push_vlan push
;
595 int tpid
= ETH_TYPE_VLAN
;
600 if ((sscanf(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
) > 0
602 || (sscanf(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
603 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)
604 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
605 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)
606 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
607 &tpid
, &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
608 push
.vlan_tpid
= htons(tpid
);
609 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
610 | (pcp
<< VLAN_PCP_SHIFT
)
611 | (cfi
? VLAN_CFI
: 0));
612 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
619 if (!strncmp(s
, "pop_vlan", 8)) {
620 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
628 if (sscanf(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
) > 0
629 && percentage
>= 0. && percentage
<= 100.0
631 size_t sample_ofs
, actions_ofs
;
634 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
635 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
636 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
637 (probability
<= 0 ? 0
638 : probability
>= UINT32_MAX
? UINT32_MAX
641 actions_ofs
= nl_msg_start_nested(actions
,
642 OVS_SAMPLE_ATTR_ACTIONS
);
646 n
+= strspn(s
+ n
, delimiters
);
651 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
657 nl_msg_end_nested(actions
, actions_ofs
);
658 nl_msg_end_nested(actions
, sample_ofs
);
660 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
667 /* Parses the string representation of datapath actions, in the format output
668 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
669 * value. On success, the ODP actions are appended to 'actions' as a series of
670 * Netlink attributes. On failure, no data is appended to 'actions'. Either
671 * way, 'actions''s data might be reallocated. */
673 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
674 struct ofpbuf
*actions
)
678 if (!strcasecmp(s
, "drop")) {
682 old_size
= actions
->size
;
686 s
+= strspn(s
, delimiters
);
691 retval
= parse_odp_action(s
, port_names
, actions
);
692 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
693 actions
->size
= old_size
;
702 /* Returns the correct length of the payload for a flow key attribute of the
703 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
704 * is variable length. */
706 odp_flow_key_attr_len(uint16_t type
)
708 if (type
> OVS_KEY_ATTR_MAX
) {
712 switch ((enum ovs_key_attr
) type
) {
713 case OVS_KEY_ATTR_ENCAP
: return -2;
714 case OVS_KEY_ATTR_PRIORITY
: return 4;
715 case OVS_KEY_ATTR_SKB_MARK
: return 4;
716 case OVS_KEY_ATTR_TUNNEL
: return -2;
717 case OVS_KEY_ATTR_IN_PORT
: return 4;
718 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
719 case OVS_KEY_ATTR_VLAN
: return sizeof(ovs_be16
);
720 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
721 case OVS_KEY_ATTR_MPLS
: return sizeof(struct ovs_key_mpls
);
722 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
723 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
724 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
725 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
726 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
727 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
728 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
729 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
731 case OVS_KEY_ATTR_UNSPEC
:
732 case __OVS_KEY_ATTR_MAX
:
740 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
742 size_t len
= nl_attr_get_size(a
);
744 const uint8_t *unspec
;
747 unspec
= nl_attr_get(a
);
748 for (i
= 0; i
< len
; i
++) {
749 ds_put_char(ds
, i
? ' ': '(');
750 ds_put_format(ds
, "%02x", unspec
[i
]);
752 ds_put_char(ds
, ')');
757 ovs_frag_type_to_string(enum ovs_frag_type type
)
760 case OVS_FRAG_TYPE_NONE
:
762 case OVS_FRAG_TYPE_FIRST
:
764 case OVS_FRAG_TYPE_LATER
:
766 case __OVS_FRAG_TYPE_MAX
:
773 tunnel_key_attr_len(int type
)
776 case OVS_TUNNEL_KEY_ATTR_ID
: return 8;
777 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
: return 4;
778 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
: return 4;
779 case OVS_TUNNEL_KEY_ATTR_TOS
: return 1;
780 case OVS_TUNNEL_KEY_ATTR_TTL
: return 1;
781 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
: return 0;
782 case OVS_TUNNEL_KEY_ATTR_CSUM
: return 0;
783 case __OVS_TUNNEL_KEY_ATTR_MAX
:
790 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
793 const struct nlattr
*a
;
795 bool unknown
= false;
797 NL_NESTED_FOR_EACH(a
, left
, attr
) {
798 uint16_t type
= nl_attr_type(a
);
799 size_t len
= nl_attr_get_size(a
);
800 int expected_len
= tunnel_key_attr_len(type
);
802 if (len
!= expected_len
&& expected_len
>= 0) {
803 return ODP_FIT_ERROR
;
807 case OVS_TUNNEL_KEY_ATTR_ID
:
808 tun
->tun_id
= nl_attr_get_be64(a
);
809 tun
->flags
|= FLOW_TNL_F_KEY
;
811 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
812 tun
->ip_src
= nl_attr_get_be32(a
);
814 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
815 tun
->ip_dst
= nl_attr_get_be32(a
);
817 case OVS_TUNNEL_KEY_ATTR_TOS
:
818 tun
->ip_tos
= nl_attr_get_u8(a
);
820 case OVS_TUNNEL_KEY_ATTR_TTL
:
821 tun
->ip_ttl
= nl_attr_get_u8(a
);
824 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
825 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
827 case OVS_TUNNEL_KEY_ATTR_CSUM
:
828 tun
->flags
|= FLOW_TNL_F_CSUM
;
831 /* Allow this to show up as unexpected, if there are unknown
832 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
839 return ODP_FIT_ERROR
;
842 return ODP_FIT_TOO_MUCH
;
844 return ODP_FIT_PERFECT
;
848 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
)
852 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
854 if (tun_key
->flags
& FLOW_TNL_F_KEY
) {
855 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
857 if (tun_key
->ip_src
) {
858 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
860 if (tun_key
->ip_dst
) {
861 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
863 if (tun_key
->ip_tos
) {
864 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
866 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
867 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
868 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
870 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
871 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
874 nl_msg_end_nested(a
, tun_key_ofs
);
878 format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
)
880 const struct ovs_key_ethernet
*eth_key
;
881 const struct ovs_key_ipv4
*ipv4_key
;
882 const struct ovs_key_ipv6
*ipv6_key
;
883 const struct ovs_key_tcp
*tcp_key
;
884 const struct ovs_key_udp
*udp_key
;
885 const struct ovs_key_icmp
*icmp_key
;
886 const struct ovs_key_icmpv6
*icmpv6_key
;
887 const struct ovs_key_arp
*arp_key
;
888 const struct ovs_key_nd
*nd_key
;
889 struct flow_tnl tun_key
;
890 enum ovs_key_attr attr
= nl_attr_type(a
);
893 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
));
894 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
895 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
896 ds_put_format(ds
, "(bad length %zu, expected %d)",
898 odp_flow_key_attr_len(nl_attr_type(a
)));
899 format_generic_odp_key(a
, ds
);
904 case OVS_KEY_ATTR_ENCAP
:
905 ds_put_cstr(ds
, "(");
906 if (nl_attr_get_size(a
)) {
907 odp_flow_key_format(nl_attr_get(a
), nl_attr_get_size(a
), ds
);
909 ds_put_char(ds
, ')');
912 case OVS_KEY_ATTR_PRIORITY
:
913 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
916 case OVS_KEY_ATTR_SKB_MARK
:
917 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
920 case OVS_KEY_ATTR_TUNNEL
:
921 memset(&tun_key
, 0, sizeof tun_key
);
922 if (odp_tun_key_from_attr(a
, &tun_key
) == ODP_FIT_ERROR
) {
923 ds_put_format(ds
, "(error)");
925 ds_put_format(ds
, "(tun_id=0x%"PRIx64
",src="IP_FMT
",dst="IP_FMT
","
926 "tos=0x%"PRIx8
",ttl=%"PRIu8
",flags(",
927 ntohll(tun_key
.tun_id
),
928 IP_ARGS(tun_key
.ip_src
),
929 IP_ARGS(tun_key
.ip_dst
),
930 tun_key
.ip_tos
, tun_key
.ip_ttl
);
932 format_flags(ds
, flow_tun_flag_to_string
,
933 (uint32_t) tun_key
.flags
, ',');
934 ds_put_format(ds
, "))");
938 case OVS_KEY_ATTR_IN_PORT
:
939 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
942 case OVS_KEY_ATTR_ETHERNET
:
943 eth_key
= nl_attr_get(a
);
944 ds_put_format(ds
, "(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
945 ETH_ADDR_ARGS(eth_key
->eth_src
),
946 ETH_ADDR_ARGS(eth_key
->eth_dst
));
949 case OVS_KEY_ATTR_VLAN
:
950 ds_put_char(ds
, '(');
951 format_vlan_tci(ds
, nl_attr_get_be16(a
));
952 ds_put_char(ds
, ')');
955 case OVS_KEY_ATTR_MPLS
: {
956 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
957 ds_put_char(ds
, '(');
958 format_mpls_lse(ds
, mpls_key
->mpls_lse
);
959 ds_put_char(ds
, ')');
963 case OVS_KEY_ATTR_ETHERTYPE
:
964 ds_put_format(ds
, "(0x%04"PRIx16
")",
965 ntohs(nl_attr_get_be16(a
)));
968 case OVS_KEY_ATTR_IPV4
:
969 ipv4_key
= nl_attr_get(a
);
970 ds_put_format(ds
, "(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
971 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s)",
972 IP_ARGS(ipv4_key
->ipv4_src
),
973 IP_ARGS(ipv4_key
->ipv4_dst
),
974 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
976 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
979 case OVS_KEY_ATTR_IPV6
: {
980 char src_str
[INET6_ADDRSTRLEN
];
981 char dst_str
[INET6_ADDRSTRLEN
];
983 ipv6_key
= nl_attr_get(a
);
984 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
985 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
987 ds_put_format(ds
, "(src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
988 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s)",
989 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
990 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
991 ipv6_key
->ipv6_hlimit
,
992 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
996 case OVS_KEY_ATTR_TCP
:
997 tcp_key
= nl_attr_get(a
);
998 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
999 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
1002 case OVS_KEY_ATTR_UDP
:
1003 udp_key
= nl_attr_get(a
);
1004 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
1005 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
1008 case OVS_KEY_ATTR_ICMP
:
1009 icmp_key
= nl_attr_get(a
);
1010 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
1011 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
1014 case OVS_KEY_ATTR_ICMPV6
:
1015 icmpv6_key
= nl_attr_get(a
);
1016 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
1017 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
1020 case OVS_KEY_ATTR_ARP
:
1021 arp_key
= nl_attr_get(a
);
1022 ds_put_format(ds
, "(sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
1023 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
")",
1024 IP_ARGS(arp_key
->arp_sip
), IP_ARGS(arp_key
->arp_tip
),
1025 ntohs(arp_key
->arp_op
), ETH_ADDR_ARGS(arp_key
->arp_sha
),
1026 ETH_ADDR_ARGS(arp_key
->arp_tha
));
1029 case OVS_KEY_ATTR_ND
: {
1030 char target
[INET6_ADDRSTRLEN
];
1032 nd_key
= nl_attr_get(a
);
1033 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
1035 ds_put_format(ds
, "(target=%s", target
);
1036 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
1037 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
1038 ETH_ADDR_ARGS(nd_key
->nd_sll
));
1040 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
1041 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
1042 ETH_ADDR_ARGS(nd_key
->nd_tll
));
1044 ds_put_char(ds
, ')');
1048 case OVS_KEY_ATTR_UNSPEC
:
1049 case __OVS_KEY_ATTR_MAX
:
1051 format_generic_odp_key(a
, ds
);
1056 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1057 * OVS_KEY_ATTR_* attributes in 'key'. */
1059 odp_flow_key_format(const struct nlattr
*key
, size_t key_len
, struct ds
*ds
)
1062 const struct nlattr
*a
;
1065 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
1067 ds_put_char(ds
, ',');
1069 format_odp_key_attr(a
, ds
);
1074 if (left
== key_len
) {
1075 ds_put_cstr(ds
, "<empty>");
1077 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1078 for (i
= 0; i
< left
; i
++) {
1079 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1081 ds_put_char(ds
, ')');
1084 ds_put_cstr(ds
, "<empty>");
1089 put_nd_key(int n
, const char *nd_target_s
,
1090 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
1092 struct ovs_key_nd nd_key
;
1094 memset(&nd_key
, 0, sizeof nd_key
);
1095 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
1099 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
1102 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
1104 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
1109 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
1111 if (!strcasecmp(s
, "no")) {
1112 *type
= OVS_FRAG_TYPE_NONE
;
1113 } else if (!strcasecmp(s
, "first")) {
1114 *type
= OVS_FRAG_TYPE_FIRST
;
1115 } else if (!strcasecmp(s
, "later")) {
1116 *type
= OVS_FRAG_TYPE_LATER
;
1124 mpls_lse_from_components(int mpls_label
, int mpls_tc
, int mpls_ttl
, int mpls_bos
)
1126 return (htonl((mpls_label
<< MPLS_LABEL_SHIFT
) |
1127 (mpls_tc
<< MPLS_TC_SHIFT
) |
1128 (mpls_ttl
<< MPLS_TTL_SHIFT
) |
1129 (mpls_bos
<< MPLS_BOS_SHIFT
)));
1133 parse_odp_key_attr(const char *s
, const struct simap
*port_names
,
1136 /* Many of the sscanf calls in this function use oversized destination
1137 * fields because some sscanf() implementations truncate the range of %i
1138 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1139 * value of 0x7fff. The other alternatives are to allow only a single
1140 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1143 * The tun_id parser has to use an alternative approach because there is no
1144 * type larger than 64 bits. */
1147 unsigned long long int priority
;
1150 if (sscanf(s
, "skb_priority(%llx)%n", &priority
, &n
) > 0 && n
> 0) {
1151 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1157 unsigned long long int mark
;
1160 if (sscanf(s
, "skb_mark(%llx)%n", &mark
, &n
) > 0 && n
> 0) {
1161 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1169 struct flow_tnl tun_key
;
1172 if (sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1173 "src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
1174 ",tos=%i,ttl=%i,flags%n", tun_id_s
,
1175 IP_SCAN_ARGS(&tun_key
.ip_src
),
1176 IP_SCAN_ARGS(&tun_key
.ip_dst
), &tos
, &ttl
,
1181 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1182 tun_key
.ip_tos
= tos
;
1183 tun_key
.ip_ttl
= ttl
;
1184 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1185 tun_key
.flags
= (uint16_t) flags
;
1195 tun_key_to_attr(key
, &tun_key
);
1201 unsigned long long int in_port
;
1204 if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
1205 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1210 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
1212 const struct simap_node
*node
;
1216 name_len
= strcspn(s
, ")");
1217 node
= simap_find_len(port_names
, name
, name_len
);
1219 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, node
->data
);
1220 return 8 + name_len
+ 1;
1225 struct ovs_key_ethernet eth_key
;
1229 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
1230 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1231 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
1232 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1233 ð_key
, sizeof eth_key
);
1244 if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n", &vid
, &pcp
, &n
) > 0
1246 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1247 htons((vid
<< VLAN_VID_SHIFT
) |
1248 (pcp
<< VLAN_PCP_SHIFT
) |
1251 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
1252 &vid
, &pcp
, &cfi
, &n
) > 0
1254 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1255 htons((vid
<< VLAN_VID_SHIFT
) |
1256 (pcp
<< VLAN_PCP_SHIFT
) |
1257 (cfi
? VLAN_CFI
: 0)));
1266 if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
1267 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1273 int label
, tc
, ttl
, bos
;
1276 if (sscanf(s
, "mpls(label=%"SCNi32
",tc=%i,ttl=%i,bos=%i)%n",
1277 &label
, &tc
, &ttl
, &bos
, &n
) > 0 &&
1279 struct ovs_key_mpls
*mpls
;
1281 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1283 mpls
->mpls_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1295 enum ovs_frag_type ipv4_frag
;
1298 if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
1299 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1300 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
1301 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
1303 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1304 struct ovs_key_ipv4 ipv4_key
;
1306 ipv4_key
.ipv4_src
= ipv4_src
;
1307 ipv4_key
.ipv4_dst
= ipv4_dst
;
1308 ipv4_key
.ipv4_proto
= ipv4_proto
;
1309 ipv4_key
.ipv4_tos
= ipv4_tos
;
1310 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1311 ipv4_key
.ipv4_frag
= ipv4_frag
;
1312 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1313 &ipv4_key
, sizeof ipv4_key
);
1319 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
1320 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
1326 enum ovs_frag_type ipv6_frag
;
1329 if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
1330 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1331 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
1332 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
1334 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1335 struct ovs_key_ipv6 ipv6_key
;
1337 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1338 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
1341 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1342 ipv6_key
.ipv6_proto
= ipv6_proto
;
1343 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1344 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1345 ipv6_key
.ipv6_frag
= ipv6_frag
;
1346 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1347 &ipv6_key
, sizeof ipv6_key
);
1357 if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
1359 struct ovs_key_tcp tcp_key
;
1361 tcp_key
.tcp_src
= htons(tcp_src
);
1362 tcp_key
.tcp_dst
= htons(tcp_dst
);
1363 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1373 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
1375 struct ovs_key_udp udp_key
;
1377 udp_key
.udp_src
= htons(udp_src
);
1378 udp_key
.udp_dst
= htons(udp_dst
);
1379 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1389 if (sscanf(s
, "icmp(type=%i,code=%i)%n",
1390 &icmp_type
, &icmp_code
, &n
) > 0
1392 struct ovs_key_icmp icmp_key
;
1394 icmp_key
.icmp_type
= icmp_type
;
1395 icmp_key
.icmp_code
= icmp_code
;
1396 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1397 &icmp_key
, sizeof icmp_key
);
1403 struct ovs_key_icmpv6 icmpv6_key
;
1406 if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
1407 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
1409 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
1410 &icmpv6_key
, sizeof icmpv6_key
);
1419 uint8_t arp_sha
[ETH_ADDR_LEN
];
1420 uint8_t arp_tha
[ETH_ADDR_LEN
];
1423 if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
1424 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
1425 IP_SCAN_ARGS(&arp_sip
),
1426 IP_SCAN_ARGS(&arp_tip
),
1428 ETH_ADDR_SCAN_ARGS(arp_sha
),
1429 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
1430 struct ovs_key_arp arp_key
;
1432 memset(&arp_key
, 0, sizeof arp_key
);
1433 arp_key
.arp_sip
= arp_sip
;
1434 arp_key
.arp_tip
= arp_tip
;
1435 arp_key
.arp_op
= htons(arp_op
);
1436 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
1437 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
1438 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
1444 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
1445 uint8_t nd_sll
[ETH_ADDR_LEN
];
1446 uint8_t nd_tll
[ETH_ADDR_LEN
];
1449 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
1450 nd_target_s
, &n
) > 0 && n
> 0) {
1451 return put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
1453 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
1454 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
1456 return put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
1458 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
1459 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1461 return put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
1463 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
1464 "tll="ETH_ADDR_SCAN_FMT
")%n",
1465 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
1466 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1468 return put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
1472 if (!strncmp(s
, "encap(", 6)) {
1473 const char *start
= s
;
1476 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
1482 s
+= strspn(s
, ", \t\r\n");
1485 } else if (*s
== ')') {
1489 retval
= parse_odp_key_attr(s
, port_names
, key
);
1497 nl_msg_end_nested(key
, encap
);
1505 /* Parses the string representation of a datapath flow key, in the
1506 * format output by odp_flow_key_format(). Returns 0 if successful,
1507 * otherwise a positive errno value. On success, the flow key is
1508 * appended to 'key' as a series of Netlink attributes. On failure, no
1509 * data is appended to 'key'. Either way, 'key''s data might be
1512 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1513 * to a port number. (Port names may be used instead of port numbers in
1516 * On success, the attributes appended to 'key' are individually syntactically
1517 * valid, but they may not be valid as a sequence. 'key' might, for example,
1518 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1520 odp_flow_key_from_string(const char *s
, const struct simap
*port_names
,
1523 const size_t old_size
= key
->size
;
1527 s
+= strspn(s
, delimiters
);
1532 retval
= parse_odp_key_attr(s
, port_names
, key
);
1534 key
->size
= old_size
;
1544 ovs_to_odp_frag(uint8_t nw_frag
)
1546 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
1547 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
1548 : OVS_FRAG_TYPE_LATER
);
1551 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1552 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1553 * number rather than a datapath port number). Instead, if 'odp_in_port'
1554 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1557 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1558 * capable of being expanded to allow for that much space. */
1560 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
,
1561 uint32_t odp_in_port
)
1563 struct ovs_key_ethernet
*eth_key
;
1566 if (flow
->skb_priority
) {
1567 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, flow
->skb_priority
);
1570 if (flow
->tunnel
.ip_dst
) {
1571 tun_key_to_attr(buf
, &flow
->tunnel
);
1574 if (flow
->skb_mark
) {
1575 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, flow
->skb_mark
);
1578 if (odp_in_port
!= OVSP_NONE
) {
1579 nl_msg_put_u32(buf
, OVS_KEY_ATTR_IN_PORT
, odp_in_port
);
1582 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
1584 memcpy(eth_key
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1585 memcpy(eth_key
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1587 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1588 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
1589 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, flow
->vlan_tci
);
1590 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
1591 if (flow
->vlan_tci
== htons(0)) {
1598 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
1602 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, flow
->dl_type
);
1604 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1605 struct ovs_key_ipv4
*ipv4_key
;
1607 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
1609 ipv4_key
->ipv4_src
= flow
->nw_src
;
1610 ipv4_key
->ipv4_dst
= flow
->nw_dst
;
1611 ipv4_key
->ipv4_proto
= flow
->nw_proto
;
1612 ipv4_key
->ipv4_tos
= flow
->nw_tos
;
1613 ipv4_key
->ipv4_ttl
= flow
->nw_ttl
;
1614 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1615 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1616 struct ovs_key_ipv6
*ipv6_key
;
1618 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
1620 memcpy(ipv6_key
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
1621 memcpy(ipv6_key
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
1622 ipv6_key
->ipv6_label
= flow
->ipv6_label
;
1623 ipv6_key
->ipv6_proto
= flow
->nw_proto
;
1624 ipv6_key
->ipv6_tclass
= flow
->nw_tos
;
1625 ipv6_key
->ipv6_hlimit
= flow
->nw_ttl
;
1626 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1627 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
1628 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
1629 struct ovs_key_arp
*arp_key
;
1631 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
1633 memset(arp_key
, 0, sizeof *arp_key
);
1634 arp_key
->arp_sip
= flow
->nw_src
;
1635 arp_key
->arp_tip
= flow
->nw_dst
;
1636 arp_key
->arp_op
= htons(flow
->nw_proto
);
1637 memcpy(arp_key
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
1638 memcpy(arp_key
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
1641 if (flow
->mpls_depth
) {
1642 struct ovs_key_mpls
*mpls_key
;
1644 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
1646 mpls_key
->mpls_lse
= flow
->mpls_lse
;
1649 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1650 if (flow
->nw_proto
== IPPROTO_TCP
) {
1651 struct ovs_key_tcp
*tcp_key
;
1653 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
1655 tcp_key
->tcp_src
= flow
->tp_src
;
1656 tcp_key
->tcp_dst
= flow
->tp_dst
;
1657 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
1658 struct ovs_key_udp
*udp_key
;
1660 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
1662 udp_key
->udp_src
= flow
->tp_src
;
1663 udp_key
->udp_dst
= flow
->tp_dst
;
1664 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
1665 && flow
->nw_proto
== IPPROTO_ICMP
) {
1666 struct ovs_key_icmp
*icmp_key
;
1668 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
1670 icmp_key
->icmp_type
= ntohs(flow
->tp_src
);
1671 icmp_key
->icmp_code
= ntohs(flow
->tp_dst
);
1672 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1673 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
1674 struct ovs_key_icmpv6
*icmpv6_key
;
1676 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
1677 sizeof *icmpv6_key
);
1678 icmpv6_key
->icmpv6_type
= ntohs(flow
->tp_src
);
1679 icmpv6_key
->icmpv6_code
= ntohs(flow
->tp_dst
);
1681 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
1682 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
1683 struct ovs_key_nd
*nd_key
;
1685 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
1687 memcpy(nd_key
->nd_target
, &flow
->nd_target
,
1688 sizeof nd_key
->nd_target
);
1689 memcpy(nd_key
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
1690 memcpy(nd_key
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
1697 nl_msg_end_nested(buf
, encap
);
1702 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
1704 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
1705 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
1709 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
1710 uint64_t attrs
, int out_of_range_attr
,
1711 const struct nlattr
*key
, size_t key_len
)
1716 if (VLOG_DROP_DBG(rl
)) {
1721 for (i
= 0; i
< 64; i
++) {
1722 if (attrs
& (UINT64_C(1) << i
)) {
1723 ds_put_format(&s
, " %s", ovs_key_attr_to_string(i
));
1726 if (out_of_range_attr
) {
1727 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
1730 ds_put_cstr(&s
, ": ");
1731 odp_flow_key_format(key
, key_len
, &s
);
1733 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
1738 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
1740 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1742 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
1743 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
1747 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
1748 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
1749 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
1750 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
1757 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
1758 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
1759 int *out_of_range_attrp
)
1761 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1762 const struct nlattr
*nla
;
1763 uint64_t present_attrs
;
1766 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
1768 *out_of_range_attrp
= 0;
1769 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
1770 uint16_t type
= nl_attr_type(nla
);
1771 size_t len
= nl_attr_get_size(nla
);
1772 int expected_len
= odp_flow_key_attr_len(type
);
1774 if (len
!= expected_len
&& expected_len
>= 0) {
1775 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
1776 "length %d", ovs_key_attr_to_string(type
),
1781 if (type
> OVS_KEY_ATTR_MAX
) {
1782 *out_of_range_attrp
= type
;
1784 if (present_attrs
& (UINT64_C(1) << type
)) {
1785 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
1786 ovs_key_attr_to_string(type
));
1790 present_attrs
|= UINT64_C(1) << type
;
1795 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
1799 *present_attrsp
= present_attrs
;
1803 static enum odp_key_fitness
1804 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
1805 uint64_t expected_attrs
,
1806 const struct nlattr
*key
, size_t key_len
)
1808 uint64_t missing_attrs
;
1809 uint64_t extra_attrs
;
1811 missing_attrs
= expected_attrs
& ~present_attrs
;
1812 if (missing_attrs
) {
1813 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1814 log_odp_key_attributes(&rl
, "expected but not present",
1815 missing_attrs
, 0, key
, key_len
);
1816 return ODP_FIT_TOO_LITTLE
;
1819 extra_attrs
= present_attrs
& ~expected_attrs
;
1820 if (extra_attrs
|| out_of_range_attr
) {
1821 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1822 log_odp_key_attributes(&rl
, "present but not expected",
1823 extra_attrs
, out_of_range_attr
, key
, key_len
);
1824 return ODP_FIT_TOO_MUCH
;
1827 return ODP_FIT_PERFECT
;
1831 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1832 uint64_t present_attrs
, uint64_t *expected_attrs
,
1835 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1837 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
1838 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
1839 if (ntohs(flow
->dl_type
) < 1536) {
1840 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
1841 ntohs(flow
->dl_type
));
1844 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
1846 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
1851 static enum odp_key_fitness
1852 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1853 uint64_t present_attrs
, int out_of_range_attr
,
1854 uint64_t expected_attrs
, struct flow
*flow
,
1855 const struct nlattr
*key
, size_t key_len
)
1857 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1859 if (eth_type_mpls(flow
->dl_type
)) {
1860 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
1862 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
))) {
1863 return ODP_FIT_TOO_LITTLE
;
1865 flow
->mpls_lse
= nl_attr_get_be32(attrs
[OVS_KEY_ATTR_MPLS
]);
1867 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1868 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
1869 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1870 const struct ovs_key_ipv4
*ipv4_key
;
1872 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
1873 flow
->nw_src
= ipv4_key
->ipv4_src
;
1874 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
1875 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
1876 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
1877 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
1878 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
1879 return ODP_FIT_ERROR
;
1882 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1883 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
1884 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1885 const struct ovs_key_ipv6
*ipv6_key
;
1887 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
1888 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
1889 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
1890 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
1891 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
1892 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
1893 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
1894 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
1895 return ODP_FIT_ERROR
;
1898 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
1899 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
1900 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
1901 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1902 const struct ovs_key_arp
*arp_key
;
1904 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
1905 flow
->nw_src
= arp_key
->arp_sip
;
1906 flow
->nw_dst
= arp_key
->arp_tip
;
1907 if (arp_key
->arp_op
& htons(0xff00)) {
1908 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
1909 "key", ntohs(arp_key
->arp_op
));
1910 return ODP_FIT_ERROR
;
1912 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
1913 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
1914 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
1918 if (flow
->nw_proto
== IPPROTO_TCP
1919 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1920 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1921 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1922 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
1923 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
1924 const struct ovs_key_tcp
*tcp_key
;
1926 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
1927 flow
->tp_src
= tcp_key
->tcp_src
;
1928 flow
->tp_dst
= tcp_key
->tcp_dst
;
1930 } else if (flow
->nw_proto
== IPPROTO_UDP
1931 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1932 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1933 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1934 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
1935 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
1936 const struct ovs_key_udp
*udp_key
;
1938 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
1939 flow
->tp_src
= udp_key
->udp_src
;
1940 flow
->tp_dst
= udp_key
->udp_dst
;
1942 } else if (flow
->nw_proto
== IPPROTO_ICMP
1943 && flow
->dl_type
== htons(ETH_TYPE_IP
)
1944 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1945 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
1946 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
1947 const struct ovs_key_icmp
*icmp_key
;
1949 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
1950 flow
->tp_src
= htons(icmp_key
->icmp_type
);
1951 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
1953 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
1954 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1955 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1956 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
1957 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
1958 const struct ovs_key_icmpv6
*icmpv6_key
;
1960 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
1961 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
1962 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
1964 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
1965 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
1966 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
1967 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
1968 const struct ovs_key_nd
*nd_key
;
1970 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
1971 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
1972 sizeof flow
->nd_target
);
1973 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
1974 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
1980 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
1984 /* Parse 802.1Q header then encapsulated L3 attributes. */
1985 static enum odp_key_fitness
1986 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1987 uint64_t present_attrs
, int out_of_range_attr
,
1988 uint64_t expected_attrs
, struct flow
*flow
,
1989 const struct nlattr
*key
, size_t key_len
)
1991 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1993 const struct nlattr
*encap
1994 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
1995 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
1996 enum odp_key_fitness encap_fitness
;
1997 enum odp_key_fitness fitness
;
2000 /* Calculate fitness of outer attributes. */
2001 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
2002 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
2003 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
2004 expected_attrs
, key
, key_len
);
2006 /* Get the VLAN TCI value. */
2007 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
2008 return ODP_FIT_TOO_LITTLE
;
2010 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
2011 if (tci
== htons(0)) {
2012 /* Corner case for a truncated 802.1Q header. */
2013 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
2014 return ODP_FIT_TOO_MUCH
;
2017 } else if (!(tci
& htons(VLAN_CFI
))) {
2018 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
2019 "but CFI bit is not set", ntohs(tci
));
2020 return ODP_FIT_ERROR
;
2024 * Remove the TPID from dl_type since it's not the real Ethertype. */
2025 flow
->vlan_tci
= tci
;
2026 flow
->dl_type
= htons(0);
2028 /* Now parse the encapsulated attributes. */
2029 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
2030 attrs
, &present_attrs
, &out_of_range_attr
)) {
2031 return ODP_FIT_ERROR
;
2035 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2036 return ODP_FIT_ERROR
;
2038 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2039 expected_attrs
, flow
, key
, key_len
);
2041 /* The overall fitness is the worse of the outer and inner attributes. */
2042 return MAX(fitness
, encap_fitness
);
2045 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2046 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2047 * 'key' fits our expectations for what a flow key should contain.
2049 * The 'in_port' will be the datapath's understanding of the port. The
2050 * caller will need to translate with odp_port_to_ofp_port() if the
2051 * OpenFlow port is needed.
2053 * This function doesn't take the packet itself as an argument because none of
2054 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2055 * it is always possible to infer which additional attribute(s) should appear
2056 * by looking at the attributes for lower-level protocols, e.g. if the network
2057 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2058 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2059 * must be absent. */
2060 enum odp_key_fitness
2061 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
2064 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
2065 uint64_t expected_attrs
;
2066 uint64_t present_attrs
;
2067 int out_of_range_attr
;
2069 memset(flow
, 0, sizeof *flow
);
2071 /* Parse attributes. */
2072 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
2073 &out_of_range_attr
)) {
2074 return ODP_FIT_ERROR
;
2079 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
2080 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
2081 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
2084 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
2085 flow
->skb_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
2086 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
2089 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
2090 enum odp_key_fitness res
;
2092 res
= odp_tun_key_from_attr(attrs
[OVS_KEY_ATTR_TUNNEL
], &flow
->tunnel
);
2093 if (res
== ODP_FIT_ERROR
) {
2094 return ODP_FIT_ERROR
;
2095 } else if (res
== ODP_FIT_PERFECT
) {
2096 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
2100 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
2101 flow
->in_port
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_IN_PORT
]);
2102 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
2104 flow
->in_port
= OVSP_NONE
;
2107 /* Ethernet header. */
2108 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
2109 const struct ovs_key_ethernet
*eth_key
;
2111 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
2112 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
2113 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
2115 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
2117 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2118 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2119 return ODP_FIT_ERROR
;
2122 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2123 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
2124 expected_attrs
, flow
, key
, key_len
);
2126 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2127 expected_attrs
, flow
, key
, key_len
);
2130 /* Returns 'fitness' as a string, for use in debug messages. */
2132 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
2135 case ODP_FIT_PERFECT
:
2137 case ODP_FIT_TOO_MUCH
:
2139 case ODP_FIT_TOO_LITTLE
:
2140 return "too_little";
2148 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2149 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2150 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2151 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2152 * null, then the return value is not meaningful.) */
2154 odp_put_userspace_action(uint32_t pid
,
2155 const void *userdata
, size_t userdata_size
,
2156 struct ofpbuf
*odp_actions
)
2158 size_t userdata_ofs
;
2161 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
2162 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
2164 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
2165 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
2166 userdata
, userdata_size
);
2170 nl_msg_end_nested(odp_actions
, offset
);
2172 return userdata_ofs
;
2176 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
2177 struct ofpbuf
*odp_actions
)
2179 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2180 tun_key_to_attr(odp_actions
, tunnel
);
2181 nl_msg_end_nested(odp_actions
, offset
);
2184 /* The commit_odp_actions() function and its helpers. */
2187 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
2188 const void *key
, size_t key_size
)
2190 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2191 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
2192 nl_msg_end_nested(odp_actions
, offset
);
2196 odp_put_skb_mark_action(const uint32_t skb_mark
,
2197 struct ofpbuf
*odp_actions
)
2199 commit_set_action(odp_actions
, OVS_KEY_ATTR_SKB_MARK
, &skb_mark
,
2203 /* If any of the flow key data that ODP actions can modify are different in
2204 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2205 * 'odp_actions' that change the flow tunneling information in key from
2206 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2207 * same way. In other words, operates the same as commit_odp_actions(), but
2208 * only on tunneling information. */
2210 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
2211 struct ofpbuf
*odp_actions
)
2213 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2214 if (flow
->tunnel
.ip_dst
) {
2215 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
2218 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
2219 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
2224 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
2225 struct ofpbuf
*odp_actions
)
2227 struct ovs_key_ethernet eth_key
;
2229 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
2230 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
2234 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
2235 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
2237 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
2238 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
2240 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
2241 ð_key
, sizeof(eth_key
));
2245 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
2246 struct ofpbuf
*odp_actions
)
2248 if (base
->vlan_tci
== flow
->vlan_tci
) {
2252 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
2253 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2256 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
2257 struct ovs_action_push_vlan vlan
;
2259 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
2260 vlan
.vlan_tci
= flow
->vlan_tci
;
2261 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2262 &vlan
, sizeof vlan
);
2264 base
->vlan_tci
= flow
->vlan_tci
;
2268 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
2269 struct ofpbuf
*odp_actions
)
2271 if (flow
->mpls_lse
== base
->mpls_lse
&&
2272 flow
->mpls_depth
== base
->mpls_depth
) {
2276 if (flow
->mpls_depth
< base
->mpls_depth
) {
2277 if (base
->mpls_depth
- flow
->mpls_depth
> 1) {
2278 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2279 VLOG_WARN_RL(&rl
, "Multiple mpls_pop actions reduced to "
2280 " a single mpls_pop action");
2283 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, flow
->dl_type
);
2284 } else if (flow
->mpls_depth
> base
->mpls_depth
) {
2285 struct ovs_action_push_mpls
*mpls
;
2287 if (flow
->mpls_depth
- base
->mpls_depth
> 1) {
2288 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2289 VLOG_WARN_RL(&rl
, "Multiple mpls_push actions reduced to "
2290 " a single mpls_push action");
2293 mpls
= nl_msg_put_unspec_uninit(odp_actions
, OVS_ACTION_ATTR_PUSH_MPLS
,
2295 memset(mpls
, 0, sizeof *mpls
);
2296 mpls
->mpls_ethertype
= flow
->dl_type
;
2297 mpls
->mpls_lse
= flow
->mpls_lse
;
2299 struct ovs_key_mpls mpls_key
;
2301 mpls_key
.mpls_lse
= flow
->mpls_lse
;
2302 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
2303 &mpls_key
, sizeof(mpls_key
));
2306 base
->dl_type
= flow
->dl_type
;
2307 base
->mpls_lse
= flow
->mpls_lse
;
2308 base
->mpls_depth
= flow
->mpls_depth
;
2312 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
2313 struct ofpbuf
*odp_actions
)
2315 struct ovs_key_ipv4 ipv4_key
;
2317 if (base
->nw_src
== flow
->nw_src
&&
2318 base
->nw_dst
== flow
->nw_dst
&&
2319 base
->nw_tos
== flow
->nw_tos
&&
2320 base
->nw_ttl
== flow
->nw_ttl
&&
2321 base
->nw_frag
== flow
->nw_frag
) {
2325 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
2326 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
2327 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
2328 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
2329 ipv4_key
.ipv4_proto
= base
->nw_proto
;
2330 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
2332 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
2333 &ipv4_key
, sizeof(ipv4_key
));
2337 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
2338 struct ofpbuf
*odp_actions
)
2340 struct ovs_key_ipv6 ipv6_key
;
2342 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
2343 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
2344 base
->ipv6_label
== flow
->ipv6_label
&&
2345 base
->nw_tos
== flow
->nw_tos
&&
2346 base
->nw_ttl
== flow
->nw_ttl
&&
2347 base
->nw_frag
== flow
->nw_frag
) {
2351 base
->ipv6_src
= flow
->ipv6_src
;
2352 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
2353 base
->ipv6_dst
= flow
->ipv6_dst
;
2354 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
2356 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
2357 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
2358 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
2359 ipv6_key
.ipv6_proto
= base
->nw_proto
;
2360 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
2362 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
2363 &ipv6_key
, sizeof(ipv6_key
));
2367 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
2368 struct ofpbuf
*odp_actions
)
2370 /* Check if flow really have an IP header. */
2371 if (!flow
->nw_proto
) {
2375 if (base
->dl_type
== htons(ETH_TYPE_IP
)) {
2376 commit_set_ipv4_action(flow
, base
, odp_actions
);
2377 } else if (base
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2378 commit_set_ipv6_action(flow
, base
, odp_actions
);
2383 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
2384 struct ofpbuf
*odp_actions
)
2386 if (!is_ip_any(base
) || (!base
->tp_src
&& !base
->tp_dst
)) {
2390 if (base
->tp_src
== flow
->tp_src
&&
2391 base
->tp_dst
== flow
->tp_dst
) {
2395 if (flow
->nw_proto
== IPPROTO_TCP
) {
2396 struct ovs_key_tcp port_key
;
2398 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
2399 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
2401 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
2402 &port_key
, sizeof(port_key
));
2404 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
2405 struct ovs_key_udp port_key
;
2407 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
2408 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
2410 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
2411 &port_key
, sizeof(port_key
));
2416 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
2417 struct ofpbuf
*odp_actions
)
2419 if (base
->skb_priority
== flow
->skb_priority
) {
2422 base
->skb_priority
= flow
->skb_priority
;
2424 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
2425 &base
->skb_priority
, sizeof(base
->skb_priority
));
2429 commit_set_skb_mark_action(const struct flow
*flow
, struct flow
*base
,
2430 struct ofpbuf
*odp_actions
)
2432 if (base
->skb_mark
== flow
->skb_mark
) {
2435 base
->skb_mark
= flow
->skb_mark
;
2437 odp_put_skb_mark_action(base
->skb_mark
, odp_actions
);
2439 /* If any of the flow key data that ODP actions can modify are different in
2440 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2441 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
2442 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
2443 * in addition to this function if needed. */
2445 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
2446 struct ofpbuf
*odp_actions
)
2448 commit_set_ether_addr_action(flow
, base
, odp_actions
);
2449 commit_vlan_action(flow
, base
, odp_actions
);
2450 commit_set_nw_action(flow
, base
, odp_actions
);
2451 commit_set_port_action(flow
, base
, odp_actions
);
2452 /* Committing MPLS actions should occur after committing nw and port
2453 * actions. This is because committing MPLS actions may alter a packet so
2454 * that it is no longer IP and thus nw and port actions are no longer valid.
2456 commit_mpls_action(flow
, base
, odp_actions
);
2457 commit_set_priority_action(flow
, base
, odp_actions
);
2458 commit_set_skb_mark_action(flow
, base
, odp_actions
);