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