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
];
265 if (userdata_attr
&& nl_attr_get_size(userdata_attr
) == sizeof(uint64_t)) {
266 uint64_t userdata
= nl_attr_get_u64(a
[OVS_USERSPACE_ATTR_USERDATA
]);
267 union user_action_cookie cookie
;
269 memcpy(&cookie
, &userdata
, sizeof cookie
);
271 switch (cookie
.type
) {
272 case USER_ACTION_COOKIE_SFLOW
:
273 ds_put_format(ds
, ",sFlow("
274 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
275 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
276 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
277 cookie
.sflow
.output
);
280 case USER_ACTION_COOKIE_SLOW_PATH
:
281 ds_put_cstr(ds
, ",slow_path(");
282 format_flags(ds
, slow_path_reason_to_string
,
283 cookie
.slow_path
.reason
, ',');
284 ds_put_format(ds
, ")");
287 case USER_ACTION_COOKIE_UNSPEC
:
289 ds_put_format(ds
, ",userdata=0x%"PRIx64
, userdata
);
292 } else if (userdata_attr
) {
293 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
294 size_t len
= nl_attr_get_size(userdata_attr
);
297 ds_put_format(ds
, ",userdata(");
298 for (i
= 0; i
< len
; i
++) {
299 ds_put_format(ds
, "%02x", userdata
[i
]);
301 ds_put_char(ds
, ')');
304 ds_put_char(ds
, ')');
308 format_vlan_tci(struct ds
*ds
, ovs_be16 vlan_tci
)
310 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d",
311 vlan_tci_to_vid(vlan_tci
),
312 vlan_tci_to_pcp(vlan_tci
));
313 if (!(vlan_tci
& htons(VLAN_CFI
))) {
314 ds_put_cstr(ds
, ",cfi=0");
319 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
321 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
322 mpls_lse_to_label(mpls_lse
),
323 mpls_lse_to_tc(mpls_lse
),
324 mpls_lse_to_ttl(mpls_lse
),
325 mpls_lse_to_bos(mpls_lse
));
329 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
332 enum ovs_action_attr type
= nl_attr_type(a
);
333 const struct ovs_action_push_vlan
*vlan
;
335 expected_len
= odp_action_len(nl_attr_type(a
));
336 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
337 ds_put_format(ds
, "bad length %zu, expected %d for: ",
338 nl_attr_get_size(a
), expected_len
);
339 format_generic_odp_action(ds
, a
);
344 case OVS_ACTION_ATTR_OUTPUT
:
345 ds_put_format(ds
, "%"PRIu16
, nl_attr_get_u32(a
));
347 case OVS_ACTION_ATTR_USERSPACE
:
348 format_odp_userspace_action(ds
, a
);
350 case OVS_ACTION_ATTR_SET
:
351 ds_put_cstr(ds
, "set(");
352 format_odp_key_attr(nl_attr_get(a
), ds
);
353 ds_put_cstr(ds
, ")");
355 case OVS_ACTION_ATTR_PUSH_VLAN
:
356 vlan
= nl_attr_get(a
);
357 ds_put_cstr(ds
, "push_vlan(");
358 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
359 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
361 format_vlan_tci(ds
, vlan
->vlan_tci
);
362 ds_put_char(ds
, ')');
364 case OVS_ACTION_ATTR_POP_VLAN
:
365 ds_put_cstr(ds
, "pop_vlan");
367 case OVS_ACTION_ATTR_PUSH_MPLS
: {
368 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
369 ds_put_cstr(ds
, "push_mpls(");
370 format_mpls_lse(ds
, mpls
->mpls_lse
);
371 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
374 case OVS_ACTION_ATTR_POP_MPLS
: {
375 ovs_be16 ethertype
= nl_attr_get_be16(a
);
376 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
379 case OVS_ACTION_ATTR_SAMPLE
:
380 format_odp_sample_action(ds
, a
);
382 case OVS_ACTION_ATTR_UNSPEC
:
383 case __OVS_ACTION_ATTR_MAX
:
385 format_generic_odp_action(ds
, a
);
391 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
395 const struct nlattr
*a
;
398 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
400 ds_put_char(ds
, ',');
402 format_odp_action(ds
, a
);
407 if (left
== actions_len
) {
408 ds_put_cstr(ds
, "<empty>");
410 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
411 for (i
= 0; i
< left
; i
++) {
412 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
414 ds_put_char(ds
, ')');
417 ds_put_cstr(ds
, "drop");
422 parse_odp_action(const char *s
, const struct simap
*port_names
,
423 struct ofpbuf
*actions
)
425 /* Many of the sscanf calls in this function use oversized destination
426 * fields because some sscanf() implementations truncate the range of %i
427 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
428 * value of 0x7fff. The other alternatives are to allow only a single
429 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
432 * The tun_id parser has to use an alternative approach because there is no
433 * type larger than 64 bits. */
436 unsigned long long int port
;
439 if (sscanf(s
, "%lli%n", &port
, &n
) > 0 && n
> 0) {
440 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
446 int len
= strcspn(s
, delimiters
);
447 struct simap_node
*node
;
449 node
= simap_find_len(port_names
, s
, len
);
451 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
457 unsigned long long int pid
;
458 unsigned long long int output
;
463 if (sscanf(s
, "userspace(pid=%lli)%n", &pid
, &n
) > 0 && n
> 0) {
464 odp_put_userspace_action(pid
, NULL
, 0, actions
);
466 } else if (sscanf(s
, "userspace(pid=%lli,sFlow(vid=%i,"
467 "pcp=%i,output=%lli))%n",
468 &pid
, &vid
, &pcp
, &output
, &n
) > 0 && n
> 0) {
469 union user_action_cookie cookie
;
472 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
477 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
478 cookie
.sflow
.vlan_tci
= htons(tci
);
479 cookie
.sflow
.output
= output
;
480 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
, actions
);
482 } else if (sscanf(s
, "userspace(pid=%lli,slow_path%n", &pid
, &n
) > 0
484 union user_action_cookie cookie
;
487 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
488 cookie
.slow_path
.unused
= 0;
489 cookie
.slow_path
.reason
= 0;
491 res
= parse_flags(&s
[n
], slow_path_reason_to_string
,
492 &cookie
.slow_path
.reason
);
502 odp_put_userspace_action(pid
, &cookie
, sizeof cookie
, actions
);
504 } else if (sscanf(s
, "userspace(pid=%lli,userdata="
505 "%31[x0123456789abcdefABCDEF])%n", &pid
, userdata_s
,
509 userdata
= strtoull(userdata_s
, NULL
, 0);
510 odp_put_userspace_action(pid
, &userdata
, sizeof(userdata
),
513 } else if (sscanf(s
, "userspace(pid=%lli,userdata(%n", &pid
, &n
) > 0
518 ofpbuf_init(&buf
, 16);
519 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
520 if (end
[0] == ')' && end
[1] == ')') {
521 odp_put_userspace_action(pid
, buf
.data
, buf
.size
, actions
);
523 return (end
+ 2) - s
;
528 if (!strncmp(s
, "set(", 4)) {
532 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
533 retval
= parse_odp_key_attr(s
+ 4, port_names
, actions
);
537 if (s
[retval
+ 4] != ')') {
540 nl_msg_end_nested(actions
, start_ofs
);
545 struct ovs_action_push_vlan push
;
546 int tpid
= ETH_TYPE_VLAN
;
551 if ((sscanf(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
) > 0
553 || (sscanf(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
554 &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)
555 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
556 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)
557 || (sscanf(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
558 &tpid
, &vid
, &pcp
, &cfi
, &n
) > 0 && n
> 0)) {
559 push
.vlan_tpid
= htons(tpid
);
560 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
561 | (pcp
<< VLAN_PCP_SHIFT
)
562 | (cfi
? VLAN_CFI
: 0));
563 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
570 if (!strncmp(s
, "pop_vlan", 8)) {
571 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
579 if (sscanf(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
) > 0
580 && percentage
>= 0. && percentage
<= 100.0
582 size_t sample_ofs
, actions_ofs
;
585 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
586 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
587 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
588 (probability
<= 0 ? 0
589 : probability
>= UINT32_MAX
? UINT32_MAX
592 actions_ofs
= nl_msg_start_nested(actions
,
593 OVS_SAMPLE_ATTR_ACTIONS
);
597 n
+= strspn(s
+ n
, delimiters
);
602 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
608 nl_msg_end_nested(actions
, actions_ofs
);
609 nl_msg_end_nested(actions
, sample_ofs
);
611 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
618 /* Parses the string representation of datapath actions, in the format output
619 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
620 * value. On success, the ODP actions are appended to 'actions' as a series of
621 * Netlink attributes. On failure, no data is appended to 'actions'. Either
622 * way, 'actions''s data might be reallocated. */
624 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
625 struct ofpbuf
*actions
)
629 if (!strcasecmp(s
, "drop")) {
633 old_size
= actions
->size
;
637 s
+= strspn(s
, delimiters
);
642 retval
= parse_odp_action(s
, port_names
, actions
);
643 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
644 actions
->size
= old_size
;
653 /* Returns the correct length of the payload for a flow key attribute of the
654 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
655 * is variable length. */
657 odp_flow_key_attr_len(uint16_t type
)
659 if (type
> OVS_KEY_ATTR_MAX
) {
663 switch ((enum ovs_key_attr
) type
) {
664 case OVS_KEY_ATTR_ENCAP
: return -2;
665 case OVS_KEY_ATTR_PRIORITY
: return 4;
666 case OVS_KEY_ATTR_SKB_MARK
: return 4;
667 case OVS_KEY_ATTR_TUNNEL
: return -2;
668 case OVS_KEY_ATTR_IN_PORT
: return 4;
669 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
670 case OVS_KEY_ATTR_VLAN
: return sizeof(ovs_be16
);
671 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
672 case OVS_KEY_ATTR_MPLS
: return sizeof(struct ovs_key_mpls
);
673 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
674 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
675 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
676 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
677 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
678 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
679 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
680 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
682 case OVS_KEY_ATTR_UNSPEC
:
683 case __OVS_KEY_ATTR_MAX
:
691 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
693 size_t len
= nl_attr_get_size(a
);
695 const uint8_t *unspec
;
698 unspec
= nl_attr_get(a
);
699 for (i
= 0; i
< len
; i
++) {
700 ds_put_char(ds
, i
? ' ': '(');
701 ds_put_format(ds
, "%02x", unspec
[i
]);
703 ds_put_char(ds
, ')');
708 ovs_frag_type_to_string(enum ovs_frag_type type
)
711 case OVS_FRAG_TYPE_NONE
:
713 case OVS_FRAG_TYPE_FIRST
:
715 case OVS_FRAG_TYPE_LATER
:
717 case __OVS_FRAG_TYPE_MAX
:
724 tunnel_key_attr_len(int type
)
727 case OVS_TUNNEL_KEY_ATTR_ID
: return 8;
728 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
: return 4;
729 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
: return 4;
730 case OVS_TUNNEL_KEY_ATTR_TOS
: return 1;
731 case OVS_TUNNEL_KEY_ATTR_TTL
: return 1;
732 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
: return 0;
733 case OVS_TUNNEL_KEY_ATTR_CSUM
: return 0;
734 case __OVS_TUNNEL_KEY_ATTR_MAX
:
740 static enum odp_key_fitness
741 tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
744 const struct nlattr
*a
;
746 bool unknown
= false;
748 NL_NESTED_FOR_EACH(a
, left
, attr
) {
749 uint16_t type
= nl_attr_type(a
);
750 size_t len
= nl_attr_get_size(a
);
751 int expected_len
= tunnel_key_attr_len(type
);
753 if (len
!= expected_len
&& expected_len
>= 0) {
754 return ODP_FIT_ERROR
;
758 case OVS_TUNNEL_KEY_ATTR_ID
:
759 tun
->tun_id
= nl_attr_get_be64(a
);
760 tun
->flags
|= FLOW_TNL_F_KEY
;
762 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
763 tun
->ip_src
= nl_attr_get_be32(a
);
765 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
766 tun
->ip_dst
= nl_attr_get_be32(a
);
768 case OVS_TUNNEL_KEY_ATTR_TOS
:
769 tun
->ip_tos
= nl_attr_get_u8(a
);
771 case OVS_TUNNEL_KEY_ATTR_TTL
:
772 tun
->ip_ttl
= nl_attr_get_u8(a
);
775 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
776 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
778 case OVS_TUNNEL_KEY_ATTR_CSUM
:
779 tun
->flags
|= FLOW_TNL_F_CSUM
;
782 /* Allow this to show up as unexpected, if there are unknown
783 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
790 return ODP_FIT_ERROR
;
793 return ODP_FIT_TOO_MUCH
;
795 return ODP_FIT_PERFECT
;
799 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
)
803 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
805 if (tun_key
->flags
& FLOW_TNL_F_KEY
) {
806 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
808 if (tun_key
->ip_src
) {
809 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
811 if (tun_key
->ip_dst
) {
812 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
814 if (tun_key
->ip_tos
) {
815 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
817 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
818 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
819 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
821 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
822 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
825 nl_msg_end_nested(a
, tun_key_ofs
);
829 format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
)
831 const struct ovs_key_ethernet
*eth_key
;
832 const struct ovs_key_ipv4
*ipv4_key
;
833 const struct ovs_key_ipv6
*ipv6_key
;
834 const struct ovs_key_tcp
*tcp_key
;
835 const struct ovs_key_udp
*udp_key
;
836 const struct ovs_key_icmp
*icmp_key
;
837 const struct ovs_key_icmpv6
*icmpv6_key
;
838 const struct ovs_key_arp
*arp_key
;
839 const struct ovs_key_nd
*nd_key
;
840 struct flow_tnl tun_key
;
841 enum ovs_key_attr attr
= nl_attr_type(a
);
844 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
));
845 expected_len
= odp_flow_key_attr_len(nl_attr_type(a
));
846 if (expected_len
!= -2 && nl_attr_get_size(a
) != expected_len
) {
847 ds_put_format(ds
, "(bad length %zu, expected %d)",
849 odp_flow_key_attr_len(nl_attr_type(a
)));
850 format_generic_odp_key(a
, ds
);
855 case OVS_KEY_ATTR_ENCAP
:
856 ds_put_cstr(ds
, "(");
857 if (nl_attr_get_size(a
)) {
858 odp_flow_key_format(nl_attr_get(a
), nl_attr_get_size(a
), ds
);
860 ds_put_char(ds
, ')');
863 case OVS_KEY_ATTR_PRIORITY
:
864 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
867 case OVS_KEY_ATTR_SKB_MARK
:
868 ds_put_format(ds
, "(%#"PRIx32
")", nl_attr_get_u32(a
));
871 case OVS_KEY_ATTR_TUNNEL
:
872 memset(&tun_key
, 0, sizeof tun_key
);
873 if (tun_key_from_attr(a
, &tun_key
) == ODP_FIT_ERROR
) {
874 ds_put_format(ds
, "(error)");
876 ds_put_format(ds
, "(tun_id=0x%"PRIx64
",src="IP_FMT
",dst="IP_FMT
","
877 "tos=0x%"PRIx8
",ttl=%"PRIu8
",flags(",
878 ntohll(tun_key
.tun_id
),
879 IP_ARGS(tun_key
.ip_src
),
880 IP_ARGS(tun_key
.ip_dst
),
881 tun_key
.ip_tos
, tun_key
.ip_ttl
);
883 format_flags(ds
, flow_tun_flag_to_string
,
884 (uint32_t) tun_key
.flags
, ',');
885 ds_put_format(ds
, "))");
889 case OVS_KEY_ATTR_IN_PORT
:
890 ds_put_format(ds
, "(%"PRIu32
")", nl_attr_get_u32(a
));
893 case OVS_KEY_ATTR_ETHERNET
:
894 eth_key
= nl_attr_get(a
);
895 ds_put_format(ds
, "(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
896 ETH_ADDR_ARGS(eth_key
->eth_src
),
897 ETH_ADDR_ARGS(eth_key
->eth_dst
));
900 case OVS_KEY_ATTR_VLAN
:
901 ds_put_char(ds
, '(');
902 format_vlan_tci(ds
, nl_attr_get_be16(a
));
903 ds_put_char(ds
, ')');
906 case OVS_KEY_ATTR_MPLS
: {
907 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
908 ds_put_char(ds
, '(');
909 format_mpls_lse(ds
, mpls_key
->mpls_top_lse
);
910 ds_put_char(ds
, ')');
914 case OVS_KEY_ATTR_ETHERTYPE
:
915 ds_put_format(ds
, "(0x%04"PRIx16
")",
916 ntohs(nl_attr_get_be16(a
)));
919 case OVS_KEY_ATTR_IPV4
:
920 ipv4_key
= nl_attr_get(a
);
921 ds_put_format(ds
, "(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
922 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=%s)",
923 IP_ARGS(ipv4_key
->ipv4_src
),
924 IP_ARGS(ipv4_key
->ipv4_dst
),
925 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
,
927 ovs_frag_type_to_string(ipv4_key
->ipv4_frag
));
930 case OVS_KEY_ATTR_IPV6
: {
931 char src_str
[INET6_ADDRSTRLEN
];
932 char dst_str
[INET6_ADDRSTRLEN
];
934 ipv6_key
= nl_attr_get(a
);
935 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
936 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
938 ds_put_format(ds
, "(src=%s,dst=%s,label=%#"PRIx32
",proto=%"PRIu8
939 ",tclass=%#"PRIx8
",hlimit=%"PRIu8
",frag=%s)",
940 src_str
, dst_str
, ntohl(ipv6_key
->ipv6_label
),
941 ipv6_key
->ipv6_proto
, ipv6_key
->ipv6_tclass
,
942 ipv6_key
->ipv6_hlimit
,
943 ovs_frag_type_to_string(ipv6_key
->ipv6_frag
));
947 case OVS_KEY_ATTR_TCP
:
948 tcp_key
= nl_attr_get(a
);
949 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
950 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
953 case OVS_KEY_ATTR_UDP
:
954 udp_key
= nl_attr_get(a
);
955 ds_put_format(ds
, "(src=%"PRIu16
",dst=%"PRIu16
")",
956 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
959 case OVS_KEY_ATTR_ICMP
:
960 icmp_key
= nl_attr_get(a
);
961 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
962 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
965 case OVS_KEY_ATTR_ICMPV6
:
966 icmpv6_key
= nl_attr_get(a
);
967 ds_put_format(ds
, "(type=%"PRIu8
",code=%"PRIu8
")",
968 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
971 case OVS_KEY_ATTR_ARP
:
972 arp_key
= nl_attr_get(a
);
973 ds_put_format(ds
, "(sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
974 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
")",
975 IP_ARGS(arp_key
->arp_sip
), IP_ARGS(arp_key
->arp_tip
),
976 ntohs(arp_key
->arp_op
), ETH_ADDR_ARGS(arp_key
->arp_sha
),
977 ETH_ADDR_ARGS(arp_key
->arp_tha
));
980 case OVS_KEY_ATTR_ND
: {
981 char target
[INET6_ADDRSTRLEN
];
983 nd_key
= nl_attr_get(a
);
984 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
986 ds_put_format(ds
, "(target=%s", target
);
987 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
988 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
989 ETH_ADDR_ARGS(nd_key
->nd_sll
));
991 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
992 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
993 ETH_ADDR_ARGS(nd_key
->nd_tll
));
995 ds_put_char(ds
, ')');
999 case OVS_KEY_ATTR_UNSPEC
:
1000 case __OVS_KEY_ATTR_MAX
:
1002 format_generic_odp_key(a
, ds
);
1007 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1008 * OVS_KEY_ATTR_* attributes in 'key'. */
1010 odp_flow_key_format(const struct nlattr
*key
, size_t key_len
, struct ds
*ds
)
1013 const struct nlattr
*a
;
1016 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
1018 ds_put_char(ds
, ',');
1020 format_odp_key_attr(a
, ds
);
1025 if (left
== key_len
) {
1026 ds_put_cstr(ds
, "<empty>");
1028 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1029 for (i
= 0; i
< left
; i
++) {
1030 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1032 ds_put_char(ds
, ')');
1035 ds_put_cstr(ds
, "<empty>");
1040 put_nd_key(int n
, const char *nd_target_s
,
1041 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
1043 struct ovs_key_nd nd_key
;
1045 memset(&nd_key
, 0, sizeof nd_key
);
1046 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
1050 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
1053 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
1055 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
1060 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
1062 if (!strcasecmp(s
, "no")) {
1063 *type
= OVS_FRAG_TYPE_NONE
;
1064 } else if (!strcasecmp(s
, "first")) {
1065 *type
= OVS_FRAG_TYPE_FIRST
;
1066 } else if (!strcasecmp(s
, "later")) {
1067 *type
= OVS_FRAG_TYPE_LATER
;
1075 mpls_lse_from_components(int mpls_label
, int mpls_tc
, int mpls_ttl
, int mpls_bos
)
1077 return (htonl((mpls_label
<< MPLS_LABEL_SHIFT
) |
1078 (mpls_tc
<< MPLS_TC_SHIFT
) |
1079 (mpls_ttl
<< MPLS_TTL_SHIFT
) |
1080 (mpls_bos
<< MPLS_BOS_SHIFT
)));
1084 parse_odp_key_attr(const char *s
, const struct simap
*port_names
,
1087 /* Many of the sscanf calls in this function use oversized destination
1088 * fields because some sscanf() implementations truncate the range of %i
1089 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1090 * value of 0x7fff. The other alternatives are to allow only a single
1091 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1094 * The tun_id parser has to use an alternative approach because there is no
1095 * type larger than 64 bits. */
1098 unsigned long long int priority
;
1101 if (sscanf(s
, "skb_priority(%llx)%n", &priority
, &n
) > 0 && n
> 0) {
1102 nl_msg_put_u32(key
, OVS_KEY_ATTR_PRIORITY
, priority
);
1108 unsigned long long int mark
;
1111 if (sscanf(s
, "skb_mark(%llx)%n", &mark
, &n
) > 0 && n
> 0) {
1112 nl_msg_put_u32(key
, OVS_KEY_ATTR_SKB_MARK
, mark
);
1120 struct flow_tnl tun_key
;
1123 if (sscanf(s
, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1124 "src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
1125 ",tos=%i,ttl=%i,flags%n", tun_id_s
,
1126 IP_SCAN_ARGS(&tun_key
.ip_src
),
1127 IP_SCAN_ARGS(&tun_key
.ip_dst
), &tos
, &ttl
,
1132 tun_key
.tun_id
= htonll(strtoull(tun_id_s
, NULL
, 0));
1133 tun_key
.ip_tos
= tos
;
1134 tun_key
.ip_ttl
= ttl
;
1135 res
= parse_flags(&s
[n
], flow_tun_flag_to_string
, &flags
);
1136 tun_key
.flags
= (uint16_t) flags
;
1146 tun_key_to_attr(key
, &tun_key
);
1152 unsigned long long int in_port
;
1155 if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
1156 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
1161 if (port_names
&& !strncmp(s
, "in_port(", 8)) {
1163 const struct simap_node
*node
;
1167 name_len
= strcspn(s
, ")");
1168 node
= simap_find_len(port_names
, name
, name_len
);
1170 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, node
->data
);
1171 return 8 + name_len
+ 1;
1176 struct ovs_key_ethernet eth_key
;
1180 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
1181 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
1182 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
1183 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
1184 ð_key
, sizeof eth_key
);
1195 if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n", &vid
, &pcp
, &n
) > 0
1197 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1198 htons((vid
<< VLAN_VID_SHIFT
) |
1199 (pcp
<< VLAN_PCP_SHIFT
) |
1202 } else if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i,cfi=%i)%n",
1203 &vid
, &pcp
, &cfi
, &n
) > 0
1205 nl_msg_put_be16(key
, OVS_KEY_ATTR_VLAN
,
1206 htons((vid
<< VLAN_VID_SHIFT
) |
1207 (pcp
<< VLAN_PCP_SHIFT
) |
1208 (cfi
? VLAN_CFI
: 0)));
1217 if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
1218 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
1224 int label
, tc
, ttl
, bos
;
1227 if (sscanf(s
, "mpls(label=%"SCNi32
",tc=%i,ttl=%i,bos=%i)%n",
1228 &label
, &tc
, &ttl
, &bos
, &n
) > 0 &&
1230 struct ovs_key_mpls
*mpls
;
1232 mpls
= nl_msg_put_unspec_uninit(key
, OVS_KEY_ATTR_MPLS
,
1234 mpls
->mpls_top_lse
= mpls_lse_from_components(label
, tc
, ttl
, bos
);
1246 enum ovs_frag_type ipv4_frag
;
1249 if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
1250 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1251 IP_SCAN_ARGS(&ipv4_src
), IP_SCAN_ARGS(&ipv4_dst
),
1252 &ipv4_proto
, &ipv4_tos
, &ipv4_ttl
, frag
, &n
) > 0
1254 && ovs_frag_type_from_string(frag
, &ipv4_frag
)) {
1255 struct ovs_key_ipv4 ipv4_key
;
1257 ipv4_key
.ipv4_src
= ipv4_src
;
1258 ipv4_key
.ipv4_dst
= ipv4_dst
;
1259 ipv4_key
.ipv4_proto
= ipv4_proto
;
1260 ipv4_key
.ipv4_tos
= ipv4_tos
;
1261 ipv4_key
.ipv4_ttl
= ipv4_ttl
;
1262 ipv4_key
.ipv4_frag
= ipv4_frag
;
1263 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
1264 &ipv4_key
, sizeof ipv4_key
);
1270 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
1271 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
1277 enum ovs_frag_type ipv6_frag
;
1280 if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
1281 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1282 ipv6_src_s
, ipv6_dst_s
, &ipv6_label
,
1283 &ipv6_proto
, &ipv6_tclass
, &ipv6_hlimit
, frag
, &n
) > 0
1285 && ovs_frag_type_from_string(frag
, &ipv6_frag
)) {
1286 struct ovs_key_ipv6 ipv6_key
;
1288 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
1289 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
1292 ipv6_key
.ipv6_label
= htonl(ipv6_label
);
1293 ipv6_key
.ipv6_proto
= ipv6_proto
;
1294 ipv6_key
.ipv6_tclass
= ipv6_tclass
;
1295 ipv6_key
.ipv6_hlimit
= ipv6_hlimit
;
1296 ipv6_key
.ipv6_frag
= ipv6_frag
;
1297 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
1298 &ipv6_key
, sizeof ipv6_key
);
1308 if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
1310 struct ovs_key_tcp tcp_key
;
1312 tcp_key
.tcp_src
= htons(tcp_src
);
1313 tcp_key
.tcp_dst
= htons(tcp_dst
);
1314 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
1324 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
1326 struct ovs_key_udp udp_key
;
1328 udp_key
.udp_src
= htons(udp_src
);
1329 udp_key
.udp_dst
= htons(udp_dst
);
1330 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
1340 if (sscanf(s
, "icmp(type=%i,code=%i)%n",
1341 &icmp_type
, &icmp_code
, &n
) > 0
1343 struct ovs_key_icmp icmp_key
;
1345 icmp_key
.icmp_type
= icmp_type
;
1346 icmp_key
.icmp_code
= icmp_code
;
1347 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
1348 &icmp_key
, sizeof icmp_key
);
1354 struct ovs_key_icmpv6 icmpv6_key
;
1357 if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
1358 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
1360 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
1361 &icmpv6_key
, sizeof icmpv6_key
);
1370 uint8_t arp_sha
[ETH_ADDR_LEN
];
1371 uint8_t arp_tha
[ETH_ADDR_LEN
];
1374 if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
1375 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
1376 IP_SCAN_ARGS(&arp_sip
),
1377 IP_SCAN_ARGS(&arp_tip
),
1379 ETH_ADDR_SCAN_ARGS(arp_sha
),
1380 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
1381 struct ovs_key_arp arp_key
;
1383 memset(&arp_key
, 0, sizeof arp_key
);
1384 arp_key
.arp_sip
= arp_sip
;
1385 arp_key
.arp_tip
= arp_tip
;
1386 arp_key
.arp_op
= htons(arp_op
);
1387 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
1388 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
1389 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
1395 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
1396 uint8_t nd_sll
[ETH_ADDR_LEN
];
1397 uint8_t nd_tll
[ETH_ADDR_LEN
];
1400 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
1401 nd_target_s
, &n
) > 0 && n
> 0) {
1402 return put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
1404 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
1405 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
1407 return put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
1409 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
1410 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1412 return put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
1414 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
1415 "tll="ETH_ADDR_SCAN_FMT
")%n",
1416 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
1417 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
1419 return put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
1423 if (!strncmp(s
, "encap(", 6)) {
1424 const char *start
= s
;
1427 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
1433 s
+= strspn(s
, ", \t\r\n");
1436 } else if (*s
== ')') {
1440 retval
= parse_odp_key_attr(s
, port_names
, key
);
1448 nl_msg_end_nested(key
, encap
);
1456 /* Parses the string representation of a datapath flow key, in the
1457 * format output by odp_flow_key_format(). Returns 0 if successful,
1458 * otherwise a positive errno value. On success, the flow key is
1459 * appended to 'key' as a series of Netlink attributes. On failure, no
1460 * data is appended to 'key'. Either way, 'key''s data might be
1463 * If 'port_names' is nonnull, it points to an simap that maps from a port name
1464 * to a port number. (Port names may be used instead of port numbers in
1467 * On success, the attributes appended to 'key' are individually syntactically
1468 * valid, but they may not be valid as a sequence. 'key' might, for example,
1469 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
1471 odp_flow_key_from_string(const char *s
, const struct simap
*port_names
,
1474 const size_t old_size
= key
->size
;
1478 s
+= strspn(s
, delimiters
);
1483 retval
= parse_odp_key_attr(s
, port_names
, key
);
1485 key
->size
= old_size
;
1495 ovs_to_odp_frag(uint8_t nw_frag
)
1497 return (nw_frag
== 0 ? OVS_FRAG_TYPE_NONE
1498 : nw_frag
== FLOW_NW_FRAG_ANY
? OVS_FRAG_TYPE_FIRST
1499 : OVS_FRAG_TYPE_LATER
);
1502 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
1503 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
1504 * number rather than a datapath port number). Instead, if 'odp_in_port'
1505 * is anything other than OVSP_NONE, it is included in 'buf' as the input
1508 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
1509 * capable of being expanded to allow for that much space. */
1511 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
,
1512 uint32_t odp_in_port
)
1514 struct ovs_key_ethernet
*eth_key
;
1517 if (flow
->skb_priority
) {
1518 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, flow
->skb_priority
);
1521 if (flow
->tunnel
.ip_dst
) {
1522 tun_key_to_attr(buf
, &flow
->tunnel
);
1525 if (flow
->skb_mark
) {
1526 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, flow
->skb_mark
);
1529 if (odp_in_port
!= OVSP_NONE
) {
1530 nl_msg_put_u32(buf
, OVS_KEY_ATTR_IN_PORT
, odp_in_port
);
1533 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
1535 memcpy(eth_key
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
1536 memcpy(eth_key
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
1538 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
1539 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
1540 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, flow
->vlan_tci
);
1541 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
1542 if (flow
->vlan_tci
== htons(0)) {
1549 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
1553 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, flow
->dl_type
);
1555 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1556 struct ovs_key_ipv4
*ipv4_key
;
1558 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
1560 ipv4_key
->ipv4_src
= flow
->nw_src
;
1561 ipv4_key
->ipv4_dst
= flow
->nw_dst
;
1562 ipv4_key
->ipv4_proto
= flow
->nw_proto
;
1563 ipv4_key
->ipv4_tos
= flow
->nw_tos
;
1564 ipv4_key
->ipv4_ttl
= flow
->nw_ttl
;
1565 ipv4_key
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1566 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1567 struct ovs_key_ipv6
*ipv6_key
;
1569 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
1571 memcpy(ipv6_key
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
1572 memcpy(ipv6_key
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
1573 ipv6_key
->ipv6_label
= flow
->ipv6_label
;
1574 ipv6_key
->ipv6_proto
= flow
->nw_proto
;
1575 ipv6_key
->ipv6_tclass
= flow
->nw_tos
;
1576 ipv6_key
->ipv6_hlimit
= flow
->nw_ttl
;
1577 ipv6_key
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
);
1578 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
1579 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
1580 struct ovs_key_arp
*arp_key
;
1582 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
1584 memset(arp_key
, 0, sizeof *arp_key
);
1585 arp_key
->arp_sip
= flow
->nw_src
;
1586 arp_key
->arp_tip
= flow
->nw_dst
;
1587 arp_key
->arp_op
= htons(flow
->nw_proto
);
1588 memcpy(arp_key
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
1589 memcpy(arp_key
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
1592 if (flow
->mpls_depth
) {
1593 struct ovs_key_mpls
*mpls_key
;
1595 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
1597 mpls_key
->mpls_top_lse
= flow
->mpls_lse
;
1600 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1601 if (flow
->nw_proto
== IPPROTO_TCP
) {
1602 struct ovs_key_tcp
*tcp_key
;
1604 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
1606 tcp_key
->tcp_src
= flow
->tp_src
;
1607 tcp_key
->tcp_dst
= flow
->tp_dst
;
1608 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
1609 struct ovs_key_udp
*udp_key
;
1611 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
1613 udp_key
->udp_src
= flow
->tp_src
;
1614 udp_key
->udp_dst
= flow
->tp_dst
;
1615 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
1616 && flow
->nw_proto
== IPPROTO_ICMP
) {
1617 struct ovs_key_icmp
*icmp_key
;
1619 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
1621 icmp_key
->icmp_type
= ntohs(flow
->tp_src
);
1622 icmp_key
->icmp_code
= ntohs(flow
->tp_dst
);
1623 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1624 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
1625 struct ovs_key_icmpv6
*icmpv6_key
;
1627 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
1628 sizeof *icmpv6_key
);
1629 icmpv6_key
->icmpv6_type
= ntohs(flow
->tp_src
);
1630 icmpv6_key
->icmpv6_code
= ntohs(flow
->tp_dst
);
1632 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
1633 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
1634 struct ovs_key_nd
*nd_key
;
1636 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
1638 memcpy(nd_key
->nd_target
, &flow
->nd_target
,
1639 sizeof nd_key
->nd_target
);
1640 memcpy(nd_key
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
1641 memcpy(nd_key
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
1648 nl_msg_end_nested(buf
, encap
);
1653 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
1655 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
1656 return hash_words((const uint32_t *) key
, key_len
/ sizeof(uint32_t), 0);
1660 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
1661 uint64_t attrs
, int out_of_range_attr
,
1662 const struct nlattr
*key
, size_t key_len
)
1667 if (VLOG_DROP_DBG(rl
)) {
1672 for (i
= 0; i
< 64; i
++) {
1673 if (attrs
& (UINT64_C(1) << i
)) {
1674 ds_put_format(&s
, " %s", ovs_key_attr_to_string(i
));
1677 if (out_of_range_attr
) {
1678 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
1681 ds_put_cstr(&s
, ": ");
1682 odp_flow_key_format(key
, key_len
, &s
);
1684 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
1689 odp_to_ovs_frag(uint8_t odp_frag
, struct flow
*flow
)
1691 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1693 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
1694 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
1698 if (odp_frag
!= OVS_FRAG_TYPE_NONE
) {
1699 flow
->nw_frag
|= FLOW_NW_FRAG_ANY
;
1700 if (odp_frag
== OVS_FRAG_TYPE_LATER
) {
1701 flow
->nw_frag
|= FLOW_NW_FRAG_LATER
;
1708 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
1709 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
1710 int *out_of_range_attrp
)
1712 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1713 const struct nlattr
*nla
;
1714 uint64_t present_attrs
;
1718 *out_of_range_attrp
= 0;
1719 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
1720 uint16_t type
= nl_attr_type(nla
);
1721 size_t len
= nl_attr_get_size(nla
);
1722 int expected_len
= odp_flow_key_attr_len(type
);
1724 if (len
!= expected_len
&& expected_len
>= 0) {
1725 VLOG_ERR_RL(&rl
, "attribute %s has length %zu but should have "
1726 "length %d", ovs_key_attr_to_string(type
),
1731 if (type
>= CHAR_BIT
* sizeof present_attrs
) {
1732 *out_of_range_attrp
= type
;
1734 if (present_attrs
& (UINT64_C(1) << type
)) {
1735 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
1736 ovs_key_attr_to_string(type
));
1740 present_attrs
|= UINT64_C(1) << type
;
1745 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
1749 *present_attrsp
= present_attrs
;
1753 static enum odp_key_fitness
1754 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
1755 uint64_t expected_attrs
,
1756 const struct nlattr
*key
, size_t key_len
)
1758 uint64_t missing_attrs
;
1759 uint64_t extra_attrs
;
1761 missing_attrs
= expected_attrs
& ~present_attrs
;
1762 if (missing_attrs
) {
1763 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1764 log_odp_key_attributes(&rl
, "expected but not present",
1765 missing_attrs
, 0, key
, key_len
);
1766 return ODP_FIT_TOO_LITTLE
;
1769 extra_attrs
= present_attrs
& ~expected_attrs
;
1770 if (extra_attrs
|| out_of_range_attr
) {
1771 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
1772 log_odp_key_attributes(&rl
, "present but not expected",
1773 extra_attrs
, out_of_range_attr
, key
, key_len
);
1774 return ODP_FIT_TOO_MUCH
;
1777 return ODP_FIT_PERFECT
;
1781 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1782 uint64_t present_attrs
, uint64_t *expected_attrs
,
1785 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1787 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
1788 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
1789 if (ntohs(flow
->dl_type
) < 1536) {
1790 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
1791 ntohs(flow
->dl_type
));
1794 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
1796 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
1801 static enum odp_key_fitness
1802 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1803 uint64_t present_attrs
, int out_of_range_attr
,
1804 uint64_t expected_attrs
, struct flow
*flow
,
1805 const struct nlattr
*key
, size_t key_len
)
1807 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1810 /* Parse MPLS label stack entry */
1811 if (eth_type_mpls(flow
->dl_type
)) {
1812 /* Calculate fitness of outer attributes. */
1813 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
1815 /* Get the MPLS LSE value. */
1816 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
))) {
1817 return ODP_FIT_TOO_LITTLE
;
1819 flow
->mpls_lse
= nl_attr_get_be32(attrs
[OVS_KEY_ATTR_MPLS
]);
1822 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1823 flow
->encap_dl_type
= htons(ETH_TYPE_IP
);
1824 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1825 flow
->encap_dl_type
= htons(ETH_TYPE_IPV6
);
1826 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1827 flow
->encap_dl_type
= htons(ETH_TYPE_ARP
);
1831 dl_type
= flow_innermost_dl_type(flow
);
1833 if (dl_type
== htons(ETH_TYPE_IP
)) {
1834 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
1835 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
1836 const struct ovs_key_ipv4
*ipv4_key
;
1838 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
1839 flow
->nw_src
= ipv4_key
->ipv4_src
;
1840 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
1841 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
1842 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
1843 flow
->nw_ttl
= ipv4_key
->ipv4_ttl
;
1844 if (!odp_to_ovs_frag(ipv4_key
->ipv4_frag
, flow
)) {
1845 return ODP_FIT_ERROR
;
1848 } else if (dl_type
== htons(ETH_TYPE_IPV6
)) {
1849 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
1850 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
1851 const struct ovs_key_ipv6
*ipv6_key
;
1853 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
1854 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
1855 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
1856 flow
->ipv6_label
= ipv6_key
->ipv6_label
;
1857 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
1858 flow
->nw_tos
= ipv6_key
->ipv6_tclass
;
1859 flow
->nw_ttl
= ipv6_key
->ipv6_hlimit
;
1860 if (!odp_to_ovs_frag(ipv6_key
->ipv6_frag
, flow
)) {
1861 return ODP_FIT_ERROR
;
1864 } else if (dl_type
== htons(ETH_TYPE_ARP
) ||
1865 dl_type
== htons(ETH_TYPE_RARP
)) {
1866 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
1867 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
1868 const struct ovs_key_arp
*arp_key
;
1870 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
1871 flow
->nw_src
= arp_key
->arp_sip
;
1872 flow
->nw_dst
= arp_key
->arp_tip
;
1873 if (arp_key
->arp_op
& htons(0xff00)) {
1874 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
1875 "key", ntohs(arp_key
->arp_op
));
1876 return ODP_FIT_ERROR
;
1878 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
1879 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
1880 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
1884 if (flow
->nw_proto
== IPPROTO_TCP
1885 && (dl_type
== htons(ETH_TYPE_IP
) ||
1886 dl_type
== htons(ETH_TYPE_IPV6
))
1887 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1888 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
1889 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
1890 const struct ovs_key_tcp
*tcp_key
;
1892 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
1893 flow
->tp_src
= tcp_key
->tcp_src
;
1894 flow
->tp_dst
= tcp_key
->tcp_dst
;
1896 } else if (flow
->nw_proto
== IPPROTO_UDP
1897 && (dl_type
== htons(ETH_TYPE_IP
) ||
1898 dl_type
== htons(ETH_TYPE_IPV6
))
1899 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1900 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
1901 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
1902 const struct ovs_key_udp
*udp_key
;
1904 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
1905 flow
->tp_src
= udp_key
->udp_src
;
1906 flow
->tp_dst
= udp_key
->udp_dst
;
1908 } else if (flow
->nw_proto
== IPPROTO_ICMP
1909 && dl_type
== htons(ETH_TYPE_IP
)
1910 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1911 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
1912 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
1913 const struct ovs_key_icmp
*icmp_key
;
1915 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
1916 flow
->tp_src
= htons(icmp_key
->icmp_type
);
1917 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
1919 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
1920 && dl_type
== htons(ETH_TYPE_IPV6
)
1921 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
1922 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
1923 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
1924 const struct ovs_key_icmpv6
*icmpv6_key
;
1926 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
1927 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
1928 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
1930 if (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
1931 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
1932 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
1933 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
1934 const struct ovs_key_nd
*nd_key
;
1936 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
1937 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
1938 sizeof flow
->nd_target
);
1939 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
1940 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
1946 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
1950 /* Parse 802.1Q header then encapsulated L3 attributes. */
1951 static enum odp_key_fitness
1952 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
1953 uint64_t present_attrs
, int out_of_range_attr
,
1954 uint64_t expected_attrs
, struct flow
*flow
,
1955 const struct nlattr
*key
, size_t key_len
)
1957 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1959 const struct nlattr
*encap
1960 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
1961 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
1962 enum odp_key_fitness encap_fitness
;
1963 enum odp_key_fitness fitness
;
1966 /* Calulate fitness of outer attributes. */
1967 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
1968 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
1969 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
1970 expected_attrs
, key
, key_len
);
1972 /* Get the VLAN TCI value. */
1973 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
1974 return ODP_FIT_TOO_LITTLE
;
1976 tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
1977 if (tci
== htons(0)) {
1978 /* Corner case for a truncated 802.1Q header. */
1979 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
1980 return ODP_FIT_TOO_MUCH
;
1983 } else if (!(tci
& htons(VLAN_CFI
))) {
1984 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
1985 "but CFI bit is not set", ntohs(tci
));
1986 return ODP_FIT_ERROR
;
1990 * Remove the TPID from dl_type since it's not the real Ethertype. */
1991 flow
->vlan_tci
= tci
;
1992 flow
->dl_type
= htons(0);
1994 /* Now parse the encapsulated attributes. */
1995 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
1996 attrs
, &present_attrs
, &out_of_range_attr
)) {
1997 return ODP_FIT_ERROR
;
2001 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2002 return ODP_FIT_ERROR
;
2004 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2005 expected_attrs
, flow
, key
, key_len
);
2007 /* The overall fitness is the worse of the outer and inner attributes. */
2008 return MAX(fitness
, encap_fitness
);
2011 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
2012 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
2013 * 'key' fits our expectations for what a flow key should contain.
2015 * The 'in_port' will be the datapath's understanding of the port. The
2016 * caller will need to translate with odp_port_to_ofp_port() if the
2017 * OpenFlow port is needed.
2019 * This function doesn't take the packet itself as an argument because none of
2020 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
2021 * it is always possible to infer which additional attribute(s) should appear
2022 * by looking at the attributes for lower-level protocols, e.g. if the network
2023 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
2024 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
2025 * must be absent. */
2026 enum odp_key_fitness
2027 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
2030 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
2031 uint64_t expected_attrs
;
2032 uint64_t present_attrs
;
2033 int out_of_range_attr
;
2035 memset(flow
, 0, sizeof *flow
);
2037 /* Parse attributes. */
2038 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
2039 &out_of_range_attr
)) {
2040 return ODP_FIT_ERROR
;
2045 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
2046 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
2047 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
2050 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
2051 flow
->skb_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
2052 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
2055 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
2056 enum odp_key_fitness res
;
2058 res
= tun_key_from_attr(attrs
[OVS_KEY_ATTR_TUNNEL
], &flow
->tunnel
);
2059 if (res
== ODP_FIT_ERROR
) {
2060 return ODP_FIT_ERROR
;
2061 } else if (res
== ODP_FIT_PERFECT
) {
2062 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
2066 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
2067 flow
->in_port
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_IN_PORT
]);
2068 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
2070 flow
->in_port
= OVSP_NONE
;
2073 /* Ethernet header. */
2074 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
2075 const struct ovs_key_ethernet
*eth_key
;
2077 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
2078 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
2079 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
2081 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
2083 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
2084 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
)) {
2085 return ODP_FIT_ERROR
;
2088 if (flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
2089 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
2090 expected_attrs
, flow
, key
, key_len
);
2092 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
2093 expected_attrs
, flow
, key
, key_len
);
2096 /* Returns 'fitness' as a string, for use in debug messages. */
2098 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
2101 case ODP_FIT_PERFECT
:
2103 case ODP_FIT_TOO_MUCH
:
2105 case ODP_FIT_TOO_LITTLE
:
2106 return "too_little";
2114 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
2115 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
2116 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
2117 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
2118 * null, then the return value is not meaningful.) */
2120 odp_put_userspace_action(uint32_t pid
,
2121 const void *userdata
, size_t userdata_size
,
2122 struct ofpbuf
*odp_actions
)
2124 size_t userdata_ofs
;
2127 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
2128 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
2130 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
2131 nl_msg_put_unspec(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
2132 userdata
, userdata_size
);
2136 nl_msg_end_nested(odp_actions
, offset
);
2138 return userdata_ofs
;
2142 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
2143 struct ofpbuf
*odp_actions
)
2145 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2146 tun_key_to_attr(odp_actions
, tunnel
);
2147 nl_msg_end_nested(odp_actions
, offset
);
2150 /* The commit_odp_actions() function and its helpers. */
2153 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
2154 const void *key
, size_t key_size
)
2156 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
2157 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
2158 nl_msg_end_nested(odp_actions
, offset
);
2162 odp_put_skb_mark_action(const uint32_t skb_mark
,
2163 struct ofpbuf
*odp_actions
)
2165 commit_set_action(odp_actions
, OVS_KEY_ATTR_SKB_MARK
, &skb_mark
,
2169 /* If any of the flow key data that ODP actions can modify are different in
2170 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
2171 * 'odp_actions' that change the flow tunneling information in key from
2172 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
2173 * same way. In other words, operates the same as commit_odp_actions(), but
2174 * only on tunneling information. */
2176 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
2177 struct ofpbuf
*odp_actions
)
2179 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
2182 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
2184 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
2185 if (flow
->tunnel
.ip_dst
) {
2186 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
2191 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base
,
2192 struct ofpbuf
*odp_actions
)
2194 struct ovs_key_ethernet eth_key
;
2196 if (eth_addr_equals(base
->dl_src
, flow
->dl_src
) &&
2197 eth_addr_equals(base
->dl_dst
, flow
->dl_dst
)) {
2201 memcpy(base
->dl_src
, flow
->dl_src
, ETH_ADDR_LEN
);
2202 memcpy(base
->dl_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
2204 memcpy(eth_key
.eth_src
, base
->dl_src
, ETH_ADDR_LEN
);
2205 memcpy(eth_key
.eth_dst
, base
->dl_dst
, ETH_ADDR_LEN
);
2207 commit_set_action(odp_actions
, OVS_KEY_ATTR_ETHERNET
,
2208 ð_key
, sizeof(eth_key
));
2212 commit_vlan_action(const struct flow
*flow
, struct flow
*base
,
2213 struct ofpbuf
*odp_actions
)
2215 if (base
->vlan_tci
== flow
->vlan_tci
) {
2219 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
2220 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
2223 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
2224 struct ovs_action_push_vlan vlan
;
2226 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
2227 vlan
.vlan_tci
= flow
->vlan_tci
;
2228 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2229 &vlan
, sizeof vlan
);
2231 base
->vlan_tci
= flow
->vlan_tci
;
2235 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
2236 struct ofpbuf
*odp_actions
)
2238 if (flow
->mpls_lse
== base
->mpls_lse
&&
2239 flow
->mpls_depth
== base
->mpls_depth
) {
2243 if (flow
->mpls_depth
< base
->mpls_depth
) {
2244 if (base
->mpls_depth
- flow
->mpls_depth
> 1) {
2245 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2246 VLOG_WARN_RL(&rl
, "Multiple mpls_pop actions reduced to "
2247 " a single mpls_pop action");
2250 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, flow
->dl_type
);
2251 } else if (flow
->mpls_depth
> base
->mpls_depth
) {
2252 struct ovs_action_push_mpls
*mpls
;
2254 if (flow
->mpls_depth
- base
->mpls_depth
> 1) {
2255 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
2256 VLOG_WARN_RL(&rl
, "Multiple mpls_push actions reduced to "
2257 " a single mpls_push action");
2260 mpls
= nl_msg_put_unspec_uninit(odp_actions
, OVS_ACTION_ATTR_PUSH_MPLS
,
2262 memset(mpls
, 0, sizeof *mpls
);
2263 mpls
->mpls_ethertype
= flow
->dl_type
;
2264 mpls
->mpls_lse
= flow
->mpls_lse
;
2266 struct ovs_key_mpls mpls_key
;
2268 mpls_key
.mpls_top_lse
= flow
->mpls_lse
;
2269 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
2270 &mpls_key
, sizeof(mpls_key
));
2273 base
->dl_type
= flow
->dl_type
;
2274 base
->mpls_lse
= flow
->mpls_lse
;
2275 base
->mpls_depth
= flow
->mpls_depth
;
2279 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base
,
2280 struct ofpbuf
*odp_actions
)
2282 struct ovs_key_ipv4 ipv4_key
;
2284 if (base
->nw_src
== flow
->nw_src
&&
2285 base
->nw_dst
== flow
->nw_dst
&&
2286 base
->nw_tos
== flow
->nw_tos
&&
2287 base
->nw_ttl
== flow
->nw_ttl
&&
2288 base
->nw_frag
== flow
->nw_frag
) {
2292 ipv4_key
.ipv4_src
= base
->nw_src
= flow
->nw_src
;
2293 ipv4_key
.ipv4_dst
= base
->nw_dst
= flow
->nw_dst
;
2294 ipv4_key
.ipv4_tos
= base
->nw_tos
= flow
->nw_tos
;
2295 ipv4_key
.ipv4_ttl
= base
->nw_ttl
= flow
->nw_ttl
;
2296 ipv4_key
.ipv4_proto
= base
->nw_proto
;
2297 ipv4_key
.ipv4_frag
= ovs_to_odp_frag(base
->nw_frag
);
2299 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV4
,
2300 &ipv4_key
, sizeof(ipv4_key
));
2304 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base
,
2305 struct ofpbuf
*odp_actions
)
2307 struct ovs_key_ipv6 ipv6_key
;
2309 if (ipv6_addr_equals(&base
->ipv6_src
, &flow
->ipv6_src
) &&
2310 ipv6_addr_equals(&base
->ipv6_dst
, &flow
->ipv6_dst
) &&
2311 base
->ipv6_label
== flow
->ipv6_label
&&
2312 base
->nw_tos
== flow
->nw_tos
&&
2313 base
->nw_ttl
== flow
->nw_ttl
&&
2314 base
->nw_frag
== flow
->nw_frag
) {
2318 base
->ipv6_src
= flow
->ipv6_src
;
2319 memcpy(&ipv6_key
.ipv6_src
, &base
->ipv6_src
, sizeof(ipv6_key
.ipv6_src
));
2320 base
->ipv6_dst
= flow
->ipv6_dst
;
2321 memcpy(&ipv6_key
.ipv6_dst
, &base
->ipv6_dst
, sizeof(ipv6_key
.ipv6_dst
));
2323 ipv6_key
.ipv6_label
= base
->ipv6_label
= flow
->ipv6_label
;
2324 ipv6_key
.ipv6_tclass
= base
->nw_tos
= flow
->nw_tos
;
2325 ipv6_key
.ipv6_hlimit
= base
->nw_ttl
= flow
->nw_ttl
;
2326 ipv6_key
.ipv6_proto
= base
->nw_proto
;
2327 ipv6_key
.ipv6_frag
= ovs_to_odp_frag(base
->nw_frag
);
2329 commit_set_action(odp_actions
, OVS_KEY_ATTR_IPV6
,
2330 &ipv6_key
, sizeof(ipv6_key
));
2334 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
2335 struct ofpbuf
*odp_actions
)
2337 ovs_be16 dl_type
= flow_innermost_dl_type(flow
);
2339 /* Check if flow really have an IP header. */
2340 if (!flow
->nw_proto
) {
2344 if (dl_type
== htons(ETH_TYPE_IP
)) {
2345 commit_set_ipv4_action(flow
, base
, odp_actions
);
2346 } else if (dl_type
== htons(ETH_TYPE_IPV6
)) {
2347 commit_set_ipv6_action(flow
, base
, odp_actions
);
2352 commit_set_port_action(const struct flow
*flow
, struct flow
*base
,
2353 struct ofpbuf
*odp_actions
)
2355 if (!base
->tp_src
&& !base
->tp_dst
) {
2359 if (base
->tp_src
== flow
->tp_src
&&
2360 base
->tp_dst
== flow
->tp_dst
) {
2364 if (flow
->nw_proto
== IPPROTO_TCP
) {
2365 struct ovs_key_tcp port_key
;
2367 port_key
.tcp_src
= base
->tp_src
= flow
->tp_src
;
2368 port_key
.tcp_dst
= base
->tp_dst
= flow
->tp_dst
;
2370 commit_set_action(odp_actions
, OVS_KEY_ATTR_TCP
,
2371 &port_key
, sizeof(port_key
));
2373 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
2374 struct ovs_key_udp port_key
;
2376 port_key
.udp_src
= base
->tp_src
= flow
->tp_src
;
2377 port_key
.udp_dst
= base
->tp_dst
= flow
->tp_dst
;
2379 commit_set_action(odp_actions
, OVS_KEY_ATTR_UDP
,
2380 &port_key
, sizeof(port_key
));
2385 commit_set_priority_action(const struct flow
*flow
, struct flow
*base
,
2386 struct ofpbuf
*odp_actions
)
2388 if (base
->skb_priority
== flow
->skb_priority
) {
2391 base
->skb_priority
= flow
->skb_priority
;
2393 commit_set_action(odp_actions
, OVS_KEY_ATTR_PRIORITY
,
2394 &base
->skb_priority
, sizeof(base
->skb_priority
));
2398 commit_set_skb_mark_action(const struct flow
*flow
, struct flow
*base
,
2399 struct ofpbuf
*odp_actions
)
2401 if (base
->skb_mark
== flow
->skb_mark
) {
2404 base
->skb_mark
= flow
->skb_mark
;
2406 odp_put_skb_mark_action(base
->skb_mark
, odp_actions
);
2408 /* If any of the flow key data that ODP actions can modify are different in
2409 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
2410 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
2411 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
2412 * in addition to this function if needed. */
2414 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
2415 struct ofpbuf
*odp_actions
)
2417 commit_set_ether_addr_action(flow
, base
, odp_actions
);
2418 commit_vlan_action(flow
, base
, odp_actions
);
2419 commit_mpls_action(flow
, base
, odp_actions
);
2420 commit_set_nw_action(flow
, base
, odp_actions
);
2421 commit_set_port_action(flow
, base
, odp_actions
);
2422 commit_set_priority_action(flow
, base
, odp_actions
);
2423 commit_set_skb_mark_action(flow
, base
, odp_actions
);