nx-match: Rename and export functions for parsing and formatting subfields.
[cascardo/ovs.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     flow_wildcards_t wildcard;  /* FWW_* 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 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 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_nx_reg(const struct nxm_field *f,
145              struct flow *flow, struct flow_wildcards *wc,
146              const void *value, const void *maskp)
147 {
148     int idx = NXM_NX_REG_IDX(f->header);
149     if (wc->reg_masks[idx]) {
150         return NXM_DUP_TYPE;
151     } else {
152         flow_wildcards_set_reg_mask(wc, idx,
153                                     (NXM_HASMASK(f->header)
154                                      ? ntohl(get_unaligned_be32(maskp))
155                                      : UINT32_MAX));
156         flow->regs[idx] = ntohl(get_unaligned_be32(value));
157         flow->regs[idx] &= wc->reg_masks[idx];
158         return 0;
159     }
160 }
161
162 static int
163 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
164                 const void *value, const void *mask)
165 {
166     struct flow_wildcards *wc = &rule->wc;
167     struct flow *flow = &rule->flow;
168
169     switch (f->index) {
170         /* Metadata. */
171     case NFI_NXM_OF_IN_PORT:
172         flow->in_port = ntohs(get_unaligned_be16(value));
173         if (flow->in_port == OFPP_LOCAL) {
174             flow->in_port = ODPP_LOCAL;
175         }
176         return 0;
177
178         /* Ethernet header. */
179     case NFI_NXM_OF_ETH_DST:
180         if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
181             != (FWW_DL_DST | FWW_ETH_MCAST)) {
182             return NXM_DUP_TYPE;
183         } else {
184             wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
185             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
186             return 0;
187         }
188     case NFI_NXM_OF_ETH_DST_W:
189         if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
190             != (FWW_DL_DST | FWW_ETH_MCAST)) {
191             return NXM_DUP_TYPE;
192         } else if (eth_addr_equals(mask, eth_mcast_1)) {
193             wc->wildcards &= ~FWW_ETH_MCAST;
194             flow->dl_dst[0] = *(uint8_t *) value & 0x01;
195         } else if (eth_addr_equals(mask, eth_mcast_0)) {
196             wc->wildcards &= ~FWW_DL_DST;
197             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
198             flow->dl_dst[0] &= 0xfe;
199         } else if (eth_addr_equals(mask, eth_all_0s)) {
200             return 0;
201         } else if (eth_addr_equals(mask, eth_all_1s)) {
202             wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
203             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
204             return 0;
205         } else {
206             return NXM_BAD_MASK;
207         }
208     case NFI_NXM_OF_ETH_SRC:
209         memcpy(flow->dl_src, value, ETH_ADDR_LEN);
210         return 0;
211     case NFI_NXM_OF_ETH_TYPE:
212         flow->dl_type = get_unaligned_be16(value);
213         return 0;
214
215         /* 802.1Q header. */
216     case NFI_NXM_OF_VLAN_TCI:
217         if (wc->vlan_tci_mask) {
218             return NXM_DUP_TYPE;
219         } else {
220             cls_rule_set_dl_tci(rule, get_unaligned_be16(value));
221             return 0;
222         }
223     case NFI_NXM_OF_VLAN_TCI_W:
224         if (wc->vlan_tci_mask) {
225             return NXM_DUP_TYPE;
226         } else {
227             cls_rule_set_dl_tci_masked(rule, get_unaligned_be16(value),
228                                        get_unaligned_be16(mask));
229             return 0;
230         }
231
232         /* IP header. */
233     case NFI_NXM_OF_IP_TOS:
234         if (*(uint8_t *) value & 0x03) {
235             return NXM_BAD_VALUE;
236         } else {
237             flow->nw_tos = *(uint8_t *) value;
238             return 0;
239         }
240     case NFI_NXM_OF_IP_PROTO:
241         flow->nw_proto = *(uint8_t *) value;
242         return 0;
243
244         /* IP addresses in IP and ARP headers. */
245     case NFI_NXM_OF_IP_SRC:
246     case NFI_NXM_OF_ARP_SPA:
247         if (wc->nw_src_mask) {
248             return NXM_DUP_TYPE;
249         } else {
250             cls_rule_set_nw_src(rule, get_unaligned_be32(value));
251             return 0;
252         }
253     case NFI_NXM_OF_IP_SRC_W:
254     case NFI_NXM_OF_ARP_SPA_W:
255         if (wc->nw_src_mask) {
256             return NXM_DUP_TYPE;
257         } else {
258             ovs_be32 ip = get_unaligned_be32(value);
259             ovs_be32 netmask = get_unaligned_be32(mask);
260             if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
261                 return NXM_BAD_MASK;
262             }
263             return 0;
264         }
265     case NFI_NXM_OF_IP_DST:
266     case NFI_NXM_OF_ARP_TPA:
267         if (wc->nw_dst_mask) {
268             return NXM_DUP_TYPE;
269         } else {
270             cls_rule_set_nw_dst(rule, get_unaligned_be32(value));
271             return 0;
272         }
273     case NFI_NXM_OF_IP_DST_W:
274     case NFI_NXM_OF_ARP_TPA_W:
275         if (wc->nw_dst_mask) {
276             return NXM_DUP_TYPE;
277         } else {
278             ovs_be32 ip = get_unaligned_be32(value);
279             ovs_be32 netmask = get_unaligned_be32(mask);
280             if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
281                 return NXM_BAD_MASK;
282             }
283             return 0;
284         }
285
286         /* TCP header. */
287     case NFI_NXM_OF_TCP_SRC:
288         flow->tp_src = get_unaligned_be16(value);
289         return 0;
290     case NFI_NXM_OF_TCP_DST:
291         flow->tp_dst = get_unaligned_be16(value);
292         return 0;
293
294         /* UDP header. */
295     case NFI_NXM_OF_UDP_SRC:
296         flow->tp_src = get_unaligned_be16(value);
297         return 0;
298     case NFI_NXM_OF_UDP_DST:
299         flow->tp_dst = get_unaligned_be16(value);
300         return 0;
301
302         /* ICMP header. */
303     case NFI_NXM_OF_ICMP_TYPE:
304         flow->tp_src = htons(*(uint8_t *) value);
305         return 0;
306     case NFI_NXM_OF_ICMP_CODE:
307         flow->tp_dst = htons(*(uint8_t *) value);
308         return 0;
309
310         /* ARP header. */
311     case NFI_NXM_OF_ARP_OP:
312         if (ntohs(get_unaligned_be16(value)) > 255) {
313             return NXM_BAD_VALUE;
314         } else {
315             flow->nw_proto = ntohs(get_unaligned_be16(value));
316             return 0;
317         }
318
319         /* Tunnel ID. */
320     case NFI_NXM_NX_TUN_ID:
321         flow->tun_id = get_unaligned_be64(value);
322         return 0;
323
324         /* Registers. */
325     case NFI_NXM_NX_REG0:
326     case NFI_NXM_NX_REG0_W:
327 #if FLOW_N_REGS >= 2
328     case NFI_NXM_NX_REG1:
329     case NFI_NXM_NX_REG1_W:
330 #endif
331 #if FLOW_N_REGS >= 3
332     case NFI_NXM_NX_REG2:
333     case NFI_NXM_NX_REG2_W:
334 #endif
335 #if FLOW_N_REGS >= 4
336     case NFI_NXM_NX_REG3:
337     case NFI_NXM_NX_REG3_W:
338 #endif
339 #if FLOW_N_REGS > 4
340 #error
341 #endif
342         return parse_nx_reg(f, flow, wc, value, mask);
343
344     case N_NXM_FIELDS:
345         NOT_REACHED();
346     }
347     NOT_REACHED();
348 }
349
350 static bool
351 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
352 {
353     return (!field->dl_type
354             || (field->dl_type == flow->dl_type
355                 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
356 }
357
358 static uint32_t
359 nx_entry_ok(const void *p, unsigned int match_len)
360 {
361     unsigned int payload_len;
362     ovs_be32 header_be;
363     uint32_t header;
364
365     if (match_len < 4) {
366         if (match_len) {
367             VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
368         }
369         return 0;
370     }
371     memcpy(&header_be, p, 4);
372     header = ntohl(header_be);
373
374     payload_len = NXM_LENGTH(header);
375     if (!payload_len) {
376         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
377                     "length 0", header);
378         return 0;
379     }
380     if (match_len < payload_len + 4) {
381         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
382                     "%u bytes left in nx_match", payload_len + 4, match_len);
383         return 0;
384     }
385
386     return header;
387 }
388
389 int
390 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
391               struct cls_rule *rule)
392 {
393     uint32_t header;
394     uint8_t *p;
395
396     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
397     if (!p) {
398         VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
399                     "multiple of 8, is longer than space in message (max "
400                     "length %zu)", match_len, b->size);
401         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
402     }
403
404     cls_rule_init_catchall(rule, priority);
405     while ((header = nx_entry_ok(p, match_len)) != 0) {
406         unsigned length = NXM_LENGTH(header);
407         const struct nxm_field *f;
408         int error;
409
410         f = nxm_field_lookup(header);
411         if (!f) {
412             error = NXM_BAD_TYPE;
413         } else if (!nxm_prereqs_ok(f, &rule->flow)) {
414             error = NXM_BAD_PREREQ;
415         } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
416             error = NXM_DUP_TYPE;
417         } else {
418             /* 'hasmask' and 'length' are known to be correct at this point
419              * because they are included in 'header' and nxm_field_lookup()
420              * checked them already. */
421             rule->wc.wildcards &= ~f->wildcard;
422             error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
423         }
424         if (error) {
425             VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
426                         "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
427                         "(error %x)",
428                         NXM_VENDOR(header), NXM_FIELD(header),
429                         NXM_HASMASK(header), NXM_TYPE(header),
430                         error);
431             return error;
432         }
433
434
435         p += 4 + length;
436         match_len -= 4 + length;
437     }
438
439     return match_len ? NXM_INVALID : 0;
440 }
441 \f
442 /* nx_put_match() and helpers.
443  *
444  * 'put' functions whose names end in 'w' add a wildcarded field.
445  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
446  * Other 'put' functions add exact-match fields.
447  */
448
449 static void
450 nxm_put_header(struct ofpbuf *b, uint32_t header)
451 {
452     ovs_be32 n_header = htonl(header);
453     ofpbuf_put(b, &n_header, sizeof n_header);
454 }
455
456 static void
457 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
458 {
459     nxm_put_header(b, header);
460     ofpbuf_put(b, &value, sizeof value);
461 }
462
463 static void
464 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
465 {
466     nxm_put_header(b, header);
467     ofpbuf_put(b, &value, sizeof value);
468 }
469
470 static void
471 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
472 {
473     nxm_put_header(b, header);
474     ofpbuf_put(b, &value, sizeof value);
475     ofpbuf_put(b, &mask, sizeof mask);
476 }
477
478 static void
479 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
480 {
481     switch (mask) {
482     case 0:
483         break;
484
485     case CONSTANT_HTONS(UINT16_MAX):
486         nxm_put_16(b, header, value);
487         break;
488
489     default:
490         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
491         break;
492     }
493 }
494
495 static void
496 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
497 {
498     nxm_put_header(b, header);
499     ofpbuf_put(b, &value, sizeof value);
500 }
501
502 static void
503 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
504 {
505     nxm_put_header(b, header);
506     ofpbuf_put(b, &value, sizeof value);
507     ofpbuf_put(b, &mask, sizeof mask);
508 }
509
510 static void
511 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
512 {
513     switch (mask) {
514     case 0:
515         break;
516
517     case CONSTANT_HTONL(UINT32_MAX):
518         nxm_put_32(b, header, value);
519         break;
520
521     default:
522         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
523         break;
524     }
525 }
526
527 static void
528 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
529 {
530     nxm_put_header(b, header);
531     ofpbuf_put(b, &value, sizeof value);
532 }
533
534 static void
535 nxm_put_eth(struct ofpbuf *b, uint32_t header,
536             const uint8_t value[ETH_ADDR_LEN])
537 {
538     nxm_put_header(b, header);
539     ofpbuf_put(b, value, ETH_ADDR_LEN);
540 }
541
542 static void
543 nxm_put_eth_dst(struct ofpbuf *b,
544                 uint32_t wc, const uint8_t value[ETH_ADDR_LEN])
545 {
546     switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
547     case FWW_DL_DST | FWW_ETH_MCAST:
548         break;
549     case FWW_DL_DST:
550         nxm_put_header(b, NXM_OF_ETH_DST_W);
551         ofpbuf_put(b, value, ETH_ADDR_LEN);
552         ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
553         break;
554     case FWW_ETH_MCAST:
555         nxm_put_header(b, NXM_OF_ETH_DST_W);
556         ofpbuf_put(b, value, ETH_ADDR_LEN);
557         ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
558         break;
559     case 0:
560         nxm_put_eth(b, NXM_OF_ETH_DST, value);
561         break;
562     }
563 }
564
565 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
566  * 'cr->priority', because priority is not part of nx_match), plus enough
567  * zero bytes to pad the nx_match out to a multiple of 8.
568  *
569  * This function can cause 'b''s data to be reallocated.
570  *
571  * Returns the number of bytes appended to 'b', excluding padding.
572  *
573  * If 'cr' is a catch-all rule that matches every packet, then this function
574  * appends nothing to 'b' and returns 0. */
575 int
576 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
577 {
578     const flow_wildcards_t wc = cr->wc.wildcards;
579     const struct flow *flow = &cr->flow;
580     const size_t start_len = b->size;
581     int match_len;
582     int i;
583
584     /* Metadata. */
585     if (!(wc & FWW_IN_PORT)) {
586         uint16_t in_port = flow->in_port;
587         if (in_port == ODPP_LOCAL) {
588             in_port = OFPP_LOCAL;
589         }
590         nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
591     }
592
593     /* Ethernet. */
594     nxm_put_eth_dst(b, wc, flow->dl_dst);
595     if (!(wc & FWW_DL_SRC)) {
596         nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
597     }
598     if (!(wc & FWW_DL_TYPE)) {
599         nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
600     }
601
602     /* 802.1Q. */
603     nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
604
605     /* L3. */
606     if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
607         /* IP. */
608         if (!(wc & FWW_NW_TOS)) {
609             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
610         }
611         nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
612         nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
613
614         if (!(wc & FWW_NW_PROTO)) {
615             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
616             switch (flow->nw_proto) {
617                 /* TCP. */
618             case IP_TYPE_TCP:
619                 if (!(wc & FWW_TP_SRC)) {
620                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
621                 }
622                 if (!(wc & FWW_TP_DST)) {
623                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
624                 }
625                 break;
626
627                 /* UDP. */
628             case IP_TYPE_UDP:
629                 if (!(wc & FWW_TP_SRC)) {
630                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
631                 }
632                 if (!(wc & FWW_TP_DST)) {
633                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
634                 }
635                 break;
636
637                 /* ICMP. */
638             case IP_TYPE_ICMP:
639                 if (!(wc & FWW_TP_SRC)) {
640                     nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
641                 }
642                 if (!(wc & FWW_TP_DST)) {
643                     nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
644                 }
645                 break;
646             }
647         }
648     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
649         /* ARP. */
650         if (!(wc & FWW_NW_PROTO)) {
651             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
652         }
653         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
654         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
655     }
656
657     /* Tunnel ID. */
658     if (!(wc & FWW_TUN_ID)) {
659         nxm_put_64(b, NXM_NX_TUN_ID, flow->tun_id);
660     }
661
662     /* Registers. */
663     for (i = 0; i < FLOW_N_REGS; i++) {
664         nxm_put_32m(b, NXM_NX_REG(i),
665                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
666     }
667
668     match_len = b->size - start_len;
669     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
670     return match_len;
671 }
672 \f
673 /* nx_match_to_string() and helpers. */
674
675 static void format_nxm_field_name(struct ds *, uint32_t header);
676
677 char *
678 nx_match_to_string(const uint8_t *p, unsigned int match_len)
679 {
680     uint32_t header;
681     struct ds s;
682
683     if (!match_len) {
684         return xstrdup("<any>");
685     }
686
687     ds_init(&s);
688     while ((header = nx_entry_ok(p, match_len)) != 0) {
689         unsigned int length = NXM_LENGTH(header);
690         unsigned int value_len = nxm_field_bytes(header);
691         const uint8_t *value = p + 4;
692         const uint8_t *mask = value + value_len;
693         unsigned int i;
694
695         if (s.length) {
696             ds_put_cstr(&s, ", ");
697         }
698
699         format_nxm_field_name(&s, header);
700         ds_put_char(&s, '(');
701
702         for (i = 0; i < value_len; i++) {
703             ds_put_format(&s, "%02x", value[i]);
704         }
705         if (NXM_HASMASK(header)) {
706             ds_put_char(&s, '/');
707             for (i = 0; i < value_len; i++) {
708                 ds_put_format(&s, "%02x", mask[i]);
709             }
710         }
711         ds_put_char(&s, ')');
712
713         p += 4 + length;
714         match_len -= 4 + length;
715     }
716
717     if (match_len) {
718         if (s.length) {
719             ds_put_cstr(&s, ", ");
720         }
721
722         ds_put_format(&s, "<%u invalid bytes>", match_len);
723     }
724
725     return ds_steal_cstr(&s);
726 }
727
728 static void
729 format_nxm_field_name(struct ds *s, uint32_t header)
730 {
731     const struct nxm_field *f = nxm_field_lookup(header);
732     if (f) {
733         ds_put_cstr(s, f->name);
734     } else {
735         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
736     }
737 }
738
739 static uint32_t
740 parse_nxm_field_name(const char *name, int name_len)
741 {
742     const struct nxm_field *f;
743
744     /* Check whether it's a field name. */
745     for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
746         if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
747             return f->header;
748         }
749     }
750
751     /* Check whether it's a 32-bit field header value as hex.
752      * (This isn't ordinarily useful except for testing error behavior.) */
753     if (name_len == 8) {
754         uint32_t header = hexits_value(name, name_len, NULL);
755         if (header != UINT_MAX) {
756             return header;
757         }
758     }
759
760     return 0;
761 }
762 \f
763 /* nx_match_from_string(). */
764
765 int
766 nx_match_from_string(const char *s, struct ofpbuf *b)
767 {
768     const char *full_s = s;
769     const size_t start_len = b->size;
770     int match_len;
771
772     if (!strcmp(s, "<any>")) {
773         /* Ensure that 'b->data' isn't actually null. */
774         ofpbuf_prealloc_tailroom(b, 1);
775         return 0;
776     }
777
778     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
779         const char *name;
780         uint32_t header;
781         int name_len;
782         size_t n;
783
784         name = s;
785         name_len = strcspn(s, "(");
786         if (s[name_len] != '(') {
787             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
788         }
789
790         header = parse_nxm_field_name(name, name_len);
791         if (!header) {
792             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
793         }
794
795         s += name_len + 1;
796
797         nxm_put_header(b, header);
798         s = ofpbuf_put_hex(b, s, &n);
799         if (n != nxm_field_bytes(header)) {
800             ovs_fatal(0, "%.2s: hex digits expected", s);
801         }
802         if (NXM_HASMASK(header)) {
803             s += strspn(s, " ");
804             if (*s != '/') {
805                 ovs_fatal(0, "%s: missing / in masked field %.*s",
806                           full_s, name_len, name);
807             }
808             s = ofpbuf_put_hex(b, s + 1, &n);
809             if (n != nxm_field_bytes(header)) {
810                 ovs_fatal(0, "%.2s: hex digits expected", s);
811             }
812         }
813
814         s += strspn(s, " ");
815         if (*s != ')') {
816             ovs_fatal(0, "%s: missing ) following field %.*s",
817                       full_s, name_len, name);
818         }
819         s++;
820     }
821
822     match_len = b->size - start_len;
823     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
824     return match_len;
825 }
826 \f
827 const char *
828 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
829 {
830     const char *full_s = s;
831     const char *name;
832     uint32_t header;
833     int start, end;
834     int name_len;
835     int width;
836
837     name = s;
838     name_len = strcspn(s, "[");
839     if (s[name_len] != '[') {
840         ovs_fatal(0, "%s: missing [ looking for field name", full_s);
841     }
842
843     header = parse_nxm_field_name(name, name_len);
844     if (!header) {
845         ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
846     }
847     width = nxm_field_bits(header);
848
849     s += name_len;
850     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
851         /* Nothing to do. */
852     } else if (sscanf(s, "[%d]", &start) == 1) {
853         end = start;
854     } else if (!strncmp(s, "[]", 2)) {
855         start = 0;
856         end = width - 1;
857     } else {
858         ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
859                   "[<start>..<end>]", full_s);
860     }
861     s = strchr(s, ']') + 1;
862
863     if (start > end) {
864         ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
865                   full_s, start, end);
866     } else if (start >= width) {
867         ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
868                   "%d bits wide", full_s, start, width);
869     } else if (end >= width){
870         ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
871                   "%d bits wide", full_s, end, width);
872     }
873
874     *headerp = header;
875     *ofsp = start;
876     *n_bitsp = end - start + 1;
877
878     return s;
879 }
880
881 void
882 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
883 {
884     const char *full_s = s;
885     uint32_t src, dst;
886     int src_ofs, dst_ofs;
887     int src_n_bits, dst_n_bits;
888
889     s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
890     if (strncmp(s, "->", 2)) {
891         ovs_fatal(0, "%s: missing `->' following source", full_s);
892     }
893     s += 2;
894     s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
895     if (*s != '\0') {
896         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
897     }
898
899     if (src_n_bits != dst_n_bits) {
900         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
901                   "%d bits wide", full_s, src_n_bits, dst_n_bits);
902     }
903
904     move->type = htons(OFPAT_VENDOR);
905     move->len = htons(sizeof *move);
906     move->vendor = htonl(NX_VENDOR_ID);
907     move->subtype = htons(NXAST_REG_MOVE);
908     move->n_bits = htons(src_n_bits);
909     move->src_ofs = htons(src_ofs);
910     move->dst_ofs = htons(dst_ofs);
911     move->src = htonl(src);
912     move->dst = htonl(dst);
913 }
914
915 void
916 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
917 {
918     const char *full_s = s;
919     uint32_t dst;
920     int ofs, n_bits;
921     uint64_t value;
922
923     value = strtoull(s, (char **) &s, 0);
924     if (strncmp(s, "->", 2)) {
925         ovs_fatal(0, "%s: missing `->' following value", full_s);
926     }
927     s += 2;
928     s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
929     if (*s != '\0') {
930         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
931     }
932
933     if (n_bits < 64 && (value >> n_bits) != 0) {
934         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
935                   full_s, value, n_bits);
936     }
937
938     load->type = htons(OFPAT_VENDOR);
939     load->len = htons(sizeof *load);
940     load->vendor = htonl(NX_VENDOR_ID);
941     load->subtype = htons(NXAST_REG_LOAD);
942     load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
943     load->dst = htonl(dst);
944     load->value = htonll(value);
945 }
946 \f
947 /* nxm_format_reg_move(), nxm_format_reg_load(). */
948
949 void
950 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
951 {
952     format_nxm_field_name(s, header);
953     if (ofs == 0 && n_bits == nxm_field_bits(header)) {
954         ds_put_cstr(s, "[]");
955     } else if (n_bits == 1) {
956         ds_put_format(s, "[%d]", ofs);
957     } else {
958         ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
959     }
960 }
961
962 void
963 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
964 {
965     int n_bits = ntohs(move->n_bits);
966     int src_ofs = ntohs(move->src_ofs);
967     int dst_ofs = ntohs(move->dst_ofs);
968     uint32_t src = ntohl(move->src);
969     uint32_t dst = ntohl(move->dst);
970
971     ds_put_format(s, "move:");
972     nxm_format_field_bits(s, src, src_ofs, n_bits);
973     ds_put_cstr(s, "->");
974     nxm_format_field_bits(s, dst, dst_ofs, n_bits);
975 }
976
977 void
978 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
979 {
980     int ofs = nxm_decode_ofs(load->ofs_nbits);
981     int n_bits = nxm_decode_n_bits(load->ofs_nbits);
982     uint32_t dst = ntohl(load->dst);
983     uint64_t value = ntohll(load->value);
984
985     ds_put_format(s, "load:%"PRIu64"->", value);
986     nxm_format_field_bits(s, dst, ofs, n_bits);
987 }
988 \f
989 /* nxm_check_reg_move(), nxm_check_reg_load(). */
990
991 static bool
992 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
993 {
994     return (f && !NXM_HASMASK(f->header)
995             && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
996 }
997
998 int
999 nxm_check_reg_move(const struct nx_action_reg_move *action,
1000                    const struct flow *flow)
1001 {
1002     const struct nxm_field *src;
1003     const struct nxm_field *dst;
1004
1005     if (action->n_bits == htons(0)) {
1006         return BAD_ARGUMENT;
1007     }
1008
1009     src = nxm_field_lookup(ntohl(action->src));
1010     if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
1011         return BAD_ARGUMENT;
1012     }
1013
1014     dst = nxm_field_lookup(ntohl(action->dst));
1015     if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
1016         return BAD_ARGUMENT;
1017     }
1018
1019     if (!NXM_IS_NX_REG(dst->header)
1020         && dst->header != NXM_OF_VLAN_TCI
1021         && dst->header != NXM_NX_TUN_ID) {
1022         return BAD_ARGUMENT;
1023     }
1024
1025     return 0;
1026 }
1027
1028 int
1029 nxm_check_reg_load(const struct nx_action_reg_load *action,
1030                    const struct flow *flow)
1031 {
1032     const struct nxm_field *dst;
1033     int ofs, n_bits;
1034
1035     ofs = nxm_decode_ofs(action->ofs_nbits);
1036     n_bits = nxm_decode_n_bits(action->ofs_nbits);
1037     dst = nxm_field_lookup(ntohl(action->dst));
1038     if (!field_ok(dst, flow, ofs + n_bits)) {
1039         return BAD_ARGUMENT;
1040     }
1041
1042     /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1043      * action->value. */
1044     if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1045         return BAD_ARGUMENT;
1046     }
1047
1048     if (!NXM_IS_NX_REG(dst->header)) {
1049         return BAD_ARGUMENT;
1050     }
1051
1052     return 0;
1053 }
1054 \f
1055 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1056
1057 static uint64_t
1058 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
1059 {
1060     switch (src->index) {
1061     case NFI_NXM_OF_IN_PORT:
1062         return flow->in_port == ODPP_LOCAL ? OFPP_LOCAL : flow->in_port;
1063
1064     case NFI_NXM_OF_ETH_DST:
1065         return eth_addr_to_uint64(flow->dl_dst);
1066
1067     case NFI_NXM_OF_ETH_SRC:
1068         return eth_addr_to_uint64(flow->dl_src);
1069
1070     case NFI_NXM_OF_ETH_TYPE:
1071         return ntohs(flow->dl_type);
1072
1073     case NFI_NXM_OF_VLAN_TCI:
1074         return ntohs(flow->vlan_tci);
1075
1076     case NFI_NXM_OF_IP_TOS:
1077         return flow->nw_tos;
1078
1079     case NFI_NXM_OF_IP_PROTO:
1080     case NFI_NXM_OF_ARP_OP:
1081         return flow->nw_proto;
1082
1083     case NFI_NXM_OF_IP_SRC:
1084     case NFI_NXM_OF_ARP_SPA:
1085         return ntohl(flow->nw_src);
1086
1087     case NFI_NXM_OF_IP_DST:
1088     case NFI_NXM_OF_ARP_TPA:
1089         return ntohl(flow->nw_dst);
1090
1091     case NFI_NXM_OF_TCP_SRC:
1092     case NFI_NXM_OF_UDP_SRC:
1093         return ntohs(flow->tp_src);
1094
1095     case NFI_NXM_OF_TCP_DST:
1096     case NFI_NXM_OF_UDP_DST:
1097         return ntohs(flow->tp_dst);
1098
1099     case NFI_NXM_OF_ICMP_TYPE:
1100         return ntohs(flow->tp_src) & 0xff;
1101
1102     case NFI_NXM_OF_ICMP_CODE:
1103         return ntohs(flow->tp_dst) & 0xff;
1104
1105     case NFI_NXM_NX_TUN_ID:
1106         return ntohll(flow->tun_id);
1107
1108 #define NXM_READ_REGISTER(IDX)                  \
1109     case NFI_NXM_NX_REG##IDX:                   \
1110         return flow->regs[IDX];                 \
1111     case NFI_NXM_NX_REG##IDX##_W:               \
1112         NOT_REACHED();
1113
1114     NXM_READ_REGISTER(0);
1115 #if FLOW_N_REGS >= 2
1116     NXM_READ_REGISTER(1);
1117 #endif
1118 #if FLOW_N_REGS >= 3
1119     NXM_READ_REGISTER(2);
1120 #endif
1121 #if FLOW_N_REGS >= 4
1122     NXM_READ_REGISTER(3);
1123 #endif
1124 #if FLOW_N_REGS > 4
1125 #error
1126 #endif
1127
1128     case NFI_NXM_OF_ETH_DST_W:
1129     case NFI_NXM_OF_VLAN_TCI_W:
1130     case NFI_NXM_OF_IP_SRC_W:
1131     case NFI_NXM_OF_IP_DST_W:
1132     case NFI_NXM_OF_ARP_SPA_W:
1133     case NFI_NXM_OF_ARP_TPA_W:
1134     case N_NXM_FIELDS:
1135         NOT_REACHED();
1136     }
1137
1138     NOT_REACHED();
1139 }
1140
1141 void
1142 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1143                      struct flow *flow)
1144 {
1145     /* Preparation. */
1146     int n_bits = ntohs(action->n_bits);
1147     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1148
1149     /* Get the interesting bits of the source field. */
1150     const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
1151     int src_ofs = ntohs(action->src_ofs);
1152     uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
1153
1154     /* Get the remaining bits of the destination field. */
1155     const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1156     int dst_ofs = ntohs(action->dst_ofs);
1157     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1158
1159     /* Get the final value. */
1160     uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
1161
1162     /* Store the result. */
1163     if (NXM_IS_NX_REG(dst->header)) {
1164         flow->regs[NXM_NX_REG_IDX(dst->header)] = new_data;
1165     } else if (dst->header == NXM_OF_VLAN_TCI) {
1166         flow->vlan_tci = htons(new_data);
1167     } else if (dst->header == NXM_NX_TUN_ID) {
1168         flow->tun_id = htonll(new_data);
1169     } else {
1170         NOT_REACHED();
1171     }
1172 }
1173
1174 void
1175 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1176                      struct flow *flow)
1177 {
1178     /* Preparation. */
1179     int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1180     uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
1181     uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(action->dst))];
1182
1183     /* Get source data. */
1184     uint32_t src_data = ntohll(action->value);
1185
1186     /* Get remaining bits of the destination field. */
1187     int dst_ofs = nxm_decode_ofs(action->ofs_nbits);
1188     uint32_t dst_data = *reg & ~(mask << dst_ofs);
1189
1190     *reg = dst_data | (src_data << dst_ofs);
1191 }