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