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_PUSH_VLAN: return 2;
53 case OVS_ACTION_ATTR_POP_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_PUSH_VLAN:
117 ds_put_format(ds, "push_vlan(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_POP_VLAN:
122 ds_put_format(ds, "pop_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 if (flow->in_port != OFPP_NONE) {
717 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
718 ofp_port_to_odp_port(flow->in_port));
721 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
723 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
724 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
726 if (flow->vlan_tci != htons(0)) {
727 struct ovs_key_8021q *q_key;
729 q_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_8021Q,
731 q_key->q_tpid = htons(ETH_TYPE_VLAN);
732 q_key->q_tci = flow->vlan_tci & ~htons(VLAN_CFI);
735 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
739 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
741 if (flow->dl_type == htons(ETH_TYPE_IP)) {
742 struct ovs_key_ipv4 *ipv4_key;
744 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
746 memset(ipv4_key, 0, sizeof *ipv4_key);
747 ipv4_key->ipv4_src = flow->nw_src;
748 ipv4_key->ipv4_dst = flow->nw_dst;
749 ipv4_key->ipv4_proto = flow->nw_proto;
750 ipv4_key->ipv4_tos = flow->nw_tos;
751 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
752 struct ovs_key_ipv6 *ipv6_key;
754 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
756 memset(ipv6_key, 0, sizeof *ipv6_key);
757 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
758 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
759 ipv6_key->ipv6_proto = flow->nw_proto;
760 ipv6_key->ipv6_tos = flow->nw_tos;
761 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
762 struct ovs_key_arp *arp_key;
764 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
766 memset(arp_key, 0, sizeof *arp_key);
767 arp_key->arp_sip = flow->nw_src;
768 arp_key->arp_tip = flow->nw_dst;
769 arp_key->arp_op = htons(flow->nw_proto);
770 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
771 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
774 if (flow->dl_type == htons(ETH_TYPE_IP)
775 || flow->dl_type == htons(ETH_TYPE_IPV6)) {
777 if (flow->nw_proto == IPPROTO_TCP) {
778 struct ovs_key_tcp *tcp_key;
780 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
782 tcp_key->tcp_src = flow->tp_src;
783 tcp_key->tcp_dst = flow->tp_dst;
784 } else if (flow->nw_proto == IPPROTO_UDP) {
785 struct ovs_key_udp *udp_key;
787 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
789 udp_key->udp_src = flow->tp_src;
790 udp_key->udp_dst = flow->tp_dst;
791 } else if (flow->dl_type == htons(ETH_TYPE_IP)
792 && flow->nw_proto == IPPROTO_ICMP) {
793 struct ovs_key_icmp *icmp_key;
795 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
797 icmp_key->icmp_type = ntohs(flow->tp_src);
798 icmp_key->icmp_code = ntohs(flow->tp_dst);
799 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
800 && flow->nw_proto == IPPROTO_ICMPV6) {
801 struct ovs_key_icmpv6 *icmpv6_key;
803 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
805 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
806 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
808 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
809 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
810 struct ovs_key_nd *nd_key;
812 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
814 memcpy(nd_key->nd_target, &flow->nd_target,
815 sizeof nd_key->nd_target);
816 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
817 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
823 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
824 * structure in 'flow'. Returns 0 if successful, otherwise EINVAL. */
826 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
829 const struct nlattr *nla;
830 enum ovs_key_type prev_type;
833 memset(flow, 0, sizeof *flow);
834 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
835 flow->in_port = OFPP_NONE;
837 prev_type = OVS_KEY_ATTR_UNSPEC;
838 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
839 const struct ovs_key_ethernet *eth_key;
840 const struct ovs_key_8021q *q_key;
841 const struct ovs_key_ipv4 *ipv4_key;
842 const struct ovs_key_ipv6 *ipv6_key;
843 const struct ovs_key_tcp *tcp_key;
844 const struct ovs_key_udp *udp_key;
845 const struct ovs_key_icmp *icmp_key;
846 const struct ovs_key_icmpv6 *icmpv6_key;
847 const struct ovs_key_arp *arp_key;
848 const struct ovs_key_nd *nd_key;
850 uint16_t type = nl_attr_type(nla);
851 int len = odp_flow_key_attr_len(type);
853 if (nl_attr_get_size(nla) != len && len != -1) {
857 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
858 switch (TRANSITION(prev_type, type)) {
859 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_TUN_ID):
860 flow->tun_id = nl_attr_get_be64(nla);
863 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_IN_PORT):
864 case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_IN_PORT):
865 if (nl_attr_get_u32(nla) >= UINT16_MAX) {
868 flow->in_port = odp_port_to_ofp_port(nl_attr_get_u32(nla));
871 case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_ETHERNET):
872 case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_ETHERNET):
873 case TRANSITION(OVS_KEY_ATTR_IN_PORT, OVS_KEY_ATTR_ETHERNET):
874 eth_key = nl_attr_get(nla);
875 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
876 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
879 case TRANSITION(OVS_KEY_ATTR_ETHERNET, OVS_KEY_ATTR_8021Q):
880 q_key = nl_attr_get(nla);
881 if (q_key->q_tpid != htons(ETH_TYPE_VLAN)) {
882 /* Only standard 0x8100 VLANs currently supported. */
885 if (q_key->q_tci & htons(VLAN_CFI)) {
888 flow->vlan_tci = q_key->q_tci | htons(VLAN_CFI);
891 case TRANSITION(OVS_KEY_ATTR_8021Q, OVS_KEY_ATTR_ETHERTYPE):
892 case TRANSITION(OVS_KEY_ATTR_ETHERNET, OVS_KEY_ATTR_ETHERTYPE):
893 flow->dl_type = nl_attr_get_be16(nla);
894 if (ntohs(flow->dl_type) < 1536) {
899 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_IPV4):
900 if (flow->dl_type != htons(ETH_TYPE_IP)) {
903 ipv4_key = nl_attr_get(nla);
904 flow->nw_src = ipv4_key->ipv4_src;
905 flow->nw_dst = ipv4_key->ipv4_dst;
906 flow->nw_proto = ipv4_key->ipv4_proto;
907 flow->nw_tos = ipv4_key->ipv4_tos;
908 if (flow->nw_tos & IP_ECN_MASK) {
913 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_IPV6):
914 if (flow->dl_type != htons(ETH_TYPE_IPV6)) {
917 ipv6_key = nl_attr_get(nla);
918 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
919 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
920 flow->nw_proto = ipv6_key->ipv6_proto;
921 flow->nw_tos = ipv6_key->ipv6_tos;
922 if (flow->nw_tos & IP_ECN_MASK) {
927 case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_TCP):
928 case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_TCP):
929 if (flow->nw_proto != IPPROTO_TCP) {
932 tcp_key = nl_attr_get(nla);
933 flow->tp_src = tcp_key->tcp_src;
934 flow->tp_dst = tcp_key->tcp_dst;
937 case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_UDP):
938 case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_UDP):
939 if (flow->nw_proto != IPPROTO_UDP) {
942 udp_key = nl_attr_get(nla);
943 flow->tp_src = udp_key->udp_src;
944 flow->tp_dst = udp_key->udp_dst;
947 case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_ICMP):
948 if (flow->nw_proto != IPPROTO_ICMP) {
951 icmp_key = nl_attr_get(nla);
952 flow->tp_src = htons(icmp_key->icmp_type);
953 flow->tp_dst = htons(icmp_key->icmp_code);
956 case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_ICMPV6):
957 if (flow->nw_proto != IPPROTO_ICMPV6) {
960 icmpv6_key = nl_attr_get(nla);
961 flow->tp_src = htons(icmpv6_key->icmpv6_type);
962 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
965 case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_ARP):
966 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
969 arp_key = nl_attr_get(nla);
970 flow->nw_src = arp_key->arp_sip;
971 flow->nw_dst = arp_key->arp_tip;
972 if (arp_key->arp_op & htons(0xff00)) {
975 flow->nw_proto = ntohs(arp_key->arp_op);
976 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
977 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
980 case TRANSITION(OVS_KEY_ATTR_ICMPV6, OVS_KEY_ATTR_ND):
981 if (flow->tp_src != htons(ND_NEIGHBOR_SOLICIT)
982 && flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
985 nd_key = nl_attr_get(nla);
986 memcpy(&flow->nd_target, nd_key->nd_target, sizeof flow->nd_target);
987 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
988 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
992 if (type == OVS_KEY_ATTR_UNSPEC
993 || prev_type == OVS_KEY_ATTR_UNSPEC) {
1005 switch (prev_type) {
1006 case OVS_KEY_ATTR_UNSPEC:
1009 case OVS_KEY_ATTR_TUN_ID:
1010 case OVS_KEY_ATTR_IN_PORT:
1013 case OVS_KEY_ATTR_ETHERNET:
1014 case OVS_KEY_ATTR_8021Q:
1017 case OVS_KEY_ATTR_ETHERTYPE:
1018 if (flow->dl_type == htons(ETH_TYPE_IP)
1019 || flow->dl_type == htons(ETH_TYPE_IPV6)
1020 || flow->dl_type == htons(ETH_TYPE_ARP)) {
1025 case OVS_KEY_ATTR_IPV4:
1026 if (flow->nw_proto == IPPROTO_TCP
1027 || flow->nw_proto == IPPROTO_UDP
1028 || flow->nw_proto == IPPROTO_ICMP) {
1033 case OVS_KEY_ATTR_IPV6:
1034 if (flow->nw_proto == IPPROTO_TCP
1035 || flow->nw_proto == IPPROTO_UDP
1036 || flow->nw_proto == IPPROTO_ICMPV6) {
1041 case OVS_KEY_ATTR_ICMPV6:
1042 if (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT)
1043 || flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)) {
1048 case OVS_KEY_ATTR_TCP:
1049 case OVS_KEY_ATTR_UDP:
1050 case OVS_KEY_ATTR_ICMP:
1051 case OVS_KEY_ATTR_ARP:
1052 case OVS_KEY_ATTR_ND:
1055 case __OVS_KEY_ATTR_MAX: