2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <arpa/inet.h>
22 #include <netinet/icmp6.h>
25 #include "byte-order.h"
27 #include "dynamic-string.h"
31 #include "openvswitch/tunnel.h"
36 /* The interface between userspace and kernel uses an "OVS_*" prefix.
37 * Since this is fairly non-specific for the OVS userspace components,
38 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
39 * interactions with the datapath.
43 odp_action_len(uint16_t type
)
45 if (type
> OVS_ACTION_ATTR_MAX
) {
49 switch ((enum ovs_action_type
) type
) {
50 case OVS_ACTION_ATTR_OUTPUT
: return 4;
51 case OVS_ACTION_ATTR_USERSPACE
: return 8;
52 case OVS_ACTION_ATTR_SET_DL_TCI
: return 2;
53 case OVS_ACTION_ATTR_STRIP_VLAN
: return 0;
54 case OVS_ACTION_ATTR_SET_DL_SRC
: return ETH_ADDR_LEN
;
55 case OVS_ACTION_ATTR_SET_DL_DST
: return ETH_ADDR_LEN
;
56 case OVS_ACTION_ATTR_SET_NW_SRC
: return 4;
57 case OVS_ACTION_ATTR_SET_NW_DST
: return 4;
58 case OVS_ACTION_ATTR_SET_NW_TOS
: return 1;
59 case OVS_ACTION_ATTR_SET_TP_SRC
: return 2;
60 case OVS_ACTION_ATTR_SET_TP_DST
: return 2;
61 case OVS_ACTION_ATTR_SET_TUNNEL
: return 8;
62 case OVS_ACTION_ATTR_SET_PRIORITY
: return 4;
63 case OVS_ACTION_ATTR_POP_PRIORITY
: return 0;
65 case OVS_ACTION_ATTR_UNSPEC
:
66 case __OVS_ACTION_ATTR_MAX
:
74 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
76 size_t len
= nl_attr_get_size(a
);
78 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
80 const uint8_t *unspec
;
83 unspec
= nl_attr_get(a
);
84 for (i
= 0; i
< len
; i
++) {
85 ds_put_char(ds
, i
? ' ': '(');
86 ds_put_format(ds
, "%02x", unspec
[i
]);
93 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
98 if (nl_attr_get_size(a
) != odp_action_len(nl_attr_type(a
))) {
99 ds_put_format(ds
, "bad length %zu, expected %d for: ",
100 nl_attr_get_size(a
), odp_action_len(nl_attr_type(a
)));
101 format_generic_odp_action(ds
, a
);
105 switch (nl_attr_type(a
)) {
106 case OVS_ACTION_ATTR_OUTPUT
:
107 ds_put_format(ds
, "%"PRIu16
, nl_attr_get_u32(a
));
109 case OVS_ACTION_ATTR_USERSPACE
:
110 ds_put_format(ds
, "userspace(%"PRIu64
")", nl_attr_get_u64(a
));
112 case OVS_ACTION_ATTR_SET_TUNNEL
:
113 ds_put_format(ds
, "set_tunnel(%#"PRIx64
")",
114 ntohll(nl_attr_get_be64(a
)));
116 case OVS_ACTION_ATTR_SET_DL_TCI
:
117 ds_put_format(ds
, "set_tci(vid=%"PRIu16
",pcp=%d)",
118 vlan_tci_to_vid(nl_attr_get_be16(a
)),
119 vlan_tci_to_pcp(nl_attr_get_be16(a
)));
121 case OVS_ACTION_ATTR_STRIP_VLAN
:
122 ds_put_format(ds
, "strip_vlan");
124 case OVS_ACTION_ATTR_SET_DL_SRC
:
125 eth
= nl_attr_get_unspec(a
, ETH_ADDR_LEN
);
126 ds_put_format(ds
, "set_dl_src("ETH_ADDR_FMT
")", ETH_ADDR_ARGS(eth
));
128 case OVS_ACTION_ATTR_SET_DL_DST
:
129 eth
= nl_attr_get_unspec(a
, ETH_ADDR_LEN
);
130 ds_put_format(ds
, "set_dl_dst("ETH_ADDR_FMT
")", ETH_ADDR_ARGS(eth
));
132 case OVS_ACTION_ATTR_SET_NW_SRC
:
133 ip
= nl_attr_get_be32(a
);
134 ds_put_format(ds
, "set_nw_src("IP_FMT
")", IP_ARGS(&ip
));
136 case OVS_ACTION_ATTR_SET_NW_DST
:
137 ip
= nl_attr_get_be32(a
);
138 ds_put_format(ds
, "set_nw_dst("IP_FMT
")", IP_ARGS(&ip
));
140 case OVS_ACTION_ATTR_SET_NW_TOS
:
141 ds_put_format(ds
, "set_nw_tos(%"PRIu8
")", nl_attr_get_u8(a
));
143 case OVS_ACTION_ATTR_SET_TP_SRC
:
144 ds_put_format(ds
, "set_tp_src(%"PRIu16
")", ntohs(nl_attr_get_be16(a
)));
146 case OVS_ACTION_ATTR_SET_TP_DST
:
147 ds_put_format(ds
, "set_tp_dst(%"PRIu16
")", ntohs(nl_attr_get_be16(a
)));
149 case OVS_ACTION_ATTR_SET_PRIORITY
:
150 ds_put_format(ds
, "set_priority(%#"PRIx32
")", nl_attr_get_u32(a
));
152 case OVS_ACTION_ATTR_POP_PRIORITY
:
153 ds_put_cstr(ds
, "pop_priority");
156 format_generic_odp_action(ds
, a
);
162 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
166 const struct nlattr
*a
;
169 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
171 ds_put_char(ds
, ',');
173 format_odp_action(ds
, a
);
176 if (left
== actions_len
) {
177 ds_put_cstr(ds
, "<empty>");
179 ds_put_format(ds
, ",***%u leftover bytes***", left
);
182 ds_put_cstr(ds
, "drop");
186 /* Returns the correct length of the payload for a flow key attribute of the
187 * specified 'type', or -1 if 'type' is unknown. */
189 odp_flow_key_attr_len(uint16_t type
)
191 if (type
> OVS_KEY_ATTR_MAX
) {
195 switch ((enum ovs_key_type
) type
) {
196 case OVS_KEY_ATTR_TUN_ID
: return 8;
197 case OVS_KEY_ATTR_IN_PORT
: return 4;
198 case OVS_KEY_ATTR_ETHERNET
: return sizeof(struct ovs_key_ethernet
);
199 case OVS_KEY_ATTR_8021Q
: return sizeof(struct ovs_key_8021q
);
200 case OVS_KEY_ATTR_ETHERTYPE
: return 2;
201 case OVS_KEY_ATTR_IPV4
: return sizeof(struct ovs_key_ipv4
);
202 case OVS_KEY_ATTR_IPV6
: return sizeof(struct ovs_key_ipv6
);
203 case OVS_KEY_ATTR_TCP
: return sizeof(struct ovs_key_tcp
);
204 case OVS_KEY_ATTR_UDP
: return sizeof(struct ovs_key_udp
);
205 case OVS_KEY_ATTR_ICMP
: return sizeof(struct ovs_key_icmp
);
206 case OVS_KEY_ATTR_ICMPV6
: return sizeof(struct ovs_key_icmpv6
);
207 case OVS_KEY_ATTR_ARP
: return sizeof(struct ovs_key_arp
);
208 case OVS_KEY_ATTR_ND
: return sizeof(struct ovs_key_nd
);
210 case OVS_KEY_ATTR_UNSPEC
:
211 case __OVS_KEY_ATTR_MAX
:
220 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
222 size_t len
= nl_attr_get_size(a
);
224 ds_put_format(ds
, "key%"PRId16
, nl_attr_type(a
));
226 const uint8_t *unspec
;
229 unspec
= nl_attr_get(a
);
230 for (i
= 0; i
< len
; i
++) {
231 ds_put_char(ds
, i
? ' ': '(');
232 ds_put_format(ds
, "%02x", unspec
[i
]);
234 ds_put_char(ds
, ')');
239 format_odp_key_attr(const struct nlattr
*a
, struct ds
*ds
)
241 const struct ovs_key_ethernet
*eth_key
;
242 const struct ovs_key_8021q
*q_key
;
243 const struct ovs_key_ipv4
*ipv4_key
;
244 const struct ovs_key_ipv6
*ipv6_key
;
245 const struct ovs_key_tcp
*tcp_key
;
246 const struct ovs_key_udp
*udp_key
;
247 const struct ovs_key_icmp
*icmp_key
;
248 const struct ovs_key_icmpv6
*icmpv6_key
;
249 const struct ovs_key_arp
*arp_key
;
250 const struct ovs_key_nd
*nd_key
;
252 if (nl_attr_get_size(a
) != odp_flow_key_attr_len(nl_attr_type(a
))) {
253 ds_put_format(ds
, "bad length %zu, expected %d for: ",
255 odp_flow_key_attr_len(nl_attr_type(a
)));
256 format_generic_odp_key(a
, ds
);
260 switch (nl_attr_type(a
)) {
261 case OVS_KEY_ATTR_TUN_ID
:
262 ds_put_format(ds
, "tun_id(%#"PRIx64
")", ntohll(nl_attr_get_be64(a
)));
265 case OVS_KEY_ATTR_IN_PORT
:
266 ds_put_format(ds
, "in_port(%"PRIu32
")", nl_attr_get_u32(a
));
269 case OVS_KEY_ATTR_ETHERNET
:
270 eth_key
= nl_attr_get(a
);
271 ds_put_format(ds
, "eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
272 ETH_ADDR_ARGS(eth_key
->eth_src
),
273 ETH_ADDR_ARGS(eth_key
->eth_dst
));
276 case OVS_KEY_ATTR_8021Q
:
277 q_key
= nl_attr_get(a
);
278 ds_put_cstr(ds
, "vlan(");
279 if (q_key
->q_tpid
!= htons(ETH_TYPE_VLAN
)) {
280 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(q_key
->q_tpid
));
282 ds_put_format(ds
, "vid=%"PRIu16
",pcp=%d)",
283 vlan_tci_to_vid(q_key
->q_tci
),
284 vlan_tci_to_pcp(q_key
->q_tci
));
287 case OVS_KEY_ATTR_ETHERTYPE
:
288 ds_put_format(ds
, "eth_type(0x%04"PRIx16
")",
289 ntohs(nl_attr_get_be16(a
)));
292 case OVS_KEY_ATTR_IPV4
:
293 ipv4_key
= nl_attr_get(a
);
294 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
","
295 "proto=%"PRId8
",tos=%"PRIu8
")",
296 IP_ARGS(&ipv4_key
->ipv4_src
),
297 IP_ARGS(&ipv4_key
->ipv4_dst
),
298 ipv4_key
->ipv4_proto
, ipv4_key
->ipv4_tos
);
301 case OVS_KEY_ATTR_IPV6
: {
302 char src_str
[INET6_ADDRSTRLEN
];
303 char dst_str
[INET6_ADDRSTRLEN
];
305 ipv6_key
= nl_attr_get(a
);
306 inet_ntop(AF_INET6
, ipv6_key
->ipv6_src
, src_str
, sizeof src_str
);
307 inet_ntop(AF_INET6
, ipv6_key
->ipv6_dst
, dst_str
, sizeof dst_str
);
309 ds_put_format(ds
, "ipv6(src=%s,dst=%s,proto=%"PRId8
",tos=%"PRIu8
")",
310 src_str
, dst_str
, ipv6_key
->ipv6_proto
,
315 case OVS_KEY_ATTR_TCP
:
316 tcp_key
= nl_attr_get(a
);
317 ds_put_format(ds
, "tcp(src=%"PRIu16
",dst=%"PRIu16
")",
318 ntohs(tcp_key
->tcp_src
), ntohs(tcp_key
->tcp_dst
));
321 case OVS_KEY_ATTR_UDP
:
322 udp_key
= nl_attr_get(a
);
323 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
")",
324 ntohs(udp_key
->udp_src
), ntohs(udp_key
->udp_dst
));
327 case OVS_KEY_ATTR_ICMP
:
328 icmp_key
= nl_attr_get(a
);
329 ds_put_format(ds
, "icmp(type=%"PRIu8
",code=%"PRIu8
")",
330 icmp_key
->icmp_type
, icmp_key
->icmp_code
);
333 case OVS_KEY_ATTR_ICMPV6
:
334 icmpv6_key
= nl_attr_get(a
);
335 ds_put_format(ds
, "icmpv6(type=%"PRIu8
",code=%"PRIu8
")",
336 icmpv6_key
->icmpv6_type
, icmpv6_key
->icmpv6_code
);
339 case OVS_KEY_ATTR_ARP
:
340 arp_key
= nl_attr_get(a
);
341 ds_put_format(ds
, "arp(sip="IP_FMT
",tip="IP_FMT
",op=%"PRIu16
","
342 "sha="ETH_ADDR_FMT
",tha="ETH_ADDR_FMT
")",
343 IP_ARGS(&arp_key
->arp_sip
), IP_ARGS(&arp_key
->arp_tip
),
344 ntohs(arp_key
->arp_op
), ETH_ADDR_ARGS(arp_key
->arp_sha
),
345 ETH_ADDR_ARGS(arp_key
->arp_tha
));
348 case OVS_KEY_ATTR_ND
: {
349 char target
[INET6_ADDRSTRLEN
];
351 nd_key
= nl_attr_get(a
);
352 inet_ntop(AF_INET6
, nd_key
->nd_target
, target
, sizeof target
);
354 ds_put_format(ds
, "nd(target=%s", target
);
355 if (!eth_addr_is_zero(nd_key
->nd_sll
)) {
356 ds_put_format(ds
, ",sll="ETH_ADDR_FMT
,
357 ETH_ADDR_ARGS(nd_key
->nd_sll
));
359 if (!eth_addr_is_zero(nd_key
->nd_tll
)) {
360 ds_put_format(ds
, ",tll="ETH_ADDR_FMT
,
361 ETH_ADDR_ARGS(nd_key
->nd_tll
));
363 ds_put_char(ds
, ')');
368 format_generic_odp_key(a
, ds
);
373 /* Appends to 'ds' a string representation of the 'key_len' bytes of
374 * OVS_KEY_ATTR_* attributes in 'key'. */
376 odp_flow_key_format(const struct nlattr
*key
, size_t key_len
, struct ds
*ds
)
379 const struct nlattr
*a
;
382 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
384 ds_put_char(ds
, ',');
386 format_odp_key_attr(a
, ds
);
389 if (left
== key_len
) {
390 ds_put_cstr(ds
, "<empty>");
392 ds_put_format(ds
, ",***%u leftover bytes***", left
);
395 ds_put_cstr(ds
, "<empty>");
400 put_nd_key(int n
, const char *nd_target_s
,
401 const uint8_t *nd_sll
, const uint8_t *nd_tll
, struct ofpbuf
*key
)
403 struct ovs_key_nd nd_key
;
405 memset(&nd_key
, 0, sizeof nd_key
);
406 if (inet_pton(AF_INET6
, nd_target_s
, nd_key
.nd_target
) != 1) {
410 memcpy(nd_key
.nd_sll
, nd_sll
, ETH_ADDR_LEN
);
413 memcpy(nd_key
.nd_tll
, nd_tll
, ETH_ADDR_LEN
);
415 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ND
, &nd_key
, sizeof nd_key
);
420 parse_odp_key_attr(const char *s
, struct ofpbuf
*key
)
422 /* Many of the sscanf calls in this function use oversized destination
423 * fields because some sscanf() implementations truncate the range of %i
424 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
425 * value of 0x7fff. The other alternatives are to allow only a single
426 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
429 * The tun_id parser has to use an alternative approach because there is no
430 * type larger than 64 bits. */
436 if (sscanf(s
, "tun_id(%31[x0123456789abcdefABCDEF])%n",
437 tun_id_s
, &n
) > 0 && n
> 0) {
438 uint64_t tun_id
= strtoull(tun_id_s
, NULL
, 0);
439 nl_msg_put_be64(key
, OVS_KEY_ATTR_TUN_ID
, htonll(tun_id
));
445 unsigned long long int in_port
;
448 if (sscanf(s
, "in_port(%lli)%n", &in_port
, &n
) > 0 && n
> 0) {
449 nl_msg_put_u32(key
, OVS_KEY_ATTR_IN_PORT
, in_port
);
455 struct ovs_key_ethernet eth_key
;
459 "eth(src="ETH_ADDR_SCAN_FMT
",dst="ETH_ADDR_SCAN_FMT
")%n",
460 ETH_ADDR_SCAN_ARGS(eth_key
.eth_src
),
461 ETH_ADDR_SCAN_ARGS(eth_key
.eth_dst
), &n
) > 0 && n
> 0) {
462 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ETHERNET
,
463 ð_key
, sizeof eth_key
);
469 uint16_t tpid
= ETH_TYPE_VLAN
;
474 if ((sscanf(s
, "vlan(vid=%"SCNi16
",pcp=%i)%n",
475 &vid
, &pcp
, &n
) > 0 && n
> 0) ||
476 (sscanf(s
, "vlan(tpid=%"SCNi16
",vid=%"SCNi16
",pcp=%i)%n",
477 &tpid
, &vid
, &pcp
, &n
) > 0 && n
> 0)) {
478 struct ovs_key_8021q q_key
;
480 q_key
.q_tpid
= htons(tpid
);
481 q_key
.q_tci
= htons((vid
<< VLAN_VID_SHIFT
) |
482 (pcp
<< VLAN_PCP_SHIFT
));
483 nl_msg_put_unspec(key
, OVS_KEY_ATTR_8021Q
, &q_key
, sizeof q_key
);
492 if (sscanf(s
, "eth_type(%i)%n", ð_type
, &n
) > 0 && n
> 0) {
493 nl_msg_put_be16(key
, OVS_KEY_ATTR_ETHERTYPE
, htons(eth_type
));
505 if (sscanf(s
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
","
506 "proto=%i,tos=%i)%n",
507 IP_SCAN_ARGS(&ipv4_src
),
508 IP_SCAN_ARGS(&ipv4_dst
), &ipv4_proto
, &ipv4_tos
, &n
) > 0
510 struct ovs_key_ipv4 ipv4_key
;
512 memset(&ipv4_key
, 0, sizeof ipv4_key
);
513 ipv4_key
.ipv4_src
= ipv4_src
;
514 ipv4_key
.ipv4_dst
= ipv4_dst
;
515 ipv4_key
.ipv4_proto
= ipv4_proto
;
516 ipv4_key
.ipv4_tos
= ipv4_tos
;
517 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV4
,
518 &ipv4_key
, sizeof ipv4_key
);
524 char ipv6_src_s
[IPV6_SCAN_LEN
+ 1];
525 char ipv6_dst_s
[IPV6_SCAN_LEN
+ 1];
530 if (sscanf(s
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
","
531 "proto=%i,tos=%i)%n",
532 ipv6_src_s
, ipv6_dst_s
,
533 &ipv6_proto
, &ipv6_tos
, &n
) > 0 && n
> 0) {
534 struct ovs_key_ipv6 ipv6_key
;
536 memset(&ipv6_key
, 0, sizeof ipv6_key
);
537 if (inet_pton(AF_INET6
, ipv6_src_s
, &ipv6_key
.ipv6_src
) != 1 ||
538 inet_pton(AF_INET6
, ipv6_dst_s
, &ipv6_key
.ipv6_dst
) != 1) {
541 ipv6_key
.ipv6_proto
= ipv6_proto
;
542 ipv6_key
.ipv6_tos
= ipv6_tos
;
543 nl_msg_put_unspec(key
, OVS_KEY_ATTR_IPV6
,
544 &ipv6_key
, sizeof ipv6_key
);
554 if (sscanf(s
, "tcp(src=%i,dst=%i)%n",&tcp_src
, &tcp_dst
, &n
) > 0
556 struct ovs_key_tcp tcp_key
;
558 tcp_key
.tcp_src
= htons(tcp_src
);
559 tcp_key
.tcp_dst
= htons(tcp_dst
);
560 nl_msg_put_unspec(key
, OVS_KEY_ATTR_TCP
, &tcp_key
, sizeof tcp_key
);
570 if (sscanf(s
, "udp(src=%i,dst=%i)%n", &udp_src
, &udp_dst
, &n
) > 0
572 struct ovs_key_udp udp_key
;
574 udp_key
.udp_src
= htons(udp_src
);
575 udp_key
.udp_dst
= htons(udp_dst
);
576 nl_msg_put_unspec(key
, OVS_KEY_ATTR_UDP
, &udp_key
, sizeof udp_key
);
586 if (sscanf(s
, "icmp(type=%i,code=%i)%n",
587 &icmp_type
, &icmp_code
, &n
) > 0
589 struct ovs_key_icmp icmp_key
;
591 icmp_key
.icmp_type
= icmp_type
;
592 icmp_key
.icmp_code
= icmp_code
;
593 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMP
,
594 &icmp_key
, sizeof icmp_key
);
600 struct ovs_key_icmpv6 icmpv6_key
;
603 if (sscanf(s
, "icmpv6(type=%"SCNi8
",code=%"SCNi8
")%n",
604 &icmpv6_key
.icmpv6_type
, &icmpv6_key
.icmpv6_code
,&n
) > 0
606 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ICMPV6
,
607 &icmpv6_key
, sizeof icmpv6_key
);
616 uint8_t arp_sha
[ETH_ADDR_LEN
];
617 uint8_t arp_tha
[ETH_ADDR_LEN
];
620 if (sscanf(s
, "arp(sip="IP_SCAN_FMT
",tip="IP_SCAN_FMT
","
621 "op=%i,sha="ETH_ADDR_SCAN_FMT
",tha="ETH_ADDR_SCAN_FMT
")%n",
622 IP_SCAN_ARGS(&arp_sip
),
623 IP_SCAN_ARGS(&arp_tip
),
625 ETH_ADDR_SCAN_ARGS(arp_sha
),
626 ETH_ADDR_SCAN_ARGS(arp_tha
), &n
) > 0 && n
> 0) {
627 struct ovs_key_arp arp_key
;
629 memset(&arp_key
, 0, sizeof arp_key
);
630 arp_key
.arp_sip
= arp_sip
;
631 arp_key
.arp_tip
= arp_tip
;
632 arp_key
.arp_op
= htons(arp_op
);
633 memcpy(arp_key
.arp_sha
, arp_sha
, ETH_ADDR_LEN
);
634 memcpy(arp_key
.arp_tha
, arp_tha
, ETH_ADDR_LEN
);
635 nl_msg_put_unspec(key
, OVS_KEY_ATTR_ARP
, &arp_key
, sizeof arp_key
);
641 char nd_target_s
[IPV6_SCAN_LEN
+ 1];
642 uint8_t nd_sll
[ETH_ADDR_LEN
];
643 uint8_t nd_tll
[ETH_ADDR_LEN
];
646 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
")%n",
647 nd_target_s
, &n
) > 0 && n
> 0) {
648 return put_nd_key(n
, nd_target_s
, NULL
, NULL
, key
);
650 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
")%n",
651 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
), &n
) > 0
653 return put_nd_key(n
, nd_target_s
, nd_sll
, NULL
, key
);
655 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",tll="ETH_ADDR_SCAN_FMT
")%n",
656 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
658 return put_nd_key(n
, nd_target_s
, NULL
, nd_tll
, key
);
660 if (sscanf(s
, "nd(target="IPV6_SCAN_FMT
",sll="ETH_ADDR_SCAN_FMT
","
661 "tll="ETH_ADDR_SCAN_FMT
")%n",
662 nd_target_s
, ETH_ADDR_SCAN_ARGS(nd_sll
),
663 ETH_ADDR_SCAN_ARGS(nd_tll
), &n
) > 0
665 return put_nd_key(n
, nd_target_s
, nd_sll
, nd_tll
, key
);
672 /* Parses the string representation of a datapath flow key, in the
673 * format output by odp_flow_key_format(). Returns 0 if successful,
674 * otherwise a positive errno value. On success, the flow key is
675 * appended to 'key' as a series of Netlink attributes. On failure, no
676 * data is appended to 'key'. Either way, 'key''s data might be
679 * On success, the attributes appended to 'key' are individually syntactically
680 * valid, but they may not be valid as a sequence. 'key' might, for example,
681 * be missing an "in_port" key, have duplicated keys, or have keys in the wrong
682 * order. odp_flow_key_to_flow() will detect those errors. */
684 odp_flow_key_from_string(const char *s
, struct ofpbuf
*key
)
686 const size_t old_size
= key
->size
;
690 s
+= strspn(s
, ", \t\r\n");
695 retval
= parse_odp_key_attr(s
, key
);
697 key
->size
= old_size
;
706 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
708 odp_flow_key_from_flow(struct ofpbuf
*buf
, const struct flow
*flow
)
710 struct ovs_key_ethernet
*eth_key
;
712 if (flow
->tun_id
!= htonll(0)) {
713 nl_msg_put_be64(buf
, OVS_KEY_ATTR_TUN_ID
, flow
->tun_id
);
716 nl_msg_put_u32(buf
, OVS_KEY_ATTR_IN_PORT
,
717 ofp_port_to_odp_port(flow
->in_port
));
719 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
721 memcpy(eth_key
->eth_src
, flow
->dl_src
, ETH_ADDR_LEN
);
722 memcpy(eth_key
->eth_dst
, flow
->dl_dst
, ETH_ADDR_LEN
);
724 if (flow
->vlan_tci
!= htons(0)) {
725 struct ovs_key_8021q
*q_key
;
727 q_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_8021Q
,
729 q_key
->q_tpid
= htons(ETH_TYPE_VLAN
);
730 q_key
->q_tci
= flow
->vlan_tci
& ~htons(VLAN_CFI
);
733 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
737 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, flow
->dl_type
);
739 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
740 struct ovs_key_ipv4
*ipv4_key
;
742 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
744 memset(ipv4_key
, 0, sizeof *ipv4_key
);
745 ipv4_key
->ipv4_src
= flow
->nw_src
;
746 ipv4_key
->ipv4_dst
= flow
->nw_dst
;
747 ipv4_key
->ipv4_proto
= flow
->nw_proto
;
748 ipv4_key
->ipv4_tos
= flow
->nw_tos
;
749 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
750 struct ovs_key_ipv6
*ipv6_key
;
752 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
754 memset(ipv6_key
, 0, sizeof *ipv6_key
);
755 memcpy(ipv6_key
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6_key
->ipv6_src
);
756 memcpy(ipv6_key
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6_key
->ipv6_dst
);
757 ipv6_key
->ipv6_proto
= flow
->nw_proto
;
758 ipv6_key
->ipv6_tos
= flow
->nw_tos
;
759 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
760 struct ovs_key_arp
*arp_key
;
762 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
764 memset(arp_key
, 0, sizeof *arp_key
);
765 arp_key
->arp_sip
= flow
->nw_src
;
766 arp_key
->arp_tip
= flow
->nw_dst
;
767 arp_key
->arp_op
= htons(flow
->nw_proto
);
768 memcpy(arp_key
->arp_sha
, flow
->arp_sha
, ETH_ADDR_LEN
);
769 memcpy(arp_key
->arp_tha
, flow
->arp_tha
, ETH_ADDR_LEN
);
772 if (flow
->dl_type
== htons(ETH_TYPE_IP
)
773 || flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
775 if (flow
->nw_proto
== IPPROTO_TCP
) {
776 struct ovs_key_tcp
*tcp_key
;
778 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
780 tcp_key
->tcp_src
= flow
->tp_src
;
781 tcp_key
->tcp_dst
= flow
->tp_dst
;
782 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
783 struct ovs_key_udp
*udp_key
;
785 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
787 udp_key
->udp_src
= flow
->tp_src
;
788 udp_key
->udp_dst
= flow
->tp_dst
;
789 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
790 && flow
->nw_proto
== IPPROTO_ICMP
) {
791 struct ovs_key_icmp
*icmp_key
;
793 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
795 icmp_key
->icmp_type
= ntohs(flow
->tp_src
);
796 icmp_key
->icmp_code
= ntohs(flow
->tp_dst
);
797 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
798 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
799 struct ovs_key_icmpv6
*icmpv6_key
;
801 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
803 icmpv6_key
->icmpv6_type
= ntohs(flow
->tp_src
);
804 icmpv6_key
->icmpv6_code
= ntohs(flow
->tp_dst
);
806 if (icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_SOLICIT
807 || icmpv6_key
->icmpv6_type
== ND_NEIGHBOR_ADVERT
) {
808 struct ovs_key_nd
*nd_key
;
810 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
812 memcpy(nd_key
->nd_target
, &flow
->nd_target
,
813 sizeof nd_key
->nd_target
);
814 memcpy(nd_key
->nd_sll
, flow
->arp_sha
, ETH_ADDR_LEN
);
815 memcpy(nd_key
->nd_tll
, flow
->arp_tha
, ETH_ADDR_LEN
);
821 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
822 * structure in 'flow'. Returns 0 if successful, otherwise EINVAL. */
824 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
827 const struct nlattr
*nla
;
828 enum ovs_key_type prev_type
;
831 memset(flow
, 0, sizeof *flow
);
832 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
834 prev_type
= OVS_KEY_ATTR_UNSPEC
;
835 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
836 const struct ovs_key_ethernet
*eth_key
;
837 const struct ovs_key_8021q
*q_key
;
838 const struct ovs_key_ipv4
*ipv4_key
;
839 const struct ovs_key_ipv6
*ipv6_key
;
840 const struct ovs_key_tcp
*tcp_key
;
841 const struct ovs_key_udp
*udp_key
;
842 const struct ovs_key_icmp
*icmp_key
;
843 const struct ovs_key_icmpv6
*icmpv6_key
;
844 const struct ovs_key_arp
*arp_key
;
845 const struct ovs_key_nd
*nd_key
;
847 uint16_t type
= nl_attr_type(nla
);
848 int len
= odp_flow_key_attr_len(type
);
850 if (nl_attr_get_size(nla
) != len
&& len
!= -1) {
854 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
855 switch (TRANSITION(prev_type
, type
)) {
856 case TRANSITION(OVS_KEY_ATTR_UNSPEC
, OVS_KEY_ATTR_TUN_ID
):
857 flow
->tun_id
= nl_attr_get_be64(nla
);
860 case TRANSITION(OVS_KEY_ATTR_UNSPEC
, OVS_KEY_ATTR_IN_PORT
):
861 case TRANSITION(OVS_KEY_ATTR_TUN_ID
, OVS_KEY_ATTR_IN_PORT
):
862 if (nl_attr_get_u32(nla
) >= UINT16_MAX
) {
865 flow
->in_port
= odp_port_to_ofp_port(nl_attr_get_u32(nla
));
868 case TRANSITION(OVS_KEY_ATTR_IN_PORT
, OVS_KEY_ATTR_ETHERNET
):
869 eth_key
= nl_attr_get(nla
);
870 memcpy(flow
->dl_src
, eth_key
->eth_src
, ETH_ADDR_LEN
);
871 memcpy(flow
->dl_dst
, eth_key
->eth_dst
, ETH_ADDR_LEN
);
874 case TRANSITION(OVS_KEY_ATTR_ETHERNET
, OVS_KEY_ATTR_8021Q
):
875 q_key
= nl_attr_get(nla
);
876 if (q_key
->q_tpid
!= htons(ETH_TYPE_VLAN
)) {
877 /* Only standard 0x8100 VLANs currently supported. */
880 if (q_key
->q_tci
& htons(VLAN_CFI
)) {
883 flow
->vlan_tci
= q_key
->q_tci
| htons(VLAN_CFI
);
886 case TRANSITION(OVS_KEY_ATTR_8021Q
, OVS_KEY_ATTR_ETHERTYPE
):
887 case TRANSITION(OVS_KEY_ATTR_ETHERNET
, OVS_KEY_ATTR_ETHERTYPE
):
888 flow
->dl_type
= nl_attr_get_be16(nla
);
889 if (ntohs(flow
->dl_type
) < 1536) {
894 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE
, OVS_KEY_ATTR_IPV4
):
895 if (flow
->dl_type
!= htons(ETH_TYPE_IP
)) {
898 ipv4_key
= nl_attr_get(nla
);
899 flow
->nw_src
= ipv4_key
->ipv4_src
;
900 flow
->nw_dst
= ipv4_key
->ipv4_dst
;
901 flow
->nw_proto
= ipv4_key
->ipv4_proto
;
902 flow
->nw_tos
= ipv4_key
->ipv4_tos
;
903 if (flow
->nw_tos
& IP_ECN_MASK
) {
908 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE
, OVS_KEY_ATTR_IPV6
):
909 if (flow
->dl_type
!= htons(ETH_TYPE_IPV6
)) {
912 ipv6_key
= nl_attr_get(nla
);
913 memcpy(&flow
->ipv6_src
, ipv6_key
->ipv6_src
, sizeof flow
->ipv6_src
);
914 memcpy(&flow
->ipv6_dst
, ipv6_key
->ipv6_dst
, sizeof flow
->ipv6_dst
);
915 flow
->nw_proto
= ipv6_key
->ipv6_proto
;
916 flow
->nw_tos
= ipv6_key
->ipv6_tos
;
917 if (flow
->nw_tos
& IP_ECN_MASK
) {
922 case TRANSITION(OVS_KEY_ATTR_IPV4
, OVS_KEY_ATTR_TCP
):
923 case TRANSITION(OVS_KEY_ATTR_IPV6
, OVS_KEY_ATTR_TCP
):
924 if (flow
->nw_proto
!= IPPROTO_TCP
) {
927 tcp_key
= nl_attr_get(nla
);
928 flow
->tp_src
= tcp_key
->tcp_src
;
929 flow
->tp_dst
= tcp_key
->tcp_dst
;
932 case TRANSITION(OVS_KEY_ATTR_IPV4
, OVS_KEY_ATTR_UDP
):
933 case TRANSITION(OVS_KEY_ATTR_IPV6
, OVS_KEY_ATTR_UDP
):
934 if (flow
->nw_proto
!= IPPROTO_UDP
) {
937 udp_key
= nl_attr_get(nla
);
938 flow
->tp_src
= udp_key
->udp_src
;
939 flow
->tp_dst
= udp_key
->udp_dst
;
942 case TRANSITION(OVS_KEY_ATTR_IPV4
, OVS_KEY_ATTR_ICMP
):
943 if (flow
->nw_proto
!= IPPROTO_ICMP
) {
946 icmp_key
= nl_attr_get(nla
);
947 flow
->tp_src
= htons(icmp_key
->icmp_type
);
948 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
951 case TRANSITION(OVS_KEY_ATTR_IPV6
, OVS_KEY_ATTR_ICMPV6
):
952 if (flow
->nw_proto
!= IPPROTO_ICMPV6
) {
955 icmpv6_key
= nl_attr_get(nla
);
956 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
957 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
960 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE
, OVS_KEY_ATTR_ARP
):
961 if (flow
->dl_type
!= htons(ETH_TYPE_ARP
)) {
964 arp_key
= nl_attr_get(nla
);
965 flow
->nw_src
= arp_key
->arp_sip
;
966 flow
->nw_dst
= arp_key
->arp_tip
;
967 if (arp_key
->arp_op
& htons(0xff00)) {
970 flow
->nw_proto
= ntohs(arp_key
->arp_op
);
971 memcpy(flow
->arp_sha
, arp_key
->arp_sha
, ETH_ADDR_LEN
);
972 memcpy(flow
->arp_tha
, arp_key
->arp_tha
, ETH_ADDR_LEN
);
975 case TRANSITION(OVS_KEY_ATTR_ICMPV6
, OVS_KEY_ATTR_ND
):
976 if (flow
->tp_src
!= htons(ND_NEIGHBOR_SOLICIT
)
977 && flow
->tp_src
!= htons(ND_NEIGHBOR_ADVERT
)) {
980 nd_key
= nl_attr_get(nla
);
981 memcpy(&flow
->nd_target
, nd_key
->nd_target
, sizeof flow
->nd_target
);
982 memcpy(flow
->arp_sha
, nd_key
->nd_sll
, ETH_ADDR_LEN
);
983 memcpy(flow
->arp_tha
, nd_key
->nd_tll
, ETH_ADDR_LEN
);
987 if (type
== OVS_KEY_ATTR_UNSPEC
988 || prev_type
== OVS_KEY_ATTR_UNSPEC
) {
1000 switch (prev_type
) {
1001 case OVS_KEY_ATTR_UNSPEC
:
1004 case OVS_KEY_ATTR_TUN_ID
:
1005 case OVS_KEY_ATTR_IN_PORT
:
1008 case OVS_KEY_ATTR_ETHERNET
:
1009 case OVS_KEY_ATTR_8021Q
:
1012 case OVS_KEY_ATTR_ETHERTYPE
:
1013 if (flow
->dl_type
== htons(ETH_TYPE_IP
)
1014 || flow
->dl_type
== htons(ETH_TYPE_IPV6
)
1015 || flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
1020 case OVS_KEY_ATTR_IPV4
:
1021 if (flow
->nw_proto
== IPPROTO_TCP
1022 || flow
->nw_proto
== IPPROTO_UDP
1023 || flow
->nw_proto
== IPPROTO_ICMP
) {
1028 case OVS_KEY_ATTR_IPV6
:
1029 if (flow
->nw_proto
== IPPROTO_TCP
1030 || flow
->nw_proto
== IPPROTO_UDP
1031 || flow
->nw_proto
== IPPROTO_ICMPV6
) {
1036 case OVS_KEY_ATTR_ICMPV6
:
1037 if (flow
->icmp_type
== htons(ND_NEIGHBOR_SOLICIT
)
1038 || flow
->icmp_type
== htons(ND_NEIGHBOR_ADVERT
)) {
1043 case OVS_KEY_ATTR_TCP
:
1044 case OVS_KEY_ATTR_UDP
:
1045 case OVS_KEY_ATTR_ICMP
:
1046 case OVS_KEY_ATTR_ARP
:
1047 case OVS_KEY_ATTR_ND
:
1050 case __OVS_KEY_ATTR_MAX
: