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"
37 odp_action_len(uint16_t type)
39 if (type > ODP_ACTION_ATTR_MAX) {
43 switch ((enum odp_action_type) type) {
44 case ODP_ACTION_ATTR_OUTPUT: return 4;
45 case ODP_ACTION_ATTR_USERSPACE: return 8;
46 case ODP_ACTION_ATTR_SET_DL_TCI: return 2;
47 case ODP_ACTION_ATTR_STRIP_VLAN: return 0;
48 case ODP_ACTION_ATTR_SET_DL_SRC: return ETH_ADDR_LEN;
49 case ODP_ACTION_ATTR_SET_DL_DST: return ETH_ADDR_LEN;
50 case ODP_ACTION_ATTR_SET_NW_SRC: return 4;
51 case ODP_ACTION_ATTR_SET_NW_DST: return 4;
52 case ODP_ACTION_ATTR_SET_NW_TOS: return 1;
53 case ODP_ACTION_ATTR_SET_TP_SRC: return 2;
54 case ODP_ACTION_ATTR_SET_TP_DST: return 2;
55 case ODP_ACTION_ATTR_SET_TUNNEL: return 8;
56 case ODP_ACTION_ATTR_SET_PRIORITY: return 4;
57 case ODP_ACTION_ATTR_POP_PRIORITY: return 0;
59 case ODP_ACTION_ATTR_UNSPEC:
60 case __ODP_ACTION_ATTR_MAX:
68 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
70 size_t len = nl_attr_get_size(a);
72 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
74 const uint8_t *unspec;
77 unspec = nl_attr_get(a);
78 for (i = 0; i < len; i++) {
79 ds_put_char(ds, i ? ' ': '(');
80 ds_put_format(ds, "%02x", unspec[i]);
87 format_odp_action(struct ds *ds, const struct nlattr *a)
92 if (nl_attr_get_size(a) != odp_action_len(nl_attr_type(a))) {
93 ds_put_format(ds, "bad length %zu, expected %d for: ",
94 nl_attr_get_size(a), odp_action_len(nl_attr_type(a)));
95 format_generic_odp_action(ds, a);
99 switch (nl_attr_type(a)) {
100 case ODP_ACTION_ATTR_OUTPUT:
101 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
103 case ODP_ACTION_ATTR_USERSPACE:
104 ds_put_format(ds, "userspace(%"PRIu64")", nl_attr_get_u64(a));
106 case ODP_ACTION_ATTR_SET_TUNNEL:
107 ds_put_format(ds, "set_tunnel(%#"PRIx64")",
108 ntohll(nl_attr_get_be64(a)));
110 case ODP_ACTION_ATTR_SET_DL_TCI:
111 ds_put_format(ds, "set_tci(vid=%"PRIu16",pcp=%d)",
112 vlan_tci_to_vid(nl_attr_get_be16(a)),
113 vlan_tci_to_pcp(nl_attr_get_be16(a)));
115 case ODP_ACTION_ATTR_STRIP_VLAN:
116 ds_put_format(ds, "strip_vlan");
118 case ODP_ACTION_ATTR_SET_DL_SRC:
119 eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
120 ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
122 case ODP_ACTION_ATTR_SET_DL_DST:
123 eth = nl_attr_get_unspec(a, ETH_ADDR_LEN);
124 ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")", ETH_ADDR_ARGS(eth));
126 case ODP_ACTION_ATTR_SET_NW_SRC:
127 ip = nl_attr_get_be32(a);
128 ds_put_format(ds, "set_nw_src("IP_FMT")", IP_ARGS(&ip));
130 case ODP_ACTION_ATTR_SET_NW_DST:
131 ip = nl_attr_get_be32(a);
132 ds_put_format(ds, "set_nw_dst("IP_FMT")", IP_ARGS(&ip));
134 case ODP_ACTION_ATTR_SET_NW_TOS:
135 ds_put_format(ds, "set_nw_tos(%"PRIu8")", nl_attr_get_u8(a));
137 case ODP_ACTION_ATTR_SET_TP_SRC:
138 ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
140 case ODP_ACTION_ATTR_SET_TP_DST:
141 ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(nl_attr_get_be16(a)));
143 case ODP_ACTION_ATTR_SET_PRIORITY:
144 ds_put_format(ds, "set_priority(%#"PRIx32")", nl_attr_get_u32(a));
146 case ODP_ACTION_ATTR_POP_PRIORITY:
147 ds_put_cstr(ds, "pop_priority");
150 format_generic_odp_action(ds, a);
156 format_odp_actions(struct ds *ds, const struct nlattr *actions,
160 const struct nlattr *a;
163 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
165 ds_put_char(ds, ',');
167 format_odp_action(ds, a);
170 if (left == actions_len) {
171 ds_put_cstr(ds, "<empty>");
173 ds_put_format(ds, ",***%u leftover bytes***", left);
176 ds_put_cstr(ds, "drop");
180 /* Returns the correct length of the payload for a flow key attribute of the
181 * specified 'type', or -1 if 'type' is unknown. */
183 odp_flow_key_attr_len(uint16_t type)
185 if (type > ODP_KEY_ATTR_MAX) {
189 switch ((enum odp_key_type) type) {
190 case ODP_KEY_ATTR_TUN_ID: return 8;
191 case ODP_KEY_ATTR_IN_PORT: return 4;
192 case ODP_KEY_ATTR_ETHERNET: return sizeof(struct odp_key_ethernet);
193 case ODP_KEY_ATTR_8021Q: return sizeof(struct odp_key_8021q);
194 case ODP_KEY_ATTR_ETHERTYPE: return 2;
195 case ODP_KEY_ATTR_IPV4: return sizeof(struct odp_key_ipv4);
196 case ODP_KEY_ATTR_IPV6: return sizeof(struct odp_key_ipv6);
197 case ODP_KEY_ATTR_TCP: return sizeof(struct odp_key_tcp);
198 case ODP_KEY_ATTR_UDP: return sizeof(struct odp_key_udp);
199 case ODP_KEY_ATTR_ICMP: return sizeof(struct odp_key_icmp);
200 case ODP_KEY_ATTR_ICMPV6: return sizeof(struct odp_key_icmpv6);
201 case ODP_KEY_ATTR_ARP: return sizeof(struct odp_key_arp);
202 case ODP_KEY_ATTR_ND: return sizeof(struct odp_key_nd);
204 case ODP_KEY_ATTR_UNSPEC:
205 case __ODP_KEY_ATTR_MAX:
214 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
216 size_t len = nl_attr_get_size(a);
218 ds_put_format(ds, "key%"PRId16, nl_attr_type(a));
220 const uint8_t *unspec;
223 unspec = nl_attr_get(a);
224 for (i = 0; i < len; i++) {
225 ds_put_char(ds, i ? ' ': '(');
226 ds_put_format(ds, "%02x", unspec[i]);
228 ds_put_char(ds, ')');
233 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
235 const struct odp_key_ethernet *eth_key;
236 const struct odp_key_8021q *q_key;
237 const struct odp_key_ipv4 *ipv4_key;
238 const struct odp_key_ipv6 *ipv6_key;
239 const struct odp_key_tcp *tcp_key;
240 const struct odp_key_udp *udp_key;
241 const struct odp_key_icmp *icmp_key;
242 const struct odp_key_icmpv6 *icmpv6_key;
243 const struct odp_key_arp *arp_key;
244 const struct odp_key_nd *nd_key;
246 if (nl_attr_get_size(a) != odp_flow_key_attr_len(nl_attr_type(a))) {
247 ds_put_format(ds, "bad length %zu, expected %d for: ",
249 odp_flow_key_attr_len(nl_attr_type(a)));
250 format_generic_odp_key(a, ds);
254 switch (nl_attr_type(a)) {
255 case ODP_KEY_ATTR_TUN_ID:
256 ds_put_format(ds, "tun_id(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
259 case ODP_KEY_ATTR_IN_PORT:
260 ds_put_format(ds, "in_port(%"PRIu32")", nl_attr_get_u32(a));
263 case ODP_KEY_ATTR_ETHERNET:
264 eth_key = nl_attr_get(a);
265 ds_put_format(ds, "eth(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
266 ETH_ADDR_ARGS(eth_key->eth_src),
267 ETH_ADDR_ARGS(eth_key->eth_dst));
270 case ODP_KEY_ATTR_8021Q:
271 q_key = nl_attr_get(a);
272 ds_put_cstr(ds, "vlan(");
273 if (q_key->q_tpid != htons(ETH_TYPE_VLAN)) {
274 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(q_key->q_tpid));
276 ds_put_format(ds, "vid=%"PRIu16",pcp=%d)",
277 vlan_tci_to_vid(q_key->q_tci),
278 vlan_tci_to_pcp(q_key->q_tci));
281 case ODP_KEY_ATTR_ETHERTYPE:
282 ds_put_format(ds, "eth_type(0x%04"PRIx16")",
283 ntohs(nl_attr_get_be16(a)));
286 case ODP_KEY_ATTR_IPV4:
287 ipv4_key = nl_attr_get(a);
288 ds_put_format(ds, "ipv4(src="IP_FMT",dst="IP_FMT","
289 "proto=%"PRId8",tos=%"PRIu8")",
290 IP_ARGS(&ipv4_key->ipv4_src),
291 IP_ARGS(&ipv4_key->ipv4_dst),
292 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos);
295 case ODP_KEY_ATTR_IPV6: {
296 char src_str[INET6_ADDRSTRLEN];
297 char dst_str[INET6_ADDRSTRLEN];
299 ipv6_key = nl_attr_get(a);
300 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
301 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
303 ds_put_format(ds, "ipv6(src=%s,dst=%s,proto=%"PRId8",tos=%"PRIu8")",
304 src_str, dst_str, ipv6_key->ipv6_proto,
309 case ODP_KEY_ATTR_TCP:
310 tcp_key = nl_attr_get(a);
311 ds_put_format(ds, "tcp(src=%"PRIu16",dst=%"PRIu16")",
312 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
315 case ODP_KEY_ATTR_UDP:
316 udp_key = nl_attr_get(a);
317 ds_put_format(ds, "udp(src=%"PRIu16",dst=%"PRIu16")",
318 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
321 case ODP_KEY_ATTR_ICMP:
322 icmp_key = nl_attr_get(a);
323 ds_put_format(ds, "icmp(type=%"PRIu8",code=%"PRIu8")",
324 icmp_key->icmp_type, icmp_key->icmp_code);
327 case ODP_KEY_ATTR_ICMPV6:
328 icmpv6_key = nl_attr_get(a);
329 ds_put_format(ds, "icmpv6(type=%"PRIu8",code=%"PRIu8")",
330 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
333 case ODP_KEY_ATTR_ARP:
334 arp_key = nl_attr_get(a);
335 ds_put_format(ds, "arp(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
336 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
337 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
338 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
339 ETH_ADDR_ARGS(arp_key->arp_tha));
342 case ODP_KEY_ATTR_ND: {
343 char target[INET6_ADDRSTRLEN];
345 nd_key = nl_attr_get(a);
346 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
348 ds_put_format(ds, "nd(target=%s", target);
349 if (!eth_addr_is_zero(nd_key->nd_sll)) {
350 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
351 ETH_ADDR_ARGS(nd_key->nd_sll));
353 if (!eth_addr_is_zero(nd_key->nd_tll)) {
354 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
355 ETH_ADDR_ARGS(nd_key->nd_tll));
357 ds_put_char(ds, ')');
362 format_generic_odp_key(a, ds);
367 /* Appends to 'ds' a string representation of the 'key_len' bytes of
368 * ODP_KEY_ATTR_* attributes in 'key'. */
370 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
373 const struct nlattr *a;
376 NL_ATTR_FOR_EACH (a, left, key, key_len) {
378 ds_put_char(ds, ',');
380 format_odp_key_attr(a, ds);
383 if (left == key_len) {
384 ds_put_cstr(ds, "<empty>");
386 ds_put_format(ds, ",***%u leftover bytes***", left);
389 ds_put_cstr(ds, "<empty>");
394 put_nd_key(int n, const char *nd_target_s,
395 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
397 struct odp_key_nd nd_key;
399 memset(&nd_key, 0, sizeof nd_key);
400 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
404 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
407 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
409 nl_msg_put_unspec(key, ODP_KEY_ATTR_ND, &nd_key, sizeof nd_key);
414 parse_odp_key_attr(const char *s, struct ofpbuf *key)
416 /* Many of the sscanf calls in this function use oversized destination
417 * fields because some sscanf() implementations truncate the range of %i
418 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
419 * value of 0x7fff. The other alternatives are to allow only a single
420 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
423 * The tun_id parser has to use an alternative approach because there is no
424 * type larger than 64 bits. */
430 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
431 tun_id_s, &n) > 0 && n > 0) {
432 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
433 nl_msg_put_be64(key, ODP_KEY_ATTR_TUN_ID, htonll(tun_id));
439 unsigned long long int in_port;
442 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
443 nl_msg_put_u32(key, ODP_KEY_ATTR_IN_PORT, in_port);
449 struct odp_key_ethernet eth_key;
453 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
454 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
455 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
456 nl_msg_put_unspec(key, ODP_KEY_ATTR_ETHERNET,
457 ð_key, sizeof eth_key);
463 uint16_t tpid = ETH_TYPE_VLAN;
468 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
469 &vid, &pcp, &n) > 0 && n > 0) ||
470 (sscanf(s, "vlan(tpid=%"SCNi16",vid=%"SCNi16",pcp=%i)%n",
471 &tpid, &vid, &pcp, &n) > 0 && n > 0)) {
472 struct odp_key_8021q q_key;
474 q_key.q_tpid = htons(tpid);
475 q_key.q_tci = htons((vid << VLAN_VID_SHIFT) |
476 (pcp << VLAN_PCP_SHIFT));
477 nl_msg_put_unspec(key, ODP_KEY_ATTR_8021Q, &q_key, sizeof q_key);
486 if (sscanf(s, "eth_type(%"SCNi16")%n", ð_type, &n) > 0 && n > 0) {
487 nl_msg_put_be16(key, ODP_KEY_ATTR_ETHERTYPE, htons(eth_type));
499 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
500 "proto=%i,tos=%i)%n",
501 IP_SCAN_ARGS(&ipv4_src),
502 IP_SCAN_ARGS(&ipv4_dst), &ipv4_proto, &ipv4_tos, &n) > 0
504 struct odp_key_ipv4 ipv4_key;
506 memset(&ipv4_key, 0, sizeof ipv4_key);
507 ipv4_key.ipv4_src = ipv4_src;
508 ipv4_key.ipv4_dst = ipv4_dst;
509 ipv4_key.ipv4_proto = ipv4_proto;
510 ipv4_key.ipv4_tos = ipv4_tos;
511 nl_msg_put_unspec(key, ODP_KEY_ATTR_IPV4,
512 &ipv4_key, sizeof ipv4_key);
518 char ipv6_src_s[IPV6_SCAN_LEN + 1];
519 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
524 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
525 "proto=%i,tos=%i)%n",
526 ipv6_src_s, ipv6_dst_s,
527 &ipv6_proto, &ipv6_tos, &n) > 0 && n > 0) {
528 struct odp_key_ipv6 ipv6_key;
530 memset(&ipv6_key, 0, sizeof ipv6_key);
531 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
532 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
535 ipv6_key.ipv6_proto = ipv6_proto;
536 ipv6_key.ipv6_tos = ipv6_tos;
537 nl_msg_put_unspec(key, ODP_KEY_ATTR_IPV6,
538 &ipv6_key, sizeof ipv6_key);
548 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
550 struct odp_key_tcp tcp_key;
552 tcp_key.tcp_src = htons(tcp_src);
553 tcp_key.tcp_dst = htons(tcp_dst);
554 nl_msg_put_unspec(key, ODP_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
564 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
566 struct odp_key_udp udp_key;
568 udp_key.udp_src = htons(udp_src);
569 udp_key.udp_dst = htons(udp_dst);
570 nl_msg_put_unspec(key, ODP_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
580 if (sscanf(s, "icmp(type=%i,code=%i)%n",
581 &icmp_type, &icmp_code, &n) > 0
583 struct odp_key_icmp icmp_key;
585 icmp_key.icmp_type = icmp_type;
586 icmp_key.icmp_code = icmp_code;
587 nl_msg_put_unspec(key, ODP_KEY_ATTR_ICMP,
588 &icmp_key, sizeof icmp_key);
594 struct odp_key_icmpv6 icmpv6_key;
597 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
598 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
600 nl_msg_put_unspec(key, ODP_KEY_ATTR_ICMPV6,
601 &icmpv6_key, sizeof icmpv6_key);
610 uint8_t arp_sha[ETH_ADDR_LEN];
611 uint8_t arp_tha[ETH_ADDR_LEN];
614 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
615 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
616 IP_SCAN_ARGS(&arp_sip),
617 IP_SCAN_ARGS(&arp_tip),
619 ETH_ADDR_SCAN_ARGS(arp_sha),
620 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
621 struct odp_key_arp arp_key;
623 memset(&arp_key, 0, sizeof arp_key);
624 arp_key.arp_sip = arp_sip;
625 arp_key.arp_tip = arp_tip;
626 arp_key.arp_op = htons(arp_op);
627 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
628 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
629 nl_msg_put_unspec(key, ODP_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
635 char nd_target_s[IPV6_SCAN_LEN + 1];
636 uint8_t nd_sll[ETH_ADDR_LEN];
637 uint8_t nd_tll[ETH_ADDR_LEN];
640 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
641 nd_target_s, &n) > 0 && n > 0) {
642 return put_nd_key(n, nd_target_s, NULL, NULL, key);
644 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
645 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
647 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
649 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
650 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
652 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
654 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
655 "tll="ETH_ADDR_SCAN_FMT")%n",
656 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
657 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
659 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
666 /* Parses the string representation of an ODP flow key, in the format output by
667 * odp_flow_key_format(). Returns 0 if successful, otherwise a positive errno
668 * value. On success, the flow key is appended to 'key' as a series of Netlink
669 * attributes. On failure, no data is appended to 'key'. Either way, 'key''s
670 * data might be reallocated.
672 * On success, the attributes appended to 'key' are individually syntactically
673 * valid, but they may not be valid as a sequence. 'key' might, for example,
674 * be missing an "in_port" key, have duplicated keys, or have keys in the wrong
675 * order. odp_flow_key_to_flow() will detect those errors. */
677 odp_flow_key_from_string(const char *s, struct ofpbuf *key)
679 const size_t old_size = key->size;
683 s += strspn(s, ", \t\r\n");
688 retval = parse_odp_key_attr(s, key);
690 key->size = old_size;
699 /* Appends a representation of 'flow' as ODP_KEY_ATTR_* attributes to 'buf'. */
701 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
703 struct odp_key_ethernet *eth_key;
705 if (flow->tun_id != htonll(0)) {
706 nl_msg_put_be64(buf, ODP_KEY_ATTR_TUN_ID, flow->tun_id);
709 nl_msg_put_u32(buf, ODP_KEY_ATTR_IN_PORT,
710 ofp_port_to_odp_port(flow->in_port));
712 eth_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ETHERNET,
714 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
715 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
717 if (flow->vlan_tci != htons(0)) {
718 struct odp_key_8021q *q_key;
720 q_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_8021Q,
722 q_key->q_tpid = htons(ETH_TYPE_VLAN);
723 q_key->q_tci = flow->vlan_tci & ~htons(VLAN_CFI);
726 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
730 nl_msg_put_be16(buf, ODP_KEY_ATTR_ETHERTYPE, flow->dl_type);
732 if (flow->dl_type == htons(ETH_TYPE_IP)) {
733 struct odp_key_ipv4 *ipv4_key;
735 ipv4_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_IPV4,
737 memset(ipv4_key, 0, sizeof *ipv4_key);
738 ipv4_key->ipv4_src = flow->nw_src;
739 ipv4_key->ipv4_dst = flow->nw_dst;
740 ipv4_key->ipv4_proto = flow->nw_proto;
741 ipv4_key->ipv4_tos = flow->nw_tos;
742 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
743 struct odp_key_ipv6 *ipv6_key;
745 ipv6_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_IPV6,
747 memset(ipv6_key, 0, sizeof *ipv6_key);
748 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
749 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
750 ipv6_key->ipv6_proto = flow->nw_proto;
751 ipv6_key->ipv6_tos = flow->nw_tos;
752 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
753 struct odp_key_arp *arp_key;
755 arp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ARP,
757 memset(arp_key, 0, sizeof *arp_key);
758 arp_key->arp_sip = flow->nw_src;
759 arp_key->arp_tip = flow->nw_dst;
760 arp_key->arp_op = htons(flow->nw_proto);
761 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
762 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
765 if (flow->dl_type == htons(ETH_TYPE_IP)
766 || flow->dl_type == htons(ETH_TYPE_IPV6)) {
768 if (flow->nw_proto == IPPROTO_TCP) {
769 struct odp_key_tcp *tcp_key;
771 tcp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_TCP,
773 tcp_key->tcp_src = flow->tp_src;
774 tcp_key->tcp_dst = flow->tp_dst;
775 } else if (flow->nw_proto == IPPROTO_UDP) {
776 struct odp_key_udp *udp_key;
778 udp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_UDP,
780 udp_key->udp_src = flow->tp_src;
781 udp_key->udp_dst = flow->tp_dst;
782 } else if (flow->dl_type == htons(ETH_TYPE_IP)
783 && flow->nw_proto == IPPROTO_ICMP) {
784 struct odp_key_icmp *icmp_key;
786 icmp_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ICMP,
788 icmp_key->icmp_type = ntohs(flow->tp_src);
789 icmp_key->icmp_code = ntohs(flow->tp_dst);
790 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
791 && flow->nw_proto == IPPROTO_ICMPV6) {
792 struct odp_key_icmpv6 *icmpv6_key;
794 icmpv6_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ICMPV6,
796 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
797 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
799 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
800 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
801 struct odp_key_nd *nd_key;
803 nd_key = nl_msg_put_unspec_uninit(buf, ODP_KEY_ATTR_ND,
805 memcpy(nd_key->nd_target, &flow->nd_target,
806 sizeof nd_key->nd_target);
807 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
808 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
814 /* Converts the 'key_len' bytes of ODP_KEY_ATTR_* attributes in 'key' to a flow
815 * structure in 'flow'. Returns 0 if successful, otherwise EINVAL. */
817 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
820 const struct nlattr *nla;
821 enum odp_key_type prev_type;
824 memset(flow, 0, sizeof *flow);
825 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
827 prev_type = ODP_KEY_ATTR_UNSPEC;
828 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
829 const struct odp_key_ethernet *eth_key;
830 const struct odp_key_8021q *q_key;
831 const struct odp_key_ipv4 *ipv4_key;
832 const struct odp_key_ipv6 *ipv6_key;
833 const struct odp_key_tcp *tcp_key;
834 const struct odp_key_udp *udp_key;
835 const struct odp_key_icmp *icmp_key;
836 const struct odp_key_icmpv6 *icmpv6_key;
837 const struct odp_key_arp *arp_key;
838 const struct odp_key_nd *nd_key;
840 uint16_t type = nl_attr_type(nla);
841 int len = odp_flow_key_attr_len(type);
843 if (nl_attr_get_size(nla) != len && len != -1) {
847 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
848 switch (TRANSITION(prev_type, type)) {
849 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID):
850 flow->tun_id = nl_attr_get_be64(nla);
853 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT):
854 case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT):
855 if (nl_attr_get_u32(nla) >= UINT16_MAX) {
858 flow->in_port = odp_port_to_ofp_port(nl_attr_get_u32(nla));
861 case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET):
862 eth_key = nl_attr_get(nla);
863 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
864 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
867 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q):
868 q_key = nl_attr_get(nla);
869 if (q_key->q_tpid != htons(ETH_TYPE_VLAN)) {
870 /* Only standard 0x8100 VLANs currently supported. */
873 if (q_key->q_tci & htons(VLAN_CFI)) {
876 flow->vlan_tci = q_key->q_tci | htons(VLAN_CFI);
879 case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE):
880 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE):
881 flow->dl_type = nl_attr_get_be16(nla);
882 if (ntohs(flow->dl_type) < 1536) {
887 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4):
888 if (flow->dl_type != htons(ETH_TYPE_IP)) {
891 ipv4_key = nl_attr_get(nla);
892 flow->nw_src = ipv4_key->ipv4_src;
893 flow->nw_dst = ipv4_key->ipv4_dst;
894 flow->nw_proto = ipv4_key->ipv4_proto;
895 flow->nw_tos = ipv4_key->ipv4_tos;
896 if (flow->nw_tos & IP_ECN_MASK) {
901 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV6):
902 if (flow->dl_type != htons(ETH_TYPE_IPV6)) {
905 ipv6_key = nl_attr_get(nla);
906 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
907 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
908 flow->nw_proto = ipv6_key->ipv6_proto;
909 flow->nw_tos = ipv6_key->ipv6_tos;
910 if (flow->nw_tos & IP_ECN_MASK) {
915 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP):
916 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_TCP):
917 if (flow->nw_proto != IPPROTO_TCP) {
920 tcp_key = nl_attr_get(nla);
921 flow->tp_src = tcp_key->tcp_src;
922 flow->tp_dst = tcp_key->tcp_dst;
925 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP):
926 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_UDP):
927 if (flow->nw_proto != IPPROTO_UDP) {
930 udp_key = nl_attr_get(nla);
931 flow->tp_src = udp_key->udp_src;
932 flow->tp_dst = udp_key->udp_dst;
935 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP):
936 if (flow->nw_proto != IPPROTO_ICMP) {
939 icmp_key = nl_attr_get(nla);
940 flow->tp_src = htons(icmp_key->icmp_type);
941 flow->tp_dst = htons(icmp_key->icmp_code);
944 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_ICMPV6):
945 if (flow->nw_proto != IPPROTO_ICMPV6) {
948 icmpv6_key = nl_attr_get(nla);
949 flow->tp_src = htons(icmpv6_key->icmpv6_type);
950 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
953 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP):
954 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
957 arp_key = nl_attr_get(nla);
958 flow->nw_src = arp_key->arp_sip;
959 flow->nw_dst = arp_key->arp_tip;
960 if (arp_key->arp_op & htons(0xff00)) {
963 flow->nw_proto = ntohs(arp_key->arp_op);
964 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
965 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
968 case TRANSITION(ODP_KEY_ATTR_ICMPV6, ODP_KEY_ATTR_ND):
969 if (flow->tp_src != htons(ND_NEIGHBOR_SOLICIT)
970 && flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
973 nd_key = nl_attr_get(nla);
974 memcpy(&flow->nd_target, nd_key->nd_target, sizeof flow->nd_target);
975 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
976 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
980 if (type == ODP_KEY_ATTR_UNSPEC
981 || prev_type == ODP_KEY_ATTR_UNSPEC) {
994 case ODP_KEY_ATTR_UNSPEC:
997 case ODP_KEY_ATTR_TUN_ID:
998 case ODP_KEY_ATTR_IN_PORT:
1001 case ODP_KEY_ATTR_ETHERNET:
1002 case ODP_KEY_ATTR_8021Q:
1005 case ODP_KEY_ATTR_ETHERTYPE:
1006 if (flow->dl_type == htons(ETH_TYPE_IP)
1007 || flow->dl_type == htons(ETH_TYPE_IPV6)
1008 || flow->dl_type == htons(ETH_TYPE_ARP)) {
1013 case ODP_KEY_ATTR_IPV4:
1014 if (flow->nw_proto == IPPROTO_TCP
1015 || flow->nw_proto == IPPROTO_UDP
1016 || flow->nw_proto == IPPROTO_ICMP) {
1021 case ODP_KEY_ATTR_IPV6:
1022 if (flow->nw_proto == IPPROTO_TCP
1023 || flow->nw_proto == IPPROTO_UDP
1024 || flow->nw_proto == IPPROTO_ICMPV6) {
1029 case ODP_KEY_ATTR_ICMPV6:
1030 if (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT)
1031 || flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)) {
1036 case ODP_KEY_ATTR_TCP:
1037 case ODP_KEY_ATTR_UDP:
1038 case ODP_KEY_ATTR_ICMP:
1039 case ODP_KEY_ATTR_ARP:
1040 case ODP_KEY_ATTR_ND:
1043 case __ODP_KEY_ATTR_MAX: