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(uint32_t data
)
175 enum slow_path_reason bit
= (enum slow_path_reason
) data
;
186 case SLOW_CONTROLLER
:
196 parse_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
207 while (s
[n
] != ')') {
208 unsigned long long int flags
;
212 if (sscanf(&s
[n
], "%lli%n", &flags
, &n0
) > 0 && n0
> 0) {
213 n
+= n0
+ (s
[n
+ n0
] == ',');
218 for (bit
= 1; bit
; bit
<<= 1) {
219 const char *name
= bit_to_string(bit
);
227 if (!strncmp(s
+ n
, name
, len
) &&
228 (s
[n
+ len
] == ',' || s
[n
+ len
] == ')')) {
230 n
+= len
+ (s
[n
+ len
] == ',');
246 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
248 static const struct nl_policy ovs_userspace_policy
[] = {
249 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
250 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
253 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
254 const struct nlattr
*userdata_attr
;
256 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
257 ds_put_cstr(ds
, "userspace(error)");
261 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
262 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
264 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
267 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
268 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
269 bool userdata_unspec
= true;
270 union user_action_cookie cookie
;
272 if (userdata_len
>= sizeof cookie
.type
273 && userdata_len
<= sizeof cookie
) {
275 memset(&cookie
, 0, sizeof cookie
);
276 memcpy(&cookie
, userdata
, userdata_len
);
278 userdata_unspec
= false;
280 if (userdata_len
== sizeof cookie
.sflow
281 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
282 ds_put_format(ds
, ",sFlow("
283 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
284 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
285 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
286 cookie
.sflow
.output
);
287 } else if (userdata_len
== sizeof cookie
.slow_path
288 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
289 ds_put_cstr(ds
, ",slow_path(");
290 format_flags(ds
, slow_path_reason_to_string
,
291 cookie
.slow_path
.reason
, ',');
292 ds_put_format(ds
, ")");
293 } else if (userdata_len
== sizeof cookie
.flow_sample
294 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
295 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
296 ",collector_set_id=%"PRIu32
297 ",obs_domain_id=%"PRIu32
298 ",obs_point_id=%"PRIu32
")",
299 cookie
.flow_sample
.probability
,
300 cookie
.flow_sample
.collector_set_id
,
301 cookie
.flow_sample
.obs_domain_id
,
302 cookie
.flow_sample
.obs_point_id
);
303 } else if (userdata_len
== sizeof cookie
.ipfix
304 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
305 ds_put_format(ds
, ",ipfix");
307 userdata_unspec
= true;
311 if (userdata_unspec
) {
313 ds_put_format(ds
, ",userdata(");
314 for (i
= 0; i
< userdata_len
; i
++) {
315 ds_put_format(ds
, "%02x", userdata
[i
]);
317 ds_put_char(ds
, ')');
321 ds_put_char(ds
, ')');
325 format_vlan_tci(struct ds
*ds
, ovs_be16 vlan_tci
)
327 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d",
328 vlan_tci_to_vid(vlan_tci
),
329 vlan_tci_to_pcp(vlan_tci
));
330 if (!(vlan_tci
& htons(VLAN_CFI
))) {
331 ds_put_cstr(ds
, ",cfi=0");
336 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
338 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
339 mpls_lse_to_label(mpls_lse
),
340 mpls_lse_to_tc(mpls_lse
),
341 mpls_lse_to_ttl(mpls_lse
),
342 mpls_lse_to_bos(mpls_lse
));
346 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
349 enum ovs_action_attr type
= nl_attr_type(a
);
350 const struct ovs_action_push_vlan
*vlan
;
352 expected_len
= odp_action_len(nl_attr_type(a
));
353 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
354 ds_put_format(ds
, "bad length %zu, expected %d for: ",
355 nl_attr_get_size(a
), expected_len
);
356 format_generic_odp_action(ds
, a
);
361 case OVS_ACTION_ATTR_OUTPUT
:
362 ds_put_format(ds
, "%"PRIu16
, nl_attr_get_u32(a
));
364 case OVS_ACTION_ATTR_USERSPACE
:
365 format_odp_userspace_action(ds
, a
);
367 case OVS_ACTION_ATTR_SET
:
368 ds_put_cstr(ds
, "set(");
369 format_odp_key_attr(nl_attr_get(a
), ds
);
370 ds_put_cstr(ds
, ")");
372 case OVS_ACTION_ATTR_PUSH_VLAN
:
373 vlan
= nl_attr_get(a
);
374 ds_put_cstr(ds
, "push_vlan(");
375 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
376 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
378 format_vlan_tci(ds
, vlan
->vlan_tci
);
379 ds_put_char(ds
, ')');
381 case OVS_ACTION_ATTR_POP_VLAN
:
382 ds_put_cstr(ds
, "pop_vlan");
384 case OVS_ACTION_ATTR_PUSH_MPLS
: {
385 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
386 ds_put_cstr(ds
, "push_mpls(");
387 format_mpls_lse(ds
, mpls
->mpls_lse
);
388 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
391 case OVS_ACTION_ATTR_POP_MPLS
: {
392 ovs_be16 ethertype
= nl_attr_get_be16(a
);
393 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
396 case OVS_ACTION_ATTR_SAMPLE
:
397 format_odp_sample_action(ds
, a
);
399 case OVS_ACTION_ATTR_UNSPEC
:
400 case __OVS_ACTION_ATTR_MAX
:
402 format_generic_odp_action(ds
, a
);
408 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
412 const struct nlattr
*a
;
415 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
417 ds_put_char(ds
, ',');
419 format_odp_action(ds
, a
);
424 if (left
== actions_len
) {
425 ds_put_cstr(ds
, "<empty>");
427 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
428 for (i
= 0; i
< left
; i
++) {
429 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
431 ds_put_char(ds
, ')');
434 ds_put_cstr(ds
, "drop");
439 parse_odp_action(const char *s
, const struct simap
*port_names
,
440 struct ofpbuf
*actions
)
442 /* Many of the sscanf calls in this function use oversized destination
443 * fields because some sscanf() implementations truncate the range of %i
444 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
445 * value of 0x7fff. The other alternatives are to allow only a single
446 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
449 * The tun_id parser has to use an alternative approach because there is no
450 * type larger than 64 bits. */
453 unsigned long long int port
;
456 if (sscanf(s
, "%lli%n", &port
, &n
) > 0 && n
> 0) {
457 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
463 int len
= strcspn(s
, delimiters
);
464 struct simap_node
*node
;
466 node
= simap_find_len(port_names
, s
, len
);
468 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
474 unsigned long long int pid
;
475 unsigned long long int output
;
476 unsigned long long int probability
;
477 unsigned long long int collector_set_id
;
478 unsigned long long int obs_domain_id
;
479 unsigned long long int obs_point_id
;
483 if (sscanf(s
, "userspace(pid=%lli)%n", &pid
, &n
) > 0 && n
> 0) {
484 odp_put_userspace_action(pid
, NULL
, 0, actions
);
486 } else if (sscanf(s
, "userspace(pid=%lli,sFlow(vid=%i,"
487 "pcp=%i,output=%lli))%n",
488 &pid
, &vid
, &pcp
, &output
, &n
) > 0 && n
> 0) {
489 union user_action_cookie cookie
;
492 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
497 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
498 cookie
.sflow
.vlan_tci
= htons(tci
);
499 cookie
.sflow
.output
= output
;
500 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.sflow
,
503 } else if (sscanf(s
, "userspace(pid=%lli,slow_path%n", &pid
, &n
) > 0
505 union user_action_cookie cookie
;
508 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
509 cookie
.slow_path
.unused
= 0;
510 cookie
.slow_path
.reason
= 0;
512 res
= parse_flags(&s
[n
], slow_path_reason_to_string
,
513 &cookie
.slow_path
.reason
);
523 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.slow_path
,
526 } else if (sscanf(s
, "userspace(pid=%lli,flow_sample(probability=%lli,"
527 "collector_set_id=%lli,obs_domain_id=%lli,"
528 "obs_point_id=%lli))%n",
529 &pid
, &probability
, &collector_set_id
,
530 &obs_domain_id
, &obs_point_id
, &n
) > 0 && n
> 0) {
531 union user_action_cookie cookie
;
533 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
534 cookie
.flow_sample
.probability
= probability
;
535 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
536 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
537 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
538 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.flow_sample
,
541 } else if (sscanf(s
, "userspace(pid=%lli,ipfix)%n", &pid
, &n
) > 0
543 union user_action_cookie cookie
;
545 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
546 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
.ipfix
,
549 } else if (sscanf(s
, "userspace(pid=%lli,userdata(%n", &pid
, &n
) > 0
554 ofpbuf_init(&buf
, 16);
555 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
556 if (end
[0] == ')' && end
[1] == ')') {
557 odp_put_userspace_action(pid
, buf
.data
, buf
.size
, actions
);
559 return (end
+ 2) - s
;
564 if (!strncmp(s
, "set(", 4)) {
568 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
569 retval
= parse_odp_key_attr(s
+ 4, port_names
, actions
);
573 if (s
[retval
+ 4] != ')') {
576 nl_msg_end_nested(actions
, start_ofs
);
581 struct ovs_action_push_vlan push
;
582 int tpid
= ETH_TYPE_VLAN
;
587 if ((sscanf(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
) > 0
589 || (sscanf(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
590 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)
591 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
592 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)
593 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
594 &tpid
, &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
595 push
.vlan_tpid
= htons(tpid
);
596 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
597 | (pcp
<< VLAN_PCP_SHIFT
)
598 | (cfi
? VLAN_CFI
: 0));
599 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
606 if (!strncmp(s
, "pop_vlan", 8)) {
607 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
615 if (sscanf(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
) > 0
616 && percentage
>= 0. && percentage
<= 100.0
618 size_t sample_ofs
, actions_ofs
;
621 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
622 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
623 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
624 (probability
<= 0 ? 0
625 : probability
>= UINT32_MAX
? UINT32_MAX
628 actions_ofs
= nl_msg_start_nested(actions
,
629 OVS_SAMPLE_ATTR_ACTIONS
);
633 n
+= strspn(s
+ n
, delimiters
);
638 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
644 nl_msg_end_nested(actions
, actions_ofs
);
645 nl_msg_end_nested(actions
, sample_ofs
);
647 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
654 /* Parses the string representation of datapath actions, in the format output
655 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
656 * value. On success, the ODP actions are appended to 'actions' as a series of
657 * Netlink attributes. On failure, no data is appended to 'actions'. Either
658 * way, 'actions''s data might be reallocated. */
660 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
661 struct ofpbuf
*actions
)
665 if (!strcasecmp(s
, "drop")) {
669 old_size
= actions
->size
;
673 s
+= strspn(s
, delimiters
);
678 retval
= parse_odp_action(s
, port_names
, actions
);
679 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
680 actions
->size
= old_size
;
689 /* Returns the correct length of the payload for a flow key attribute of the
690 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
691 * is variable length. */
693 odp_flow_key_attr_len(uint16_t type
)
695 if (type
> OVS_KEY_ATTR_MAX
) {
699 switch ((enum ovs_key_attr
) type
) {
700 case OVS_KEY_ATTR_ENCAP
: return -2;
701 case OVS_KEY_ATTR_PRIORITY
: return 4;
702 case OVS_KEY_ATTR_SKB_MARK
: return 4;
703 case OVS_KEY_ATTR_TUNNEL
: return -2;
704 case OVS_KEY_ATTR_IN_PORT
: return 4;
705 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
706 case OVS_KEY_ATTR_VLAN
: return sizeof(ovs_be16
);
707 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
708 case OVS_KEY_ATTR_MPLS
: return sizeof(struct ovs_key_mpls
);
709 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
710 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
711 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
712 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
713 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
714 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
715 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
716 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
718 case OVS_KEY_ATTR_UNSPEC
:
719 case __OVS_KEY_ATTR_MAX
:
727 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
729 size_t len
= nl_attr_get_size(a
);
731 const uint8_t *unspec
;
734 unspec
= nl_attr_get(a
);
735 for (i
= 0; i
< len
; i
++) {
736 ds_put_char(ds
, i
? ' ': '(');
737 ds_put_format(ds
, "%02x", unspec
[i
]);
739 ds_put_char(ds
, ')');
744 ovs_frag_type_to_string(enum ovs_frag_type type
)
747 case OVS_FRAG_TYPE_NONE
:
749 case OVS_FRAG_TYPE_FIRST
:
751 case OVS_FRAG_TYPE_LATER
:
753 case __OVS_FRAG_TYPE_MAX
:
760 tunnel_key_attr_len(int type
)
763 case OVS_TUNNEL_KEY_ATTR_ID
: return 8;
764 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
: return 4;
765 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
: return 4;
766 case OVS_TUNNEL_KEY_ATTR_TOS
: return 1;
767 case OVS_TUNNEL_KEY_ATTR_TTL
: return 1;
768 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
: return 0;
769 case OVS_TUNNEL_KEY_ATTR_CSUM
: return 0;
770 case __OVS_TUNNEL_KEY_ATTR_MAX
:
776 static enum odp_key_fitness
777 tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
780 const struct nlattr
*a
;
782 bool unknown
= false;
784 NL_NESTED_FOR_EACH(a
, left
, attr
) {
785 uint16_t type
= nl_attr_type(a
);
786 size_t len
= nl_attr_get_size(a
);
787 int expected_len
= tunnel_key_attr_len(type
);
789 if (len
!= expected_len
&& expected_len
>= 0) {
790 return ODP_FIT_ERROR
;
794 case OVS_TUNNEL_KEY_ATTR_ID
:
795 tun
->tun_id
= nl_attr_get_be64(a
);
796 tun
->flags
|= FLOW_TNL_F_KEY
;
798 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
799 tun
->ip_src
= nl_attr_get_be32(a
);
801 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
802 tun
->ip_dst
= nl_attr_get_be32(a
);
804 case OVS_TUNNEL_KEY_ATTR_TOS
:
805 tun
->ip_tos
= nl_attr_get_u8(a
);
807 case OVS_TUNNEL_KEY_ATTR_TTL
:
808 tun
->ip_ttl
= nl_attr_get_u8(a
);
811 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
812 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
814 case OVS_TUNNEL_KEY_ATTR_CSUM
:
815 tun
->flags
|= FLOW_TNL_F_CSUM
;
818 /* Allow this to show up as unexpected, if there are unknown
819 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
826 return ODP_FIT_ERROR
;
829 return ODP_FIT_TOO_MUCH
;
831 return ODP_FIT_PERFECT
;
835 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
)
839 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
841 if (tun_key
->flags
& FLOW_TNL_F_KEY
) {
842 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
844 if (tun_key
->ip_src
) {
845 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
847 if (tun_key
->ip_dst
) {
848 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
850 if (tun_key
->ip_tos
) {
851 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
853 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
854 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
855 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
857 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
858 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
861 nl_msg_end_nested(a
, tun_key_ofs
);
865 format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
)
867 const struct ovs_key_ethernet
*eth_key
;
868 const struct ovs_key_ipv4
*ipv4_key
;
869 const struct ovs_key_ipv6
*ipv6_key
;
870 const struct ovs_key_tcp
*tcp_key
;
871 const struct ovs_key_udp
*udp_key
;
872 const struct ovs_key_icmp
*icmp_key
;
873 const struct ovs_key_icmpv6
*icmpv6_key
;
874 const struct ovs_key_arp
*arp_key
;
875 const struct ovs_key_nd
*nd_key
;
876 struct flow_tnl tun_key
;
877 enum ovs_key_attr attr
= nl_attr_type(a
);
880 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
));
881 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
882 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
883 ds_put_format(ds
, "(bad length %zu, expected %d)",
885 odp_flow_key_attr_len(nl_attr_type(a
)));
886 format_generic_odp_key(a
, ds
);
891 case OVS_KEY_ATTR_ENCAP
:
892 ds_put_cstr(ds
, "(");
893 if (nl_attr_get_size(a
)) {
894 odp_flow_key_format(nl_attr_get(a
), nl_attr_get_size(a
), ds
);
896 ds_put_char(ds
, ')');
899 case OVS_KEY_ATTR_PRIORITY
:
900 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
903 case OVS_KEY_ATTR_SKB_MARK
:
904 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
907 case OVS_KEY_ATTR_TUNNEL
:
908 memset(&tun_key
, 0, sizeof tun_key
);
909 if (tun_key_from_attr(a
, &tun_key
) == ODP_FIT_ERROR
) {
910 ds_put_format(ds
, "(error)");
912 ds_put_format(ds
, "(tun_id=0x%"PRIx64
",src="IP_FMT
",dst="IP_FMT
","
913 "tos=0x%"PRIx8
",ttl=%"PRIu8
",flags(",
914 ntohll(tun_key
.tun_id
),
915 IP_ARGS(tun_key
.ip_src
),
916 IP_ARGS(tun_key
.ip_dst
),
917 tun_key
.ip_tos
, tun_key
.ip_ttl
);
919 format_flags(ds
, flow_tun_flag_to_string
,
920 (uint32_t) tun_key
.flags
, ',');
921 ds_put_format(ds
, "))");
925 case OVS_KEY_ATTR_IN_PORT
:
926 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
929 case OVS_KEY_ATTR_ETHERNET
:
930 eth_key
= nl_attr_get(a
);
931 ds_put_format(ds
, "(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
932 ETH_ADDR_ARGS(eth_key
->eth_src
),
933 ETH_ADDR_ARGS(eth_key
->eth_dst
));
936 case OVS_KEY_ATTR_VLAN
:
937 ds_put_char(ds
, '(');
938 format_vlan_tci(ds
, nl_attr_get_be16(a
));
939 ds_put_char(ds
, ')');
942 case OVS_KEY_ATTR_MPLS
: {
943 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
944 ds_put_char(ds
, '(');
945 format_mpls_lse(ds
, mpls_key
->mpls_lse
);
946 ds_put_char(ds
, ')');
950 case OVS_KEY_ATTR_ETHERTYPE
:
951 ds_put_format(ds
, "(0x%04"PRIx16
")",
952 ntohs(nl_attr_get_be16(a
)));
955 case OVS_KEY_ATTR_IPV4
:
956 ipv4_key
= nl_attr_get(a
);
957 ds_put_format(ds
, "(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
958 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s)",
959 IP_ARGS(ipv4_key
->ipv4_src
),
960 IP_ARGS(ipv4_key
->ipv4_dst
),
961 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
963 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
966 case OVS_KEY_ATTR_IPV6
: {
967 char src_str
[INET6_ADDRSTRLEN
];
968 char dst_str
[INET6_ADDRSTRLEN
];
970 ipv6_key
= nl_attr_get(a
);
971 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
972 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
974 ds_put_format(ds
, "(src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
975 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s)",
976 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
977 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
978 ipv6_key
->ipv6_hlimit
,
979 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
983 case OVS_KEY_ATTR_TCP
:
984 tcp_key
= nl_attr_get(a
);
985 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
986 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
989 case OVS_KEY_ATTR_UDP
:
990 udp_key
= nl_attr_get(a
);
991 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
992 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
995 case OVS_KEY_ATTR_ICMP
:
996 icmp_key
= nl_attr_get(a
);
997 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
998 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
1001 case OVS_KEY_ATTR_ICMPV6
:
1002 icmpv6_key
= nl_attr_get(a
);
1003 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
1004 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
1007 case OVS_KEY_ATTR_ARP
:
1008 arp_key
= nl_attr_get(a
);
1009 ds_put_format(ds
, "(sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
1010 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
")",
1011 IP_ARGS(arp_key
->arp_sip
), IP_ARGS(arp_key
->arp_tip
),
1012 ntohs(arp_key
->arp_op
), ETH_ADDR_ARGS(arp_key
->arp_sha
),
1013 ETH_ADDR_ARGS(arp_key
->arp_tha
));
1016 case OVS_KEY_ATTR_ND
: {
1017 char target
[INET6_ADDRSTRLEN
];
1019 nd_key
= nl_attr_get(a
);
1020 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
1022 ds_put_format(ds
, "(target=%s", target
);
1023 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
1024 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
1025 ETH_ADDR_ARGS(nd_key
->nd_sll
));
1027 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
1028 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
1029 ETH_ADDR_ARGS(nd_key
->nd_tll
));
1031 ds_put_char(ds
, ')');
1035 case OVS_KEY_ATTR_UNSPEC
:
1036 case __OVS_KEY_ATTR_MAX
:
1038 format_generic_odp_key(a
, ds
);
1043 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1044 * OVS_KEY_ATTR_* attributes in 'key'. */
1046 odp_flow_key_format(const struct nlattr
*key
, size_t key_len
, struct ds
*ds
)
1049 const struct nlattr
*a
;
1052 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
1054 ds_put_char(ds
, ',');
1056 format_odp_key_attr(a
, ds
);
1061 if (left
== key_len
) {
1062 ds_put_cstr(ds
, "<empty>");
1064 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1065 for (i
= 0; i
< left
; i
++) {
1066 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1068 ds_put_char(ds
, ')');
1071 ds_put_cstr(ds
, "<empty>");
1076 put_nd_key(int n
, const char *nd_target_s
,
1077 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
1079 struct ovs_key_nd nd_key
;
1081 memset(&nd_key
, 0, sizeof nd_key
);
1082 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
1086 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
1089 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
1091 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
1096 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
1098 if (!strcasecmp(s
, "no")) {
1099 *type
= OVS_FRAG_TYPE_NONE
;
1100 } else if (!strcasecmp(s
, "first")) {
1101 *type
= OVS_FRAG_TYPE_FIRST
;
1102 } else if (!strcasecmp(s
, "later")) {
1103 *type
= OVS_FRAG_TYPE_LATER
;
1111 mpls_lse_from_components(int mpls_label
, int mpls_tc
, int mpls_ttl
, int mpls_bos
)
1113 return (htonl((mpls_label
<< MPLS_LABEL_SHIFT
) |
1114 (mpls_tc
<< MPLS_TC_SHIFT
) |
1115 (mpls_ttl
<< MPLS_TTL_SHIFT
) |
1116 (mpls_bos
<< MPLS_BOS_SHIFT
)));
1120 parse_odp_key_attr(const char *s
, const struct simap
*port_names
,
1123 /* Many of the sscanf calls in this function use oversized destination
1124 * fields because some sscanf() implementations truncate the range of %i
1125 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1126 * value of 0x7fff. The other alternatives are to allow only a single
1127 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1130 * The tun_id parser has to use an alternative approach because there is no
1131 * type larger than 64 bits. */
1134 unsigned long long int priority
;
1137 if (sscanf(s
, "skb_priority(%llx)%n", &priority
, &n
) > 0 && n
> 0) {
1138 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1144 unsigned long long int mark
;
1147 if (sscanf(s
, "skb_mark(%llx)%n", &mark
, &n
) > 0 && n
> 0) {
1148 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1156 struct flow_tnl tun_key
;
1159 if (sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1160 "src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
1161 ",tos=%i,ttl=%i,flags%n", tun_id_s
,
1162 IP_SCAN_ARGS(&tun_key
.ip_src
),
1163 IP_SCAN_ARGS(&tun_key
.ip_dst
), &tos
, &ttl
,
1168 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1169 tun_key
.ip_tos
= tos
;
1170 tun_key
.ip_ttl
= ttl
;
1171 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1172 tun_key
.flags
= (uint16_t) flags
;
1182 tun_key_to_attr(key
, &tun_key
);
1188 unsigned long long int in_port
;
1191 if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
1192 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1197 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
1199 const struct simap_node
*node
;
1203 name_len
= strcspn(s
, ")");
1204 node
= simap_find_len(port_names
, name
, name_len
);
1206 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, node
->data
);
1207 return 8 + name_len
+ 1;
1212 struct ovs_key_ethernet eth_key
;
1216 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
1217 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1218 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
1219 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1220 ð_key
, sizeof eth_key
);
1231 if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n", &vid
, &pcp
, &n
) > 0
1233 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1234 htons((vid
<< VLAN_VID_SHIFT
) |
1235 (pcp
<< VLAN_PCP_SHIFT
) |
1238 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
1239 &vid
, &pcp
, &cfi
, &n
) > 0
1241 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1242 htons((vid
<< VLAN_VID_SHIFT
) |
1243 (pcp
<< VLAN_PCP_SHIFT
) |
1244 (cfi
? VLAN_CFI
: 0)));
1253 if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
1254 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1260 int label
, tc
, ttl
, bos
;
1263 if (sscanf(s
, "mpls(label=%"SCNi32
",tc=%i,ttl=%i,bos=%i)%n",
1264 &label
, &tc
, &ttl
, &bos
, &n
) > 0 &&
1266 struct ovs_key_mpls
*mpls
;
1268 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1270 mpls
->mpls_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1282 enum ovs_frag_type ipv4_frag
;
1285 if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
1286 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1287 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
1288 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
1290 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1291 struct ovs_key_ipv4 ipv4_key
;
1293 ipv4_key
.ipv4_src
= ipv4_src
;
1294 ipv4_key
.ipv4_dst
= ipv4_dst
;
1295 ipv4_key
.ipv4_proto
= ipv4_proto
;
1296 ipv4_key
.ipv4_tos
= ipv4_tos
;
1297 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1298 ipv4_key
.ipv4_frag
= ipv4_frag
;
1299 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1300 &ipv4_key
, sizeof ipv4_key
);
1306 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
1307 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
1313 enum ovs_frag_type ipv6_frag
;
1316 if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
1317 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1318 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
1319 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
1321 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1322 struct ovs_key_ipv6 ipv6_key
;
1324 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1325 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
1328 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1329 ipv6_key
.ipv6_proto
= ipv6_proto
;
1330 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1331 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1332 ipv6_key
.ipv6_frag
= ipv6_frag
;
1333 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1334 &ipv6_key
, sizeof ipv6_key
);
1344 if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
1346 struct ovs_key_tcp tcp_key
;
1348 tcp_key
.tcp_src
= htons(tcp_src
);
1349 tcp_key
.tcp_dst
= htons(tcp_dst
);
1350 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1360 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
1362 struct ovs_key_udp udp_key
;
1364 udp_key
.udp_src
= htons(udp_src
);
1365 udp_key
.udp_dst
= htons(udp_dst
);
1366 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1376 if (sscanf(s
, "icmp(type=%i,code=%i)%n",
1377 &icmp_type
, &icmp_code
, &n
) > 0
1379 struct ovs_key_icmp icmp_key
;
1381 icmp_key
.icmp_type
= icmp_type
;
1382 icmp_key
.icmp_code
= icmp_code
;
1383 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1384 &icmp_key
, sizeof icmp_key
);
1390 struct ovs_key_icmpv6 icmpv6_key
;
1393 if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
1394 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
1396 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
1397 &icmpv6_key
, sizeof icmpv6_key
);
1406 uint8_t arp_sha
[ETH_ADDR_LEN
];
1407 uint8_t arp_tha
[ETH_ADDR_LEN
];
1410 if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
1411 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
1412 IP_SCAN_ARGS(&arp_sip
),
1413 IP_SCAN_ARGS(&arp_tip
),
1415 ETH_ADDR_SCAN_ARGS(arp_sha
),
1416 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
1417 struct ovs_key_arp arp_key
;
1419 memset(&arp_key
, 0, sizeof arp_key
);
1420 arp_key
.arp_sip
= arp_sip
;
1421 arp_key
.arp_tip
= arp_tip
;
1422 arp_key
.arp_op
= htons(arp_op
);
1423 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
1424 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
1425 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
1431 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
1432 uint8_t nd_sll
[ETH_ADDR_LEN
];
1433 uint8_t nd_tll
[ETH_ADDR_LEN
];
1436 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
1437 nd_target_s
, &n
) > 0 && n
> 0) {
1438 return put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
1440 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
1441 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
1443 return put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
1445 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
1446 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1448 return put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
1450 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
1451 "tll="ETH_ADDR_SCAN_FMT
")%n",
1452 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
1453 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1455 return put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
1459 if (!strncmp(s
, "encap(", 6)) {
1460 const char *start
= s
;
1463 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
1469 s
+= strspn(s
, ", \t\r\n");
1472 } else if (*s
== ')') {
1476 retval
= parse_odp_key_attr(s
, port_names
, key
);
1484 nl_msg_end_nested(key
, encap
);
1492 /* Parses the string representation of a datapath flow key, in the
1493 * format output by odp_flow_key_format(). Returns 0 if successful,
1494 * otherwise a positive errno value. On success, the flow key is
1495 * appended to 'key' as a series of Netlink attributes. On failure, no
1496 * data is appended to 'key'. Either way, 'key''s data might be
1499 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1500 * to a port number. (Port names may be used instead of port numbers in
1503 * On success, the attributes appended to 'key' are individually syntactically
1504 * valid, but they may not be valid as a sequence. 'key' might, for example,
1505 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1507 odp_flow_key_from_string(const char *s
, const struct simap
*port_names
,
1510 const size_t old_size
= key
->size
;
1514 s
+= strspn(s
, delimiters
);
1519 retval
= parse_odp_key_attr(s
, port_names
, key
);
1521 key
->size
= old_size
;
1531 ovs_to_odp_frag(uint8_t nw_frag
)
1533 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
1534 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
1535 : OVS_FRAG_TYPE_LATER
);
1538 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1539 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1540 * number rather than a datapath port number). Instead, if 'odp_in_port'
1541 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1544 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1545 * capable of being expanded to allow for that much space. */
1547 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
,
1548 uint32_t odp_in_port
)
1550 struct ovs_key_ethernet
*eth_key
;
1553 if (flow
->skb_priority
) {
1554 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, flow
->skb_priority
);
1557 if (flow
->tunnel
.ip_dst
) {
1558 tun_key_to_attr(buf
, &flow
->tunnel
);
1561 if (flow
->skb_mark
) {
1562 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, flow
->skb_mark
);
1565 if (odp_in_port
!= OVSP_NONE
) {
1566 nl_msg_put_u32(buf
, OVS_KEY_ATTR_IN_PORT
, odp_in_port
);
1569 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
1571 memcpy(eth_key
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1572 memcpy(eth_key
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1574 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1575 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
1576 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, flow
->vlan_tci
);
1577 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
1578 if (flow
->vlan_tci
== htons(0)) {
1585 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
1589 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, flow
->dl_type
);
1591 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1592 struct ovs_key_ipv4
*ipv4_key
;
1594 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
1596 ipv4_key
->ipv4_src
= flow
->nw_src
;
1597 ipv4_key
->ipv4_dst
= flow
->nw_dst
;
1598 ipv4_key
->ipv4_proto
= flow
->nw_proto
;
1599 ipv4_key
->ipv4_tos
= flow
->nw_tos
;
1600 ipv4_key
->ipv4_ttl
= flow
->nw_ttl
;
1601 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1602 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1603 struct ovs_key_ipv6
*ipv6_key
;
1605 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
1607 memcpy(ipv6_key
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
1608 memcpy(ipv6_key
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
1609 ipv6_key
->ipv6_label
= flow
->ipv6_label
;
1610 ipv6_key
->ipv6_proto
= flow
->nw_proto
;
1611 ipv6_key
->ipv6_tclass
= flow
->nw_tos
;
1612 ipv6_key
->ipv6_hlimit
= flow
->nw_ttl
;
1613 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1614 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
1615 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
1616 struct ovs_key_arp
*arp_key
;
1618 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
1620 memset(arp_key
, 0, sizeof *arp_key
);
1621 arp_key
->arp_sip
= flow
->nw_src
;
1622 arp_key
->arp_tip
= flow
->nw_dst
;
1623 arp_key
->arp_op
= htons(flow
->nw_proto
);
1624 memcpy(arp_key
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
1625 memcpy(arp_key
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
1628 if (flow
->mpls_depth
) {
1629 struct ovs_key_mpls
*mpls_key
;
1631 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
1633 mpls_key
->mpls_lse
= flow
->mpls_lse
;
1636 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1637 if (flow
->nw_proto
== IPPROTO_TCP
) {
1638 struct ovs_key_tcp
*tcp_key
;
1640 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
1642 tcp_key
->tcp_src
= flow
->tp_src
;
1643 tcp_key
->tcp_dst
= flow
->tp_dst
;
1644 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
1645 struct ovs_key_udp
*udp_key
;
1647 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
1649 udp_key
->udp_src
= flow
->tp_src
;
1650 udp_key
->udp_dst
= flow
->tp_dst
;
1651 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
1652 && flow
->nw_proto
== IPPROTO_ICMP
) {
1653 struct ovs_key_icmp
*icmp_key
;
1655 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
1657 icmp_key
->icmp_type
= ntohs(flow
->tp_src
);
1658 icmp_key
->icmp_code
= ntohs(flow
->tp_dst
);
1659 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1660 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
1661 struct ovs_key_icmpv6
*icmpv6_key
;
1663 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
1664 sizeof *icmpv6_key
);
1665 icmpv6_key
->icmpv6_type
= ntohs(flow
->tp_src
);
1666 icmpv6_key
->icmpv6_code
= ntohs(flow
->tp_dst
);
1668 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
1669 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
1670 struct ovs_key_nd
*nd_key
;
1672 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
1674 memcpy(nd_key
->nd_target
, &flow
->nd_target
,
1675 sizeof nd_key
->nd_target
);
1676 memcpy(nd_key
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
1677 memcpy(nd_key
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
1684 nl_msg_end_nested(buf
, encap
);
1689 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
1691 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
1692 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
1696 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
1697 uint64_t attrs
, int out_of_range_attr
,
1698 const struct nlattr
*key
, size_t key_len
)
1703 if (VLOG_DROP_DBG(rl
)) {
1708 for (i
= 0; i
< 64; i
++) {
1709 if (attrs
& (UINT64_C(1) << i
)) {
1710 ds_put_format(&s
, " %s", ovs_key_attr_to_string(i
));
1713 if (out_of_range_attr
) {
1714 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
1717 ds_put_cstr(&s
, ": ");
1718 odp_flow_key_format(key
, key_len
, &s
);
1720 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
1725 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
1727 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1729 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
1730 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
1734 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
1735 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
1736 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
1737 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
1744 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
1745 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
1746 int *out_of_range_attrp
)
1748 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1749 const struct nlattr
*nla
;
1750 uint64_t present_attrs
;
1753 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
1755 *out_of_range_attrp
= 0;
1756 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
1757 uint16_t type
= nl_attr_type(nla
);
1758 size_t len
= nl_attr_get_size(nla
);
1759 int expected_len
= odp_flow_key_attr_len(type
);
1761 if (len
!= expected_len
&& expected_len
>= 0) {
1762 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
1763 "length %d", ovs_key_attr_to_string(type
),
1768 if (type
> OVS_KEY_ATTR_MAX
) {
1769 *out_of_range_attrp
= type
;
1771 if (present_attrs
& (UINT64_C(1) << type
)) {
1772 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
1773 ovs_key_attr_to_string(type
));
1777 present_attrs
|= UINT64_C(1) << type
;
1782 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
1786 *present_attrsp
= present_attrs
;
1790 static enum odp_key_fitness
1791 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
1792 uint64_t expected_attrs
,
1793 const struct nlattr
*key
, size_t key_len
)
1795 uint64_t missing_attrs
;
1796 uint64_t extra_attrs
;
1798 missing_attrs
= expected_attrs
& ~present_attrs
;
1799 if (missing_attrs
) {
1800 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1801 log_odp_key_attributes(&rl
, "expected but not present",
1802 missing_attrs
, 0, key
, key_len
);
1803 return ODP_FIT_TOO_LITTLE
;
1806 extra_attrs
= present_attrs
& ~expected_attrs
;
1807 if (extra_attrs
|| out_of_range_attr
) {
1808 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1809 log_odp_key_attributes(&rl
, "present but not expected",
1810 extra_attrs
, out_of_range_attr
, key
, key_len
);
1811 return ODP_FIT_TOO_MUCH
;
1814 return ODP_FIT_PERFECT
;
1818 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1819 uint64_t present_attrs
, uint64_t *expected_attrs
,
1822 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1824 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
1825 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
1826 if (ntohs(flow
->dl_type
) < 1536) {
1827 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
1828 ntohs(flow
->dl_type
));
1831 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
1833 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
1838 static enum odp_key_fitness
1839 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1840 uint64_t present_attrs
, int out_of_range_attr
,
1841 uint64_t expected_attrs
, struct flow
*flow
,
1842 const struct nlattr
*key
, size_t key_len
)
1844 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1846 if (eth_type_mpls(flow
->dl_type
)) {
1847 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
1849 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
))) {
1850 return ODP_FIT_TOO_LITTLE
;
1852 flow
->mpls_lse
= nl_attr_get_be32(attrs
[OVS_KEY_ATTR_MPLS
]);
1854 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1855 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
1856 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1857 const struct ovs_key_ipv4
*ipv4_key
;
1859 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
1860 flow
->nw_src
= ipv4_key
->ipv4_src
;
1861 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
1862 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
1863 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
1864 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
1865 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
1866 return ODP_FIT_ERROR
;
1869 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1870 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
1871 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1872 const struct ovs_key_ipv6
*ipv6_key
;
1874 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
1875 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
1876 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
1877 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
1878 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
1879 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
1880 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
1881 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
1882 return ODP_FIT_ERROR
;
1885 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
1886 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
1887 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
1888 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1889 const struct ovs_key_arp
*arp_key
;
1891 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
1892 flow
->nw_src
= arp_key
->arp_sip
;
1893 flow
->nw_dst
= arp_key
->arp_tip
;
1894 if (arp_key
->arp_op
& htons(0xff00)) {
1895 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
1896 "key", ntohs(arp_key
->arp_op
));
1897 return ODP_FIT_ERROR
;
1899 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
1900 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
1901 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
1905 if (flow
->nw_proto
== IPPROTO_TCP
1906 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1907 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1908 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1909 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
1910 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
1911 const struct ovs_key_tcp
*tcp_key
;
1913 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
1914 flow
->tp_src
= tcp_key
->tcp_src
;
1915 flow
->tp_dst
= tcp_key
->tcp_dst
;
1917 } else if (flow
->nw_proto
== IPPROTO_UDP
1918 && (flow
->dl_type
== htons(ETH_TYPE_IP
) ||
1919 flow
->dl_type
== htons(ETH_TYPE_IPV6
))
1920 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1921 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
1922 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
1923 const struct ovs_key_udp
*udp_key
;
1925 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
1926 flow
->tp_src
= udp_key
->udp_src
;
1927 flow
->tp_dst
= udp_key
->udp_dst
;
1929 } else if (flow
->nw_proto
== IPPROTO_ICMP
1930 && flow
->dl_type
== htons(ETH_TYPE_IP
)
1931 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1932 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
1933 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
1934 const struct ovs_key_icmp
*icmp_key
;
1936 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
1937 flow
->tp_src
= htons(icmp_key
->icmp_type
);
1938 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
1940 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
1941 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1942 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1943 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
1944 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
1945 const struct ovs_key_icmpv6
*icmpv6_key
;
1947 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
1948 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
1949 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
1951 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
1952 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
1953 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
1954 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
1955 const struct ovs_key_nd
*nd_key
;
1957 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
1958 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
1959 sizeof flow
->nd_target
);
1960 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
1961 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
1967 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
1971 /* Parse 802.1Q header then encapsulated L3 attributes. */
1972 static enum odp_key_fitness
1973 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1974 uint64_t present_attrs
, int out_of_range_attr
,
1975 uint64_t expected_attrs
, struct flow
*flow
,
1976 const struct nlattr
*key
, size_t key_len
)
1978 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1980 const struct nlattr
*encap
1981 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
1982 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
1983 enum odp_key_fitness encap_fitness
;
1984 enum odp_key_fitness fitness
;
1987 /* Calulate fitness of outer attributes. */
1988 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
1989 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
1990 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
1991 expected_attrs
, key
, key_len
);
1993 /* Get the VLAN TCI value. */
1994 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
1995 return ODP_FIT_TOO_LITTLE
;
1997 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
1998 if (tci
== htons(0)) {
1999 /* Corner case for a truncated 802.1Q header. */
2000 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
2001 return ODP_FIT_TOO_MUCH
;
2004 } else if (!(tci
& htons(VLAN_CFI
))) {
2005 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
2006 "but CFI bit is not set", ntohs(tci
));
2007 return ODP_FIT_ERROR
;
2011 * Remove the TPID from dl_type since it's not the real Ethertype. */
2012 flow
->vlan_tci
= tci
;
2013 flow
->dl_type
= htons(0);
2015 /* Now parse the encapsulated attributes. */
2016 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
2017 attrs
, &present_attrs
, &out_of_range_attr
)) {
2018 return ODP_FIT_ERROR
;
2022 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2023 return ODP_FIT_ERROR
;
2025 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2026 expected_attrs
, flow
, key
, key_len
);
2028 /* The overall fitness is the worse of the outer and inner attributes. */
2029 return MAX(fitness
, encap_fitness
);
2032 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2033 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2034 * 'key' fits our expectations for what a flow key should contain.
2036 * The 'in_port' will be the datapath's understanding of the port. The
2037 * caller will need to translate with odp_port_to_ofp_port() if the
2038 * OpenFlow port is needed.
2040 * This function doesn't take the packet itself as an argument because none of
2041 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2042 * it is always possible to infer which additional attribute(s) should appear
2043 * by looking at the attributes for lower-level protocols, e.g. if the network
2044 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2045 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2046 * must be absent. */
2047 enum odp_key_fitness
2048 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
2051 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
2052 uint64_t expected_attrs
;
2053 uint64_t present_attrs
;
2054 int out_of_range_attr
;
2056 memset(flow
, 0, sizeof *flow
);
2058 /* Parse attributes. */
2059 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
2060 &out_of_range_attr
)) {
2061 return ODP_FIT_ERROR
;
2066 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
2067 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
2068 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
2071 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
2072 flow
->skb_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
2073 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
2076 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
2077 enum odp_key_fitness res
;
2079 res
= tun_key_from_attr(attrs
[OVS_KEY_ATTR_TUNNEL
], &flow
->tunnel
);
2080 if (res
== ODP_FIT_ERROR
) {
2081 return ODP_FIT_ERROR
;
2082 } else if (res
== ODP_FIT_PERFECT
) {
2083 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
2087 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
2088 flow
->in_port
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_IN_PORT
]);
2089 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
2091 flow
->in_port
= OVSP_NONE
;
2094 /* Ethernet header. */
2095 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
2096 const struct ovs_key_ethernet
*eth_key
;
2098 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
2099 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
2100 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
2102 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
2104 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2105 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2106 return ODP_FIT_ERROR
;
2109 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2110 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
2111 expected_attrs
, flow
, key
, key_len
);
2113 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2114 expected_attrs
, flow
, key
, key_len
);
2117 /* Returns 'fitness' as a string, for use in debug messages. */
2119 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
2122 case ODP_FIT_PERFECT
:
2124 case ODP_FIT_TOO_MUCH
:
2126 case ODP_FIT_TOO_LITTLE
:
2127 return "too_little";
2135 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2136 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2137 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2138 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2139 * null, then the return value is not meaningful.) */
2141 odp_put_userspace_action(uint32_t pid
,
2142 const void *userdata
, size_t userdata_size
,
2143 struct ofpbuf
*odp_actions
)
2145 size_t userdata_ofs
;
2148 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
2149 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
2151 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
2152 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
2153 userdata
, userdata_size
);
2157 nl_msg_end_nested(odp_actions
, offset
);
2159 return userdata_ofs
;
2163 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
2164 struct ofpbuf
*odp_actions
)
2166 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2167 tun_key_to_attr(odp_actions
, tunnel
);
2168 nl_msg_end_nested(odp_actions
, offset
);
2171 /* The commit_odp_actions() function and its helpers. */
2174 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
2175 const void *key
, size_t key_size
)
2177 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2178 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
2179 nl_msg_end_nested(odp_actions
, offset
);
2183 odp_put_skb_mark_action(const uint32_t skb_mark
,
2184 struct ofpbuf
*odp_actions
)
2186 commit_set_action(odp_actions
, OVS_KEY_ATTR_SKB_MARK
, &skb_mark
,
2190 /* If any of the flow key data that ODP actions can modify are different in
2191 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2192 * 'odp_actions' that change the flow tunneling information in key from
2193 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2194 * same way. In other words, operates the same as commit_odp_actions(), but
2195 * only on tunneling information. */
2197 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
2198 struct ofpbuf
*odp_actions
)
2200 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2201 if (flow
->tunnel
.ip_dst
) {
2202 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
2205 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
2206 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
2211 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
2212 struct ofpbuf
*odp_actions
)
2214 struct ovs_key_ethernet eth_key
;
2216 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
2217 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
2221 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
2222 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
2224 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
2225 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
2227 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
2228 ð_key
, sizeof(eth_key
));
2232 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
2233 struct ofpbuf
*odp_actions
)
2235 if (base
->vlan_tci
== flow
->vlan_tci
) {
2239 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
2240 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2243 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
2244 struct ovs_action_push_vlan vlan
;
2246 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
2247 vlan
.vlan_tci
= flow
->vlan_tci
;
2248 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2249 &vlan
, sizeof vlan
);
2251 base
->vlan_tci
= flow
->vlan_tci
;
2255 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
2256 struct ofpbuf
*odp_actions
)
2258 if (flow
->mpls_lse
== base
->mpls_lse
&&
2259 flow
->mpls_depth
== base
->mpls_depth
) {
2263 if (flow
->mpls_depth
< base
->mpls_depth
) {
2264 if (base
->mpls_depth
- flow
->mpls_depth
> 1) {
2265 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2266 VLOG_WARN_RL(&rl
, "Multiple mpls_pop actions reduced to "
2267 " a single mpls_pop action");
2270 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, flow
->dl_type
);
2271 } else if (flow
->mpls_depth
> base
->mpls_depth
) {
2272 struct ovs_action_push_mpls
*mpls
;
2274 if (flow
->mpls_depth
- base
->mpls_depth
> 1) {
2275 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2276 VLOG_WARN_RL(&rl
, "Multiple mpls_push actions reduced to "
2277 " a single mpls_push action");
2280 mpls
= nl_msg_put_unspec_uninit(odp_actions
, OVS_ACTION_ATTR_PUSH_MPLS
,
2282 memset(mpls
, 0, sizeof *mpls
);
2283 mpls
->mpls_ethertype
= flow
->dl_type
;
2284 mpls
->mpls_lse
= flow
->mpls_lse
;
2286 struct ovs_key_mpls mpls_key
;
2288 mpls_key
.mpls_lse
= flow
->mpls_lse
;
2289 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
2290 &mpls_key
, sizeof(mpls_key
));
2293 base
->dl_type
= flow
->dl_type
;
2294 base
->mpls_lse
= flow
->mpls_lse
;
2295 base
->mpls_depth
= flow
->mpls_depth
;
2299 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
2300 struct ofpbuf
*odp_actions
)
2302 struct ovs_key_ipv4 ipv4_key
;
2304 if (base
->nw_src
== flow
->nw_src
&&
2305 base
->nw_dst
== flow
->nw_dst
&&
2306 base
->nw_tos
== flow
->nw_tos
&&
2307 base
->nw_ttl
== flow
->nw_ttl
&&
2308 base
->nw_frag
== flow
->nw_frag
) {
2312 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
2313 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
2314 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
2315 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
2316 ipv4_key
.ipv4_proto
= base
->nw_proto
;
2317 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
2319 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
2320 &ipv4_key
, sizeof(ipv4_key
));
2324 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
2325 struct ofpbuf
*odp_actions
)
2327 struct ovs_key_ipv6 ipv6_key
;
2329 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
2330 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
2331 base
->ipv6_label
== flow
->ipv6_label
&&
2332 base
->nw_tos
== flow
->nw_tos
&&
2333 base
->nw_ttl
== flow
->nw_ttl
&&
2334 base
->nw_frag
== flow
->nw_frag
) {
2338 base
->ipv6_src
= flow
->ipv6_src
;
2339 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
2340 base
->ipv6_dst
= flow
->ipv6_dst
;
2341 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
2343 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
2344 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
2345 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
2346 ipv6_key
.ipv6_proto
= base
->nw_proto
;
2347 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
2349 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
2350 &ipv6_key
, sizeof(ipv6_key
));
2354 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
2355 struct ofpbuf
*odp_actions
)
2357 /* Check if flow really have an IP header. */
2358 if (!flow
->nw_proto
) {
2362 if (base
->dl_type
== htons(ETH_TYPE_IP
)) {
2363 commit_set_ipv4_action(flow
, base
, odp_actions
);
2364 } else if (base
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2365 commit_set_ipv6_action(flow
, base
, odp_actions
);
2370 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
2371 struct ofpbuf
*odp_actions
)
2373 if (!is_ip_any(base
) || (!base
->tp_src
&& !base
->tp_dst
)) {
2377 if (base
->tp_src
== flow
->tp_src
&&
2378 base
->tp_dst
== flow
->tp_dst
) {
2382 if (flow
->nw_proto
== IPPROTO_TCP
) {
2383 struct ovs_key_tcp port_key
;
2385 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
2386 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
2388 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
2389 &port_key
, sizeof(port_key
));
2391 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
2392 struct ovs_key_udp port_key
;
2394 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
2395 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
2397 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
2398 &port_key
, sizeof(port_key
));
2403 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
2404 struct ofpbuf
*odp_actions
)
2406 if (base
->skb_priority
== flow
->skb_priority
) {
2409 base
->skb_priority
= flow
->skb_priority
;
2411 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
2412 &base
->skb_priority
, sizeof(base
->skb_priority
));
2416 commit_set_skb_mark_action(const struct flow
*flow
, struct flow
*base
,
2417 struct ofpbuf
*odp_actions
)
2419 if (base
->skb_mark
== flow
->skb_mark
) {
2422 base
->skb_mark
= flow
->skb_mark
;
2424 odp_put_skb_mark_action(base
->skb_mark
, odp_actions
);
2426 /* If any of the flow key data that ODP actions can modify are different in
2427 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2428 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
2429 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
2430 * in addition to this function if needed. */
2432 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
2433 struct ofpbuf
*odp_actions
)
2435 commit_set_ether_addr_action(flow
, base
, odp_actions
);
2436 commit_vlan_action(flow
, base
, odp_actions
);
2437 commit_set_nw_action(flow
, base
, odp_actions
);
2438 commit_set_port_action(flow
, base
, odp_actions
);
2439 /* Commiting MPLS actions should occur after committing nw and port
2440 * actions. This is because committing MPLS actions may alter a packet so
2441 * that it is no longer IP and thus nw and port actions are no longer valid.
2443 commit_mpls_action(flow
, base
, odp_actions
);
2444 commit_set_priority_action(flow
, base
, odp_actions
);
2445 commit_set_skb_mark_action(flow
, base
, odp_actions
);