]> git.proxmox.com Git - mirror_ovs.git/blob - lib/odp-util.c
openvswitch/types.h: New macros OVS_BE16_MAX, OVS_BE32_MAX, OVS_BE64_MAX.
[mirror_ovs.git] / lib / odp-util.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include <arpa/inet.h>
19 #include "odp-util.h"
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <math.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "byte-order.h"
28 #include "coverage.h"
29 #include "dynamic-string.h"
30 #include "flow.h"
31 #include "netlink.h"
32 #include "ofpbuf.h"
33 #include "packets.h"
34 #include "simap.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(odp_util);
40
41 /* The interface between userspace and kernel uses an "OVS_*" prefix.
42 * Since this is fairly non-specific for the OVS userspace components,
43 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
44 * interactions with the datapath.
45 */
46
47 /* The set of characters that may separate one action or one key attribute
48 * from another. */
49 static const char *delimiters = ", \t\r\n";
50
51 static int parse_odp_key_mask_attr(const char *, const struct simap *port_names,
52 struct ofpbuf *, struct ofpbuf *);
53 static void format_odp_key_attr(const struct nlattr *a,
54 const struct nlattr *ma, struct ds *ds,
55 bool verbose);
56
57 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
58 * 'type':
59 *
60 * - For an action whose argument has a fixed length, returned that
61 * nonnegative length in bytes.
62 *
63 * - For an action with a variable-length argument, returns -2.
64 *
65 * - For an invalid 'type', returns -1. */
66 static int
67 odp_action_len(uint16_t type)
68 {
69 if (type > OVS_ACTION_ATTR_MAX) {
70 return -1;
71 }
72
73 switch ((enum ovs_action_attr) type) {
74 case OVS_ACTION_ATTR_OUTPUT: return sizeof(uint32_t);
75 case OVS_ACTION_ATTR_USERSPACE: return -2;
76 case OVS_ACTION_ATTR_PUSH_VLAN: return sizeof(struct ovs_action_push_vlan);
77 case OVS_ACTION_ATTR_POP_VLAN: return 0;
78 case OVS_ACTION_ATTR_PUSH_MPLS: return sizeof(struct ovs_action_push_mpls);
79 case OVS_ACTION_ATTR_POP_MPLS: return sizeof(ovs_be16);
80 case OVS_ACTION_ATTR_SET: return -2;
81 case OVS_ACTION_ATTR_SAMPLE: return -2;
82
83 case OVS_ACTION_ATTR_UNSPEC:
84 case __OVS_ACTION_ATTR_MAX:
85 return -1;
86 }
87
88 return -1;
89 }
90
91 /* Returns a string form of 'attr'. The return value is either a statically
92 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
93 * should be at least OVS_KEY_ATTR_BUFSIZE. */
94 enum { OVS_KEY_ATTR_BUFSIZE = 3 + INT_STRLEN(unsigned int) + 1 };
95 static const char *
96 ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize)
97 {
98 switch (attr) {
99 case OVS_KEY_ATTR_UNSPEC: return "unspec";
100 case OVS_KEY_ATTR_ENCAP: return "encap";
101 case OVS_KEY_ATTR_PRIORITY: return "skb_priority";
102 case OVS_KEY_ATTR_SKB_MARK: return "skb_mark";
103 case OVS_KEY_ATTR_TUNNEL: return "tunnel";
104 case OVS_KEY_ATTR_IN_PORT: return "in_port";
105 case OVS_KEY_ATTR_ETHERNET: return "eth";
106 case OVS_KEY_ATTR_VLAN: return "vlan";
107 case OVS_KEY_ATTR_ETHERTYPE: return "eth_type";
108 case OVS_KEY_ATTR_IPV4: return "ipv4";
109 case OVS_KEY_ATTR_IPV6: return "ipv6";
110 case OVS_KEY_ATTR_TCP: return "tcp";
111 case OVS_KEY_ATTR_UDP: return "udp";
112 case OVS_KEY_ATTR_SCTP: return "sctp";
113 case OVS_KEY_ATTR_ICMP: return "icmp";
114 case OVS_KEY_ATTR_ICMPV6: return "icmpv6";
115 case OVS_KEY_ATTR_ARP: return "arp";
116 case OVS_KEY_ATTR_ND: return "nd";
117 case OVS_KEY_ATTR_MPLS: return "mpls";
118
119 case __OVS_KEY_ATTR_MAX:
120 default:
121 snprintf(namebuf, bufsize, "key%u", (unsigned int) attr);
122 return namebuf;
123 }
124 }
125
126 static void
127 format_generic_odp_action(struct ds *ds, const struct nlattr *a)
128 {
129 size_t len = nl_attr_get_size(a);
130
131 ds_put_format(ds, "action%"PRId16, nl_attr_type(a));
132 if (len) {
133 const uint8_t *unspec;
134 unsigned int i;
135
136 unspec = nl_attr_get(a);
137 for (i = 0; i < len; i++) {
138 ds_put_char(ds, i ? ' ': '(');
139 ds_put_format(ds, "%02x", unspec[i]);
140 }
141 ds_put_char(ds, ')');
142 }
143 }
144
145 static void
146 format_odp_sample_action(struct ds *ds, const struct nlattr *attr)
147 {
148 static const struct nl_policy ovs_sample_policy[] = {
149 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_SAMPLE_ATTR_UNSPEC */
150 { NL_A_U32, 0, 0, false }, /* OVS_SAMPLE_ATTR_PROBABILITY */
151 { NL_A_NESTED, 0, 0, false }, /* OVS_SAMPLE_ATTR_ACTIONS */
152 };
153 struct nlattr *a[ARRAY_SIZE(ovs_sample_policy)];
154 double percentage;
155 const struct nlattr *nla_acts;
156 int len;
157
158 ds_put_cstr(ds, "sample");
159
160 if (!nl_parse_nested(attr, ovs_sample_policy, a, ARRAY_SIZE(a))) {
161 ds_put_cstr(ds, "(error)");
162 return;
163 }
164
165 percentage = (100.0 * nl_attr_get_u32(a[OVS_SAMPLE_ATTR_PROBABILITY])) /
166 UINT32_MAX;
167
168 ds_put_format(ds, "(sample=%.1f%%,", percentage);
169
170 ds_put_cstr(ds, "actions(");
171 nla_acts = nl_attr_get(a[OVS_SAMPLE_ATTR_ACTIONS]);
172 len = nl_attr_get_size(a[OVS_SAMPLE_ATTR_ACTIONS]);
173 format_odp_actions(ds, nla_acts, len);
174 ds_put_format(ds, "))");
175 }
176
177 static const char *
178 slow_path_reason_to_string(enum slow_path_reason reason)
179 {
180 switch (reason) {
181 case SLOW_CFM:
182 return "cfm";
183 case SLOW_LACP:
184 return "lacp";
185 case SLOW_STP:
186 return "stp";
187 case SLOW_BFD:
188 return "bfd";
189 case SLOW_CONTROLLER:
190 return "controller";
191 case __SLOW_MAX:
192 default:
193 return NULL;
194 }
195 }
196
197 static enum slow_path_reason
198 string_to_slow_path_reason(const char *string)
199 {
200 enum slow_path_reason i;
201
202 for (i = 1; i < __SLOW_MAX; i++) {
203 if (!strcmp(string, slow_path_reason_to_string(i))) {
204 return i;
205 }
206 }
207
208 return 0;
209 }
210
211 static int
212 parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
213 uint32_t *res)
214 {
215 uint32_t result = 0;
216 int n = 0;
217
218 if (s[n] != '(') {
219 return -EINVAL;
220 }
221 n++;
222
223 while (s[n] != ')') {
224 unsigned long long int flags;
225 uint32_t bit;
226 int n0;
227
228 if (sscanf(&s[n], "%lli%n", &flags, &n0) > 0 && n0 > 0) {
229 n += n0 + (s[n + n0] == ',');
230 result |= flags;
231 continue;
232 }
233
234 for (bit = 1; bit; bit <<= 1) {
235 const char *name = bit_to_string(bit);
236 size_t len;
237
238 if (!name) {
239 continue;
240 }
241
242 len = strlen(name);
243 if (!strncmp(s + n, name, len) &&
244 (s[n + len] == ',' || s[n + len] == ')')) {
245 result |= bit;
246 n += len + (s[n + len] == ',');
247 break;
248 }
249 }
250
251 if (!bit) {
252 return -EINVAL;
253 }
254 }
255 n++;
256
257 *res = result;
258 return n;
259 }
260
261 static void
262 format_odp_userspace_action(struct ds *ds, const struct nlattr *attr)
263 {
264 static const struct nl_policy ovs_userspace_policy[] = {
265 { NL_A_NO_ATTR, 0, 0, false }, /* OVS_USERSPACE_ATTR_UNSPEC */
266 { NL_A_U32, 0, 0, false }, /* OVS_USERSPACE_ATTR_PID */
267 { NL_A_UNSPEC, 0, 0, true }, /* OVS_USERSPACE_ATTR_USERDATA */
268 };
269 struct nlattr *a[ARRAY_SIZE(ovs_userspace_policy)];
270 const struct nlattr *userdata_attr;
271
272 if (!nl_parse_nested(attr, ovs_userspace_policy, a, ARRAY_SIZE(a))) {
273 ds_put_cstr(ds, "userspace(error)");
274 return;
275 }
276
277 ds_put_format(ds, "userspace(pid=%"PRIu32,
278 nl_attr_get_u32(a[OVS_USERSPACE_ATTR_PID]));
279
280 userdata_attr = a[OVS_USERSPACE_ATTR_USERDATA];
281
282 if (userdata_attr) {
283 const uint8_t *userdata = nl_attr_get(userdata_attr);
284 size_t userdata_len = nl_attr_get_size(userdata_attr);
285 bool userdata_unspec = true;
286 union user_action_cookie cookie;
287
288 if (userdata_len >= sizeof cookie.type
289 && userdata_len <= sizeof cookie) {
290
291 memset(&cookie, 0, sizeof cookie);
292 memcpy(&cookie, userdata, userdata_len);
293
294 userdata_unspec = false;
295
296 if (userdata_len == sizeof cookie.sflow
297 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
298 ds_put_format(ds, ",sFlow("
299 "vid=%"PRIu16",pcp=%"PRIu8",output=%"PRIu32")",
300 vlan_tci_to_vid(cookie.sflow.vlan_tci),
301 vlan_tci_to_pcp(cookie.sflow.vlan_tci),
302 cookie.sflow.output);
303 } else if (userdata_len == sizeof cookie.slow_path
304 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
305 const char *reason;
306 reason = slow_path_reason_to_string(cookie.slow_path.reason);
307 reason = reason ? reason : "";
308 ds_put_format(ds, ",slow_path(%s)", reason);
309 } else if (userdata_len == sizeof cookie.flow_sample
310 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
311 ds_put_format(ds, ",flow_sample(probability=%"PRIu16
312 ",collector_set_id=%"PRIu32
313 ",obs_domain_id=%"PRIu32
314 ",obs_point_id=%"PRIu32")",
315 cookie.flow_sample.probability,
316 cookie.flow_sample.collector_set_id,
317 cookie.flow_sample.obs_domain_id,
318 cookie.flow_sample.obs_point_id);
319 } else if (userdata_len == sizeof cookie.ipfix
320 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
321 ds_put_format(ds, ",ipfix");
322 } else {
323 userdata_unspec = true;
324 }
325 }
326
327 if (userdata_unspec) {
328 size_t i;
329 ds_put_format(ds, ",userdata(");
330 for (i = 0; i < userdata_len; i++) {
331 ds_put_format(ds, "%02x", userdata[i]);
332 }
333 ds_put_char(ds, ')');
334 }
335 }
336
337 ds_put_char(ds, ')');
338 }
339
340 static void
341 format_vlan_tci(struct ds *ds, ovs_be16 vlan_tci)
342 {
343 ds_put_format(ds, "vid=%"PRIu16",pcp=%d",
344 vlan_tci_to_vid(vlan_tci),
345 vlan_tci_to_pcp(vlan_tci));
346 if (!(vlan_tci & htons(VLAN_CFI))) {
347 ds_put_cstr(ds, ",cfi=0");
348 }
349 }
350
351 static void
352 format_mpls_lse(struct ds *ds, ovs_be32 mpls_lse)
353 {
354 ds_put_format(ds, "label=%"PRIu32",tc=%d,ttl=%d,bos=%d",
355 mpls_lse_to_label(mpls_lse),
356 mpls_lse_to_tc(mpls_lse),
357 mpls_lse_to_ttl(mpls_lse),
358 mpls_lse_to_bos(mpls_lse));
359 }
360
361 static void
362 format_mpls(struct ds *ds, const struct ovs_key_mpls *mpls_key,
363 const struct ovs_key_mpls *mpls_mask)
364 {
365 ovs_be32 key = mpls_key->mpls_lse;
366
367 if (mpls_mask == NULL) {
368 format_mpls_lse(ds, key);
369 } else {
370 ovs_be32 mask = mpls_mask->mpls_lse;
371
372 ds_put_format(ds, "label=%"PRIu32"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
373 mpls_lse_to_label(key), mpls_lse_to_label(mask),
374 mpls_lse_to_tc(key), mpls_lse_to_tc(mask),
375 mpls_lse_to_ttl(key), mpls_lse_to_ttl(mask),
376 mpls_lse_to_bos(key), mpls_lse_to_bos(mask));
377 }
378 }
379
380 static void
381 format_odp_action(struct ds *ds, const struct nlattr *a)
382 {
383 int expected_len;
384 enum ovs_action_attr type = nl_attr_type(a);
385 const struct ovs_action_push_vlan *vlan;
386
387 expected_len = odp_action_len(nl_attr_type(a));
388 if (expected_len != -2 && nl_attr_get_size(a) != expected_len) {
389 ds_put_format(ds, "bad length %zu, expected %d for: ",
390 nl_attr_get_size(a), expected_len);
391 format_generic_odp_action(ds, a);
392 return;
393 }
394
395 switch (type) {
396 case OVS_ACTION_ATTR_OUTPUT:
397 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
398 break;
399 case OVS_ACTION_ATTR_USERSPACE:
400 format_odp_userspace_action(ds, a);
401 break;
402 case OVS_ACTION_ATTR_SET:
403 ds_put_cstr(ds, "set(");
404 format_odp_key_attr(nl_attr_get(a), NULL, ds, true);
405 ds_put_cstr(ds, ")");
406 break;
407 case OVS_ACTION_ATTR_PUSH_VLAN:
408 vlan = nl_attr_get(a);
409 ds_put_cstr(ds, "push_vlan(");
410 if (vlan->vlan_tpid != htons(ETH_TYPE_VLAN)) {
411 ds_put_format(ds, "tpid=0x%04"PRIx16",", ntohs(vlan->vlan_tpid));
412 }
413 format_vlan_tci(ds, vlan->vlan_tci);
414 ds_put_char(ds, ')');
415 break;
416 case OVS_ACTION_ATTR_POP_VLAN:
417 ds_put_cstr(ds, "pop_vlan");
418 break;
419 case OVS_ACTION_ATTR_PUSH_MPLS: {
420 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
421 ds_put_cstr(ds, "push_mpls(");
422 format_mpls_lse(ds, mpls->mpls_lse);
423 ds_put_format(ds, ",eth_type=0x%"PRIx16")", ntohs(mpls->mpls_ethertype));
424 break;
425 }
426 case OVS_ACTION_ATTR_POP_MPLS: {
427 ovs_be16 ethertype = nl_attr_get_be16(a);
428 ds_put_format(ds, "pop_mpls(eth_type=0x%"PRIx16")", ntohs(ethertype));
429 break;
430 }
431 case OVS_ACTION_ATTR_SAMPLE:
432 format_odp_sample_action(ds, a);
433 break;
434 case OVS_ACTION_ATTR_UNSPEC:
435 case __OVS_ACTION_ATTR_MAX:
436 default:
437 format_generic_odp_action(ds, a);
438 break;
439 }
440 }
441
442 void
443 format_odp_actions(struct ds *ds, const struct nlattr *actions,
444 size_t actions_len)
445 {
446 if (actions_len) {
447 const struct nlattr *a;
448 unsigned int left;
449
450 NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
451 if (a != actions) {
452 ds_put_char(ds, ',');
453 }
454 format_odp_action(ds, a);
455 }
456 if (left) {
457 int i;
458
459 if (left == actions_len) {
460 ds_put_cstr(ds, "<empty>");
461 }
462 ds_put_format(ds, ",***%u leftover bytes*** (", left);
463 for (i = 0; i < left; i++) {
464 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
465 }
466 ds_put_char(ds, ')');
467 }
468 } else {
469 ds_put_cstr(ds, "drop");
470 }
471 }
472
473 static int
474 parse_odp_action(const char *s, const struct simap *port_names,
475 struct ofpbuf *actions)
476 {
477 /* Many of the sscanf calls in this function use oversized destination
478 * fields because some sscanf() implementations truncate the range of %i
479 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
480 * value of 0x7fff. The other alternatives are to allow only a single
481 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
482 * parsers.
483 *
484 * The tun_id parser has to use an alternative approach because there is no
485 * type larger than 64 bits. */
486
487 {
488 unsigned long long int port;
489 int n = -1;
490
491 if (sscanf(s, "%lli%n", &port, &n) > 0 && n > 0) {
492 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, port);
493 return n;
494 }
495 }
496
497 if (port_names) {
498 int len = strcspn(s, delimiters);
499 struct simap_node *node;
500
501 node = simap_find_len(port_names, s, len);
502 if (node) {
503 nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
504 return len;
505 }
506 }
507
508 {
509 unsigned long long int pid;
510 unsigned long long int output;
511 unsigned long long int probability;
512 unsigned long long int collector_set_id;
513 unsigned long long int obs_domain_id;
514 unsigned long long int obs_point_id;
515 int vid, pcp;
516 int n = -1;
517
518 if (sscanf(s, "userspace(pid=%lli)%n", &pid, &n) > 0 && n > 0) {
519 odp_put_userspace_action(pid, NULL, 0, actions);
520 return n;
521 } else if (sscanf(s, "userspace(pid=%lli,sFlow(vid=%i,"
522 "pcp=%i,output=%lli))%n",
523 &pid, &vid, &pcp, &output, &n) > 0 && n > 0) {
524 union user_action_cookie cookie;
525 uint16_t tci;
526
527 tci = vid | (pcp << VLAN_PCP_SHIFT);
528 if (tci) {
529 tci |= VLAN_CFI;
530 }
531
532 cookie.type = USER_ACTION_COOKIE_SFLOW;
533 cookie.sflow.vlan_tci = htons(tci);
534 cookie.sflow.output = output;
535 odp_put_userspace_action(pid, &cookie, sizeof cookie.sflow,
536 actions);
537 return n;
538 } else if (sscanf(s, "userspace(pid=%lli,slow_path(%n", &pid, &n) > 0
539 && n > 0) {
540 union user_action_cookie cookie;
541 char reason[32];
542
543 if (s[n] == ')' && s[n + 1] == ')') {
544 reason[0] = '\0';
545 n += 2;
546 } else if (sscanf(s + n, "%31[^)]))", reason) > 0) {
547 n += strlen(reason) + 2;
548 } else {
549 return -EINVAL;
550 }
551
552 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
553 cookie.slow_path.unused = 0;
554 cookie.slow_path.reason = string_to_slow_path_reason(reason);
555
556 if (reason[0] && !cookie.slow_path.reason) {
557 return -EINVAL;
558 }
559
560 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
561 actions);
562 return n;
563 } else if (sscanf(s, "userspace(pid=%lli,flow_sample(probability=%lli,"
564 "collector_set_id=%lli,obs_domain_id=%lli,"
565 "obs_point_id=%lli))%n",
566 &pid, &probability, &collector_set_id,
567 &obs_domain_id, &obs_point_id, &n) > 0 && n > 0) {
568 union user_action_cookie cookie;
569
570 cookie.type = USER_ACTION_COOKIE_FLOW_SAMPLE;
571 cookie.flow_sample.probability = probability;
572 cookie.flow_sample.collector_set_id = collector_set_id;
573 cookie.flow_sample.obs_domain_id = obs_domain_id;
574 cookie.flow_sample.obs_point_id = obs_point_id;
575 odp_put_userspace_action(pid, &cookie, sizeof cookie.flow_sample,
576 actions);
577 return n;
578 } else if (sscanf(s, "userspace(pid=%lli,ipfix)%n", &pid, &n) > 0
579 && n > 0) {
580 union user_action_cookie cookie;
581
582 cookie.type = USER_ACTION_COOKIE_IPFIX;
583 odp_put_userspace_action(pid, &cookie, sizeof cookie.ipfix,
584 actions);
585 return n;
586 } else if (sscanf(s, "userspace(pid=%lli,userdata(%n", &pid, &n) > 0
587 && n > 0) {
588 struct ofpbuf buf;
589 char *end;
590
591 ofpbuf_init(&buf, 16);
592 end = ofpbuf_put_hex(&buf, &s[n], NULL);
593 if (end[0] == ')' && end[1] == ')') {
594 odp_put_userspace_action(pid, buf.data, buf.size, actions);
595 ofpbuf_uninit(&buf);
596 return (end + 2) - s;
597 }
598 }
599 }
600
601 if (!strncmp(s, "set(", 4)) {
602 size_t start_ofs;
603 int retval;
604
605 start_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SET);
606 retval = parse_odp_key_mask_attr(s + 4, port_names, actions, NULL);
607 if (retval < 0) {
608 return retval;
609 }
610 if (s[retval + 4] != ')') {
611 return -EINVAL;
612 }
613 nl_msg_end_nested(actions, start_ofs);
614 return retval + 5;
615 }
616
617 {
618 struct ovs_action_push_vlan push;
619 int tpid = ETH_TYPE_VLAN;
620 int vid, pcp;
621 int cfi = 1;
622 int n = -1;
623
624 if ((sscanf(s, "push_vlan(vid=%i,pcp=%i)%n", &vid, &pcp, &n) > 0
625 && n > 0)
626 || (sscanf(s, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
627 &vid, &pcp, &cfi, &n) > 0 && n > 0)
628 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
629 &tpid, &vid, &pcp, &n) > 0 && n > 0)
630 || (sscanf(s, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
631 &tpid, &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
632 push.vlan_tpid = htons(tpid);
633 push.vlan_tci = htons((vid << VLAN_VID_SHIFT)
634 | (pcp << VLAN_PCP_SHIFT)
635 | (cfi ? VLAN_CFI : 0));
636 nl_msg_put_unspec(actions, OVS_ACTION_ATTR_PUSH_VLAN,
637 &push, sizeof push);
638
639 return n;
640 }
641 }
642
643 if (!strncmp(s, "pop_vlan", 8)) {
644 nl_msg_put_flag(actions, OVS_ACTION_ATTR_POP_VLAN);
645 return 8;
646 }
647
648 {
649 double percentage;
650 int n = -1;
651
652 if (sscanf(s, "sample(sample=%lf%%,actions(%n", &percentage, &n) > 0
653 && percentage >= 0. && percentage <= 100.0
654 && n > 0) {
655 size_t sample_ofs, actions_ofs;
656 double probability;
657
658 probability = floor(UINT32_MAX * (percentage / 100.0) + .5);
659 sample_ofs = nl_msg_start_nested(actions, OVS_ACTION_ATTR_SAMPLE);
660 nl_msg_put_u32(actions, OVS_SAMPLE_ATTR_PROBABILITY,
661 (probability <= 0 ? 0
662 : probability >= UINT32_MAX ? UINT32_MAX
663 : probability));
664
665 actions_ofs = nl_msg_start_nested(actions,
666 OVS_SAMPLE_ATTR_ACTIONS);
667 for (;;) {
668 int retval;
669
670 n += strspn(s + n, delimiters);
671 if (s[n] == ')') {
672 break;
673 }
674
675 retval = parse_odp_action(s + n, port_names, actions);
676 if (retval < 0) {
677 return retval;
678 }
679 n += retval;
680 }
681 nl_msg_end_nested(actions, actions_ofs);
682 nl_msg_end_nested(actions, sample_ofs);
683
684 return s[n + 1] == ')' ? n + 2 : -EINVAL;
685 }
686 }
687
688 return -EINVAL;
689 }
690
691 /* Parses the string representation of datapath actions, in the format output
692 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
693 * value. On success, the ODP actions are appended to 'actions' as a series of
694 * Netlink attributes. On failure, no data is appended to 'actions'. Either
695 * way, 'actions''s data might be reallocated. */
696 int
697 odp_actions_from_string(const char *s, const struct simap *port_names,
698 struct ofpbuf *actions)
699 {
700 size_t old_size;
701
702 if (!strcasecmp(s, "drop")) {
703 return 0;
704 }
705
706 old_size = actions->size;
707 for (;;) {
708 int retval;
709
710 s += strspn(s, delimiters);
711 if (!*s) {
712 return 0;
713 }
714
715 retval = parse_odp_action(s, port_names, actions);
716 if (retval < 0 || !strchr(delimiters, s[retval])) {
717 actions->size = old_size;
718 return -retval;
719 }
720 s += retval;
721 }
722
723 return 0;
724 }
725 \f
726 /* Returns the correct length of the payload for a flow key attribute of the
727 * specified 'type', -1 if 'type' is unknown, or -2 if the attribute's payload
728 * is variable length. */
729 static int
730 odp_flow_key_attr_len(uint16_t type)
731 {
732 if (type > OVS_KEY_ATTR_MAX) {
733 return -1;
734 }
735
736 switch ((enum ovs_key_attr) type) {
737 case OVS_KEY_ATTR_ENCAP: return -2;
738 case OVS_KEY_ATTR_PRIORITY: return 4;
739 case OVS_KEY_ATTR_SKB_MARK: return 4;
740 case OVS_KEY_ATTR_TUNNEL: return -2;
741 case OVS_KEY_ATTR_IN_PORT: return 4;
742 case OVS_KEY_ATTR_ETHERNET: return sizeof(struct ovs_key_ethernet);
743 case OVS_KEY_ATTR_VLAN: return sizeof(ovs_be16);
744 case OVS_KEY_ATTR_ETHERTYPE: return 2;
745 case OVS_KEY_ATTR_MPLS: return sizeof(struct ovs_key_mpls);
746 case OVS_KEY_ATTR_IPV4: return sizeof(struct ovs_key_ipv4);
747 case OVS_KEY_ATTR_IPV6: return sizeof(struct ovs_key_ipv6);
748 case OVS_KEY_ATTR_TCP: return sizeof(struct ovs_key_tcp);
749 case OVS_KEY_ATTR_UDP: return sizeof(struct ovs_key_udp);
750 case OVS_KEY_ATTR_SCTP: return sizeof(struct ovs_key_sctp);
751 case OVS_KEY_ATTR_ICMP: return sizeof(struct ovs_key_icmp);
752 case OVS_KEY_ATTR_ICMPV6: return sizeof(struct ovs_key_icmpv6);
753 case OVS_KEY_ATTR_ARP: return sizeof(struct ovs_key_arp);
754 case OVS_KEY_ATTR_ND: return sizeof(struct ovs_key_nd);
755
756 case OVS_KEY_ATTR_UNSPEC:
757 case __OVS_KEY_ATTR_MAX:
758 return -1;
759 }
760
761 return -1;
762 }
763
764 static void
765 format_generic_odp_key(const struct nlattr *a, struct ds *ds)
766 {
767 size_t len = nl_attr_get_size(a);
768 if (len) {
769 const uint8_t *unspec;
770 unsigned int i;
771
772 unspec = nl_attr_get(a);
773 for (i = 0; i < len; i++) {
774 if (i) {
775 ds_put_char(ds, ' ');
776 }
777 ds_put_format(ds, "%02x", unspec[i]);
778 }
779 }
780 }
781
782 static const char *
783 ovs_frag_type_to_string(enum ovs_frag_type type)
784 {
785 switch (type) {
786 case OVS_FRAG_TYPE_NONE:
787 return "no";
788 case OVS_FRAG_TYPE_FIRST:
789 return "first";
790 case OVS_FRAG_TYPE_LATER:
791 return "later";
792 case __OVS_FRAG_TYPE_MAX:
793 default:
794 return "<error>";
795 }
796 }
797
798 static int
799 tunnel_key_attr_len(int type)
800 {
801 switch (type) {
802 case OVS_TUNNEL_KEY_ATTR_ID: return 8;
803 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: return 4;
804 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: return 4;
805 case OVS_TUNNEL_KEY_ATTR_TOS: return 1;
806 case OVS_TUNNEL_KEY_ATTR_TTL: return 1;
807 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: return 0;
808 case OVS_TUNNEL_KEY_ATTR_CSUM: return 0;
809 case __OVS_TUNNEL_KEY_ATTR_MAX:
810 return -1;
811 }
812 return -1;
813 }
814
815 enum odp_key_fitness
816 odp_tun_key_from_attr(const struct nlattr *attr, struct flow_tnl *tun)
817 {
818 unsigned int left;
819 const struct nlattr *a;
820 bool ttl = false;
821 bool unknown = false;
822
823 NL_NESTED_FOR_EACH(a, left, attr) {
824 uint16_t type = nl_attr_type(a);
825 size_t len = nl_attr_get_size(a);
826 int expected_len = tunnel_key_attr_len(type);
827
828 if (len != expected_len && expected_len >= 0) {
829 return ODP_FIT_ERROR;
830 }
831
832 switch (type) {
833 case OVS_TUNNEL_KEY_ATTR_ID:
834 tun->tun_id = nl_attr_get_be64(a);
835 tun->flags |= FLOW_TNL_F_KEY;
836 break;
837 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
838 tun->ip_src = nl_attr_get_be32(a);
839 break;
840 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
841 tun->ip_dst = nl_attr_get_be32(a);
842 break;
843 case OVS_TUNNEL_KEY_ATTR_TOS:
844 tun->ip_tos = nl_attr_get_u8(a);
845 break;
846 case OVS_TUNNEL_KEY_ATTR_TTL:
847 tun->ip_ttl = nl_attr_get_u8(a);
848 ttl = true;
849 break;
850 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
851 tun->flags |= FLOW_TNL_F_DONT_FRAGMENT;
852 break;
853 case OVS_TUNNEL_KEY_ATTR_CSUM:
854 tun->flags |= FLOW_TNL_F_CSUM;
855 break;
856 default:
857 /* Allow this to show up as unexpected, if there are unknown
858 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
859 unknown = true;
860 break;
861 }
862 }
863
864 if (!ttl) {
865 return ODP_FIT_ERROR;
866 }
867 if (unknown) {
868 return ODP_FIT_TOO_MUCH;
869 }
870 return ODP_FIT_PERFECT;
871 }
872
873 static void
874 tun_key_to_attr(struct ofpbuf *a, const struct flow_tnl *tun_key)
875 {
876 size_t tun_key_ofs;
877
878 tun_key_ofs = nl_msg_start_nested(a, OVS_KEY_ATTR_TUNNEL);
879
880 if (tun_key->flags & FLOW_TNL_F_KEY) {
881 nl_msg_put_be64(a, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id);
882 }
883 if (tun_key->ip_src) {
884 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ip_src);
885 }
886 if (tun_key->ip_dst) {
887 nl_msg_put_be32(a, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ip_dst);
888 }
889 if (tun_key->ip_tos) {
890 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ip_tos);
891 }
892 nl_msg_put_u8(a, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ip_ttl);
893 if (tun_key->flags & FLOW_TNL_F_DONT_FRAGMENT) {
894 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT);
895 }
896 if (tun_key->flags & FLOW_TNL_F_CSUM) {
897 nl_msg_put_flag(a, OVS_TUNNEL_KEY_ATTR_CSUM);
898 }
899
900 nl_msg_end_nested(a, tun_key_ofs);
901 }
902
903 static bool
904 odp_mask_attr_is_wildcard(const struct nlattr *ma)
905 {
906 return is_all_zeros(nl_attr_get(ma), nl_attr_get_size(ma));
907 }
908
909 static bool
910 odp_mask_attr_is_exact(const struct nlattr *ma)
911 {
912 bool is_exact = false;
913 enum ovs_key_attr attr = nl_attr_type(ma);
914
915 if (attr == OVS_KEY_ATTR_TUNNEL) {
916 /* XXX this is a hack for now. Should change
917 * the exact match dection to per field
918 * instead of per attribute.
919 */
920 struct flow_tnl tun_mask;
921 memset(&tun_mask, 0, sizeof tun_mask);
922 odp_tun_key_from_attr(ma, &tun_mask);
923 if (tun_mask.flags == (FLOW_TNL_F_KEY
924 | FLOW_TNL_F_DONT_FRAGMENT
925 | FLOW_TNL_F_CSUM)) {
926 /* The flags are exact match, check the remaining fields. */
927 tun_mask.flags = 0xffff;
928 is_exact = is_all_ones((uint8_t *)&tun_mask,
929 offsetof(struct flow_tnl, ip_ttl));
930 }
931 } else {
932 is_exact = is_all_ones(nl_attr_get(ma), nl_attr_get_size(ma));
933 }
934
935 return is_exact;
936 }
937
938
939 static void
940 format_odp_key_attr(const struct nlattr *a, const struct nlattr *ma,
941 struct ds *ds, bool verbose)
942 {
943 struct flow_tnl tun_key;
944 enum ovs_key_attr attr = nl_attr_type(a);
945 char namebuf[OVS_KEY_ATTR_BUFSIZE];
946 int expected_len;
947 bool is_exact;
948
949 is_exact = ma ? odp_mask_attr_is_exact(ma) : true;
950
951 ds_put_cstr(ds, ovs_key_attr_to_string(attr, namebuf, sizeof namebuf));
952
953 {
954 expected_len = odp_flow_key_attr_len(nl_attr_type(a));
955 if (expected_len != -2) {
956 bool bad_key_len = nl_attr_get_size(a) != expected_len;
957 bool bad_mask_len = ma && nl_attr_get_size(a) != expected_len;
958
959 if (bad_key_len || bad_mask_len) {
960 if (bad_key_len) {
961 ds_put_format(ds, "(bad key length %zu, expected %d)(",
962 nl_attr_get_size(a),
963 odp_flow_key_attr_len(nl_attr_type(a)));
964 }
965 format_generic_odp_key(a, ds);
966 if (bad_mask_len) {
967 ds_put_char(ds, '/');
968 ds_put_format(ds, "(bad mask length %zu, expected %d)(",
969 nl_attr_get_size(ma),
970 odp_flow_key_attr_len(nl_attr_type(ma)));
971 }
972 format_generic_odp_key(ma, ds);
973 ds_put_char(ds, ')');
974 return;
975 }
976 }
977 }
978
979 ds_put_char(ds, '(');
980 switch (attr) {
981 case OVS_KEY_ATTR_ENCAP:
982 if (ma && nl_attr_get_size(ma) && nl_attr_get_size(a)) {
983 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a),
984 nl_attr_get(ma), nl_attr_get_size(ma), ds, verbose);
985 } else if (nl_attr_get_size(a)) {
986 odp_flow_format(nl_attr_get(a), nl_attr_get_size(a), NULL, 0, ds,
987 verbose);
988 }
989 break;
990
991 case OVS_KEY_ATTR_PRIORITY:
992 case OVS_KEY_ATTR_SKB_MARK:
993 ds_put_format(ds, "%#"PRIx32, nl_attr_get_u32(a));
994 if (!is_exact) {
995 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
996 }
997 break;
998
999 case OVS_KEY_ATTR_TUNNEL:
1000 memset(&tun_key, 0, sizeof tun_key);
1001 if (odp_tun_key_from_attr(a, &tun_key) == ODP_FIT_ERROR) {
1002 ds_put_format(ds, "error");
1003 } else if (!is_exact) {
1004 struct flow_tnl tun_mask;
1005
1006 memset(&tun_mask, 0, sizeof tun_mask);
1007 odp_tun_key_from_attr(ma, &tun_mask);
1008 ds_put_format(ds, "tun_id=%#"PRIx64"/%#"PRIx64
1009 ",src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1010 ",tos=%#"PRIx8"/%#"PRIx8",ttl=%"PRIu8"/%#"PRIx8
1011 ",flags(",
1012 ntohll(tun_key.tun_id), ntohll(tun_mask.tun_id),
1013 IP_ARGS(tun_key.ip_src), IP_ARGS(tun_mask.ip_src),
1014 IP_ARGS(tun_key.ip_dst), IP_ARGS(tun_mask.ip_dst),
1015 tun_key.ip_tos, tun_mask.ip_tos,
1016 tun_key.ip_ttl, tun_mask.ip_ttl);
1017
1018 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1019
1020 /* XXX This code is correct, but enabling it would break the unit
1021 test. Disable it for now until the input parser is fixed.
1022
1023 ds_put_char(ds, '/');
1024 format_flags(ds, flow_tun_flag_to_string, tun_mask.flags, ',');
1025 */
1026 ds_put_char(ds, ')');
1027 } else {
1028 ds_put_format(ds, "tun_id=0x%"PRIx64",src="IP_FMT",dst="IP_FMT","
1029 "tos=0x%"PRIx8",ttl=%"PRIu8",flags(",
1030 ntohll(tun_key.tun_id),
1031 IP_ARGS(tun_key.ip_src),
1032 IP_ARGS(tun_key.ip_dst),
1033 tun_key.ip_tos, tun_key.ip_ttl);
1034
1035 format_flags(ds, flow_tun_flag_to_string, tun_key.flags, ',');
1036 ds_put_char(ds, ')');
1037 }
1038 break;
1039
1040 case OVS_KEY_ATTR_IN_PORT:
1041 ds_put_format(ds, "%"PRIu32, nl_attr_get_u32(a));
1042 if (!is_exact) {
1043 ds_put_format(ds, "/%#"PRIx32, nl_attr_get_u32(ma));
1044 }
1045 break;
1046
1047 case OVS_KEY_ATTR_ETHERNET:
1048 if (!is_exact) {
1049 const struct ovs_key_ethernet *eth_mask = nl_attr_get(ma);
1050 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1051
1052 ds_put_format(ds, "src="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1053 ",dst="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1054 ETH_ADDR_ARGS(eth_key->eth_src),
1055 ETH_ADDR_ARGS(eth_mask->eth_src),
1056 ETH_ADDR_ARGS(eth_key->eth_dst),
1057 ETH_ADDR_ARGS(eth_mask->eth_dst));
1058 } else {
1059 const struct ovs_key_ethernet *eth_key = nl_attr_get(a);
1060
1061 ds_put_format(ds, "src="ETH_ADDR_FMT",dst="ETH_ADDR_FMT,
1062 ETH_ADDR_ARGS(eth_key->eth_src),
1063 ETH_ADDR_ARGS(eth_key->eth_dst));
1064 }
1065 break;
1066
1067 case OVS_KEY_ATTR_VLAN:
1068 {
1069 ovs_be16 vlan_tci = nl_attr_get_be16(a);
1070 if (!is_exact) {
1071 ovs_be16 mask = nl_attr_get_be16(ma);
1072 ds_put_format(ds, "vid=%"PRIu16"/0x%"PRIx16",pcp=%d/0x%x,cfi=%d/%d",
1073 vlan_tci_to_vid(vlan_tci),
1074 vlan_tci_to_vid(mask),
1075 vlan_tci_to_pcp(vlan_tci),
1076 vlan_tci_to_pcp(mask),
1077 vlan_tci_to_cfi(vlan_tci),
1078 vlan_tci_to_cfi(mask));
1079 } else {
1080 format_vlan_tci(ds, vlan_tci);
1081 }
1082 }
1083 break;
1084
1085 case OVS_KEY_ATTR_MPLS: {
1086 const struct ovs_key_mpls *mpls_key = nl_attr_get(a);
1087 const struct ovs_key_mpls *mpls_mask = NULL;
1088 if (!is_exact) {
1089 mpls_mask = nl_attr_get(ma);
1090 }
1091 format_mpls(ds, mpls_key, mpls_mask);
1092 break;
1093 }
1094
1095 case OVS_KEY_ATTR_ETHERTYPE:
1096 ds_put_format(ds, "0x%04"PRIx16, ntohs(nl_attr_get_be16(a)));
1097 if (!is_exact) {
1098 ds_put_format(ds, "/0x%04"PRIx16, ntohs(nl_attr_get_be16(ma)));
1099 }
1100 break;
1101
1102 case OVS_KEY_ATTR_IPV4:
1103 if (!is_exact) {
1104 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1105 const struct ovs_key_ipv4 *ipv4_mask = nl_attr_get(ma);
1106
1107 ds_put_format(ds, "src="IP_FMT"/"IP_FMT",dst="IP_FMT"/"IP_FMT
1108 ",proto=%"PRIu8"/%#"PRIx8",tos=%#"PRIx8"/%#"PRIx8
1109 ",ttl=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1110 IP_ARGS(ipv4_key->ipv4_src),
1111 IP_ARGS(ipv4_mask->ipv4_src),
1112 IP_ARGS(ipv4_key->ipv4_dst),
1113 IP_ARGS(ipv4_mask->ipv4_dst),
1114 ipv4_key->ipv4_proto, ipv4_mask->ipv4_proto,
1115 ipv4_key->ipv4_tos, ipv4_mask->ipv4_tos,
1116 ipv4_key->ipv4_ttl, ipv4_mask->ipv4_ttl,
1117 ovs_frag_type_to_string(ipv4_key->ipv4_frag),
1118 ipv4_mask->ipv4_frag);
1119 } else {
1120 const struct ovs_key_ipv4 *ipv4_key = nl_attr_get(a);
1121
1122 ds_put_format(ds, "src="IP_FMT",dst="IP_FMT",proto=%"PRIu8
1123 ",tos=%#"PRIx8",ttl=%"PRIu8",frag=%s",
1124 IP_ARGS(ipv4_key->ipv4_src),
1125 IP_ARGS(ipv4_key->ipv4_dst),
1126 ipv4_key->ipv4_proto, ipv4_key->ipv4_tos,
1127 ipv4_key->ipv4_ttl,
1128 ovs_frag_type_to_string(ipv4_key->ipv4_frag));
1129 }
1130 break;
1131
1132 case OVS_KEY_ATTR_IPV6:
1133 if (!is_exact) {
1134 const struct ovs_key_ipv6 *ipv6_key, *ipv6_mask;
1135 char src_str[INET6_ADDRSTRLEN];
1136 char dst_str[INET6_ADDRSTRLEN];
1137 char src_mask[INET6_ADDRSTRLEN];
1138 char dst_mask[INET6_ADDRSTRLEN];
1139
1140 ipv6_key = nl_attr_get(a);
1141 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1142 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1143
1144 ipv6_mask = nl_attr_get(ma);
1145 inet_ntop(AF_INET6, ipv6_mask->ipv6_src, src_mask, sizeof src_mask);
1146 inet_ntop(AF_INET6, ipv6_mask->ipv6_dst, dst_mask, sizeof dst_mask);
1147
1148 ds_put_format(ds, "src=%s/%s,dst=%s/%s,label=%#"PRIx32"/%#"PRIx32
1149 ",proto=%"PRIu8"/%#"PRIx8",tclass=%#"PRIx8"/%#"PRIx8
1150 ",hlimit=%"PRIu8"/%#"PRIx8",frag=%s/%#"PRIx8,
1151 src_str, src_mask, dst_str, dst_mask,
1152 ntohl(ipv6_key->ipv6_label),
1153 ntohl(ipv6_mask->ipv6_label),
1154 ipv6_key->ipv6_proto, ipv6_mask->ipv6_proto,
1155 ipv6_key->ipv6_tclass, ipv6_mask->ipv6_tclass,
1156 ipv6_key->ipv6_hlimit, ipv6_mask->ipv6_hlimit,
1157 ovs_frag_type_to_string(ipv6_key->ipv6_frag),
1158 ipv6_mask->ipv6_frag);
1159 } else {
1160 const struct ovs_key_ipv6 *ipv6_key;
1161 char src_str[INET6_ADDRSTRLEN];
1162 char dst_str[INET6_ADDRSTRLEN];
1163
1164 ipv6_key = nl_attr_get(a);
1165 inet_ntop(AF_INET6, ipv6_key->ipv6_src, src_str, sizeof src_str);
1166 inet_ntop(AF_INET6, ipv6_key->ipv6_dst, dst_str, sizeof dst_str);
1167
1168 ds_put_format(ds, "src=%s,dst=%s,label=%#"PRIx32",proto=%"PRIu8
1169 ",tclass=%#"PRIx8",hlimit=%"PRIu8",frag=%s",
1170 src_str, dst_str, ntohl(ipv6_key->ipv6_label),
1171 ipv6_key->ipv6_proto, ipv6_key->ipv6_tclass,
1172 ipv6_key->ipv6_hlimit,
1173 ovs_frag_type_to_string(ipv6_key->ipv6_frag));
1174 }
1175 break;
1176
1177 case OVS_KEY_ATTR_TCP:
1178 if (!is_exact) {
1179 const struct ovs_key_tcp *tcp_mask = nl_attr_get(ma);
1180 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1181
1182 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1183 ",dst=%"PRIu16"/%#"PRIx16,
1184 ntohs(tcp_key->tcp_src), ntohs(tcp_mask->tcp_src),
1185 ntohs(tcp_key->tcp_dst), ntohs(tcp_mask->tcp_dst));
1186 } else {
1187 const struct ovs_key_tcp *tcp_key = nl_attr_get(a);
1188
1189 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1190 ntohs(tcp_key->tcp_src), ntohs(tcp_key->tcp_dst));
1191 }
1192 break;
1193
1194 case OVS_KEY_ATTR_UDP:
1195 if (!is_exact) {
1196 const struct ovs_key_udp *udp_mask = nl_attr_get(ma);
1197 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1198
1199 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1200 ",dst=%"PRIu16"/%#"PRIx16,
1201 ntohs(udp_key->udp_src), ntohs(udp_mask->udp_src),
1202 ntohs(udp_key->udp_dst), ntohs(udp_mask->udp_dst));
1203 } else {
1204 const struct ovs_key_udp *udp_key = nl_attr_get(a);
1205
1206 ds_put_format(ds, "src=%"PRIu16",dst=%"PRIu16,
1207 ntohs(udp_key->udp_src), ntohs(udp_key->udp_dst));
1208 }
1209 break;
1210
1211 case OVS_KEY_ATTR_SCTP:
1212 if (ma) {
1213 const struct ovs_key_sctp *sctp_mask = nl_attr_get(ma);
1214 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1215
1216 ds_put_format(ds, "src=%"PRIu16"/%#"PRIx16
1217 ",dst=%"PRIu16"/%#"PRIx16,
1218 ntohs(sctp_key->sctp_src), ntohs(sctp_mask->sctp_src),
1219 ntohs(sctp_key->sctp_dst), ntohs(sctp_mask->sctp_dst));
1220 } else {
1221 const struct ovs_key_sctp *sctp_key = nl_attr_get(a);
1222
1223 ds_put_format(ds, "(src=%"PRIu16",dst=%"PRIu16")",
1224 ntohs(sctp_key->sctp_src), ntohs(sctp_key->sctp_dst));
1225 }
1226 break;
1227
1228 case OVS_KEY_ATTR_ICMP:
1229 if (!is_exact) {
1230 const struct ovs_key_icmp *icmp_mask = nl_attr_get(ma);
1231 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1232
1233 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1234 icmp_key->icmp_type, icmp_mask->icmp_type,
1235 icmp_key->icmp_code, icmp_mask->icmp_code);
1236 } else {
1237 const struct ovs_key_icmp *icmp_key = nl_attr_get(a);
1238
1239 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1240 icmp_key->icmp_type, icmp_key->icmp_code);
1241 }
1242 break;
1243
1244 case OVS_KEY_ATTR_ICMPV6:
1245 if (!is_exact) {
1246 const struct ovs_key_icmpv6 *icmpv6_mask = nl_attr_get(ma);
1247 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1248
1249 ds_put_format(ds, "type=%"PRIu8"/%#"PRIx8",code=%"PRIu8"/%#"PRIx8,
1250 icmpv6_key->icmpv6_type, icmpv6_mask->icmpv6_type,
1251 icmpv6_key->icmpv6_code, icmpv6_mask->icmpv6_code);
1252 } else {
1253 const struct ovs_key_icmpv6 *icmpv6_key = nl_attr_get(a);
1254
1255 ds_put_format(ds, "type=%"PRIu8",code=%"PRIu8,
1256 icmpv6_key->icmpv6_type, icmpv6_key->icmpv6_code);
1257 }
1258 break;
1259
1260 case OVS_KEY_ATTR_ARP:
1261 if (!is_exact) {
1262 const struct ovs_key_arp *arp_mask = nl_attr_get(ma);
1263 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1264
1265 ds_put_format(ds, "sip="IP_FMT"/"IP_FMT",tip="IP_FMT"/"IP_FMT
1266 ",op=%"PRIu16"/%#"PRIx16
1267 ",sha="ETH_ADDR_FMT"/"ETH_ADDR_FMT
1268 ",tha="ETH_ADDR_FMT"/"ETH_ADDR_FMT,
1269 IP_ARGS(arp_key->arp_sip),
1270 IP_ARGS(arp_mask->arp_sip),
1271 IP_ARGS(arp_key->arp_tip),
1272 IP_ARGS(arp_mask->arp_tip),
1273 ntohs(arp_key->arp_op), ntohs(arp_mask->arp_op),
1274 ETH_ADDR_ARGS(arp_key->arp_sha),
1275 ETH_ADDR_ARGS(arp_mask->arp_sha),
1276 ETH_ADDR_ARGS(arp_key->arp_tha),
1277 ETH_ADDR_ARGS(arp_mask->arp_tha));
1278 } else {
1279 const struct ovs_key_arp *arp_key = nl_attr_get(a);
1280
1281 ds_put_format(ds, "sip="IP_FMT",tip="IP_FMT",op=%"PRIu16","
1282 "sha="ETH_ADDR_FMT",tha="ETH_ADDR_FMT,
1283 IP_ARGS(arp_key->arp_sip), IP_ARGS(arp_key->arp_tip),
1284 ntohs(arp_key->arp_op),
1285 ETH_ADDR_ARGS(arp_key->arp_sha),
1286 ETH_ADDR_ARGS(arp_key->arp_tha));
1287 }
1288 break;
1289
1290 case OVS_KEY_ATTR_ND: {
1291 const struct ovs_key_nd *nd_key, *nd_mask = NULL;
1292 char target[INET6_ADDRSTRLEN];
1293
1294 nd_key = nl_attr_get(a);
1295 if (!is_exact) {
1296 nd_mask = nl_attr_get(ma);
1297 }
1298
1299 inet_ntop(AF_INET6, nd_key->nd_target, target, sizeof target);
1300 ds_put_format(ds, "target=%s", target);
1301 if (!is_exact) {
1302 inet_ntop(AF_INET6, nd_mask->nd_target, target, sizeof target);
1303 ds_put_format(ds, "/%s", target);
1304 }
1305
1306 if (!eth_addr_is_zero(nd_key->nd_sll)) {
1307 ds_put_format(ds, ",sll="ETH_ADDR_FMT,
1308 ETH_ADDR_ARGS(nd_key->nd_sll));
1309 if (!is_exact) {
1310 ds_put_format(ds, "/"ETH_ADDR_FMT,
1311 ETH_ADDR_ARGS(nd_mask->nd_sll));
1312 }
1313 }
1314 if (!eth_addr_is_zero(nd_key->nd_tll)) {
1315 ds_put_format(ds, ",tll="ETH_ADDR_FMT,
1316 ETH_ADDR_ARGS(nd_key->nd_tll));
1317 if (!is_exact) {
1318 ds_put_format(ds, "/"ETH_ADDR_FMT,
1319 ETH_ADDR_ARGS(nd_mask->nd_tll));
1320 }
1321 }
1322 break;
1323 }
1324
1325 case OVS_KEY_ATTR_UNSPEC:
1326 case __OVS_KEY_ATTR_MAX:
1327 default:
1328 format_generic_odp_key(a, ds);
1329 if (!is_exact) {
1330 ds_put_char(ds, '/');
1331 format_generic_odp_key(ma, ds);
1332 }
1333 break;
1334 }
1335 ds_put_char(ds, ')');
1336 }
1337
1338 static struct nlattr *
1339 generate_all_wildcard_mask(struct ofpbuf *ofp, const struct nlattr *key)
1340 {
1341 const struct nlattr *a;
1342 unsigned int left;
1343 int type = nl_attr_type(key);
1344 int size = nl_attr_get_size(key);
1345
1346 if (odp_flow_key_attr_len(type) >=0) {
1347 memset(nl_msg_put_unspec_uninit(ofp, type, size), 0, size);
1348 } else {
1349 size_t nested_mask;
1350
1351 nested_mask = nl_msg_start_nested(ofp, type);
1352 NL_ATTR_FOR_EACH(a, left, key, nl_attr_get_size(key)) {
1353 generate_all_wildcard_mask(ofp, nl_attr_get(a));
1354 }
1355 nl_msg_end_nested(ofp, nested_mask);
1356 }
1357
1358 return ofp->base;
1359 }
1360
1361 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1362 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
1363 * 'mask_len' bytes of 'mask' which apply to 'key'. */
1364 void
1365 odp_flow_format(const struct nlattr *key, size_t key_len,
1366 const struct nlattr *mask, size_t mask_len,
1367 struct ds *ds, bool verbose)
1368 {
1369 if (key_len) {
1370 const struct nlattr *a;
1371 unsigned int left;
1372 bool has_ethtype_key = false;
1373 const struct nlattr *ma = NULL;
1374 struct ofpbuf ofp;
1375 bool first_field = true;
1376
1377 ofpbuf_init(&ofp, 100);
1378 NL_ATTR_FOR_EACH (a, left, key, key_len) {
1379 bool is_nested_attr;
1380 bool is_wildcard = false;
1381 int attr_type = nl_attr_type(a);
1382
1383 if (attr_type == OVS_KEY_ATTR_ETHERTYPE) {
1384 has_ethtype_key = true;
1385 }
1386
1387 is_nested_attr = (odp_flow_key_attr_len(attr_type) == -2);
1388
1389 if (mask && mask_len) {
1390 ma = nl_attr_find__(mask, mask_len, nl_attr_type(a));
1391 is_wildcard = ma ? odp_mask_attr_is_wildcard(ma) : true;
1392 }
1393
1394 if (verbose || !is_wildcard || is_nested_attr) {
1395 if (is_wildcard && !ma) {
1396 ma = generate_all_wildcard_mask(&ofp, a);
1397 }
1398 if (!first_field) {
1399 ds_put_char(ds, ',');
1400 }
1401 format_odp_key_attr(a, ma, ds, verbose);
1402 first_field = false;
1403 }
1404 ofpbuf_clear(&ofp);
1405 }
1406 ofpbuf_uninit(&ofp);
1407
1408 if (left) {
1409 int i;
1410
1411 if (left == key_len) {
1412 ds_put_cstr(ds, "<empty>");
1413 }
1414 ds_put_format(ds, ",***%u leftover bytes*** (", left);
1415 for (i = 0; i < left; i++) {
1416 ds_put_format(ds, "%02x", ((const uint8_t *) a)[i]);
1417 }
1418 ds_put_char(ds, ')');
1419 }
1420 if (!has_ethtype_key) {
1421 ma = nl_attr_find__(mask, mask_len, OVS_KEY_ATTR_ETHERTYPE);
1422 if (ma) {
1423 ds_put_format(ds, ",eth_type(0/0x%04"PRIx16")",
1424 ntohs(nl_attr_get_be16(ma)));
1425 }
1426 }
1427 } else {
1428 ds_put_cstr(ds, "<empty>");
1429 }
1430 }
1431
1432 /* Appends to 'ds' a string representation of the 'key_len' bytes of
1433 * OVS_KEY_ATTR_* attributes in 'key'. */
1434 void
1435 odp_flow_key_format(const struct nlattr *key,
1436 size_t key_len, struct ds *ds)
1437 {
1438 odp_flow_format(key, key_len, NULL, 0, ds, true);
1439 }
1440
1441 static void
1442 put_nd(struct ovs_key_nd* nd_key, const uint8_t *nd_sll,
1443 const uint8_t *nd_tll, struct ofpbuf *key)
1444 {
1445 if (nd_sll) {
1446 memcpy(nd_key->nd_sll, nd_sll, ETH_ADDR_LEN);
1447 }
1448
1449 if (nd_tll) {
1450 memcpy(nd_key->nd_tll, nd_tll, ETH_ADDR_LEN);
1451 }
1452
1453 nl_msg_put_unspec(key, OVS_KEY_ATTR_ND, nd_key, sizeof *nd_key);
1454 }
1455
1456 static int
1457 put_nd_key(int n, const char *nd_target_s, const uint8_t *nd_sll,
1458 const uint8_t *nd_tll, struct ofpbuf *key)
1459 {
1460 struct ovs_key_nd nd_key;
1461
1462 memset(&nd_key, 0, sizeof nd_key);
1463
1464 if (inet_pton(AF_INET6, nd_target_s, nd_key.nd_target) != 1) {
1465 return -EINVAL;
1466 }
1467
1468 put_nd(&nd_key, nd_sll, nd_tll, key);
1469 return n;
1470 }
1471
1472 static int
1473 put_nd_mask(int n, const char *nd_target_s,
1474 const uint8_t *nd_sll, const uint8_t *nd_tll, struct ofpbuf *mask)
1475 {
1476 struct ovs_key_nd nd_mask;
1477
1478 memset(&nd_mask, 0xff, sizeof nd_mask);
1479
1480 if (strlen(nd_target_s) != 0 &&
1481 inet_pton(AF_INET6, nd_target_s, nd_mask.nd_target) != 1) {
1482 return -EINVAL;
1483 }
1484
1485 put_nd(&nd_mask, nd_sll, nd_tll, mask);
1486 return n;
1487 }
1488
1489 static bool
1490 ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
1491 {
1492 if (!strcasecmp(s, "no")) {
1493 *type = OVS_FRAG_TYPE_NONE;
1494 } else if (!strcasecmp(s, "first")) {
1495 *type = OVS_FRAG_TYPE_FIRST;
1496 } else if (!strcasecmp(s, "later")) {
1497 *type = OVS_FRAG_TYPE_LATER;
1498 } else {
1499 return false;
1500 }
1501 return true;
1502 }
1503
1504 static ovs_be32
1505 mpls_lse_from_components(int mpls_label, int mpls_tc, int mpls_ttl, int mpls_bos)
1506 {
1507 return (htonl((mpls_label << MPLS_LABEL_SHIFT) |
1508 (mpls_tc << MPLS_TC_SHIFT) |
1509 (mpls_ttl << MPLS_TTL_SHIFT) |
1510 (mpls_bos << MPLS_BOS_SHIFT)));
1511 }
1512
1513 static int
1514 parse_odp_key_mask_attr(const char *s, const struct simap *port_names,
1515 struct ofpbuf *key, struct ofpbuf *mask)
1516 {
1517 /* Many of the sscanf calls in this function use oversized destination
1518 * fields because some sscanf() implementations truncate the range of %i
1519 * directives, so that e.g. "%"SCNi16 interprets input of "0xfedc" as a
1520 * value of 0x7fff. The other alternatives are to allow only a single
1521 * radix (e.g. decimal or hexadecimal) or to write more sophisticated
1522 * parsers.
1523 *
1524 * The tun_id parser has to use an alternative approach because there is no
1525 * type larger than 64 bits. */
1526
1527 {
1528 unsigned long long int priority;
1529 unsigned long long int priority_mask;
1530 int n = -1;
1531
1532 if (mask && sscanf(s, "skb_priority(%lli/%lli)%n", &priority,
1533 &priority_mask, &n) > 0 && n > 0) {
1534 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1535 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, priority_mask);
1536 return n;
1537 } else if (sscanf(s, "skb_priority(%lli)%n",
1538 &priority, &n) > 0 && n > 0) {
1539 nl_msg_put_u32(key, OVS_KEY_ATTR_PRIORITY, priority);
1540 if (mask) {
1541 nl_msg_put_u32(mask, OVS_KEY_ATTR_PRIORITY, UINT32_MAX);
1542 }
1543 return n;
1544 }
1545 }
1546
1547 {
1548 unsigned long long int mark;
1549 unsigned long long int mark_mask;
1550 int n = -1;
1551
1552 if (mask && sscanf(s, "skb_mark(%lli/%lli)%n", &mark,
1553 &mark_mask, &n) > 0 && n > 0) {
1554 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1555 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, mark_mask);
1556 return n;
1557 } else if (sscanf(s, "skb_mark(%lli)%n", &mark, &n) > 0 && n > 0) {
1558 nl_msg_put_u32(key, OVS_KEY_ATTR_SKB_MARK, mark);
1559 if (mask) {
1560 nl_msg_put_u32(mask, OVS_KEY_ATTR_SKB_MARK, UINT32_MAX);
1561 }
1562 return n;
1563 }
1564 }
1565
1566 {
1567 char tun_id_s[32];
1568 int tos, tos_mask, ttl, ttl_mask;
1569 struct flow_tnl tun_key, tun_key_mask;
1570 unsigned long long tun_id_mask;
1571 int n = -1;
1572
1573 if (mask && sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF]/%llx,"
1574 "src="IP_SCAN_FMT"/"IP_SCAN_FMT",dst="IP_SCAN_FMT
1575 "/"IP_SCAN_FMT",tos=%i/%i,ttl=%i/%i,flags%n",
1576 tun_id_s, &tun_id_mask,
1577 IP_SCAN_ARGS(&tun_key.ip_src),
1578 IP_SCAN_ARGS(&tun_key_mask.ip_src),
1579 IP_SCAN_ARGS(&tun_key.ip_dst),
1580 IP_SCAN_ARGS(&tun_key_mask.ip_dst),
1581 &tos, &tos_mask, &ttl, &ttl_mask,
1582 &n) > 0 && n > 0) {
1583 int res;
1584 uint32_t flags;
1585
1586 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1587 tun_key_mask.tun_id = htonll(tun_id_mask);
1588 tun_key.ip_tos = tos;
1589 tun_key_mask.ip_tos = tos_mask;
1590 tun_key.ip_ttl = ttl;
1591 tun_key_mask.ip_ttl = ttl_mask;
1592 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1593 tun_key.flags = flags;
1594 tun_key_mask.flags = UINT16_MAX;
1595
1596 if (res < 0) {
1597 return res;
1598 }
1599 n += res;
1600 if (s[n] != ')') {
1601 return -EINVAL;
1602 }
1603 n++;
1604 tun_key_to_attr(key, &tun_key);
1605 if (mask) {
1606 tun_key_to_attr(mask, &tun_key_mask);
1607 }
1608 return n;
1609 } else if (sscanf(s, "tunnel(tun_id=%31[x0123456789abcdefABCDEF],"
1610 "src="IP_SCAN_FMT",dst="IP_SCAN_FMT
1611 ",tos=%i,ttl=%i,flags%n", tun_id_s,
1612 IP_SCAN_ARGS(&tun_key.ip_src),
1613 IP_SCAN_ARGS(&tun_key.ip_dst), &tos, &ttl,
1614 &n) > 0 && n > 0) {
1615 int res;
1616 uint32_t flags;
1617
1618 tun_key.tun_id = htonll(strtoull(tun_id_s, NULL, 0));
1619 tun_key.ip_tos = tos;
1620 tun_key.ip_ttl = ttl;
1621 res = parse_flags(&s[n], flow_tun_flag_to_string, &flags);
1622 tun_key.flags = flags;
1623
1624 if (res < 0) {
1625 return res;
1626 }
1627 n += res;
1628 if (s[n] != ')') {
1629 return -EINVAL;
1630 }
1631 n++;
1632 tun_key_to_attr(key, &tun_key);
1633
1634 if (mask) {
1635 memset(&tun_key, 0xff, sizeof tun_key);
1636 tun_key_to_attr(mask, &tun_key);
1637 }
1638 return n;
1639 }
1640 }
1641
1642 {
1643 unsigned long long int in_port;
1644 unsigned long long int in_port_mask;
1645 int n = -1;
1646
1647 if (mask && sscanf(s, "in_port(%lli/%lli)%n", &in_port,
1648 &in_port_mask, &n) > 0 && n > 0) {
1649 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1650 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, in_port_mask);
1651 return n;
1652 } else if (sscanf(s, "in_port(%lli)%n", &in_port, &n) > 0 && n > 0) {
1653 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, in_port);
1654 if (mask) {
1655 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1656 }
1657 return n;
1658 }
1659 }
1660
1661
1662 if (port_names && !strncmp(s, "in_port(", 8)) {
1663 const char *name;
1664 const struct simap_node *node;
1665 int name_len;
1666
1667 name = s + 8;
1668 name_len = strcspn(s, ")");
1669 node = simap_find_len(port_names, name, name_len);
1670 if (node) {
1671 nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
1672
1673 if (mask) {
1674 nl_msg_put_u32(mask, OVS_KEY_ATTR_IN_PORT, UINT32_MAX);
1675 }
1676 return 8 + name_len + 1;
1677 }
1678 }
1679
1680 {
1681 struct ovs_key_ethernet eth_key;
1682 struct ovs_key_ethernet eth_key_mask;
1683 int n = -1;
1684
1685 if (mask && sscanf(s,
1686 "eth(src="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
1687 "dst="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
1688 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1689 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_src),
1690 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst),
1691 ETH_ADDR_SCAN_ARGS(eth_key_mask.eth_dst), &n) > 0 && n > 0) {
1692
1693 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1694 &eth_key, sizeof eth_key);
1695 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1696 &eth_key_mask, sizeof eth_key_mask);
1697 return n;
1698 } else if (sscanf(s,
1699 "eth(src="ETH_ADDR_SCAN_FMT",dst="ETH_ADDR_SCAN_FMT")%n",
1700 ETH_ADDR_SCAN_ARGS(eth_key.eth_src),
1701 ETH_ADDR_SCAN_ARGS(eth_key.eth_dst), &n) > 0 && n > 0) {
1702 nl_msg_put_unspec(key, OVS_KEY_ATTR_ETHERNET,
1703 &eth_key, sizeof eth_key);
1704
1705 if (mask) {
1706 memset(&eth_key, 0xff, sizeof eth_key);
1707 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ETHERNET,
1708 &eth_key, sizeof eth_key);
1709 }
1710 return n;
1711 }
1712 }
1713
1714 {
1715 uint16_t vid, vid_mask;
1716 int pcp, pcp_mask;
1717 int cfi, cfi_mask;
1718 int n = -1;
1719
1720 if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i)%n",
1721 &vid, &vid_mask, &pcp, &pcp_mask, &n) > 0 && n > 0)) {
1722 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1723 htons((vid << VLAN_VID_SHIFT) |
1724 (pcp << VLAN_PCP_SHIFT) |
1725 VLAN_CFI));
1726 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1727 htons((vid_mask << VLAN_VID_SHIFT) |
1728 (pcp_mask << VLAN_PCP_SHIFT) |
1729 (1 << VLAN_CFI_SHIFT)));
1730 return n;
1731 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i)%n",
1732 &vid, &pcp, &n) > 0 && n > 0)) {
1733 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1734 htons((vid << VLAN_VID_SHIFT) |
1735 (pcp << VLAN_PCP_SHIFT) |
1736 VLAN_CFI));
1737 if (mask) {
1738 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1739 }
1740 return n;
1741 } else if (mask && (sscanf(s, "vlan(vid=%"SCNi16"/%"SCNi16",pcp=%i/%i,cfi=%i/%i)%n",
1742 &vid, &vid_mask, &pcp, &pcp_mask, &cfi, &cfi_mask, &n) > 0 && n > 0)) {
1743 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1744 htons((vid << VLAN_VID_SHIFT) |
1745 (pcp << VLAN_PCP_SHIFT) |
1746 (cfi ? VLAN_CFI : 0)));
1747 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN,
1748 htons((vid_mask << VLAN_VID_SHIFT) |
1749 (pcp_mask << VLAN_PCP_SHIFT) |
1750 (cfi_mask << VLAN_CFI_SHIFT)));
1751 return n;
1752 } else if ((sscanf(s, "vlan(vid=%"SCNi16",pcp=%i,cfi=%i)%n",
1753 &vid, &pcp, &cfi, &n) > 0 && n > 0)) {
1754 nl_msg_put_be16(key, OVS_KEY_ATTR_VLAN,
1755 htons((vid << VLAN_VID_SHIFT) |
1756 (pcp << VLAN_PCP_SHIFT) |
1757 (cfi ? VLAN_CFI : 0)));
1758 if (mask) {
1759 nl_msg_put_be16(mask, OVS_KEY_ATTR_VLAN, OVS_BE16_MAX);
1760 }
1761 return n;
1762 }
1763 }
1764
1765 {
1766 int eth_type;
1767 int eth_type_mask;
1768 int n = -1;
1769
1770 if (mask && sscanf(s, "eth_type(%i/%i)%n",
1771 &eth_type, &eth_type_mask, &n) > 0 && n > 0) {
1772 if (eth_type != 0) {
1773 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1774 }
1775 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type_mask));
1776 return n;
1777 } else if (sscanf(s, "eth_type(%i)%n", &eth_type, &n) > 0 && n > 0) {
1778 nl_msg_put_be16(key, OVS_KEY_ATTR_ETHERTYPE, htons(eth_type));
1779 if (mask) {
1780 nl_msg_put_be16(mask, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
1781 }
1782 return n;
1783 }
1784 }
1785
1786 {
1787 int label, tc, ttl, bos;
1788 int label_mask, tc_mask, ttl_mask, bos_mask;
1789 int n = -1;
1790
1791 if (mask && sscanf(s, "mpls(label=%"SCNi32"/%"SCNi32",tc=%i/%i,ttl=%i/%i,bos=%i/%i)%n",
1792 &label, &label_mask, &tc, &tc_mask, &ttl, &ttl_mask, &bos, &bos_mask, &n) > 0 && n > 0) {
1793 struct ovs_key_mpls *mpls, *mpls_mask;
1794
1795 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1796 sizeof *mpls);
1797 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1798
1799 mpls_mask = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1800 sizeof *mpls_mask);
1801 mpls_mask->mpls_lse = mpls_lse_from_components(
1802 label_mask, tc_mask, ttl_mask, bos_mask);
1803 return n;
1804 } else if (sscanf(s, "mpls(label=%"SCNi32",tc=%i,ttl=%i,bos=%i)%n",
1805 &label, &tc, &ttl, &bos, &n) > 0 &&
1806 n > 0) {
1807 struct ovs_key_mpls *mpls;
1808
1809 mpls = nl_msg_put_unspec_uninit(key, OVS_KEY_ATTR_MPLS,
1810 sizeof *mpls);
1811 mpls->mpls_lse = mpls_lse_from_components(label, tc, ttl, bos);
1812 if (mask) {
1813 mpls = nl_msg_put_unspec_uninit(mask, OVS_KEY_ATTR_MPLS,
1814 sizeof *mpls);
1815 mpls->mpls_lse = OVS_BE32_MAX;
1816 }
1817 return n;
1818 }
1819 }
1820
1821
1822 {
1823 ovs_be32 ipv4_src, ipv4_src_mask;
1824 ovs_be32 ipv4_dst, ipv4_dst_mask;
1825 int ipv4_proto, ipv4_proto_mask;
1826 int ipv4_tos, ipv4_tos_mask;
1827 int ipv4_ttl, ipv4_ttl_mask;
1828 char frag[8];
1829 int ipv4_frag_mask;
1830 enum ovs_frag_type ipv4_frag;
1831 int n = -1;
1832
1833 if (mask && sscanf(s, "ipv4(src="IP_SCAN_FMT"/"IP_SCAN_FMT","
1834 "dst="IP_SCAN_FMT"/"IP_SCAN_FMT","
1835 "proto=%i/%i,tos=%i/%i,ttl=%i/%i,"
1836 "frag=%7[a-z]/%i)%n",
1837 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_src_mask),
1838 IP_SCAN_ARGS(&ipv4_dst), IP_SCAN_ARGS(&ipv4_dst_mask),
1839 &ipv4_proto, &ipv4_proto_mask,
1840 &ipv4_tos, &ipv4_tos_mask, &ipv4_ttl, &ipv4_ttl_mask,
1841 frag, &ipv4_frag_mask, &n) > 0
1842 && n > 0
1843 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1844 struct ovs_key_ipv4 ipv4_key;
1845 struct ovs_key_ipv4 ipv4_mask;
1846
1847 ipv4_key.ipv4_src = ipv4_src;
1848 ipv4_key.ipv4_dst = ipv4_dst;
1849 ipv4_key.ipv4_proto = ipv4_proto;
1850 ipv4_key.ipv4_tos = ipv4_tos;
1851 ipv4_key.ipv4_ttl = ipv4_ttl;
1852 ipv4_key.ipv4_frag = ipv4_frag;
1853 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1854 &ipv4_key, sizeof ipv4_key);
1855
1856 ipv4_mask.ipv4_src = ipv4_src_mask;
1857 ipv4_mask.ipv4_dst = ipv4_dst_mask;
1858 ipv4_mask.ipv4_proto = ipv4_proto_mask;
1859 ipv4_mask.ipv4_tos = ipv4_tos_mask;
1860 ipv4_mask.ipv4_ttl = ipv4_ttl_mask;
1861 ipv4_mask.ipv4_frag = ipv4_frag_mask;
1862 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1863 &ipv4_mask, sizeof ipv4_mask);
1864 return n;
1865 } else if (sscanf(s, "ipv4(src="IP_SCAN_FMT",dst="IP_SCAN_FMT","
1866 "proto=%i,tos=%i,ttl=%i,frag=%7[a-z])%n",
1867 IP_SCAN_ARGS(&ipv4_src), IP_SCAN_ARGS(&ipv4_dst),
1868 &ipv4_proto, &ipv4_tos, &ipv4_ttl, frag, &n) > 0
1869 && n > 0
1870 && ovs_frag_type_from_string(frag, &ipv4_frag)) {
1871 struct ovs_key_ipv4 ipv4_key;
1872
1873 ipv4_key.ipv4_src = ipv4_src;
1874 ipv4_key.ipv4_dst = ipv4_dst;
1875 ipv4_key.ipv4_proto = ipv4_proto;
1876 ipv4_key.ipv4_tos = ipv4_tos;
1877 ipv4_key.ipv4_ttl = ipv4_ttl;
1878 ipv4_key.ipv4_frag = ipv4_frag;
1879 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV4,
1880 &ipv4_key, sizeof ipv4_key);
1881
1882 if (mask) {
1883 memset(&ipv4_key, 0xff, sizeof ipv4_key);
1884 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV4,
1885 &ipv4_key, sizeof ipv4_key);
1886 }
1887 return n;
1888 }
1889 }
1890
1891 {
1892 char ipv6_src_s[IPV6_SCAN_LEN + 1];
1893 char ipv6_src_mask_s[IPV6_SCAN_LEN + 1];
1894 char ipv6_dst_s[IPV6_SCAN_LEN + 1];
1895 char ipv6_dst_mask_s[IPV6_SCAN_LEN + 1];
1896 int ipv6_label, ipv6_label_mask;
1897 int ipv6_proto, ipv6_proto_mask;
1898 int ipv6_tclass, ipv6_tclass_mask;
1899 int ipv6_hlimit, ipv6_hlimit_mask;
1900 char frag[8];
1901 enum ovs_frag_type ipv6_frag;
1902 int ipv6_frag_mask;
1903 int n = -1;
1904
1905 if (mask && sscanf(s, "ipv6(src="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT",dst="
1906 IPV6_SCAN_FMT"/"IPV6_SCAN_FMT","
1907 "label=%i/%i,proto=%i/%i,tclass=%i/%i,"
1908 "hlimit=%i/%i,frag=%7[a-z]/%i)%n",
1909 ipv6_src_s, ipv6_src_mask_s, ipv6_dst_s, ipv6_dst_mask_s,
1910 &ipv6_label, &ipv6_label_mask, &ipv6_proto,
1911 &ipv6_proto_mask, &ipv6_tclass, &ipv6_tclass_mask,
1912 &ipv6_hlimit, &ipv6_hlimit_mask, frag,
1913 &ipv6_frag_mask, &n) > 0
1914 && n > 0
1915 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1916 struct ovs_key_ipv6 ipv6_key;
1917 struct ovs_key_ipv6 ipv6_mask;
1918
1919 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1920 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1 ||
1921 inet_pton(AF_INET6, ipv6_src_mask_s, &ipv6_mask.ipv6_src) != 1 ||
1922 inet_pton(AF_INET6, ipv6_dst_mask_s, &ipv6_mask.ipv6_dst) != 1) {
1923 return -EINVAL;
1924 }
1925
1926 ipv6_key.ipv6_label = htonl(ipv6_label);
1927 ipv6_key.ipv6_proto = ipv6_proto;
1928 ipv6_key.ipv6_tclass = ipv6_tclass;
1929 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1930 ipv6_key.ipv6_frag = ipv6_frag;
1931 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1932 &ipv6_key, sizeof ipv6_key);
1933
1934 ipv6_mask.ipv6_label = htonl(ipv6_label_mask);
1935 ipv6_mask.ipv6_proto = ipv6_proto_mask;
1936 ipv6_mask.ipv6_tclass = ipv6_tclass_mask;
1937 ipv6_mask.ipv6_hlimit = ipv6_hlimit_mask;
1938 ipv6_mask.ipv6_frag = ipv6_frag_mask;
1939 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1940 &ipv6_mask, sizeof ipv6_mask);
1941 return n;
1942 } else if (sscanf(s, "ipv6(src="IPV6_SCAN_FMT",dst="IPV6_SCAN_FMT","
1943 "label=%i,proto=%i,tclass=%i,hlimit=%i,frag=%7[a-z])%n",
1944 ipv6_src_s, ipv6_dst_s, &ipv6_label,
1945 &ipv6_proto, &ipv6_tclass, &ipv6_hlimit, frag, &n) > 0
1946 && n > 0
1947 && ovs_frag_type_from_string(frag, &ipv6_frag)) {
1948 struct ovs_key_ipv6 ipv6_key;
1949
1950 if (inet_pton(AF_INET6, ipv6_src_s, &ipv6_key.ipv6_src) != 1 ||
1951 inet_pton(AF_INET6, ipv6_dst_s, &ipv6_key.ipv6_dst) != 1) {
1952 return -EINVAL;
1953 }
1954 ipv6_key.ipv6_label = htonl(ipv6_label);
1955 ipv6_key.ipv6_proto = ipv6_proto;
1956 ipv6_key.ipv6_tclass = ipv6_tclass;
1957 ipv6_key.ipv6_hlimit = ipv6_hlimit;
1958 ipv6_key.ipv6_frag = ipv6_frag;
1959 nl_msg_put_unspec(key, OVS_KEY_ATTR_IPV6,
1960 &ipv6_key, sizeof ipv6_key);
1961
1962 if (mask) {
1963 memset(&ipv6_key, 0xff, sizeof ipv6_key);
1964 nl_msg_put_unspec(mask, OVS_KEY_ATTR_IPV6,
1965 &ipv6_key, sizeof ipv6_key);
1966 }
1967 return n;
1968 }
1969 }
1970
1971 {
1972 int tcp_src;
1973 int tcp_dst;
1974 int tcp_src_mask;
1975 int tcp_dst_mask;
1976 int n = -1;
1977
1978 if (mask && sscanf(s, "tcp(src=%i/%i,dst=%i/%i)%n",
1979 &tcp_src, &tcp_src_mask, &tcp_dst, &tcp_dst_mask, &n) > 0
1980 && n > 0) {
1981 struct ovs_key_tcp tcp_key;
1982 struct ovs_key_tcp tcp_mask;
1983
1984 tcp_key.tcp_src = htons(tcp_src);
1985 tcp_key.tcp_dst = htons(tcp_dst);
1986 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
1987
1988 tcp_mask.tcp_src = htons(tcp_src_mask);
1989 tcp_mask.tcp_dst = htons(tcp_dst_mask);
1990 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
1991 &tcp_mask, sizeof tcp_mask);
1992 return n;
1993 } else if (sscanf(s, "tcp(src=%i,dst=%i)%n",&tcp_src, &tcp_dst, &n) > 0
1994 && n > 0) {
1995 struct ovs_key_tcp tcp_key;
1996
1997 tcp_key.tcp_src = htons(tcp_src);
1998 tcp_key.tcp_dst = htons(tcp_dst);
1999 nl_msg_put_unspec(key, OVS_KEY_ATTR_TCP, &tcp_key, sizeof tcp_key);
2000
2001 if (mask) {
2002 memset(&tcp_key, 0xff, sizeof tcp_key);
2003 nl_msg_put_unspec(mask, OVS_KEY_ATTR_TCP,
2004 &tcp_key, sizeof tcp_key);
2005 }
2006 return n;
2007 }
2008 }
2009
2010 {
2011 int udp_src;
2012 int udp_dst;
2013 int udp_src_mask;
2014 int udp_dst_mask;
2015 int n = -1;
2016
2017 if (mask && sscanf(s, "udp(src=%i/%i,dst=%i/%i)%n",
2018 &udp_src, &udp_src_mask,
2019 &udp_dst, &udp_dst_mask, &n) > 0 && n > 0) {
2020 struct ovs_key_udp udp_key;
2021 struct ovs_key_udp udp_mask;
2022
2023 udp_key.udp_src = htons(udp_src);
2024 udp_key.udp_dst = htons(udp_dst);
2025 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2026
2027 udp_mask.udp_src = htons(udp_src_mask);
2028 udp_mask.udp_dst = htons(udp_dst_mask);
2029 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP,
2030 &udp_mask, sizeof udp_mask);
2031 return n;
2032 }
2033 if (sscanf(s, "udp(src=%i,dst=%i)%n", &udp_src, &udp_dst, &n) > 0
2034 && n > 0) {
2035 struct ovs_key_udp udp_key;
2036
2037 udp_key.udp_src = htons(udp_src);
2038 udp_key.udp_dst = htons(udp_dst);
2039 nl_msg_put_unspec(key, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2040
2041 if (mask) {
2042 memset(&udp_key, 0xff, sizeof udp_key);
2043 nl_msg_put_unspec(mask, OVS_KEY_ATTR_UDP, &udp_key, sizeof udp_key);
2044 }
2045 return n;
2046 }
2047 }
2048
2049 {
2050 int sctp_src;
2051 int sctp_dst;
2052 int sctp_src_mask;
2053 int sctp_dst_mask;
2054 int n = -1;
2055
2056 if (mask && sscanf(s, "sctp(src=%i/%i,dst=%i/%i)%n",
2057 &sctp_src, &sctp_src_mask,
2058 &sctp_dst, &sctp_dst_mask, &n) > 0 && n > 0) {
2059 struct ovs_key_sctp sctp_key;
2060 struct ovs_key_sctp sctp_mask;
2061
2062 sctp_key.sctp_src = htons(sctp_src);
2063 sctp_key.sctp_dst = htons(sctp_dst);
2064 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2065
2066 sctp_mask.sctp_src = htons(sctp_src_mask);
2067 sctp_mask.sctp_dst = htons(sctp_dst_mask);
2068 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP,
2069 &sctp_mask, sizeof sctp_mask);
2070 return n;
2071 }
2072 if (sscanf(s, "sctp(src=%i,dst=%i)%n", &sctp_src, &sctp_dst, &n) > 0
2073 && n > 0) {
2074 struct ovs_key_sctp sctp_key;
2075
2076 sctp_key.sctp_src = htons(sctp_src);
2077 sctp_key.sctp_dst = htons(sctp_dst);
2078 nl_msg_put_unspec(key, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2079
2080 if (mask) {
2081 memset(&sctp_key, 0xff, sizeof sctp_key);
2082 nl_msg_put_unspec(mask, OVS_KEY_ATTR_SCTP, &sctp_key, sizeof sctp_key);
2083 }
2084 return n;
2085 }
2086 }
2087
2088 {
2089 int icmp_type;
2090 int icmp_code;
2091 int icmp_type_mask;
2092 int icmp_code_mask;
2093 int n = -1;
2094
2095 if (mask && sscanf(s, "icmp(type=%i/%i,code=%i/%i)%n",
2096 &icmp_type, &icmp_type_mask,
2097 &icmp_code, &icmp_code_mask, &n) > 0 && n > 0) {
2098 struct ovs_key_icmp icmp_key;
2099 struct ovs_key_icmp icmp_mask;
2100
2101 icmp_key.icmp_type = icmp_type;
2102 icmp_key.icmp_code = icmp_code;
2103 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2104 &icmp_key, sizeof icmp_key);
2105
2106 icmp_mask.icmp_type = icmp_type_mask;
2107 icmp_mask.icmp_code = icmp_code_mask;
2108 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP,
2109 &icmp_mask, sizeof icmp_mask);
2110 return n;
2111 } else if (sscanf(s, "icmp(type=%i,code=%i)%n",
2112 &icmp_type, &icmp_code, &n) > 0
2113 && n > 0) {
2114 struct ovs_key_icmp icmp_key;
2115
2116 icmp_key.icmp_type = icmp_type;
2117 icmp_key.icmp_code = icmp_code;
2118 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMP,
2119 &icmp_key, sizeof icmp_key);
2120 if (mask) {
2121 memset(&icmp_key, 0xff, sizeof icmp_key);
2122 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMP, &icmp_key,
2123 sizeof icmp_key);
2124 }
2125 return n;
2126 }
2127 }
2128
2129 {
2130 struct ovs_key_icmpv6 icmpv6_key;
2131 struct ovs_key_icmpv6 icmpv6_mask;
2132 int icmpv6_type_mask;
2133 int icmpv6_code_mask;
2134 int n = -1;
2135
2136 if (mask && sscanf(s, "icmpv6(type=%"SCNi8"/%i,code=%"SCNi8"/%i)%n",
2137 &icmpv6_key.icmpv6_type, &icmpv6_type_mask,
2138 &icmpv6_key.icmpv6_code, &icmpv6_code_mask, &n) > 0
2139 && n > 0) {
2140 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2141 &icmpv6_key, sizeof icmpv6_key);
2142
2143 icmpv6_mask.icmpv6_type = icmpv6_type_mask;
2144 icmpv6_mask.icmpv6_code = icmpv6_code_mask;
2145 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_mask,
2146 sizeof icmpv6_mask);
2147 return n;
2148 } else if (sscanf(s, "icmpv6(type=%"SCNi8",code=%"SCNi8")%n",
2149 &icmpv6_key.icmpv6_type, &icmpv6_key.icmpv6_code,&n) > 0
2150 && n > 0) {
2151 nl_msg_put_unspec(key, OVS_KEY_ATTR_ICMPV6,
2152 &icmpv6_key, sizeof icmpv6_key);
2153
2154 if (mask) {
2155 memset(&icmpv6_key, 0xff, sizeof icmpv6_key);
2156 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ICMPV6, &icmpv6_key,
2157 sizeof icmpv6_key);
2158 }
2159 return n;
2160 }
2161 }
2162
2163 {
2164 ovs_be32 arp_sip, arp_sip_mask;
2165 ovs_be32 arp_tip, arp_tip_mask;
2166 int arp_op, arp_op_mask;
2167 uint8_t arp_sha[ETH_ADDR_LEN];
2168 uint8_t arp_sha_mask[ETH_ADDR_LEN];
2169 uint8_t arp_tha[ETH_ADDR_LEN];
2170 uint8_t arp_tha_mask[ETH_ADDR_LEN];
2171 int n = -1;
2172
2173 if (mask && sscanf(s, "arp(sip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2174 "tip="IP_SCAN_FMT"/"IP_SCAN_FMT","
2175 "op=%i/%i,sha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2176 "tha="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2177 IP_SCAN_ARGS(&arp_sip), IP_SCAN_ARGS(&arp_sip_mask),
2178 IP_SCAN_ARGS(&arp_tip), IP_SCAN_ARGS(&arp_tip_mask),
2179 &arp_op, &arp_op_mask,
2180 ETH_ADDR_SCAN_ARGS(arp_sha),
2181 ETH_ADDR_SCAN_ARGS(arp_sha_mask),
2182 ETH_ADDR_SCAN_ARGS(arp_tha),
2183 ETH_ADDR_SCAN_ARGS(arp_tha_mask), &n) > 0 && n > 0) {
2184 struct ovs_key_arp arp_key;
2185 struct ovs_key_arp arp_mask;
2186
2187 memset(&arp_key, 0, sizeof arp_key);
2188 arp_key.arp_sip = arp_sip;
2189 arp_key.arp_tip = arp_tip;
2190 arp_key.arp_op = htons(arp_op);
2191 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2192 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2193 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2194
2195 arp_mask.arp_sip = arp_sip_mask;
2196 arp_mask.arp_tip = arp_tip_mask;
2197 arp_mask.arp_op = htons(arp_op_mask);
2198 memcpy(arp_mask.arp_sha, arp_sha_mask, ETH_ADDR_LEN);
2199 memcpy(arp_mask.arp_tha, arp_tha_mask, ETH_ADDR_LEN);
2200 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2201 &arp_mask, sizeof arp_mask);
2202 return n;
2203 } else if (sscanf(s, "arp(sip="IP_SCAN_FMT",tip="IP_SCAN_FMT","
2204 "op=%i,sha="ETH_ADDR_SCAN_FMT",tha="ETH_ADDR_SCAN_FMT")%n",
2205 IP_SCAN_ARGS(&arp_sip),
2206 IP_SCAN_ARGS(&arp_tip),
2207 &arp_op,
2208 ETH_ADDR_SCAN_ARGS(arp_sha),
2209 ETH_ADDR_SCAN_ARGS(arp_tha), &n) > 0 && n > 0) {
2210 struct ovs_key_arp arp_key;
2211
2212 memset(&arp_key, 0, sizeof arp_key);
2213 arp_key.arp_sip = arp_sip;
2214 arp_key.arp_tip = arp_tip;
2215 arp_key.arp_op = htons(arp_op);
2216 memcpy(arp_key.arp_sha, arp_sha, ETH_ADDR_LEN);
2217 memcpy(arp_key.arp_tha, arp_tha, ETH_ADDR_LEN);
2218 nl_msg_put_unspec(key, OVS_KEY_ATTR_ARP, &arp_key, sizeof arp_key);
2219
2220 if (mask) {
2221 memset(&arp_key, 0xff, sizeof arp_key);
2222 nl_msg_put_unspec(mask, OVS_KEY_ATTR_ARP,
2223 &arp_key, sizeof arp_key);
2224 }
2225 return n;
2226 }
2227 }
2228
2229 {
2230 char nd_target_s[IPV6_SCAN_LEN + 1];
2231 char nd_target_mask_s[IPV6_SCAN_LEN + 1];
2232 uint8_t nd_sll[ETH_ADDR_LEN];
2233 uint8_t nd_sll_mask[ETH_ADDR_LEN];
2234 uint8_t nd_tll[ETH_ADDR_LEN];
2235 uint8_t nd_tll_mask[ETH_ADDR_LEN];
2236 int n = -1;
2237
2238 nd_target_mask_s[0] = 0;
2239 memset(nd_sll_mask, 0xff, sizeof nd_sll_mask);
2240 memset(nd_tll_mask, 0xff, sizeof nd_tll_mask);
2241
2242 if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT")%n",
2243 nd_target_s, nd_target_mask_s, &n) > 0 && n > 0) {
2244 put_nd_key(n, nd_target_s, NULL, NULL, key);
2245 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2246 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT")%n",
2247 nd_target_s, &n) > 0 && n > 0) {
2248 put_nd_key(n, nd_target_s, NULL, NULL, key);
2249 if (mask) {
2250 put_nd_mask(n, nd_target_mask_s, NULL, NULL, mask);
2251 }
2252 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2253 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2254 nd_target_s, nd_target_mask_s,
2255 ETH_ADDR_SCAN_ARGS(nd_sll),
2256 ETH_ADDR_SCAN_ARGS(nd_sll_mask), &n) > 0 && n > 0) {
2257 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2258 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2259 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT")%n",
2260 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll), &n) > 0
2261 && n > 0) {
2262 put_nd_key(n, nd_target_s, nd_sll, NULL, key);
2263 if (mask) {
2264 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, NULL, mask);
2265 }
2266 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2267 ",tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2268 nd_target_s, nd_target_mask_s,
2269 ETH_ADDR_SCAN_ARGS(nd_tll),
2270 ETH_ADDR_SCAN_ARGS(nd_tll_mask), &n) > 0 && n > 0) {
2271 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2272 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2273 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",tll="ETH_ADDR_SCAN_FMT")%n",
2274 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2275 && n > 0) {
2276 put_nd_key(n, nd_target_s, NULL, nd_tll, key);
2277 if (mask) {
2278 put_nd_mask(n, nd_target_mask_s, NULL, nd_tll_mask, mask);
2279 }
2280 } else if (mask && sscanf(s, "nd(target="IPV6_SCAN_FMT"/"IPV6_SCAN_FMT
2281 ",sll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT","
2282 "tll="ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT")%n",
2283 nd_target_s, nd_target_mask_s,
2284 ETH_ADDR_SCAN_ARGS(nd_sll), ETH_ADDR_SCAN_ARGS(nd_sll_mask),
2285 ETH_ADDR_SCAN_ARGS(nd_tll), ETH_ADDR_SCAN_ARGS(nd_tll_mask),
2286 &n) > 0
2287 && n > 0) {
2288 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2289 put_nd_mask(n, nd_target_mask_s, nd_sll_mask, nd_tll_mask, mask);
2290 } else if (sscanf(s, "nd(target="IPV6_SCAN_FMT",sll="ETH_ADDR_SCAN_FMT","
2291 "tll="ETH_ADDR_SCAN_FMT")%n",
2292 nd_target_s, ETH_ADDR_SCAN_ARGS(nd_sll),
2293 ETH_ADDR_SCAN_ARGS(nd_tll), &n) > 0
2294 && n > 0) {
2295 put_nd_key(n, nd_target_s, nd_sll, nd_tll, key);
2296 if (mask) {
2297 put_nd_mask(n, nd_target_mask_s,
2298 nd_sll_mask, nd_tll_mask, mask);
2299 }
2300 }
2301
2302 if (n != -1)
2303 return n;
2304
2305 }
2306
2307 if (!strncmp(s, "encap(", 6)) {
2308 const char *start = s;
2309 size_t encap, encap_mask = 0;
2310
2311 encap = nl_msg_start_nested(key, OVS_KEY_ATTR_ENCAP);
2312 if (mask) {
2313 encap_mask = nl_msg_start_nested(mask, OVS_KEY_ATTR_ENCAP);
2314 }
2315
2316 s += 6;
2317 for (;;) {
2318 int retval;
2319
2320 s += strspn(s, ", \t\r\n");
2321 if (!*s) {
2322 return -EINVAL;
2323 } else if (*s == ')') {
2324 break;
2325 }
2326
2327 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2328 if (retval < 0) {
2329 return retval;
2330 }
2331 s += retval;
2332 }
2333 s++;
2334
2335 nl_msg_end_nested(key, encap);
2336 if (mask) {
2337 nl_msg_end_nested(mask, encap_mask);
2338 }
2339
2340 return s - start;
2341 }
2342
2343 return -EINVAL;
2344 }
2345
2346 /* Parses the string representation of a datapath flow key, in the
2347 * format output by odp_flow_key_format(). Returns 0 if successful,
2348 * otherwise a positive errno value. On success, the flow key is
2349 * appended to 'key' as a series of Netlink attributes. On failure, no
2350 * data is appended to 'key'. Either way, 'key''s data might be
2351 * reallocated.
2352 *
2353 * If 'port_names' is nonnull, it points to an simap that maps from a port name
2354 * to a port number. (Port names may be used instead of port numbers in
2355 * in_port.)
2356 *
2357 * On success, the attributes appended to 'key' are individually syntactically
2358 * valid, but they may not be valid as a sequence. 'key' might, for example,
2359 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
2360 int
2361 odp_flow_from_string(const char *s, const struct simap *port_names,
2362 struct ofpbuf *key, struct ofpbuf *mask)
2363 {
2364 const size_t old_size = key->size;
2365 for (;;) {
2366 int retval;
2367
2368 s += strspn(s, delimiters);
2369 if (!*s) {
2370 return 0;
2371 }
2372
2373 retval = parse_odp_key_mask_attr(s, port_names, key, mask);
2374 if (retval < 0) {
2375 key->size = old_size;
2376 return -retval;
2377 }
2378 s += retval;
2379 }
2380
2381 return 0;
2382 }
2383
2384 static uint8_t
2385 ovs_to_odp_frag(uint8_t nw_frag)
2386 {
2387 return (nw_frag == 0 ? OVS_FRAG_TYPE_NONE
2388 : nw_frag == FLOW_NW_FRAG_ANY ? OVS_FRAG_TYPE_FIRST
2389 : OVS_FRAG_TYPE_LATER);
2390 }
2391
2392 static uint8_t
2393 ovs_to_odp_frag_mask(uint8_t nw_frag_mask)
2394 {
2395 uint8_t frag_mask = ~(OVS_FRAG_TYPE_FIRST | OVS_FRAG_TYPE_LATER);
2396
2397 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_ANY) ? OVS_FRAG_TYPE_FIRST : 0;
2398 frag_mask |= (nw_frag_mask & FLOW_NW_FRAG_LATER) ? OVS_FRAG_TYPE_LATER : 0;
2399
2400 return frag_mask;
2401 }
2402
2403 static void
2404 odp_flow_key_from_flow__(struct ofpbuf *buf, const struct flow *data,
2405 const struct flow *flow, odp_port_t odp_in_port)
2406 {
2407 bool is_mask;
2408 struct ovs_key_ethernet *eth_key;
2409 size_t encap;
2410
2411 /* We assume that if 'data' and 'flow' are not the same, we should
2412 * treat 'data' as a mask. */
2413 is_mask = (data != flow);
2414
2415 nl_msg_put_u32(buf, OVS_KEY_ATTR_PRIORITY, data->skb_priority);
2416
2417 if (flow->tunnel.ip_dst || is_mask) {
2418 tun_key_to_attr(buf, &data->tunnel);
2419 }
2420
2421 nl_msg_put_u32(buf, OVS_KEY_ATTR_SKB_MARK, data->pkt_mark);
2422
2423 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
2424 * is not the magical value "ODPP_NONE". */
2425 if (is_mask || odp_in_port != ODPP_NONE) {
2426 nl_msg_put_odp_port(buf, OVS_KEY_ATTR_IN_PORT, odp_in_port);
2427 }
2428
2429 eth_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ETHERNET,
2430 sizeof *eth_key);
2431 memcpy(eth_key->eth_src, data->dl_src, ETH_ADDR_LEN);
2432 memcpy(eth_key->eth_dst, data->dl_dst, ETH_ADDR_LEN);
2433
2434 if (flow->vlan_tci != htons(0) || flow->dl_type == htons(ETH_TYPE_VLAN)) {
2435 if (is_mask) {
2436 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2437 } else {
2438 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_TYPE_VLAN));
2439 }
2440 nl_msg_put_be16(buf, OVS_KEY_ATTR_VLAN, data->vlan_tci);
2441 encap = nl_msg_start_nested(buf, OVS_KEY_ATTR_ENCAP);
2442 if (flow->vlan_tci == htons(0)) {
2443 goto unencap;
2444 }
2445 } else {
2446 encap = 0;
2447 }
2448
2449 if (ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2450 /* For backwards compatibility with kernels that don't support
2451 * wildcarding, the following convention is used to encode the
2452 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
2453 *
2454 * key mask matches
2455 * -------- -------- -------
2456 * >0x5ff 0xffff Specified Ethernet II Ethertype.
2457 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
2458 * <none> 0xffff Any non-Ethernet II frame (except valid
2459 * 802.3 SNAP packet with valid eth_type).
2460 */
2461 if (is_mask) {
2462 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, OVS_BE16_MAX);
2463 }
2464 goto unencap;
2465 }
2466
2467 nl_msg_put_be16(buf, OVS_KEY_ATTR_ETHERTYPE, data->dl_type);
2468
2469 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2470 struct ovs_key_ipv4 *ipv4_key;
2471
2472 ipv4_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV4,
2473 sizeof *ipv4_key);
2474 ipv4_key->ipv4_src = data->nw_src;
2475 ipv4_key->ipv4_dst = data->nw_dst;
2476 ipv4_key->ipv4_proto = data->nw_proto;
2477 ipv4_key->ipv4_tos = data->nw_tos;
2478 ipv4_key->ipv4_ttl = data->nw_ttl;
2479 ipv4_key->ipv4_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2480 : ovs_to_odp_frag(data->nw_frag);
2481 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2482 struct ovs_key_ipv6 *ipv6_key;
2483
2484 ipv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_IPV6,
2485 sizeof *ipv6_key);
2486 memcpy(ipv6_key->ipv6_src, &data->ipv6_src, sizeof ipv6_key->ipv6_src);
2487 memcpy(ipv6_key->ipv6_dst, &data->ipv6_dst, sizeof ipv6_key->ipv6_dst);
2488 ipv6_key->ipv6_label = data->ipv6_label;
2489 ipv6_key->ipv6_proto = data->nw_proto;
2490 ipv6_key->ipv6_tclass = data->nw_tos;
2491 ipv6_key->ipv6_hlimit = data->nw_ttl;
2492 ipv6_key->ipv6_frag = is_mask ? ovs_to_odp_frag_mask(data->nw_frag)
2493 : ovs_to_odp_frag(data->nw_frag);
2494 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2495 flow->dl_type == htons(ETH_TYPE_RARP)) {
2496 struct ovs_key_arp *arp_key;
2497
2498 arp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ARP,
2499 sizeof *arp_key);
2500 memset(arp_key, 0, sizeof *arp_key);
2501 arp_key->arp_sip = data->nw_src;
2502 arp_key->arp_tip = data->nw_dst;
2503 arp_key->arp_op = htons(data->nw_proto);
2504 memcpy(arp_key->arp_sha, data->arp_sha, ETH_ADDR_LEN);
2505 memcpy(arp_key->arp_tha, data->arp_tha, ETH_ADDR_LEN);
2506 }
2507
2508 if (flow->mpls_depth) {
2509 struct ovs_key_mpls *mpls_key;
2510
2511 mpls_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_MPLS,
2512 sizeof *mpls_key);
2513 mpls_key->mpls_lse = data->mpls_lse;
2514 }
2515
2516 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2517 if (flow->nw_proto == IPPROTO_TCP) {
2518 struct ovs_key_tcp *tcp_key;
2519
2520 tcp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_TCP,
2521 sizeof *tcp_key);
2522 tcp_key->tcp_src = data->tp_src;
2523 tcp_key->tcp_dst = data->tp_dst;
2524 } else if (flow->nw_proto == IPPROTO_UDP) {
2525 struct ovs_key_udp *udp_key;
2526
2527 udp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_UDP,
2528 sizeof *udp_key);
2529 udp_key->udp_src = data->tp_src;
2530 udp_key->udp_dst = data->tp_dst;
2531 } else if (flow->nw_proto == IPPROTO_SCTP) {
2532 struct ovs_key_sctp *sctp_key;
2533
2534 sctp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_SCTP,
2535 sizeof *sctp_key);
2536 sctp_key->sctp_src = data->tp_src;
2537 sctp_key->sctp_dst = data->tp_dst;
2538 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2539 && flow->nw_proto == IPPROTO_ICMP) {
2540 struct ovs_key_icmp *icmp_key;
2541
2542 icmp_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMP,
2543 sizeof *icmp_key);
2544 icmp_key->icmp_type = ntohs(data->tp_src);
2545 icmp_key->icmp_code = ntohs(data->tp_dst);
2546 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)
2547 && flow->nw_proto == IPPROTO_ICMPV6) {
2548 struct ovs_key_icmpv6 *icmpv6_key;
2549
2550 icmpv6_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ICMPV6,
2551 sizeof *icmpv6_key);
2552 icmpv6_key->icmpv6_type = ntohs(data->tp_src);
2553 icmpv6_key->icmpv6_code = ntohs(data->tp_dst);
2554
2555 if (flow->tp_dst == htons(0) &&
2556 (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2557 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) &&
2558 (!is_mask || (data->tp_src == htons(0xffff) &&
2559 data->tp_dst == htons(0xffff)))) {
2560
2561 struct ovs_key_nd *nd_key;
2562
2563 nd_key = nl_msg_put_unspec_uninit(buf, OVS_KEY_ATTR_ND,
2564 sizeof *nd_key);
2565 memcpy(nd_key->nd_target, &data->nd_target,
2566 sizeof nd_key->nd_target);
2567 memcpy(nd_key->nd_sll, data->arp_sha, ETH_ADDR_LEN);
2568 memcpy(nd_key->nd_tll, data->arp_tha, ETH_ADDR_LEN);
2569 }
2570 }
2571 }
2572
2573 unencap:
2574 if (encap) {
2575 nl_msg_end_nested(buf, encap);
2576 }
2577 }
2578
2579 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
2580 * 'flow->in_port' is ignored (since it is likely to be an OpenFlow port
2581 * number rather than a datapath port number). Instead, if 'odp_in_port'
2582 * is anything other than ODPP_NONE, it is included in 'buf' as the input
2583 * port.
2584 *
2585 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2586 * capable of being expanded to allow for that much space. */
2587 void
2588 odp_flow_key_from_flow(struct ofpbuf *buf, const struct flow *flow,
2589 odp_port_t odp_in_port)
2590 {
2591 odp_flow_key_from_flow__(buf, flow, flow, odp_in_port);
2592 }
2593
2594 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
2595 * 'buf'. 'flow' is used as a template to determine how to interpret
2596 * 'mask'. For example, the 'dl_type' of 'mask' describes the mask, but
2597 * it doesn't indicate whether the other fields should be interpreted as
2598 * ARP, IPv4, IPv6, etc.
2599 *
2600 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
2601 * capable of being expanded to allow for that much space. */
2602 void
2603 odp_flow_key_from_mask(struct ofpbuf *buf, const struct flow *mask,
2604 const struct flow *flow, uint32_t odp_in_port_mask)
2605 {
2606 odp_flow_key_from_flow__(buf, mask, flow, u32_to_odp(odp_in_port_mask));
2607 }
2608
2609 uint32_t
2610 odp_flow_key_hash(const struct nlattr *key, size_t key_len)
2611 {
2612 BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
2613 return hash_words(ALIGNED_CAST(const uint32_t *, key),
2614 key_len / sizeof(uint32_t), 0);
2615 }
2616
2617 static void
2618 log_odp_key_attributes(struct vlog_rate_limit *rl, const char *title,
2619 uint64_t attrs, int out_of_range_attr,
2620 const struct nlattr *key, size_t key_len)
2621 {
2622 struct ds s;
2623 int i;
2624
2625 if (VLOG_DROP_DBG(rl)) {
2626 return;
2627 }
2628
2629 ds_init(&s);
2630 for (i = 0; i < 64; i++) {
2631 if (attrs & (UINT64_C(1) << i)) {
2632 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2633
2634 ds_put_format(&s, " %s",
2635 ovs_key_attr_to_string(i, namebuf, sizeof namebuf));
2636 }
2637 }
2638 if (out_of_range_attr) {
2639 ds_put_format(&s, " %d (and possibly others)", out_of_range_attr);
2640 }
2641
2642 ds_put_cstr(&s, ": ");
2643 odp_flow_key_format(key, key_len, &s);
2644
2645 VLOG_DBG("%s:%s", title, ds_cstr(&s));
2646 ds_destroy(&s);
2647 }
2648
2649 static bool
2650 odp_to_ovs_frag(uint8_t odp_frag, struct flow *flow)
2651 {
2652 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2653
2654 if (odp_frag > OVS_FRAG_TYPE_LATER) {
2655 VLOG_ERR_RL(&rl, "invalid frag %"PRIu8" in flow key", odp_frag);
2656 return false;
2657 }
2658
2659 if (odp_frag != OVS_FRAG_TYPE_NONE) {
2660 flow->nw_frag |= FLOW_NW_FRAG_ANY;
2661 if (odp_frag == OVS_FRAG_TYPE_LATER) {
2662 flow->nw_frag |= FLOW_NW_FRAG_LATER;
2663 }
2664 }
2665 return true;
2666 }
2667
2668 static bool
2669 parse_flow_nlattrs(const struct nlattr *key, size_t key_len,
2670 const struct nlattr *attrs[], uint64_t *present_attrsp,
2671 int *out_of_range_attrp)
2672 {
2673 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2674 const struct nlattr *nla;
2675 uint64_t present_attrs;
2676 size_t left;
2677
2678 BUILD_ASSERT(OVS_KEY_ATTR_MAX < CHAR_BIT * sizeof present_attrs);
2679 present_attrs = 0;
2680 *out_of_range_attrp = 0;
2681 NL_ATTR_FOR_EACH (nla, left, key, key_len) {
2682 uint16_t type = nl_attr_type(nla);
2683 size_t len = nl_attr_get_size(nla);
2684 int expected_len = odp_flow_key_attr_len(type);
2685
2686 if (len != expected_len && expected_len >= 0) {
2687 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2688
2689 VLOG_ERR_RL(&rl, "attribute %s has length %zu but should have "
2690 "length %d", ovs_key_attr_to_string(type, namebuf,
2691 sizeof namebuf),
2692 len, expected_len);
2693 return false;
2694 }
2695
2696 if (type > OVS_KEY_ATTR_MAX) {
2697 *out_of_range_attrp = type;
2698 } else {
2699 if (present_attrs & (UINT64_C(1) << type)) {
2700 char namebuf[OVS_KEY_ATTR_BUFSIZE];
2701
2702 VLOG_ERR_RL(&rl, "duplicate %s attribute in flow key",
2703 ovs_key_attr_to_string(type,
2704 namebuf, sizeof namebuf));
2705 return false;
2706 }
2707
2708 present_attrs |= UINT64_C(1) << type;
2709 attrs[type] = nla;
2710 }
2711 }
2712 if (left) {
2713 VLOG_ERR_RL(&rl, "trailing garbage in flow key");
2714 return false;
2715 }
2716
2717 *present_attrsp = present_attrs;
2718 return true;
2719 }
2720
2721 static enum odp_key_fitness
2722 check_expectations(uint64_t present_attrs, int out_of_range_attr,
2723 uint64_t expected_attrs,
2724 const struct nlattr *key, size_t key_len)
2725 {
2726 uint64_t missing_attrs;
2727 uint64_t extra_attrs;
2728
2729 missing_attrs = expected_attrs & ~present_attrs;
2730 if (missing_attrs) {
2731 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2732 log_odp_key_attributes(&rl, "expected but not present",
2733 missing_attrs, 0, key, key_len);
2734 return ODP_FIT_TOO_LITTLE;
2735 }
2736
2737 extra_attrs = present_attrs & ~expected_attrs;
2738 if (extra_attrs || out_of_range_attr) {
2739 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
2740 log_odp_key_attributes(&rl, "present but not expected",
2741 extra_attrs, out_of_range_attr, key, key_len);
2742 return ODP_FIT_TOO_MUCH;
2743 }
2744
2745 return ODP_FIT_PERFECT;
2746 }
2747
2748 static bool
2749 parse_ethertype(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2750 uint64_t present_attrs, uint64_t *expected_attrs,
2751 struct flow *flow, const struct flow *src_flow)
2752 {
2753 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2754 bool is_mask = flow != src_flow;
2755
2756 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE)) {
2757 flow->dl_type = nl_attr_get_be16(attrs[OVS_KEY_ATTR_ETHERTYPE]);
2758 if (!is_mask && ntohs(flow->dl_type) < ETH_TYPE_MIN) {
2759 VLOG_ERR_RL(&rl, "invalid Ethertype %"PRIu16" in flow key",
2760 ntohs(flow->dl_type));
2761 return false;
2762 }
2763 if (is_mask && ntohs(src_flow->dl_type) < ETH_TYPE_MIN &&
2764 flow->dl_type != htons(0xffff)) {
2765 return false;
2766 }
2767 *expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE;
2768 } else {
2769 if (!is_mask) {
2770 flow->dl_type = htons(FLOW_DL_TYPE_NONE);
2771 } else if (ntohs(src_flow->dl_type) < ETH_TYPE_MIN) {
2772 /* See comments in odp_flow_key_from_flow__(). */
2773 VLOG_ERR_RL(&rl, "mask expected for non-Ethernet II frame");
2774 return false;
2775 }
2776 }
2777 return true;
2778 }
2779
2780 static enum odp_key_fitness
2781 parse_l2_5_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
2782 uint64_t present_attrs, int out_of_range_attr,
2783 uint64_t expected_attrs, struct flow *flow,
2784 const struct nlattr *key, size_t key_len,
2785 const struct flow *src_flow)
2786 {
2787 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2788 bool is_mask = src_flow != flow;
2789 const void *check_start = NULL;
2790 size_t check_len = 0;
2791 enum ovs_key_attr expected_bit = 0xff;
2792
2793 if (eth_type_mpls(src_flow->dl_type)) {
2794 if (!is_mask) {
2795 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2796
2797 if (!(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS))) {
2798 return ODP_FIT_TOO_LITTLE;
2799 }
2800 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2801 flow->mpls_depth++;
2802 } else if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_MPLS)) {
2803 flow->mpls_lse = nl_attr_get_be32(attrs[OVS_KEY_ATTR_MPLS]);
2804
2805 if (flow->mpls_lse != 0 && flow->dl_type != htons(0xffff)) {
2806 return ODP_FIT_ERROR;
2807 }
2808 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_MPLS);
2809 if (flow->mpls_lse) {
2810 /* XXX Is this needed? */
2811 flow->mpls_depth = 0xffff;
2812 }
2813 }
2814 goto done;
2815 } else if (src_flow->dl_type == htons(ETH_TYPE_IP)) {
2816 if (!is_mask) {
2817 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV4;
2818 }
2819 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV4)) {
2820 const struct ovs_key_ipv4 *ipv4_key;
2821
2822 ipv4_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV4]);
2823 flow->nw_src = ipv4_key->ipv4_src;
2824 flow->nw_dst = ipv4_key->ipv4_dst;
2825 flow->nw_proto = ipv4_key->ipv4_proto;
2826 flow->nw_tos = ipv4_key->ipv4_tos;
2827 flow->nw_ttl = ipv4_key->ipv4_ttl;
2828 if (is_mask) {
2829 flow->nw_frag = ipv4_key->ipv4_frag;
2830 check_start = ipv4_key;
2831 check_len = sizeof *ipv4_key;
2832 expected_bit = OVS_KEY_ATTR_IPV4;
2833 } else if (!odp_to_ovs_frag(ipv4_key->ipv4_frag, flow)) {
2834 return ODP_FIT_ERROR;
2835 }
2836 }
2837 } else if (src_flow->dl_type == htons(ETH_TYPE_IPV6)) {
2838 if (!is_mask) {
2839 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IPV6;
2840 }
2841 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IPV6)) {
2842 const struct ovs_key_ipv6 *ipv6_key;
2843
2844 ipv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_IPV6]);
2845 memcpy(&flow->ipv6_src, ipv6_key->ipv6_src, sizeof flow->ipv6_src);
2846 memcpy(&flow->ipv6_dst, ipv6_key->ipv6_dst, sizeof flow->ipv6_dst);
2847 flow->ipv6_label = ipv6_key->ipv6_label;
2848 flow->nw_proto = ipv6_key->ipv6_proto;
2849 flow->nw_tos = ipv6_key->ipv6_tclass;
2850 flow->nw_ttl = ipv6_key->ipv6_hlimit;
2851 if (is_mask) {
2852 flow->nw_frag = ipv6_key->ipv6_frag;
2853 check_start = ipv6_key;
2854 check_len = sizeof *ipv6_key;
2855 expected_bit = OVS_KEY_ATTR_IPV6;
2856 } else if (!odp_to_ovs_frag(ipv6_key->ipv6_frag, flow)) {
2857 return ODP_FIT_ERROR;
2858 }
2859 }
2860 } else if (src_flow->dl_type == htons(ETH_TYPE_ARP) ||
2861 src_flow->dl_type == htons(ETH_TYPE_RARP)) {
2862 if (!is_mask) {
2863 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ARP;
2864 }
2865 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ARP)) {
2866 const struct ovs_key_arp *arp_key;
2867
2868 arp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ARP]);
2869 flow->nw_src = arp_key->arp_sip;
2870 flow->nw_dst = arp_key->arp_tip;
2871 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
2872 VLOG_ERR_RL(&rl, "unsupported ARP opcode %"PRIu16" in flow "
2873 "key", ntohs(arp_key->arp_op));
2874 return ODP_FIT_ERROR;
2875 }
2876 flow->nw_proto = ntohs(arp_key->arp_op);
2877 memcpy(flow->arp_sha, arp_key->arp_sha, ETH_ADDR_LEN);
2878 memcpy(flow->arp_tha, arp_key->arp_tha, ETH_ADDR_LEN);
2879
2880 if (is_mask) {
2881 check_start = arp_key;
2882 check_len = sizeof *arp_key;
2883 expected_bit = OVS_KEY_ATTR_ARP;
2884 }
2885 }
2886 } else {
2887 goto done;
2888 }
2889 if (is_mask) {
2890 if (!is_all_zeros(check_start, check_len) &&
2891 flow->dl_type != htons(0xffff)) {
2892 return ODP_FIT_ERROR;
2893 } else {
2894 expected_attrs |= UINT64_C(1) << expected_bit;
2895 }
2896 }
2897
2898 expected_bit = OVS_KEY_ATTR_UNSPEC;
2899 if (src_flow->nw_proto == IPPROTO_TCP
2900 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2901 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2902 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2903 if (!is_mask) {
2904 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TCP;
2905 }
2906 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TCP)) {
2907 const struct ovs_key_tcp *tcp_key;
2908
2909 tcp_key = nl_attr_get(attrs[OVS_KEY_ATTR_TCP]);
2910 flow->tp_src = tcp_key->tcp_src;
2911 flow->tp_dst = tcp_key->tcp_dst;
2912 expected_bit = OVS_KEY_ATTR_TCP;
2913 }
2914 } else if (src_flow->nw_proto == IPPROTO_UDP
2915 && (src_flow->dl_type == htons(ETH_TYPE_IP) ||
2916 src_flow->dl_type == htons(ETH_TYPE_IPV6))
2917 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2918 if (!is_mask) {
2919 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_UDP;
2920 }
2921 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_UDP)) {
2922 const struct ovs_key_udp *udp_key;
2923
2924 udp_key = nl_attr_get(attrs[OVS_KEY_ATTR_UDP]);
2925 flow->tp_src = udp_key->udp_src;
2926 flow->tp_dst = udp_key->udp_dst;
2927 expected_bit = OVS_KEY_ATTR_UDP;
2928 }
2929 } else if (flow->nw_proto == IPPROTO_SCTP
2930 && (flow->dl_type == htons(ETH_TYPE_IP) ||
2931 flow->dl_type == htons(ETH_TYPE_IPV6))
2932 && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2933 if (!is_mask) {
2934 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SCTP;
2935 }
2936 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SCTP)) {
2937 const struct ovs_key_sctp *sctp_key;
2938
2939 sctp_key = nl_attr_get(attrs[OVS_KEY_ATTR_SCTP]);
2940 flow->tp_src = sctp_key->sctp_src;
2941 flow->tp_dst = sctp_key->sctp_dst;
2942 expected_bit = OVS_KEY_ATTR_SCTP;
2943 }
2944 } else if (src_flow->nw_proto == IPPROTO_ICMP
2945 && src_flow->dl_type == htons(ETH_TYPE_IP)
2946 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2947 if (!is_mask) {
2948 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMP;
2949 }
2950 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMP)) {
2951 const struct ovs_key_icmp *icmp_key;
2952
2953 icmp_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMP]);
2954 flow->tp_src = htons(icmp_key->icmp_type);
2955 flow->tp_dst = htons(icmp_key->icmp_code);
2956 expected_bit = OVS_KEY_ATTR_ICMP;
2957 }
2958 } else if (src_flow->nw_proto == IPPROTO_ICMPV6
2959 && src_flow->dl_type == htons(ETH_TYPE_IPV6)
2960 && !(src_flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2961 if (!is_mask) {
2962 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6;
2963 }
2964 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6)) {
2965 const struct ovs_key_icmpv6 *icmpv6_key;
2966
2967 icmpv6_key = nl_attr_get(attrs[OVS_KEY_ATTR_ICMPV6]);
2968 flow->tp_src = htons(icmpv6_key->icmpv6_type);
2969 flow->tp_dst = htons(icmpv6_key->icmpv6_code);
2970 expected_bit = OVS_KEY_ATTR_ICMPV6;
2971 if (src_flow->tp_dst == htons(0) &&
2972 (src_flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
2973 src_flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
2974 if (!is_mask) {
2975 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2976 }
2977 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ND)) {
2978 const struct ovs_key_nd *nd_key;
2979
2980 nd_key = nl_attr_get(attrs[OVS_KEY_ATTR_ND]);
2981 memcpy(&flow->nd_target, nd_key->nd_target,
2982 sizeof flow->nd_target);
2983 memcpy(flow->arp_sha, nd_key->nd_sll, ETH_ADDR_LEN);
2984 memcpy(flow->arp_tha, nd_key->nd_tll, ETH_ADDR_LEN);
2985 if (is_mask) {
2986 if (!is_all_zeros((const uint8_t *) nd_key,
2987 sizeof *nd_key) &&
2988 (flow->tp_src != htons(0xffff) ||
2989 flow->tp_dst != htons(0xffff))) {
2990 return ODP_FIT_ERROR;
2991 } else {
2992 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ND;
2993 }
2994 }
2995 }
2996 }
2997 }
2998 }
2999 if (is_mask && expected_bit != OVS_KEY_ATTR_UNSPEC) {
3000 if ((flow->tp_src || flow->tp_dst) && flow->nw_proto != 0xff) {
3001 return ODP_FIT_ERROR;
3002 } else {
3003 expected_attrs |= UINT64_C(1) << expected_bit;
3004 }
3005 }
3006
3007 done:
3008 return check_expectations(present_attrs, out_of_range_attr, expected_attrs,
3009 key, key_len);
3010 }
3011
3012 /* Parse 802.1Q header then encapsulated L3 attributes. */
3013 static enum odp_key_fitness
3014 parse_8021q_onward(const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1],
3015 uint64_t present_attrs, int out_of_range_attr,
3016 uint64_t expected_attrs, struct flow *flow,
3017 const struct nlattr *key, size_t key_len,
3018 const struct flow *src_flow)
3019 {
3020 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3021 bool is_mask = src_flow != flow;
3022
3023 const struct nlattr *encap
3024 = (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)
3025 ? attrs[OVS_KEY_ATTR_ENCAP] : NULL);
3026 enum odp_key_fitness encap_fitness;
3027 enum odp_key_fitness fitness;
3028 ovs_be16 tci;
3029
3030 /* Calculate fitness of outer attributes. */
3031 if (!is_mask) {
3032 expected_attrs |= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN) |
3033 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP));
3034 } else {
3035 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3036 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3037 }
3038 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP)) {
3039 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP);
3040 }
3041 }
3042 fitness = check_expectations(present_attrs, out_of_range_attr,
3043 expected_attrs, key, key_len);
3044
3045 /* Get the VLAN TCI value. */
3046 if (!is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN))) {
3047 return ODP_FIT_TOO_LITTLE;
3048 } else {
3049 tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3050 if (!is_mask) {
3051 if (tci == htons(0)) {
3052 /* Corner case for a truncated 802.1Q header. */
3053 if (fitness == ODP_FIT_PERFECT && nl_attr_get_size(encap)) {
3054 return ODP_FIT_TOO_MUCH;
3055 }
3056 return fitness;
3057 } else if (!(tci & htons(VLAN_CFI))) {
3058 VLOG_ERR_RL(&rl, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16" is nonzero "
3059 "but CFI bit is not set", ntohs(tci));
3060 return ODP_FIT_ERROR;
3061 }
3062 }
3063 /* Set vlan_tci.
3064 * Remove the TPID from dl_type since it's not the real Ethertype. */
3065 flow->dl_type = htons(0);
3066 flow->vlan_tci = tci;
3067 }
3068
3069 if (is_mask && !(present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ENCAP))) {
3070 return fitness;
3071 }
3072 /* Now parse the encapsulated attributes. */
3073 if (!parse_flow_nlattrs(nl_attr_get(encap), nl_attr_get_size(encap),
3074 attrs, &present_attrs, &out_of_range_attr)) {
3075 return ODP_FIT_ERROR;
3076 }
3077 expected_attrs = 0;
3078
3079 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow, src_flow)) {
3080 return ODP_FIT_ERROR;
3081 }
3082 encap_fitness = parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3083 expected_attrs, flow, key, key_len,
3084 src_flow);
3085
3086 /* The overall fitness is the worse of the outer and inner attributes. */
3087 return MAX(fitness, encap_fitness);
3088 }
3089
3090 static enum odp_key_fitness
3091 odp_flow_key_to_flow__(const struct nlattr *key, size_t key_len,
3092 struct flow *flow, const struct flow *src_flow)
3093 {
3094 const struct nlattr *attrs[OVS_KEY_ATTR_MAX + 1];
3095 uint64_t expected_attrs;
3096 uint64_t present_attrs;
3097 int out_of_range_attr;
3098 bool is_mask = src_flow != flow;
3099
3100 memset(flow, 0, sizeof *flow);
3101
3102 /* Parse attributes. */
3103 if (!parse_flow_nlattrs(key, key_len, attrs, &present_attrs,
3104 &out_of_range_attr)) {
3105 return ODP_FIT_ERROR;
3106 }
3107 expected_attrs = 0;
3108
3109 /* Metadata. */
3110 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY)) {
3111 flow->skb_priority = nl_attr_get_u32(attrs[OVS_KEY_ATTR_PRIORITY]);
3112 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY;
3113 }
3114
3115 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK)) {
3116 flow->pkt_mark = nl_attr_get_u32(attrs[OVS_KEY_ATTR_SKB_MARK]);
3117 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK;
3118 }
3119
3120 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL)) {
3121 enum odp_key_fitness res;
3122
3123 res = odp_tun_key_from_attr(attrs[OVS_KEY_ATTR_TUNNEL], &flow->tunnel);
3124 if (res == ODP_FIT_ERROR) {
3125 return ODP_FIT_ERROR;
3126 } else if (res == ODP_FIT_PERFECT) {
3127 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL;
3128 }
3129 }
3130
3131 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT)) {
3132 flow->in_port.odp_port
3133 = nl_attr_get_odp_port(attrs[OVS_KEY_ATTR_IN_PORT]);
3134 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT;
3135 } else if (!is_mask) {
3136 flow->in_port.odp_port = ODPP_NONE;
3137 }
3138
3139 /* Ethernet header. */
3140 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET)) {
3141 const struct ovs_key_ethernet *eth_key;
3142
3143 eth_key = nl_attr_get(attrs[OVS_KEY_ATTR_ETHERNET]);
3144 memcpy(flow->dl_src, eth_key->eth_src, ETH_ADDR_LEN);
3145 memcpy(flow->dl_dst, eth_key->eth_dst, ETH_ADDR_LEN);
3146 if (is_mask) {
3147 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3148 }
3149 }
3150 if (!is_mask) {
3151 expected_attrs |= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET;
3152 }
3153
3154 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
3155 if (!parse_ethertype(attrs, present_attrs, &expected_attrs, flow,
3156 src_flow)) {
3157 return ODP_FIT_ERROR;
3158 }
3159
3160 if ((is_mask && (src_flow->vlan_tci & htons(VLAN_CFI))) ||
3161 (!is_mask && src_flow->dl_type == htons(ETH_TYPE_VLAN))) {
3162 return parse_8021q_onward(attrs, present_attrs, out_of_range_attr,
3163 expected_attrs, flow, key, key_len, src_flow);
3164 }
3165 if (is_mask) {
3166 flow->vlan_tci = htons(0xffff);
3167 if (present_attrs & (UINT64_C(1) << OVS_KEY_ATTR_VLAN)) {
3168 flow->vlan_tci = nl_attr_get_be16(attrs[OVS_KEY_ATTR_VLAN]);
3169 expected_attrs |= (UINT64_C(1) << OVS_KEY_ATTR_VLAN);
3170 }
3171 }
3172 return parse_l2_5_onward(attrs, present_attrs, out_of_range_attr,
3173 expected_attrs, flow, key, key_len, src_flow);
3174 }
3175
3176 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
3177 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
3178 * 'key' fits our expectations for what a flow key should contain.
3179 *
3180 * The 'in_port' will be the datapath's understanding of the port. The
3181 * caller will need to translate with odp_port_to_ofp_port() if the
3182 * OpenFlow port is needed.
3183 *
3184 * This function doesn't take the packet itself as an argument because none of
3185 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
3186 * it is always possible to infer which additional attribute(s) should appear
3187 * by looking at the attributes for lower-level protocols, e.g. if the network
3188 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
3189 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
3190 * must be absent. */
3191 enum odp_key_fitness
3192 odp_flow_key_to_flow(const struct nlattr *key, size_t key_len,
3193 struct flow *flow)
3194 {
3195 return odp_flow_key_to_flow__(key, key_len, flow, flow);
3196 }
3197
3198 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a mask
3199 * structure in 'mask'. 'flow' must be a previously translated flow
3200 * corresponding to 'mask'. Returns an ODP_FIT_* value that indicates how well
3201 * 'key' fits our expectations for what a flow key should contain. */
3202 enum odp_key_fitness
3203 odp_flow_key_to_mask(const struct nlattr *key, size_t key_len,
3204 struct flow *mask, const struct flow *flow)
3205 {
3206 return odp_flow_key_to_flow__(key, key_len, mask, flow);
3207 }
3208
3209 /* Returns 'fitness' as a string, for use in debug messages. */
3210 const char *
3211 odp_key_fitness_to_string(enum odp_key_fitness fitness)
3212 {
3213 switch (fitness) {
3214 case ODP_FIT_PERFECT:
3215 return "OK";
3216 case ODP_FIT_TOO_MUCH:
3217 return "too_much";
3218 case ODP_FIT_TOO_LITTLE:
3219 return "too_little";
3220 case ODP_FIT_ERROR:
3221 return "error";
3222 default:
3223 return "<unknown>";
3224 }
3225 }
3226
3227 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
3228 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
3229 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
3230 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
3231 * null, then the return value is not meaningful.) */
3232 size_t
3233 odp_put_userspace_action(uint32_t pid,
3234 const void *userdata, size_t userdata_size,
3235 struct ofpbuf *odp_actions)
3236 {
3237 size_t userdata_ofs;
3238 size_t offset;
3239
3240 offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_USERSPACE);
3241 nl_msg_put_u32(odp_actions, OVS_USERSPACE_ATTR_PID, pid);
3242 if (userdata) {
3243 userdata_ofs = odp_actions->size + NLA_HDRLEN;
3244 nl_msg_put_unspec(odp_actions, OVS_USERSPACE_ATTR_USERDATA,
3245 userdata, userdata_size);
3246 } else {
3247 userdata_ofs = 0;
3248 }
3249 nl_msg_end_nested(odp_actions, offset);
3250
3251 return userdata_ofs;
3252 }
3253
3254 void
3255 odp_put_tunnel_action(const struct flow_tnl *tunnel,
3256 struct ofpbuf *odp_actions)
3257 {
3258 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3259 tun_key_to_attr(odp_actions, tunnel);
3260 nl_msg_end_nested(odp_actions, offset);
3261 }
3262 \f
3263 /* The commit_odp_actions() function and its helpers. */
3264
3265 static void
3266 commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type,
3267 const void *key, size_t key_size)
3268 {
3269 size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET);
3270 nl_msg_put_unspec(odp_actions, key_type, key, key_size);
3271 nl_msg_end_nested(odp_actions, offset);
3272 }
3273
3274 void
3275 odp_put_pkt_mark_action(const uint32_t pkt_mark,
3276 struct ofpbuf *odp_actions)
3277 {
3278 commit_set_action(odp_actions, OVS_KEY_ATTR_SKB_MARK, &pkt_mark,
3279 sizeof(pkt_mark));
3280 }
3281
3282 /* If any of the flow key data that ODP actions can modify are different in
3283 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
3284 * 'odp_actions' that change the flow tunneling information in key from
3285 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
3286 * same way. In other words, operates the same as commit_odp_actions(), but
3287 * only on tunneling information. */
3288 void
3289 commit_odp_tunnel_action(const struct flow *flow, struct flow *base,
3290 struct ofpbuf *odp_actions)
3291 {
3292 /* A valid IPV4_TUNNEL must have non-zero ip_dst. */
3293 if (flow->tunnel.ip_dst) {
3294 if (!memcmp(&base->tunnel, &flow->tunnel, sizeof base->tunnel)) {
3295 return;
3296 }
3297 memcpy(&base->tunnel, &flow->tunnel, sizeof base->tunnel);
3298 odp_put_tunnel_action(&base->tunnel, odp_actions);
3299 }
3300 }
3301
3302 static void
3303 commit_set_ether_addr_action(const struct flow *flow, struct flow *base,
3304 struct ofpbuf *odp_actions,
3305 struct flow_wildcards *wc)
3306 {
3307 struct ovs_key_ethernet eth_key;
3308
3309 if (eth_addr_equals(base->dl_src, flow->dl_src) &&
3310 eth_addr_equals(base->dl_dst, flow->dl_dst)) {
3311 return;
3312 }
3313
3314 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
3315 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
3316
3317 memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN);
3318 memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
3319
3320 memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN);
3321 memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN);
3322
3323 commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET,
3324 &eth_key, sizeof(eth_key));
3325 }
3326
3327 static void
3328 commit_vlan_action(const struct flow *flow, struct flow *base,
3329 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3330 {
3331 if (base->vlan_tci == flow->vlan_tci) {
3332 return;
3333 }
3334
3335 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
3336
3337 if (base->vlan_tci & htons(VLAN_CFI)) {
3338 nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN);
3339 }
3340
3341 if (flow->vlan_tci & htons(VLAN_CFI)) {
3342 struct ovs_action_push_vlan vlan;
3343
3344 vlan.vlan_tpid = htons(ETH_TYPE_VLAN);
3345 vlan.vlan_tci = flow->vlan_tci;
3346 nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN,
3347 &vlan, sizeof vlan);
3348 }
3349 base->vlan_tci = flow->vlan_tci;
3350 }
3351
3352 static void
3353 commit_mpls_action(const struct flow *flow, struct flow *base,
3354 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3355 {
3356 if (flow->mpls_lse == base->mpls_lse &&
3357 flow->mpls_depth == base->mpls_depth) {
3358 return;
3359 }
3360
3361 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
3362
3363 if (flow->mpls_depth < base->mpls_depth) {
3364 if (base->mpls_depth - flow->mpls_depth > 1) {
3365 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3366 VLOG_WARN_RL(&rl, "Multiple mpls_pop actions reduced to "
3367 " a single mpls_pop action");
3368 }
3369
3370 nl_msg_put_be16(odp_actions, OVS_ACTION_ATTR_POP_MPLS, flow->dl_type);
3371 } else if (flow->mpls_depth > base->mpls_depth) {
3372 struct ovs_action_push_mpls *mpls;
3373
3374 if (flow->mpls_depth - base->mpls_depth > 1) {
3375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
3376 VLOG_WARN_RL(&rl, "Multiple mpls_push actions reduced to "
3377 " a single mpls_push action");
3378 }
3379
3380 mpls = nl_msg_put_unspec_uninit(odp_actions, OVS_ACTION_ATTR_PUSH_MPLS,
3381 sizeof *mpls);
3382 memset(mpls, 0, sizeof *mpls);
3383 mpls->mpls_ethertype = flow->dl_type;
3384 mpls->mpls_lse = flow->mpls_lse;
3385 } else {
3386 struct ovs_key_mpls mpls_key;
3387
3388 mpls_key.mpls_lse = flow->mpls_lse;
3389 commit_set_action(odp_actions, OVS_KEY_ATTR_MPLS,
3390 &mpls_key, sizeof(mpls_key));
3391 }
3392
3393 base->dl_type = flow->dl_type;
3394 base->mpls_lse = flow->mpls_lse;
3395 base->mpls_depth = flow->mpls_depth;
3396 }
3397
3398 static void
3399 commit_set_ipv4_action(const struct flow *flow, struct flow *base,
3400 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3401 {
3402 struct ovs_key_ipv4 ipv4_key;
3403
3404 if (base->nw_src == flow->nw_src &&
3405 base->nw_dst == flow->nw_dst &&
3406 base->nw_tos == flow->nw_tos &&
3407 base->nw_ttl == flow->nw_ttl &&
3408 base->nw_frag == flow->nw_frag) {
3409 return;
3410 }
3411
3412 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
3413 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
3414 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3415 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3416 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3417 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3418
3419 ipv4_key.ipv4_src = base->nw_src = flow->nw_src;
3420 ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst;
3421 ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos;
3422 ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl;
3423 ipv4_key.ipv4_proto = base->nw_proto;
3424 ipv4_key.ipv4_frag = ovs_to_odp_frag(base->nw_frag);
3425
3426 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4,
3427 &ipv4_key, sizeof(ipv4_key));
3428 }
3429
3430 static void
3431 commit_set_ipv6_action(const struct flow *flow, struct flow *base,
3432 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3433 {
3434 struct ovs_key_ipv6 ipv6_key;
3435
3436 if (ipv6_addr_equals(&base->ipv6_src, &flow->ipv6_src) &&
3437 ipv6_addr_equals(&base->ipv6_dst, &flow->ipv6_dst) &&
3438 base->ipv6_label == flow->ipv6_label &&
3439 base->nw_tos == flow->nw_tos &&
3440 base->nw_ttl == flow->nw_ttl &&
3441 base->nw_frag == flow->nw_frag) {
3442 return;
3443 }
3444
3445 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
3446 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
3447 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
3448 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
3449 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
3450 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3451 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
3452
3453 base->ipv6_src = flow->ipv6_src;
3454 memcpy(&ipv6_key.ipv6_src, &base->ipv6_src, sizeof(ipv6_key.ipv6_src));
3455 base->ipv6_dst = flow->ipv6_dst;
3456 memcpy(&ipv6_key.ipv6_dst, &base->ipv6_dst, sizeof(ipv6_key.ipv6_dst));
3457
3458 ipv6_key.ipv6_label = base->ipv6_label = flow->ipv6_label;
3459 ipv6_key.ipv6_tclass = base->nw_tos = flow->nw_tos;
3460 ipv6_key.ipv6_hlimit = base->nw_ttl = flow->nw_ttl;
3461 ipv6_key.ipv6_proto = base->nw_proto;
3462 ipv6_key.ipv6_frag = ovs_to_odp_frag(base->nw_frag);
3463
3464 commit_set_action(odp_actions, OVS_KEY_ATTR_IPV6,
3465 &ipv6_key, sizeof(ipv6_key));
3466 }
3467
3468 static void
3469 commit_set_nw_action(const struct flow *flow, struct flow *base,
3470 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3471 {
3472 /* Check if flow really have an IP header. */
3473 if (!flow->nw_proto) {
3474 return;
3475 }
3476
3477 if (base->dl_type == htons(ETH_TYPE_IP)) {
3478 commit_set_ipv4_action(flow, base, odp_actions, wc);
3479 } else if (base->dl_type == htons(ETH_TYPE_IPV6)) {
3480 commit_set_ipv6_action(flow, base, odp_actions, wc);
3481 }
3482 }
3483
3484 static void
3485 commit_set_port_action(const struct flow *flow, struct flow *base,
3486 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3487 {
3488 if (!is_ip_any(base) || (!base->tp_src && !base->tp_dst)) {
3489 return;
3490 }
3491
3492 if (base->tp_src == flow->tp_src &&
3493 base->tp_dst == flow->tp_dst) {
3494 return;
3495 }
3496
3497 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
3498 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
3499
3500 if (flow->nw_proto == IPPROTO_TCP) {
3501 struct ovs_key_tcp port_key;
3502
3503 port_key.tcp_src = base->tp_src = flow->tp_src;
3504 port_key.tcp_dst = base->tp_dst = flow->tp_dst;
3505
3506 commit_set_action(odp_actions, OVS_KEY_ATTR_TCP,
3507 &port_key, sizeof(port_key));
3508
3509 } else if (flow->nw_proto == IPPROTO_UDP) {
3510 struct ovs_key_udp port_key;
3511
3512 port_key.udp_src = base->tp_src = flow->tp_src;
3513 port_key.udp_dst = base->tp_dst = flow->tp_dst;
3514
3515 commit_set_action(odp_actions, OVS_KEY_ATTR_UDP,
3516 &port_key, sizeof(port_key));
3517 } else if (flow->nw_proto == IPPROTO_SCTP) {
3518 struct ovs_key_sctp port_key;
3519
3520 port_key.sctp_src = base->tp_src = flow->tp_src;
3521 port_key.sctp_dst = base->tp_dst = flow->tp_dst;
3522
3523 commit_set_action(odp_actions, OVS_KEY_ATTR_SCTP,
3524 &port_key, sizeof(port_key));
3525 }
3526 }
3527
3528 static void
3529 commit_set_priority_action(const struct flow *flow, struct flow *base,
3530 struct ofpbuf *odp_actions,
3531 struct flow_wildcards *wc)
3532 {
3533 if (base->skb_priority == flow->skb_priority) {
3534 return;
3535 }
3536
3537 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3538 base->skb_priority = flow->skb_priority;
3539
3540 commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY,
3541 &base->skb_priority, sizeof(base->skb_priority));
3542 }
3543
3544 static void
3545 commit_set_pkt_mark_action(const struct flow *flow, struct flow *base,
3546 struct ofpbuf *odp_actions,
3547 struct flow_wildcards *wc)
3548 {
3549 if (base->pkt_mark == flow->pkt_mark) {
3550 return;
3551 }
3552
3553 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
3554 base->pkt_mark = flow->pkt_mark;
3555
3556 odp_put_pkt_mark_action(base->pkt_mark, odp_actions);
3557 }
3558 /* If any of the flow key data that ODP actions can modify are different in
3559 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
3560 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
3561 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
3562 * in addition to this function if needed. Sets fields in 'wc' that are
3563 * used as part of the action. */
3564 void
3565 commit_odp_actions(const struct flow *flow, struct flow *base,
3566 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
3567 {
3568 commit_set_ether_addr_action(flow, base, odp_actions, wc);
3569 commit_vlan_action(flow, base, odp_actions, wc);
3570 commit_set_nw_action(flow, base, odp_actions, wc);
3571 commit_set_port_action(flow, base, odp_actions, wc);
3572 /* Committing MPLS actions should occur after committing nw and port
3573 * actions. This is because committing MPLS actions may alter a packet so
3574 * that it is no longer IP and thus nw and port actions are no longer valid.
3575 */
3576 commit_mpls_action(flow, base, odp_actions, wc);
3577 commit_set_priority_action(flow, base, odp_actions, wc);
3578 commit_set_pkt_mark_action(flow, base, odp_actions, wc);
3579 }