Add support for matching Ethernet multicast frames.
[sliver-openvswitch.git] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
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
19 #include "nx-match.h"
20
21 #include "classifier.h"
22 #include "dynamic-string.h"
23 #include "ofp-util.h"
24 #include "ofpbuf.h"
25 #include "openflow/nicira-ext.h"
26 #include "packets.h"
27 #include "unaligned.h"
28 #include "vlog.h"
29
30 VLOG_DEFINE_THIS_MODULE(nx_match);
31
32 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
33  * peer and so there's not much point in showing a lot of them. */
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35
36 enum {
37     NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
38     NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
39     NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
40     NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
41     NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
42     NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
43     BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
44 };
45
46 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
47  * zero. */
48 enum nxm_field_index {
49 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) NFI_NXM_##HEADER,
50 #include "nx-match.def"
51     N_NXM_FIELDS
52 };
53
54 struct nxm_field {
55     struct hmap_node hmap_node;
56     enum nxm_field_index index; /* NFI_* value. */
57     uint32_t header;            /* NXM_* value. */
58     uint32_t wildcard;          /* Wildcard bit, if exactly one. */
59     ovs_be16 dl_type;           /* dl_type prerequisite, if nonzero. */
60     uint8_t nw_proto;           /* nw_proto prerequisite, if nonzero. */
61     const char *name;           /* "NXM_*" string. */
62 };
63
64 /* All the known fields. */
65 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
66 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
67     { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
68       CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER },
69 #include "nx-match.def"
70 };
71
72 /* Hash table of 'nxm_fields'. */
73 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
74
75 /* Possible masks for NXM_OF_ETH_DST_W. */
76 static const uint8_t eth_all_0s[ETH_ADDR_LEN]
77     = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
78 static const uint8_t eth_all_1s[ETH_ADDR_LEN]
79     = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
80 static const uint8_t eth_mcast_1[ETH_ADDR_LEN]
81     = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
82 static const uint8_t eth_mcast_0[ETH_ADDR_LEN]
83     = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
84
85 static void
86 nxm_init(void)
87 {
88     if (hmap_is_empty(&all_nxm_fields)) {
89         int i;
90
91         for (i = 0; i < N_NXM_FIELDS; i++) {
92             struct nxm_field *f = &nxm_fields[i];
93             hmap_insert(&all_nxm_fields, &f->hmap_node,
94                         hash_int(f->header, 0));
95         }
96
97         /* Verify that the header values are unique (duplicate "case" values
98          * cause a compile error). */
99         switch (0) {
100 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
101         case NXM_##HEADER: break;
102 #include "nx-match.def"
103         }
104     }
105 }
106
107 static const struct nxm_field *
108 nxm_field_lookup(uint32_t header)
109 {
110     struct nxm_field *f;
111
112     nxm_init();
113
114     HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
115                              &all_nxm_fields) {
116         if (f->header == header) {
117             return f;
118         }
119     }
120
121     return NULL;
122 }
123
124 /* Returns the width of the data for a field with the given 'header', in
125  * bytes. */
126 static int
127 nxm_field_bytes(uint32_t header)
128 {
129     unsigned int length = NXM_LENGTH(header);
130     return NXM_HASMASK(header) ? length / 2 : length;
131 }
132
133 /* Returns the width of the data for a field with the given 'header', in
134  * bits. */
135 static int
136 nxm_field_bits(uint32_t header)
137 {
138     return nxm_field_bytes(header) * 8;
139 }
140 \f
141 /* nx_pull_match() and helpers. */
142
143 static int
144 parse_tci(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
145 {
146     enum { OFPFW_DL_TCI = OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP };
147     if ((rule->wc.wildcards & OFPFW_DL_TCI) != OFPFW_DL_TCI) {
148         return NXM_DUP_TYPE;
149     } else {
150         return cls_rule_set_dl_tci_masked(rule, tci, mask) ? 0 : NXM_INVALID;
151     }
152 }
153
154 static int
155 parse_nx_reg(const struct nxm_field *f,
156              struct flow *flow, struct flow_wildcards *wc,
157              const void *value, const void *maskp)
158 {
159     int idx = NXM_NX_REG_IDX(f->header);
160     if (wc->reg_masks[idx]) {
161         return NXM_DUP_TYPE;
162     } else {
163         flow_wildcards_set_reg_mask(wc, idx,
164                                     (NXM_HASMASK(f->header)
165                                      ? ntohl(get_unaligned_u32(maskp))
166                                      : UINT32_MAX));
167         flow->regs[idx] = ntohl(get_unaligned_u32(value));
168         flow->regs[idx] &= wc->reg_masks[idx];
169         return 0;
170     }
171 }
172
173 static int
174 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
175                 const void *value, const void *mask)
176 {
177     struct flow_wildcards *wc = &rule->wc;
178     struct flow *flow = &rule->flow;
179
180     switch (f->index) {
181         /* Metadata. */
182     case NFI_NXM_OF_IN_PORT:
183         flow->in_port = ntohs(get_unaligned_u16(value));
184         if (flow->in_port == OFPP_LOCAL) {
185             flow->in_port = ODPP_LOCAL;
186         }
187         return 0;
188
189         /* Ethernet header. */
190     case NFI_NXM_OF_ETH_DST:
191         if ((wc->wildcards & (OFPFW_DL_DST | FWW_ETH_MCAST))
192             != (OFPFW_DL_DST | FWW_ETH_MCAST)) {
193             return NXM_DUP_TYPE;
194         } else {
195             wc->wildcards &= ~(OFPFW_DL_DST | FWW_ETH_MCAST);
196             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
197             return 0;
198         }
199     case NFI_NXM_OF_ETH_DST_W:
200         if ((wc->wildcards & (OFPFW_DL_DST | FWW_ETH_MCAST))
201             != (OFPFW_DL_DST | FWW_ETH_MCAST)) {
202             return NXM_DUP_TYPE;
203         } else if (eth_addr_equals(mask, eth_mcast_1)) {
204             wc->wildcards &= ~FWW_ETH_MCAST;
205             flow->dl_dst[0] = *(uint8_t *) value & 0x01;
206         } else if (eth_addr_equals(mask, eth_mcast_0)) {
207             wc->wildcards &= ~OFPFW_DL_DST;
208             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
209             flow->dl_dst[0] &= 0xfe;
210         } else if (eth_addr_equals(mask, eth_all_0s)) {
211             return 0;
212         } else if (eth_addr_equals(mask, eth_all_1s)) {
213             wc->wildcards &= ~(OFPFW_DL_DST | FWW_ETH_MCAST);
214             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
215             return 0;
216         } else {
217             return NXM_BAD_MASK;
218         }
219     case NFI_NXM_OF_ETH_SRC:
220         memcpy(flow->dl_src, value, ETH_ADDR_LEN);
221         return 0;
222     case NFI_NXM_OF_ETH_TYPE:
223         flow->dl_type = get_unaligned_u16(value);
224         return 0;
225
226         /* 802.1Q header. */
227     case NFI_NXM_OF_VLAN_TCI:
228         return parse_tci(rule, get_unaligned_u16(value), htons(UINT16_MAX));
229
230     case NFI_NXM_OF_VLAN_TCI_W:
231         return parse_tci(rule, get_unaligned_u16(value),
232                          get_unaligned_u16(mask));
233
234         /* IP header. */
235     case NFI_NXM_OF_IP_TOS:
236         if (*(uint8_t *) value & 0x03) {
237             return NXM_BAD_VALUE;
238         } else {
239             flow->nw_tos = *(uint8_t *) value;
240             return 0;
241         }
242     case NFI_NXM_OF_IP_PROTO:
243         flow->nw_proto = *(uint8_t *) value;
244         return 0;
245
246         /* IP addresses in IP and ARP headers. */
247     case NFI_NXM_OF_IP_SRC:
248     case NFI_NXM_OF_ARP_SPA:
249         if (wc->nw_src_mask) {
250             return NXM_DUP_TYPE;
251         } else {
252             cls_rule_set_nw_src(rule, get_unaligned_u32(value));
253             return 0;
254         }
255     case NFI_NXM_OF_IP_SRC_W:
256     case NFI_NXM_OF_ARP_SPA_W:
257         if (wc->nw_src_mask) {
258             return NXM_DUP_TYPE;
259         } else {
260             ovs_be32 ip = get_unaligned_u32(value);
261             ovs_be32 netmask = get_unaligned_u32(mask);
262             if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
263                 return NXM_BAD_MASK;
264             }
265             return 0;
266         }
267     case NFI_NXM_OF_IP_DST:
268     case NFI_NXM_OF_ARP_TPA:
269         if (wc->nw_dst_mask) {
270             return NXM_DUP_TYPE;
271         } else {
272             cls_rule_set_nw_dst(rule, get_unaligned_u32(value));
273             return 0;
274         }
275     case NFI_NXM_OF_IP_DST_W:
276     case NFI_NXM_OF_ARP_TPA_W:
277         if (wc->nw_dst_mask) {
278             return NXM_DUP_TYPE;
279         } else {
280             ovs_be32 ip = get_unaligned_u32(value);
281             ovs_be32 netmask = get_unaligned_u32(mask);
282             if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
283                 return NXM_BAD_MASK;
284             }
285             return 0;
286         }
287
288         /* TCP header. */
289     case NFI_NXM_OF_TCP_SRC:
290         flow->tp_src = get_unaligned_u16(value);
291         return 0;
292     case NFI_NXM_OF_TCP_DST:
293         flow->tp_dst = get_unaligned_u16(value);
294         return 0;
295
296         /* UDP header. */
297     case NFI_NXM_OF_UDP_SRC:
298         flow->tp_src = get_unaligned_u16(value);
299         return 0;
300     case NFI_NXM_OF_UDP_DST:
301         flow->tp_dst = get_unaligned_u16(value);
302         return 0;
303
304         /* ICMP header. */
305     case NFI_NXM_OF_ICMP_TYPE:
306         flow->tp_src = htons(*(uint8_t *) value);
307         return 0;
308     case NFI_NXM_OF_ICMP_CODE:
309         flow->tp_dst = htons(*(uint8_t *) value);
310         return 0;
311
312         /* ARP header. */
313     case NFI_NXM_OF_ARP_OP:
314         if (ntohs(get_unaligned_u16(value)) > 255) {
315             return NXM_BAD_VALUE;
316         } else {
317             flow->nw_proto = ntohs(get_unaligned_u16(value));
318             return 0;
319         }
320
321         /* Tunnel ID. */
322     case NFI_NXM_NX_TUN_ID:
323         flow->tun_id = htonl(ntohll(get_unaligned_u64(value)));
324         return 0;
325
326         /* Registers. */
327     case NFI_NXM_NX_REG0:
328     case NFI_NXM_NX_REG0_W:
329 #if FLOW_N_REGS >= 2
330     case NFI_NXM_NX_REG1:
331     case NFI_NXM_NX_REG1_W:
332 #endif
333 #if FLOW_N_REGS >= 3
334     case NFI_NXM_NX_REG2:
335     case NFI_NXM_NX_REG2_W:
336 #endif
337 #if FLOW_N_REGS >= 4
338     case NFI_NXM_NX_REG3:
339     case NFI_NXM_NX_REG3_W:
340 #endif
341 #if FLOW_N_REGS > 4
342 #error
343 #endif
344         return parse_nx_reg(f, flow, wc, value, mask);
345
346     case N_NXM_FIELDS:
347         NOT_REACHED();
348     }
349     NOT_REACHED();
350 }
351
352 static bool
353 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
354 {
355     return (!field->dl_type
356             || (field->dl_type == flow->dl_type
357                 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
358 }
359
360 static uint32_t
361 nx_entry_ok(const void *p, unsigned int match_len)
362 {
363     unsigned int payload_len;
364     ovs_be32 header_be;
365     uint32_t header;
366
367     if (match_len < 4) {
368         if (match_len) {
369             VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
370         }
371         return 0;
372     }
373     memcpy(&header_be, p, 4);
374     header = ntohl(header_be);
375
376     payload_len = NXM_LENGTH(header);
377     if (!payload_len) {
378         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
379                     "length 0", header);
380         return 0;
381     }
382     if (match_len < payload_len + 4) {
383         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
384                     "%u bytes left in nx_match", payload_len + 4, match_len);
385         return 0;
386     }
387
388     return header;
389 }
390
391 int
392 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
393               struct cls_rule *rule)
394 {
395     uint32_t header;
396     uint8_t *p;
397
398     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
399     if (!p) {
400         VLOG_DBG_RL(&rl, "nx_match length %zu, rounded up to a "
401                     "multiple of 8, is longer than space in message (max "
402                     "length %zu)", match_len, b->size);
403         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
404     }
405
406     cls_rule_init_catchall(rule, priority);
407     while ((header = nx_entry_ok(p, match_len)) != 0) {
408         unsigned length = NXM_LENGTH(header);
409         const struct nxm_field *f;
410         int error;
411
412         f = nxm_field_lookup(header);
413         if (!f) {
414             error = NXM_BAD_TYPE;
415         } else if (!nxm_prereqs_ok(f, &rule->flow)) {
416             error = NXM_BAD_PREREQ;
417         } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
418             error = NXM_DUP_TYPE;
419         } else {
420             /* 'hasmask' and 'length' are known to be correct at this point
421              * because they are included in 'header' and nxm_field_lookup()
422              * checked them already. */
423             rule->wc.wildcards &= ~f->wildcard;
424             error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
425         }
426         if (error) {
427             VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
428                         "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
429                         "(error %x)",
430                         NXM_VENDOR(header), NXM_FIELD(header),
431                         NXM_HASMASK(header), NXM_TYPE(header),
432                         error);
433             return error;
434         }
435
436
437         p += 4 + length;
438         match_len -= 4 + length;
439     }
440
441     return match_len ? NXM_INVALID : 0;
442 }
443 \f
444 /* nx_put_match() and helpers.
445  *
446  * 'put' functions whose names end in 'w' add a wildcarded field.
447  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
448  * Other 'put' functions add exact-match fields.
449  */
450
451 static void
452 nxm_put_header(struct ofpbuf *b, uint32_t header)
453 {
454     ovs_be32 n_header = htonl(header);
455     ofpbuf_put(b, &n_header, sizeof n_header);
456 }
457
458 static void
459 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
460 {
461     nxm_put_header(b, header);
462     ofpbuf_put(b, &value, sizeof value);
463 }
464
465 static void
466 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
467 {
468     nxm_put_header(b, header);
469     ofpbuf_put(b, &value, sizeof value);
470 }
471
472 static void
473 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
474 {
475     nxm_put_header(b, header);
476     ofpbuf_put(b, &value, sizeof value);
477     ofpbuf_put(b, &mask, sizeof mask);
478 }
479
480 static void
481 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
482 {
483     nxm_put_header(b, header);
484     ofpbuf_put(b, &value, sizeof value);
485 }
486
487 static void
488 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
489 {
490     nxm_put_header(b, header);
491     ofpbuf_put(b, &value, sizeof value);
492     ofpbuf_put(b, &mask, sizeof mask);
493 }
494
495 static void
496 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
497 {
498     switch (mask) {
499     case 0:
500         break;
501
502     case UINT32_MAX:
503         nxm_put_32(b, header, value);
504         break;
505
506     default:
507         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
508         break;
509     }
510 }
511
512 static void
513 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
514 {
515     nxm_put_header(b, header);
516     ofpbuf_put(b, &value, sizeof value);
517 }
518
519 static void
520 nxm_put_eth(struct ofpbuf *b, uint32_t header,
521             const uint8_t value[ETH_ADDR_LEN])
522 {
523     nxm_put_header(b, header);
524     ofpbuf_put(b, value, ETH_ADDR_LEN);
525 }
526
527 static void
528 nxm_put_eth_dst(struct ofpbuf *b,
529                 uint32_t wc, const uint8_t value[ETH_ADDR_LEN])
530 {
531     switch (wc & (OFPFW_DL_DST | FWW_ETH_MCAST)) {
532     case OFPFW_DL_DST | FWW_ETH_MCAST:
533         break;
534     case OFPFW_DL_DST:
535         nxm_put_header(b, NXM_OF_ETH_DST_W);
536         ofpbuf_put(b, value, ETH_ADDR_LEN);
537         ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
538         break;
539     case FWW_ETH_MCAST:
540         nxm_put_header(b, NXM_OF_ETH_DST_W);
541         ofpbuf_put(b, value, ETH_ADDR_LEN);
542         ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
543         break;
544     case 0:
545         nxm_put_eth(b, NXM_OF_ETH_DST, value);
546         break;
547     }
548 }
549
550 int
551 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
552 {
553     const uint32_t wc = cr->wc.wildcards;
554     const struct flow *flow = &cr->flow;
555     const size_t start_len = b->size;
556     ovs_be16 vid, pcp;
557     int match_len;
558     int i;
559
560     /* Metadata. */
561     if (!(wc & OFPFW_IN_PORT)) {
562         uint16_t in_port = flow->in_port;
563         if (in_port == ODPP_LOCAL) {
564             in_port = OFPP_LOCAL;
565         }
566         nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
567     }
568
569     /* Ethernet. */
570     nxm_put_eth_dst(b, wc, flow->dl_dst);
571     if (!(wc & OFPFW_DL_SRC)) {
572         nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
573     }
574     if (!(wc & OFPFW_DL_TYPE)) {
575         nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
576     }
577
578     /* 802.1Q. */
579     vid = flow->dl_vlan & htons(VLAN_VID_MASK);
580     pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
581     switch (wc & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
582     case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
583         break;
584     case OFPFW_DL_VLAN:
585         nxm_put_16w(b, NXM_OF_VLAN_TCI_W, pcp | htons(VLAN_CFI),
586                      htons(VLAN_PCP_MASK | VLAN_CFI));
587         break;
588     case OFPFW_DL_VLAN_PCP:
589         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
590             nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
591         } else {
592             nxm_put_16w(b, NXM_OF_VLAN_TCI_W, vid | htons(VLAN_CFI),
593                          htons(VLAN_VID_MASK | VLAN_CFI));
594         }
595         break;
596     case 0:
597         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
598             nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
599         } else {
600             nxm_put_16(b, NXM_OF_VLAN_TCI, vid | pcp | htons(VLAN_CFI));
601         }
602         break;
603     }
604
605     if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
606         /* IP. */
607         if (!(wc & OFPFW_NW_TOS)) {
608             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
609         }
610         nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
611         nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
612
613         if (!(wc & OFPFW_NW_PROTO)) {
614             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
615             switch (flow->nw_proto) {
616                 /* TCP. */
617             case IP_TYPE_TCP:
618                 if (!(wc & OFPFW_TP_SRC)) {
619                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
620                 }
621                 if (!(wc & OFPFW_TP_DST)) {
622                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
623                 }
624                 break;
625
626                 /* UDP. */
627             case IP_TYPE_UDP:
628                 if (!(wc & OFPFW_TP_SRC)) {
629                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
630                 }
631                 if (!(wc & OFPFW_TP_DST)) {
632                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
633                 }
634                 break;
635
636                 /* ICMP. */
637             case IP_TYPE_ICMP:
638                 if (!(wc & OFPFW_TP_SRC)) {
639                     nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
640                 }
641                 if (!(wc & OFPFW_TP_DST)) {
642                     nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
643                 }
644                 break;
645             }
646         }
647     } else if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
648         /* ARP. */
649         if (!(wc & OFPFW_NW_PROTO)) {
650             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
651         }
652         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
653         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
654     }
655
656     /* Tunnel ID. */
657     if (!(wc & NXFW_TUN_ID)) {
658         nxm_put_64(b, NXM_NX_TUN_ID, htonll(ntohl(flow->tun_id)));
659     }
660
661     /* Registers. */
662     for (i = 0; i < FLOW_N_REGS; i++) {
663         nxm_put_32m(b, NXM_NX_REG(i),
664                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
665     }
666
667     match_len = b->size - start_len;
668     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
669     return match_len;
670 }
671 \f
672 /* nx_match_to_string() and helpers. */
673
674 char *
675 nx_match_to_string(const uint8_t *p, unsigned int match_len)
676 {
677     uint32_t header;
678     struct ds s;
679
680     if (!match_len) {
681         return xstrdup("<any>");
682     }
683
684     ds_init(&s);
685     while ((header = nx_entry_ok(p, match_len)) != 0) {
686         unsigned int length = NXM_LENGTH(header);
687         unsigned int value_len = nxm_field_bytes(header);
688         const uint8_t *value = p + 4;
689         const uint8_t *mask = value + value_len;
690         const struct nxm_field *f;
691         unsigned int i;
692
693         if (s.length) {
694             ds_put_cstr(&s, ", ");
695         }
696
697         f = nxm_field_lookup(header);
698         if (f) {
699             ds_put_cstr(&s, f->name);
700         } else {
701             ds_put_format(&s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
702         }
703
704         ds_put_char(&s, '(');
705
706         for (i = 0; i < value_len; i++) {
707             ds_put_format(&s, "%02x", value[i]);
708         }
709         if (NXM_HASMASK(header)) {
710             ds_put_char(&s, '/');
711             for (i = 0; i < value_len; i++) {
712                 ds_put_format(&s, "%02x", mask[i]);
713             }
714         }
715         ds_put_char(&s, ')');
716
717         p += 4 + length;
718         match_len -= 4 + length;
719     }
720
721     if (match_len) {
722         if (s.length) {
723             ds_put_cstr(&s, ", ");
724         }
725
726         ds_put_format(&s, "<%u invalid bytes>", match_len);
727     }
728
729     return ds_steal_cstr(&s);
730 }
731
732 static const struct nxm_field *
733 lookup_nxm_field(const char *name, int name_len)
734 {
735     const struct nxm_field *f;
736
737     for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
738         if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
739             return f;
740         }
741     }
742
743     return NULL;
744 }
745
746 static const char *
747 parse_hex_bytes(struct ofpbuf *b, const char *s, unsigned int n)
748 {
749     while (n--) {
750         int low, high;
751         uint8_t byte;
752
753         s += strspn(s, " ");
754         low = hexit_value(*s);
755         high = low < 0 ? low : hexit_value(s[1]);
756         if (low < 0 || high < 0) {
757             ovs_fatal(0, "%.2s: hex digits expected", s);
758         }
759
760         byte = 16 * low + high;
761         ofpbuf_put(b, &byte, 1);
762         s += 2;
763     }
764     return s;
765 }
766 \f
767 /* nx_match_from_string(). */
768
769 int
770 nx_match_from_string(const char *s, struct ofpbuf *b)
771 {
772     const char *full_s = s;
773     const size_t start_len = b->size;
774     int match_len;
775
776     if (!strcmp(s, "<any>")) {
777         /* Ensure that 'b->data' isn't actually null. */
778         ofpbuf_prealloc_tailroom(b, 1);
779         return 0;
780     }
781
782     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
783         const struct nxm_field *f;
784         int name_len;
785
786         name_len = strcspn(s, "(");
787         if (s[name_len] != '(') {
788             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
789         }
790
791         f = lookup_nxm_field(s, name_len);
792         if (!f) {
793             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
794         }
795
796         s += name_len + 1;
797
798         nxm_put_header(b, f->header);
799         s = parse_hex_bytes(b, s, nxm_field_bytes(f->header));
800         if (NXM_HASMASK(f->header)) {
801             s += strspn(s, " ");
802             if (*s != '/') {
803                 ovs_fatal(0, "%s: missing / in masked field %s",
804                           full_s, f->name);
805             }
806             s = parse_hex_bytes(b, s + 1, nxm_field_bytes(f->header));
807         }
808
809         s += strspn(s, " ");
810         if (*s != ')') {
811             ovs_fatal(0, "%s: missing ) following field %s", full_s, f->name);
812         }
813         s++;
814     }
815
816     match_len = b->size - start_len;
817     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
818     return match_len;
819 }
820 \f
821 /* nxm_check_reg_move(), nxm_check_reg_load(). */
822
823 static bool
824 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
825 {
826     return (f && !NXM_HASMASK(f->header)
827             && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
828 }
829
830 int
831 nxm_check_reg_move(const struct nx_action_reg_move *action,
832                    const struct flow *flow)
833 {
834     const struct nxm_field *src;
835     const struct nxm_field *dst;
836
837     if (action->n_bits == htons(0)) {
838         return BAD_ARGUMENT;
839     }
840
841     src = nxm_field_lookup(ntohl(action->src));
842     if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
843         return BAD_ARGUMENT;
844     }
845
846     dst = nxm_field_lookup(ntohl(action->dst));
847     if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
848         return BAD_ARGUMENT;
849     }
850
851     if (!NXM_IS_NX_REG(dst->header)
852         && dst->header != NXM_OF_VLAN_TCI
853         && dst->header != NXM_NX_TUN_ID) {
854         return BAD_ARGUMENT;
855     }
856
857     return 0;
858 }
859
860 int
861 nxm_check_reg_load(const struct nx_action_reg_load *action,
862                    const struct flow *flow)
863 {
864     const struct nxm_field *dst;
865     int ofs, n_bits;
866
867     ofs = ntohs(action->ofs_nbits) >> 6;
868     n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
869     dst = nxm_field_lookup(ntohl(action->dst));
870     if (!field_ok(dst, flow, ofs + n_bits)) {
871         return BAD_ARGUMENT;
872     }
873
874     /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
875      * action->value. */
876     if (n_bits < 64 && ntohll(action->value) >> n_bits) {
877         return BAD_ARGUMENT;
878     }
879
880     if (!NXM_IS_NX_REG(dst->header)) {
881         return BAD_ARGUMENT;
882     }
883
884     return 0;
885 }
886 \f
887 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
888
889 static uint64_t
890 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
891 {
892     switch (src->index) {
893     case NFI_NXM_OF_IN_PORT:
894         return flow->in_port == ODPP_LOCAL ? OFPP_LOCAL : flow->in_port;
895
896     case NFI_NXM_OF_ETH_DST:
897         return eth_addr_to_uint64(flow->dl_dst);
898
899     case NFI_NXM_OF_ETH_SRC:
900         return eth_addr_to_uint64(flow->dl_src);
901
902     case NFI_NXM_OF_ETH_TYPE:
903         return ntohs(flow->dl_type);
904
905     case NFI_NXM_OF_VLAN_TCI:
906         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
907             return 0;
908         } else {
909             return (ntohs(flow->dl_vlan & htons(VLAN_VID_MASK))
910                     | ((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK)
911                     | VLAN_CFI);
912         }
913
914     case NFI_NXM_OF_IP_TOS:
915         return flow->nw_tos;
916
917     case NFI_NXM_OF_IP_PROTO:
918     case NFI_NXM_OF_ARP_OP:
919         return flow->nw_proto;
920
921     case NFI_NXM_OF_IP_SRC:
922     case NFI_NXM_OF_ARP_SPA:
923         return ntohl(flow->nw_src);
924
925     case NFI_NXM_OF_IP_DST:
926     case NFI_NXM_OF_ARP_TPA:
927         return ntohl(flow->nw_dst);
928
929     case NFI_NXM_OF_TCP_SRC:
930     case NFI_NXM_OF_UDP_SRC:
931         return ntohs(flow->tp_src);
932
933     case NFI_NXM_OF_TCP_DST:
934     case NFI_NXM_OF_UDP_DST:
935         return ntohs(flow->tp_dst);
936
937     case NFI_NXM_OF_ICMP_TYPE:
938         return ntohs(flow->tp_src) & 0xff;
939
940     case NFI_NXM_OF_ICMP_CODE:
941         return ntohs(flow->tp_dst) & 0xff;
942
943     case NFI_NXM_NX_TUN_ID:
944         return ntohl(flow->tun_id);
945
946 #define NXM_READ_REGISTER(IDX)                  \
947     case NFI_NXM_NX_REG##IDX:                   \
948         return flow->regs[IDX];                 \
949     case NFI_NXM_NX_REG##IDX##_W:               \
950         NOT_REACHED();
951
952     NXM_READ_REGISTER(0);
953 #if FLOW_N_REGS >= 2
954     NXM_READ_REGISTER(1);
955 #endif
956 #if FLOW_N_REGS >= 3
957     NXM_READ_REGISTER(2);
958 #endif
959 #if FLOW_N_REGS >= 4
960     NXM_READ_REGISTER(3);
961 #endif
962 #if FLOW_N_REGS > 4
963 #error
964 #endif
965
966     case NFI_NXM_OF_ETH_DST_W:
967     case NFI_NXM_OF_VLAN_TCI_W:
968     case NFI_NXM_OF_IP_SRC_W:
969     case NFI_NXM_OF_IP_DST_W:
970     case NFI_NXM_OF_ARP_SPA_W:
971     case NFI_NXM_OF_ARP_TPA_W:
972     case N_NXM_FIELDS:
973         NOT_REACHED();
974     }
975
976     NOT_REACHED();
977 }
978
979 void
980 nxm_execute_reg_move(const struct nx_action_reg_move *action,
981                      struct flow *flow)
982 {
983     /* Preparation. */
984     int n_bits = ntohs(action->n_bits);
985     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
986
987     /* Get the interesting bits of the source field. */
988     const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
989     int src_ofs = ntohs(action->src_ofs);
990     uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
991
992     /* Get the remaining bits of the destination field. */
993     const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
994     int dst_ofs = ntohs(action->dst_ofs);
995     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
996
997     /* Get the final value. */
998     uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
999
1000     /* Store the result. */
1001     if (NXM_IS_NX_REG(dst->header)) {
1002         flow->regs[NXM_NX_REG_IDX(dst->header)] = new_data;
1003     } else if (dst->header == NXM_OF_VLAN_TCI) {
1004         ovs_be16 vlan_tci = htons(new_data & VLAN_CFI ? new_data : 0);
1005         flow->dl_vlan = htons(vlan_tci_to_vid(vlan_tci));
1006         flow->dl_vlan_pcp = vlan_tci_to_pcp(vlan_tci);
1007     } else if (dst->header == NXM_NX_TUN_ID) {
1008         flow->tun_id = htonl(new_data);
1009     } else {
1010         NOT_REACHED();
1011     }
1012 }
1013
1014 void
1015 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1016                      struct flow *flow)
1017 {
1018     /* Preparation. */
1019     int n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
1020     uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
1021     uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(action->dst))];
1022
1023     /* Get source data. */
1024     uint32_t src_data = ntohll(action->value);
1025
1026     /* Get remaining bits of the destination field. */
1027     int dst_ofs = ntohs(action->ofs_nbits) >> 6;
1028     uint32_t dst_data = *reg & ~(mask << dst_ofs);
1029
1030     *reg = dst_data | (src_data << dst_ofs);
1031 }