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/in.h>
23 #include <netinet/icmp6.h>
26 #include "byte-order.h"
28 #include "dynamic-string.h"
32 #include "openvswitch/tunnel.h"
39 VLOG_DEFINE_THIS_MODULE(odp_util);
41 static int parse_odp_key_attr(const char *, const struct shash *port_names,
44 /* The interface between userspace and kernel uses an "OVS_*" prefix.
45 * Since this is fairly non-specific for the OVS userspace components,
46 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
47 * interactions with the datapath.
50 static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);
52 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
55 * - For an action whose argument has a fixed length, returned that
56 * nonnegative length in bytes.
58 * - For an action with a variable-length argument, returns -2.
60 * - For an invalid 'type', returns -1. */
62 odp_action_len(uint16_t type)
64 if (type > OVS_ACTION_ATTR_MAX) {
68 switch ((enum ovs_action_attr) type) {
69 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
70 case OVS_ACTION_ATTR_USERSPACE: return -2;
71 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
72 case OVS_ACTION_ATTR_POP_VLAN: return 0;
73 case OVS_ACTION_ATTR_SET: return -2;
74 case OVS_ACTION_ATTR_SAMPLE: return -2;
76 case OVS_ACTION_ATTR_UNSPEC:
77 case __OVS_ACTION_ATTR_MAX:
85 ovs_key_attr_to_string(enum ovs_key_attr attr)
87 static char unknown_attr[3 + INT_STRLEN(unsigned int) + 1];
90 case OVS_KEY_ATTR_UNSPEC: return "unspec";
91 case OVS_KEY_ATTR_ENCAP: return "encap";
92 case OVS_KEY_ATTR_PRIORITY: return "priority";
93 case OVS_KEY_ATTR_IN_PORT: return "in_port";
94 case OVS_KEY_ATTR_ETHERNET: return "eth";
95 case OVS_KEY_ATTR_VLAN: return "vlan";
96 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
97 case OVS_KEY_ATTR_IPV4: return "ipv4";
98 case OVS_KEY_ATTR_IPV6: return "ipv6";
99 case OVS_KEY_ATTR_TCP: return "tcp";
100 case OVS_KEY_ATTR_UDP: return "udp";
101 case OVS_KEY_ATTR_ICMP: return "icmp";
102 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
103 case OVS_KEY_ATTR_ARP: return "arp";
104 case OVS_KEY_ATTR_ND: return "nd";
105 case OVS_KEY_ATTR_TUN_ID: return "tun_id";
107 case __OVS_KEY_ATTR_MAX:
109 snprintf(unknown_attr, sizeof unknown_attr, "key%u",
110 (unsigned int) attr);
116 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
118 size_t len = nl_attr_get_size(a);
120 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
122 const uint8_t *unspec;
125 unspec = nl_attr_get(a);
126 for (i = 0; i < len; i++) {
127 ds_put_char(ds, i ? ' ': '(');
128 ds_put_format(ds, "%02x", unspec[i]);
130 ds_put_char(ds, ')');
135 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
137 static const struct nl_policy ovs_sample_policy[] = {
138 [OVS_SAMPLE_ATTR_PROBABILITY] = { .type = NL_A_U32 },
139 [OVS_SAMPLE_ATTR_ACTIONS] = { .type = NL_A_NESTED }
141 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
143 const struct nlattr *nla_acts;
146 ds_put_cstr(ds, "sample");
148 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
149 ds_put_cstr(ds, "(error)");
153 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
156 ds_put_format(ds, "(sample=%.1f%%,", percentage);
158 ds_put_cstr(ds, "actions(");
159 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
160 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
161 format_odp_actions(ds, nla_acts, len);
162 ds_put_format(ds, "))");
166 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
168 static const struct nl_policy ovs_userspace_policy[] = {
169 [OVS_USERSPACE_ATTR_PID] = { .type = NL_A_U32 },
170 [OVS_USERSPACE_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
172 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
174 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
175 ds_put_cstr(ds, "userspace(error)");
179 ds_put_format(ds, "userspace(pid=%"PRIu32,
180 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
182 if (a[OVS_USERSPACE_ATTR_USERDATA]) {
183 uint64_t userdata = nl_attr_get_u64(a[OVS_USERSPACE_ATTR_USERDATA]);
184 struct user_action_cookie cookie;
186 memcpy(&cookie, &userdata, sizeof cookie);
188 if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) {
189 ds_put_format(ds, ",controller,length=%"PRIu32, cookie.data);
190 } else if (cookie.type == USER_ACTION_COOKIE_SFLOW) {
191 ds_put_format(ds, ",sFlow,n_output=%"PRIu8","
192 "vid=%"PRIu16",pcp=%"PRIu8",ifindex=%"PRIu32,
193 cookie.n_output, vlan_tci_to_vid(cookie.vlan_tci),
194 vlan_tci_to_pcp(cookie.vlan_tci), cookie.data);
196 ds_put_format(ds, ",userdata=0x%"PRIx64, userdata);
200 ds_put_char(ds, ')');
204 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
206 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
207 vlan_tci_to_vid(vlan_tci),
208 vlan_tci_to_pcp(vlan_tci));
209 if (!(vlan_tci & htons(VLAN_CFI))) {
210 ds_put_cstr(ds, ",cfi=0");
215 format_odp_action(struct ds *ds, const struct nlattr *a)
218 enum ovs_action_attr type = nl_attr_type(a);
219 const struct ovs_action_push_vlan *vlan;
221 expected_len = odp_action_len(nl_attr_type(a));
222 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
223 ds_put_format(ds, "bad length %zu, expected %d for: ",
224 nl_attr_get_size(a), expected_len);
225 format_generic_odp_action(ds, a);
230 case OVS_ACTION_ATTR_OUTPUT:
231 ds_put_format(ds, "%"PRIu16, nl_attr_get_u32(a));
233 case OVS_ACTION_ATTR_USERSPACE:
234 format_odp_userspace_action(ds, a);
236 case OVS_ACTION_ATTR_SET:
237 ds_put_cstr(ds, "set(");
238 format_odp_key_attr(nl_attr_get(a), ds);
239 ds_put_cstr(ds, ")");
241 case OVS_ACTION_ATTR_PUSH_VLAN:
242 vlan = nl_attr_get(a);
243 ds_put_cstr(ds, "push_vlan(");
244 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
245 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
247 format_vlan_tci(ds, vlan->vlan_tci);
248 ds_put_char(ds, ')');
250 case OVS_ACTION_ATTR_POP_VLAN:
251 ds_put_cstr(ds, "pop_vlan");
253 case OVS_ACTION_ATTR_SAMPLE:
254 format_odp_sample_action(ds, a);
256 case OVS_ACTION_ATTR_UNSPEC:
257 case __OVS_ACTION_ATTR_MAX:
259 format_generic_odp_action(ds, a);
265 format_odp_actions(struct ds *ds, const struct nlattr *actions,
269 const struct nlattr *a;
272 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
274 ds_put_char(ds, ',');
276 format_odp_action(ds, a);
279 if (left == actions_len) {
280 ds_put_cstr(ds, "<empty>");
282 ds_put_format(ds, ",***%u leftover bytes***", left);
285 ds_put_cstr(ds, "drop");
289 /* Returns the correct length of the payload for a flow key attribute of the
290 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
291 * is variable length. */
293 odp_flow_key_attr_len(uint16_t type)
295 if (type > OVS_KEY_ATTR_MAX) {
299 switch ((enum ovs_key_attr) type) {
300 case OVS_KEY_ATTR_ENCAP: return -2;
301 case OVS_KEY_ATTR_PRIORITY: return 4;
302 case OVS_KEY_ATTR_TUN_ID: return 8;
303 case OVS_KEY_ATTR_IN_PORT: return 4;
304 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
305 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
306 case OVS_KEY_ATTR_ETHERTYPE: return 2;
307 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
308 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
309 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
310 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
311 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
312 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
313 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
314 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
316 case OVS_KEY_ATTR_UNSPEC:
317 case __OVS_KEY_ATTR_MAX:
325 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
327 size_t len = nl_attr_get_size(a);
329 const uint8_t *unspec;
332 unspec = nl_attr_get(a);
333 for (i = 0; i < len; i++) {
334 ds_put_char(ds, i ? ' ': '(');
335 ds_put_format(ds, "%02x", unspec[i]);
337 ds_put_char(ds, ')');
342 ovs_frag_type_to_string(enum ovs_frag_type type)
345 case OVS_FRAG_TYPE_NONE:
347 case OVS_FRAG_TYPE_FIRST:
349 case OVS_FRAG_TYPE_LATER:
351 case __OVS_FRAG_TYPE_MAX:
358 format_odp_key_attr(const struct nlattr *a, struct ds *ds)
360 const struct ovs_key_ethernet *eth_key;
361 const struct ovs_key_ipv4 *ipv4_key;
362 const struct ovs_key_ipv6 *ipv6_key;
363 const struct ovs_key_tcp *tcp_key;
364 const struct ovs_key_udp *udp_key;
365 const struct ovs_key_icmp *icmp_key;
366 const struct ovs_key_icmpv6 *icmpv6_key;
367 const struct ovs_key_arp *arp_key;
368 const struct ovs_key_nd *nd_key;
369 enum ovs_key_attr attr = nl_attr_type(a);
372 ds_put_cstr(ds, ovs_key_attr_to_string(attr));
373 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
374 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
375 ds_put_format(ds, "(bad length %zu, expected %d)",
377 odp_flow_key_attr_len(nl_attr_type(a)));
378 format_generic_odp_key(a, ds);
383 case OVS_KEY_ATTR_ENCAP:
384 ds_put_cstr(ds, "(");
385 if (nl_attr_get_size(a)) {
386 odp_flow_key_format(nl_attr_get(a), nl_attr_get_size(a), ds);
388 ds_put_char(ds, ')');
391 case OVS_KEY_ATTR_PRIORITY:
392 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
395 case OVS_KEY_ATTR_TUN_ID:
396 ds_put_format(ds, "(%#"PRIx64")", ntohll(nl_attr_get_be64(a)));
399 case OVS_KEY_ATTR_IN_PORT:
400 ds_put_format(ds, "(%"PRIu32")", nl_attr_get_u32(a));
403 case OVS_KEY_ATTR_ETHERNET:
404 eth_key = nl_attr_get(a);
405 ds_put_format(ds, "(src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT")",
406 ETH_ADDR_ARGS(eth_key->eth_src),
407 ETH_ADDR_ARGS(eth_key->eth_dst));
410 case OVS_KEY_ATTR_VLAN:
411 ds_put_char(ds, '(');
412 format_vlan_tci(ds, nl_attr_get_be16(a));
413 ds_put_char(ds, ')');
416 case OVS_KEY_ATTR_ETHERTYPE:
417 ds_put_format(ds, "(0x%04"PRIx16")",
418 ntohs(nl_attr_get_be16(a)));
421 case OVS_KEY_ATTR_IPV4:
422 ipv4_key = nl_attr_get(a);
423 ds_put_format(ds, "(src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
424 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s)",
425 IP_ARGS(&ipv4_key->ipv4_src),
426 IP_ARGS(&ipv4_key->ipv4_dst),
427 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
429 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
432 case OVS_KEY_ATTR_IPV6: {
433 char src_str[INET6_ADDRSTRLEN];
434 char dst_str[INET6_ADDRSTRLEN];
436 ipv6_key = nl_attr_get(a);
437 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
438 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
440 ds_put_format(ds, "(src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
441 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s)",
442 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
443 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
444 ipv6_key->ipv6_hlimit,
445 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
449 case OVS_KEY_ATTR_TCP:
450 tcp_key = nl_attr_get(a);
451 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
452 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
455 case OVS_KEY_ATTR_UDP:
456 udp_key = nl_attr_get(a);
457 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
458 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
461 case OVS_KEY_ATTR_ICMP:
462 icmp_key = nl_attr_get(a);
463 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
464 icmp_key->icmp_type, icmp_key->icmp_code);
467 case OVS_KEY_ATTR_ICMPV6:
468 icmpv6_key = nl_attr_get(a);
469 ds_put_format(ds, "(type=%"PRIu8",code=%"PRIu8")",
470 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
473 case OVS_KEY_ATTR_ARP:
474 arp_key = nl_attr_get(a);
475 ds_put_format(ds, "(sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
476 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT")",
477 IP_ARGS(&arp_key->arp_sip), IP_ARGS(&arp_key->arp_tip),
478 ntohs(arp_key->arp_op), ETH_ADDR_ARGS(arp_key->arp_sha),
479 ETH_ADDR_ARGS(arp_key->arp_tha));
482 case OVS_KEY_ATTR_ND: {
483 char target[INET6_ADDRSTRLEN];
485 nd_key = nl_attr_get(a);
486 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
488 ds_put_format(ds, "(target=%s", target);
489 if (!eth_addr_is_zero(nd_key->nd_sll)) {
490 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
491 ETH_ADDR_ARGS(nd_key->nd_sll));
493 if (!eth_addr_is_zero(nd_key->nd_tll)) {
494 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
495 ETH_ADDR_ARGS(nd_key->nd_tll));
497 ds_put_char(ds, ')');
501 case OVS_KEY_ATTR_UNSPEC:
502 case __OVS_KEY_ATTR_MAX:
504 format_generic_odp_key(a, ds);
509 /* Appends to 'ds' a string representation of the 'key_len' bytes of
510 * OVS_KEY_ATTR_* attributes in 'key'. */
512 odp_flow_key_format(const struct nlattr *key, size_t key_len, struct ds *ds)
515 const struct nlattr *a;
518 NL_ATTR_FOR_EACH (a, left, key, key_len) {
520 ds_put_char(ds, ',');
522 format_odp_key_attr(a, ds);
525 if (left == key_len) {
526 ds_put_cstr(ds, "<empty>");
528 ds_put_format(ds, ",***%u leftover bytes***", left);
531 ds_put_cstr(ds, "<empty>");
536 put_nd_key(int n, const char *nd_target_s,
537 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *key)
539 struct ovs_key_nd nd_key;
541 memset(&nd_key, 0, sizeof nd_key);
542 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
546 memcpy(nd_key.nd_sll, nd_sll, ETH_ADDR_LEN);
549 memcpy(nd_key.nd_tll, nd_tll, ETH_ADDR_LEN);
551 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, &nd_key, sizeof nd_key);
556 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
558 if (!strcasecmp(s, "no")) {
559 *type = OVS_FRAG_TYPE_NONE;
560 } else if (!strcasecmp(s, "first")) {
561 *type = OVS_FRAG_TYPE_FIRST;
562 } else if (!strcasecmp(s, "later")) {
563 *type = OVS_FRAG_TYPE_LATER;
571 parse_odp_key_attr(const char *s, const struct shash *port_names,
574 /* Many of the sscanf calls in this function use oversized destination
575 * fields because some sscanf() implementations truncate the range of %i
576 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
577 * value of 0x7fff. The other alternatives are to allow only a single
578 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
581 * The tun_id parser has to use an alternative approach because there is no
582 * type larger than 64 bits. */
585 unsigned long long int priority;
588 if (sscanf(s, "priority(%lli)%n", &priority, &n) > 0 && n > 0) {
589 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
598 if (sscanf(s, "tun_id(%31[x0123456789abcdefABCDEF])%n",
599 tun_id_s, &n) > 0 && n > 0) {
600 uint64_t tun_id = strtoull(tun_id_s, NULL, 0);
601 nl_msg_put_be64(key, OVS_KEY_ATTR_TUN_ID, htonll(tun_id));
607 unsigned long long int in_port;
610 if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
611 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
616 if (port_names && !strncmp(s, "in_port(", 8)) {
618 const struct shash_node *node;
622 name_len = strcspn(s, ")");
623 node = shash_find_len(port_names, name, name_len);
625 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, (uintptr_t) node->data);
626 return 8 + name_len + 1;
631 struct ovs_key_ethernet eth_key;
635 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
636 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
637 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
638 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
639 ð_key, sizeof eth_key);
650 if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n", &vid, &pcp, &n) > 0
652 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
653 htons((vid << VLAN_VID_SHIFT) |
654 (pcp << VLAN_PCP_SHIFT) |
657 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
658 &vid, &pcp, &cfi, &n) > 0
660 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
661 htons((vid << VLAN_VID_SHIFT) |
662 (pcp << VLAN_PCP_SHIFT) |
663 (cfi ? VLAN_CFI : 0)));
672 if (sscanf(s, "eth_type(%i)%n", ð_type, &n) > 0 && n > 0) {
673 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
685 enum ovs_frag_type ipv4_frag;
688 if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
689 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
690 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
691 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
693 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
694 struct ovs_key_ipv4 ipv4_key;
696 ipv4_key.ipv4_src = ipv4_src;
697 ipv4_key.ipv4_dst = ipv4_dst;
698 ipv4_key.ipv4_proto = ipv4_proto;
699 ipv4_key.ipv4_tos = ipv4_tos;
700 ipv4_key.ipv4_ttl = ipv4_ttl;
701 ipv4_key.ipv4_frag = ipv4_frag;
702 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
703 &ipv4_key, sizeof ipv4_key);
709 char ipv6_src_s[IPV6_SCAN_LEN + 1];
710 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
716 enum ovs_frag_type ipv6_frag;
719 if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
720 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
721 ipv6_src_s, ipv6_dst_s, &ipv6_label,
722 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
724 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
725 struct ovs_key_ipv6 ipv6_key;
727 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
728 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
731 ipv6_key.ipv6_label = htonl(ipv6_label);
732 ipv6_key.ipv6_proto = ipv6_proto;
733 ipv6_key.ipv6_tclass = ipv6_tclass;
734 ipv6_key.ipv6_hlimit = ipv6_hlimit;
735 ipv6_key.ipv6_frag = ipv6_frag;
736 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
737 &ipv6_key, sizeof ipv6_key);
747 if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
749 struct ovs_key_tcp tcp_key;
751 tcp_key.tcp_src = htons(tcp_src);
752 tcp_key.tcp_dst = htons(tcp_dst);
753 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
763 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
765 struct ovs_key_udp udp_key;
767 udp_key.udp_src = htons(udp_src);
768 udp_key.udp_dst = htons(udp_dst);
769 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
779 if (sscanf(s, "icmp(type=%i,code=%i)%n",
780 &icmp_type, &icmp_code, &n) > 0
782 struct ovs_key_icmp icmp_key;
784 icmp_key.icmp_type = icmp_type;
785 icmp_key.icmp_code = icmp_code;
786 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
787 &icmp_key, sizeof icmp_key);
793 struct ovs_key_icmpv6 icmpv6_key;
796 if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
797 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
799 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
800 &icmpv6_key, sizeof icmpv6_key);
809 uint8_t arp_sha[ETH_ADDR_LEN];
810 uint8_t arp_tha[ETH_ADDR_LEN];
813 if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
814 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
815 IP_SCAN_ARGS(&arp_sip),
816 IP_SCAN_ARGS(&arp_tip),
818 ETH_ADDR_SCAN_ARGS(arp_sha),
819 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
820 struct ovs_key_arp arp_key;
822 memset(&arp_key, 0, sizeof arp_key);
823 arp_key.arp_sip = arp_sip;
824 arp_key.arp_tip = arp_tip;
825 arp_key.arp_op = htons(arp_op);
826 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
827 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
828 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
834 char nd_target_s[IPV6_SCAN_LEN + 1];
835 uint8_t nd_sll[ETH_ADDR_LEN];
836 uint8_t nd_tll[ETH_ADDR_LEN];
839 if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
840 nd_target_s, &n) > 0 && n > 0) {
841 return put_nd_key(n, nd_target_s, NULL, NULL, key);
843 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
844 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
846 return put_nd_key(n, nd_target_s, nd_sll, NULL, key);
848 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
849 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
851 return put_nd_key(n, nd_target_s, NULL, nd_tll, key);
853 if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
854 "tll="ETH_ADDR_SCAN_FMT")%n",
855 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
856 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
858 return put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
862 if (!strncmp(s, "encap(", 6)) {
863 const char *start = s;
866 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
872 s += strspn(s, ", \t\r\n");
875 } else if (*s == ')') {
879 retval = parse_odp_key_attr(s, key);
887 nl_msg_end_nested(key, encap);
895 /* Parses the string representation of a datapath flow key, in the
896 * format output by odp_flow_key_format(). Returns 0 if successful,
897 * otherwise a positive errno value. On success, the flow key is
898 * appended to 'key' as a series of Netlink attributes. On failure, no
899 * data is appended to 'key'. Either way, 'key''s data might be
902 * If 'port_names' is nonnull, it points to an shash that maps from a port name
903 * to a port number cast to void *. (Port names may be used instead of port
904 * numbers in in_port.)
906 * On success, the attributes appended to 'key' are individually syntactically
907 * valid, but they may not be valid as a sequence. 'key' might, for example,
908 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
910 odp_flow_key_from_string(const char *s, const struct shash *port_names,
913 const size_t old_size = key->size;
917 s += strspn(s, ", \t\r\n");
922 retval = parse_odp_key_attr(s, port_names, key);
924 key->size = old_size;
934 ovs_to_odp_frag(uint8_t ovs_frag)
936 return (ovs_frag & FLOW_NW_FRAG_LATER ? OVS_FRAG_TYPE_LATER
937 : ovs_frag & FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
938 : OVS_FRAG_TYPE_NONE);
941 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'. */
943 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow)
945 struct ovs_key_ethernet *eth_key;
948 if (flow->priority) {
949 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, flow->priority);
952 if (flow->tun_id != htonll(0)) {
953 nl_msg_put_be64(buf, OVS_KEY_ATTR_TUN_ID, flow->tun_id);
956 if (flow->in_port != OFPP_NONE) {
957 nl_msg_put_u32(buf, OVS_KEY_ATTR_IN_PORT,
958 ofp_port_to_odp_port(flow->in_port));
961 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
963 memcpy(eth_key->eth_src, flow->dl_src, ETH_ADDR_LEN);
964 memcpy(eth_key->eth_dst, flow->dl_dst, ETH_ADDR_LEN);
966 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
967 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
968 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, flow->vlan_tci);
969 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
970 if (flow->vlan_tci == htons(0)) {
977 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
981 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, flow->dl_type);
983 if (flow->dl_type == htons(ETH_TYPE_IP)) {
984 struct ovs_key_ipv4 *ipv4_key;
986 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
988 ipv4_key->ipv4_src = flow->nw_src;
989 ipv4_key->ipv4_dst = flow->nw_dst;
990 ipv4_key->ipv4_proto = flow->nw_proto;
991 ipv4_key->ipv4_tos = flow->nw_tos;
992 ipv4_key->ipv4_ttl = flow->nw_ttl;
993 ipv4_key->ipv4_frag = ovs_to_odp_frag(flow->nw_frag);
994 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
995 struct ovs_key_ipv6 *ipv6_key;
997 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
999 memcpy(ipv6_key->ipv6_src, &flow->ipv6_src, sizeof ipv6_key->ipv6_src);
1000 memcpy(ipv6_key->ipv6_dst, &flow->ipv6_dst, sizeof ipv6_key->ipv6_dst);
1001 ipv6_key->ipv6_label = flow->ipv6_label;
1002 ipv6_key->ipv6_proto = flow->nw_proto;
1003 ipv6_key->ipv6_tclass = flow->nw_tos;
1004 ipv6_key->ipv6_hlimit = flow->nw_ttl;
1005 ipv6_key->ipv6_frag = ovs_to_odp_frag(flow->nw_frag);
1006 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1007 struct ovs_key_arp *arp_key;
1009 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
1011 memset(arp_key, 0, sizeof *arp_key);
1012 arp_key->arp_sip = flow->nw_src;
1013 arp_key->arp_tip = flow->nw_dst;
1014 arp_key->arp_op = htons(flow->nw_proto);
1015 memcpy(arp_key->arp_sha, flow->arp_sha, ETH_ADDR_LEN);
1016 memcpy(arp_key->arp_tha, flow->arp_tha, ETH_ADDR_LEN);
1019 if ((flow->dl_type == htons(ETH_TYPE_IP)
1020 || flow->dl_type == htons(ETH_TYPE_IPV6))
1021 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1023 if (flow->nw_proto == IPPROTO_TCP) {
1024 struct ovs_key_tcp *tcp_key;
1026 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
1028 tcp_key->tcp_src = flow->tp_src;
1029 tcp_key->tcp_dst = flow->tp_dst;
1030 } else if (flow->nw_proto == IPPROTO_UDP) {
1031 struct ovs_key_udp *udp_key;
1033 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
1035 udp_key->udp_src = flow->tp_src;
1036 udp_key->udp_dst = flow->tp_dst;
1037 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1038 && flow->nw_proto == IPPROTO_ICMP) {
1039 struct ovs_key_icmp *icmp_key;
1041 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
1043 icmp_key->icmp_type = ntohs(flow->tp_src);
1044 icmp_key->icmp_code = ntohs(flow->tp_dst);
1045 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
1046 && flow->nw_proto == IPPROTO_ICMPV6) {
1047 struct ovs_key_icmpv6 *icmpv6_key;
1049 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
1050 sizeof *icmpv6_key);
1051 icmpv6_key->icmpv6_type = ntohs(flow->tp_src);
1052 icmpv6_key->icmpv6_code = ntohs(flow->tp_dst);
1054 if (icmpv6_key->icmpv6_type == ND_NEIGHBOR_SOLICIT
1055 || icmpv6_key->icmpv6_type == ND_NEIGHBOR_ADVERT) {
1056 struct ovs_key_nd *nd_key;
1058 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
1060 memcpy(nd_key->nd_target, &flow->nd_target,
1061 sizeof nd_key->nd_target);
1062 memcpy(nd_key->nd_sll, flow->arp_sha, ETH_ADDR_LEN);
1063 memcpy(nd_key->nd_tll, flow->arp_tha, ETH_ADDR_LEN);
1070 nl_msg_end_nested(buf, encap);
1075 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
1077 const struct nlattr *key, size_t key_len)
1082 if (VLOG_DROP_WARN(rl)) {
1087 ds_put_format(&s, "%s:", title);
1088 for (i = 0; i < 32; i++) {
1089 if (attrs & (1u << i)) {
1090 ds_put_format(&s, " %s", ovs_key_attr_to_string(i));
1094 ds_put_cstr(&s, ": ");
1095 odp_flow_key_format(key, key_len, &s);
1097 VLOG_WARN("%s", ds_cstr(&s));
1102 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
1104 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1106 if (odp_frag > OVS_FRAG_TYPE_LATER) {
1107 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key",
1112 if (odp_frag != OVS_FRAG_TYPE_NONE) {
1113 flow->nw_frag |= FLOW_NW_FRAG_ANY;
1114 if (odp_frag == OVS_FRAG_TYPE_LATER) {
1115 flow->nw_frag |= FLOW_NW_FRAG_LATER;
1122 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
1123 const struct nlattr *attrs[], uint64_t *present_attrsp)
1125 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1126 const struct nlattr *nla;
1127 uint64_t present_attrs;
1131 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
1132 uint16_t type = nl_attr_type(nla);
1133 size_t len = nl_attr_get_size(nla);
1134 int expected_len = odp_flow_key_attr_len(type);
1136 if (len != expected_len && expected_len != -2) {
1137 if (expected_len == -1) {
1138 VLOG_ERR_RL(&rl, "unknown attribute %"PRIu16" in flow key",
1141 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
1142 "length %d", ovs_key_attr_to_string(type),
1146 } else if (present_attrs & (UINT64_C(1) << type)) {
1147 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
1148 ovs_key_attr_to_string(type));
1152 present_attrs |= UINT64_C(1) << type;
1156 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
1160 *present_attrsp = present_attrs;
1165 check_expectations(uint64_t present_attrs, uint64_t expected_attrs,
1166 const struct nlattr *key, size_t key_len)
1168 uint64_t missing_attrs;
1169 uint64_t extra_attrs;
1171 missing_attrs = expected_attrs & ~present_attrs;
1172 if (missing_attrs) {
1173 static struct vlog_rate_limit miss_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1174 log_odp_key_attributes(&miss_rl, "expected but not present",
1175 missing_attrs, key, key_len);
1179 extra_attrs = present_attrs & ~expected_attrs;
1181 static struct vlog_rate_limit extra_rl = VLOG_RATE_LIMIT_INIT(10, 10);
1182 log_odp_key_attributes(&extra_rl, "present but not expected",
1183 extra_attrs, key, key_len);
1190 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
1191 * structure in 'flow'. Returns 0 if successful, otherwise EINVAL. */
1193 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
1196 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1197 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
1198 uint64_t expected_attrs;
1199 uint64_t present_attrs;
1202 memset(flow, 0, sizeof *flow);
1204 error = parse_flow_nlattrs(key, key_len, attrs, &present_attrs);
1211 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
1212 flow->priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
1213 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
1216 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUN_ID)) {
1217 flow->tun_id = nl_attr_get_be64(attrs[OVS_KEY_ATTR_TUN_ID]);
1218 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUN_ID;
1221 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
1222 uint32_t in_port = nl_attr_get_u32(attrs[OVS_KEY_ATTR_IN_PORT]);
1223 if (in_port >= UINT16_MAX || in_port >= OFPP_MAX) {
1224 VLOG_ERR_RL(&rl, "in_port %"PRIu32" out of supported range",
1228 flow->in_port = odp_port_to_ofp_port(in_port);
1229 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
1231 flow->in_port = OFPP_NONE;
1234 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
1235 const struct ovs_key_ethernet *eth_key;
1237 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
1238 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
1239 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
1241 VLOG_ERR_RL(&rl, "missing Ethernet attribute in flow key");
1244 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
1246 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)
1247 && (nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE])
1248 == htons(ETH_TYPE_VLAN))) {
1249 /* The Ethernet type is 0x8100 so there must be a VLAN tag
1250 * and encapsulated protocol information. */
1251 const struct nlattr *encap;
1255 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE) |
1256 (UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
1257 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
1258 error = check_expectations(present_attrs, expected_attrs,
1264 encap = attrs[OVS_KEY_ATTR_ENCAP];
1265 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
1266 if (tci & htons(VLAN_CFI)) {
1267 flow->vlan_tci = tci;
1269 error = parse_flow_nlattrs(nl_attr_get(encap),
1270 nl_attr_get_size(encap),
1271 attrs, &present_attrs);
1276 } else if (tci == htons(0)) {
1277 /* Corner case for a truncated 802.1Q header. */
1278 if (nl_attr_get_size(encap)) {
1282 flow->dl_type = htons(ETH_TYPE_VLAN);
1289 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
1290 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
1291 if (ntohs(flow->dl_type) < 1536) {
1292 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
1293 ntohs(flow->dl_type));
1296 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
1298 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
1301 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1302 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
1303 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
1304 const struct ovs_key_ipv4 *ipv4_key;
1306 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
1307 flow->nw_src = ipv4_key->ipv4_src;
1308 flow->nw_dst = ipv4_key->ipv4_dst;
1309 flow->nw_proto = ipv4_key->ipv4_proto;
1310 flow->nw_tos = ipv4_key->ipv4_tos;
1311 flow->nw_ttl = ipv4_key->ipv4_ttl;
1312 if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
1316 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1317 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
1318 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
1319 const struct ovs_key_ipv6 *ipv6_key;
1321 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
1322 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
1323 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
1324 flow->ipv6_label = ipv6_key->ipv6_label;
1325 flow->nw_proto = ipv6_key->ipv6_proto;
1326 flow->nw_tos = ipv6_key->ipv6_tclass;
1327 flow->nw_ttl = ipv6_key->ipv6_hlimit;
1328 if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
1332 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1333 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
1334 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
1335 const struct ovs_key_arp *arp_key;
1337 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
1338 flow->nw_src = arp_key->arp_sip;
1339 flow->nw_dst = arp_key->arp_tip;
1340 if (arp_key->arp_op & htons(0xff00)) {
1341 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
1342 "key", ntohs(arp_key->arp_op));
1345 flow->nw_proto = ntohs(arp_key->arp_op);
1346 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
1347 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
1351 if (flow->nw_proto == IPPROTO_TCP
1352 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1353 flow->dl_type == htons(ETH_TYPE_IPV6))
1354 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1355 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
1356 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
1357 const struct ovs_key_tcp *tcp_key;
1359 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
1360 flow->tp_src = tcp_key->tcp_src;
1361 flow->tp_dst = tcp_key->tcp_dst;
1363 } else if (flow->nw_proto == IPPROTO_UDP
1364 && (flow->dl_type == htons(ETH_TYPE_IP) ||
1365 flow->dl_type == htons(ETH_TYPE_IPV6))
1366 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1367 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
1368 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
1369 const struct ovs_key_udp *udp_key;
1371 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
1372 flow->tp_src = udp_key->udp_src;
1373 flow->tp_dst = udp_key->udp_dst;
1375 } else if (flow->nw_proto == IPPROTO_ICMP
1376 && flow->dl_type == htons(ETH_TYPE_IP)
1377 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1378 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
1379 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
1380 const struct ovs_key_icmp *icmp_key;
1382 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
1383 flow->tp_src = htons(icmp_key->icmp_type);
1384 flow->tp_dst = htons(icmp_key->icmp_code);
1386 } else if (flow->nw_proto == IPPROTO_ICMPV6
1387 && flow->dl_type == htons(ETH_TYPE_IPV6)
1388 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1389 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
1390 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
1391 const struct ovs_key_icmpv6 *icmpv6_key;
1393 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
1394 flow->tp_src = htons(icmpv6_key->icmpv6_type);
1395 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
1397 if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1398 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
1399 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
1400 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
1401 const struct ovs_key_nd *nd_key;
1403 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
1404 memcpy(&flow->nd_target, nd_key->nd_target,
1405 sizeof flow->nd_target);
1406 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
1407 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
1413 return check_expectations(present_attrs, expected_attrs, key, key_len);
1416 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
1417 * Netlink PID 'pid'. If 'cookie' is nonnull, adds a userdata attribute whose
1418 * contents contains 'cookie' and returns the offset within 'odp_actions' of
1419 * the start of the cookie. (If 'cookie' is null, then the return value is not
1422 odp_put_userspace_action(uint32_t pid, const struct user_action_cookie *cookie,
1423 struct ofpbuf *odp_actions)
1427 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
1428 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
1430 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
1431 cookie, sizeof *cookie);
1433 nl_msg_end_nested(odp_actions, offset);
1435 return cookie ? odp_actions->size - NLA_ALIGN(sizeof *cookie) : 0;