nx-match: Allow NXM_NX_TUN_ID and NXM_OF_VLAN_TCI on NXAST_REG_LOAD.
[openvswitch] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010, 2011 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, WRITABLE) \
50         NFI_NXM_##HEADER,
51 #include "nx-match.def"
52     N_NXM_FIELDS
53 };
54
55 struct nxm_field {
56     struct hmap_node hmap_node;
57     enum nxm_field_index index; /* NFI_* value. */
58     uint32_t header;            /* NXM_* value. */
59     flow_wildcards_t wildcard;  /* FWW_* bit, if exactly one. */
60     ovs_be16 dl_type;           /* dl_type prerequisite, if nonzero. */
61     uint8_t nw_proto;           /* nw_proto prerequisite, if nonzero. */
62     const char *name;           /* "NXM_*" string. */
63     bool writable;              /* Writable with NXAST_REG_{MOVE,LOAD}? */
64 };
65
66 /* All the known fields. */
67 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
68 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO, WRITABLE)     \
69     { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
70       CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER, WRITABLE },
71 #include "nx-match.def"
72 };
73
74 /* Hash table of 'nxm_fields'. */
75 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
76
77 /* Possible masks for NXM_OF_ETH_DST_W. */
78 static const uint8_t eth_all_0s[ETH_ADDR_LEN]
79     = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
80 static const uint8_t eth_all_1s[ETH_ADDR_LEN]
81     = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
82 static const uint8_t eth_mcast_1[ETH_ADDR_LEN]
83     = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
84 static const uint8_t eth_mcast_0[ETH_ADDR_LEN]
85     = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
86
87 static void
88 nxm_init(void)
89 {
90     if (hmap_is_empty(&all_nxm_fields)) {
91         int i;
92
93         for (i = 0; i < N_NXM_FIELDS; i++) {
94             struct nxm_field *f = &nxm_fields[i];
95             hmap_insert(&all_nxm_fields, &f->hmap_node,
96                         hash_int(f->header, 0));
97         }
98
99         /* Verify that the header values are unique (duplicate "case" values
100          * cause a compile error). */
101         switch (0) {
102 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO, WRITABLE)  \
103         case NXM_##HEADER: break;
104 #include "nx-match.def"
105         }
106     }
107 }
108
109 static const struct nxm_field *
110 nxm_field_lookup(uint32_t header)
111 {
112     struct nxm_field *f;
113
114     nxm_init();
115
116     HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
117                              &all_nxm_fields) {
118         if (f->header == header) {
119             return f;
120         }
121     }
122
123     return NULL;
124 }
125
126 /* Returns the width of the data for a field with the given 'header', in
127  * bytes. */
128 int
129 nxm_field_bytes(uint32_t header)
130 {
131     unsigned int length = NXM_LENGTH(header);
132     return NXM_HASMASK(header) ? length / 2 : length;
133 }
134
135 /* Returns the width of the data for a field with the given 'header', in
136  * bits. */
137 int
138 nxm_field_bits(uint32_t header)
139 {
140     return nxm_field_bytes(header) * 8;
141 }
142 \f
143 /* nx_pull_match() and helpers. */
144
145 static int
146 parse_nx_reg(const struct nxm_field *f,
147              struct flow *flow, struct flow_wildcards *wc,
148              const void *value, const void *maskp)
149 {
150     int idx = NXM_NX_REG_IDX(f->header);
151     if (wc->reg_masks[idx]) {
152         return NXM_DUP_TYPE;
153     } else {
154         flow_wildcards_set_reg_mask(wc, idx,
155                                     (NXM_HASMASK(f->header)
156                                      ? ntohl(get_unaligned_be32(maskp))
157                                      : UINT32_MAX));
158         flow->regs[idx] = ntohl(get_unaligned_be32(value));
159         flow->regs[idx] &= wc->reg_masks[idx];
160         return 0;
161     }
162 }
163
164 static int
165 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
166                 const void *value, const void *mask)
167 {
168     struct flow_wildcards *wc = &rule->wc;
169     struct flow *flow = &rule->flow;
170
171     switch (f->index) {
172         /* Metadata. */
173     case NFI_NXM_OF_IN_PORT:
174         flow->in_port = ntohs(get_unaligned_be16(value));
175         if (flow->in_port == OFPP_LOCAL) {
176             flow->in_port = ODPP_LOCAL;
177         }
178         return 0;
179
180         /* Ethernet header. */
181     case NFI_NXM_OF_ETH_DST:
182         if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
183             != (FWW_DL_DST | FWW_ETH_MCAST)) {
184             return NXM_DUP_TYPE;
185         } else {
186             wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
187             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
188             return 0;
189         }
190     case NFI_NXM_OF_ETH_DST_W:
191         if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
192             != (FWW_DL_DST | FWW_ETH_MCAST)) {
193             return NXM_DUP_TYPE;
194         } else if (eth_addr_equals(mask, eth_mcast_1)) {
195             wc->wildcards &= ~FWW_ETH_MCAST;
196             flow->dl_dst[0] = *(uint8_t *) value & 0x01;
197         } else if (eth_addr_equals(mask, eth_mcast_0)) {
198             wc->wildcards &= ~FWW_DL_DST;
199             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
200             flow->dl_dst[0] &= 0xfe;
201         } else if (eth_addr_equals(mask, eth_all_0s)) {
202             return 0;
203         } else if (eth_addr_equals(mask, eth_all_1s)) {
204             wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
205             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
206             return 0;
207         } else {
208             return NXM_BAD_MASK;
209         }
210     case NFI_NXM_OF_ETH_SRC:
211         memcpy(flow->dl_src, value, ETH_ADDR_LEN);
212         return 0;
213     case NFI_NXM_OF_ETH_TYPE:
214         flow->dl_type = get_unaligned_be16(value);
215         return 0;
216
217         /* 802.1Q header. */
218     case NFI_NXM_OF_VLAN_TCI:
219         if (wc->vlan_tci_mask) {
220             return NXM_DUP_TYPE;
221         } else {
222             cls_rule_set_dl_tci(rule, get_unaligned_be16(value));
223             return 0;
224         }
225     case NFI_NXM_OF_VLAN_TCI_W:
226         if (wc->vlan_tci_mask) {
227             return NXM_DUP_TYPE;
228         } else {
229             cls_rule_set_dl_tci_masked(rule, get_unaligned_be16(value),
230                                        get_unaligned_be16(mask));
231             return 0;
232         }
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_be32(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_be32(value);
261             ovs_be32 netmask = get_unaligned_be32(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_be32(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_be32(value);
281             ovs_be32 netmask = get_unaligned_be32(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_be16(value);
291         return 0;
292     case NFI_NXM_OF_TCP_DST:
293         flow->tp_dst = get_unaligned_be16(value);
294         return 0;
295
296         /* UDP header. */
297     case NFI_NXM_OF_UDP_SRC:
298         flow->tp_src = get_unaligned_be16(value);
299         return 0;
300     case NFI_NXM_OF_UDP_DST:
301         flow->tp_dst = get_unaligned_be16(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_be16(value)) > 255) {
315             return NXM_BAD_VALUE;
316         } else {
317             flow->nw_proto = ntohs(get_unaligned_be16(value));
318             return 0;
319         }
320
321         /* Tunnel ID. */
322     case NFI_NXM_NX_TUN_ID:
323         flow->tun_id = get_unaligned_be64(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 %u, 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_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
482 {
483     switch (mask) {
484     case 0:
485         break;
486
487     case CONSTANT_HTONS(UINT16_MAX):
488         nxm_put_16(b, header, value);
489         break;
490
491     default:
492         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
493         break;
494     }
495 }
496
497 static void
498 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
499 {
500     nxm_put_header(b, header);
501     ofpbuf_put(b, &value, sizeof value);
502 }
503
504 static void
505 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
506 {
507     nxm_put_header(b, header);
508     ofpbuf_put(b, &value, sizeof value);
509     ofpbuf_put(b, &mask, sizeof mask);
510 }
511
512 static void
513 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
514 {
515     switch (mask) {
516     case 0:
517         break;
518
519     case CONSTANT_HTONL(UINT32_MAX):
520         nxm_put_32(b, header, value);
521         break;
522
523     default:
524         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
525         break;
526     }
527 }
528
529 static void
530 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
531 {
532     nxm_put_header(b, header);
533     ofpbuf_put(b, &value, sizeof value);
534 }
535
536 static void
537 nxm_put_eth(struct ofpbuf *b, uint32_t header,
538             const uint8_t value[ETH_ADDR_LEN])
539 {
540     nxm_put_header(b, header);
541     ofpbuf_put(b, value, ETH_ADDR_LEN);
542 }
543
544 static void
545 nxm_put_eth_dst(struct ofpbuf *b,
546                 uint32_t wc, const uint8_t value[ETH_ADDR_LEN])
547 {
548     switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
549     case FWW_DL_DST | FWW_ETH_MCAST:
550         break;
551     case FWW_DL_DST:
552         nxm_put_header(b, NXM_OF_ETH_DST_W);
553         ofpbuf_put(b, value, ETH_ADDR_LEN);
554         ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
555         break;
556     case FWW_ETH_MCAST:
557         nxm_put_header(b, NXM_OF_ETH_DST_W);
558         ofpbuf_put(b, value, ETH_ADDR_LEN);
559         ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
560         break;
561     case 0:
562         nxm_put_eth(b, NXM_OF_ETH_DST, value);
563         break;
564     }
565 }
566
567 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
568  * 'cr->priority', because priority is not part of nx_match), plus enough
569  * zero bytes to pad the nx_match out to a multiple of 8.
570  *
571  * This function can cause 'b''s data to be reallocated.
572  *
573  * Returns the number of bytes appended to 'b', excluding padding.
574  *
575  * If 'cr' is a catch-all rule that matches every packet, then this function
576  * appends nothing to 'b' and returns 0. */
577 int
578 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
579 {
580     const flow_wildcards_t wc = cr->wc.wildcards;
581     const struct flow *flow = &cr->flow;
582     const size_t start_len = b->size;
583     int match_len;
584     int i;
585
586     /* Metadata. */
587     if (!(wc & FWW_IN_PORT)) {
588         uint16_t in_port = flow->in_port;
589         if (in_port == ODPP_LOCAL) {
590             in_port = OFPP_LOCAL;
591         }
592         nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
593     }
594
595     /* Ethernet. */
596     nxm_put_eth_dst(b, wc, flow->dl_dst);
597     if (!(wc & FWW_DL_SRC)) {
598         nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
599     }
600     if (!(wc & FWW_DL_TYPE)) {
601         nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
602     }
603
604     /* 802.1Q. */
605     nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
606
607     /* L3. */
608     if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
609         /* IP. */
610         if (!(wc & FWW_NW_TOS)) {
611             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
612         }
613         nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
614         nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
615
616         if (!(wc & FWW_NW_PROTO)) {
617             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
618             switch (flow->nw_proto) {
619                 /* TCP. */
620             case IP_TYPE_TCP:
621                 if (!(wc & FWW_TP_SRC)) {
622                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
623                 }
624                 if (!(wc & FWW_TP_DST)) {
625                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
626                 }
627                 break;
628
629                 /* UDP. */
630             case IP_TYPE_UDP:
631                 if (!(wc & FWW_TP_SRC)) {
632                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
633                 }
634                 if (!(wc & FWW_TP_DST)) {
635                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
636                 }
637                 break;
638
639                 /* ICMP. */
640             case IP_TYPE_ICMP:
641                 if (!(wc & FWW_TP_SRC)) {
642                     nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
643                 }
644                 if (!(wc & FWW_TP_DST)) {
645                     nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
646                 }
647                 break;
648             }
649         }
650     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
651         /* ARP. */
652         if (!(wc & FWW_NW_PROTO)) {
653             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
654         }
655         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
656         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
657     }
658
659     /* Tunnel ID. */
660     if (!(wc & FWW_TUN_ID)) {
661         nxm_put_64(b, NXM_NX_TUN_ID, flow->tun_id);
662     }
663
664     /* Registers. */
665     for (i = 0; i < FLOW_N_REGS; i++) {
666         nxm_put_32m(b, NXM_NX_REG(i),
667                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
668     }
669
670     match_len = b->size - start_len;
671     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
672     return match_len;
673 }
674 \f
675 /* nx_match_to_string() and helpers. */
676
677 static void format_nxm_field_name(struct ds *, uint32_t header);
678
679 char *
680 nx_match_to_string(const uint8_t *p, unsigned int match_len)
681 {
682     uint32_t header;
683     struct ds s;
684
685     if (!match_len) {
686         return xstrdup("<any>");
687     }
688
689     ds_init(&s);
690     while ((header = nx_entry_ok(p, match_len)) != 0) {
691         unsigned int length = NXM_LENGTH(header);
692         unsigned int value_len = nxm_field_bytes(header);
693         const uint8_t *value = p + 4;
694         const uint8_t *mask = value + value_len;
695         unsigned int i;
696
697         if (s.length) {
698             ds_put_cstr(&s, ", ");
699         }
700
701         format_nxm_field_name(&s, header);
702         ds_put_char(&s, '(');
703
704         for (i = 0; i < value_len; i++) {
705             ds_put_format(&s, "%02x", value[i]);
706         }
707         if (NXM_HASMASK(header)) {
708             ds_put_char(&s, '/');
709             for (i = 0; i < value_len; i++) {
710                 ds_put_format(&s, "%02x", mask[i]);
711             }
712         }
713         ds_put_char(&s, ')');
714
715         p += 4 + length;
716         match_len -= 4 + length;
717     }
718
719     if (match_len) {
720         if (s.length) {
721             ds_put_cstr(&s, ", ");
722         }
723
724         ds_put_format(&s, "<%u invalid bytes>", match_len);
725     }
726
727     return ds_steal_cstr(&s);
728 }
729
730 static void
731 format_nxm_field_name(struct ds *s, uint32_t header)
732 {
733     const struct nxm_field *f = nxm_field_lookup(header);
734     if (f) {
735         ds_put_cstr(s, f->name);
736     } else {
737         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
738     }
739 }
740
741 static uint32_t
742 parse_nxm_field_name(const char *name, int name_len)
743 {
744     const struct nxm_field *f;
745
746     /* Check whether it's a field name. */
747     for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
748         if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
749             return f->header;
750         }
751     }
752
753     /* Check whether it's a 32-bit field header value as hex.
754      * (This isn't ordinarily useful except for testing error behavior.) */
755     if (name_len == 8) {
756         uint32_t header = hexits_value(name, name_len, NULL);
757         if (header != UINT_MAX) {
758             return header;
759         }
760     }
761
762     return 0;
763 }
764 \f
765 /* nx_match_from_string(). */
766
767 int
768 nx_match_from_string(const char *s, struct ofpbuf *b)
769 {
770     const char *full_s = s;
771     const size_t start_len = b->size;
772     int match_len;
773
774     if (!strcmp(s, "<any>")) {
775         /* Ensure that 'b->data' isn't actually null. */
776         ofpbuf_prealloc_tailroom(b, 1);
777         return 0;
778     }
779
780     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
781         const char *name;
782         uint32_t header;
783         int name_len;
784         size_t n;
785
786         name = s;
787         name_len = strcspn(s, "(");
788         if (s[name_len] != '(') {
789             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
790         }
791
792         header = parse_nxm_field_name(name, name_len);
793         if (!header) {
794             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
795         }
796
797         s += name_len + 1;
798
799         nxm_put_header(b, header);
800         s = ofpbuf_put_hex(b, s, &n);
801         if (n != nxm_field_bytes(header)) {
802             ovs_fatal(0, "%.2s: hex digits expected", s);
803         }
804         if (NXM_HASMASK(header)) {
805             s += strspn(s, " ");
806             if (*s != '/') {
807                 ovs_fatal(0, "%s: missing / in masked field %.*s",
808                           full_s, name_len, name);
809             }
810             s = ofpbuf_put_hex(b, s + 1, &n);
811             if (n != nxm_field_bytes(header)) {
812                 ovs_fatal(0, "%.2s: hex digits expected", s);
813             }
814         }
815
816         s += strspn(s, " ");
817         if (*s != ')') {
818             ovs_fatal(0, "%s: missing ) following field %.*s",
819                       full_s, name_len, name);
820         }
821         s++;
822     }
823
824     match_len = b->size - start_len;
825     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
826     return match_len;
827 }
828 \f
829 const char *
830 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
831 {
832     const char *full_s = s;
833     const char *name;
834     uint32_t header;
835     int start, end;
836     int name_len;
837     int width;
838
839     name = s;
840     name_len = strcspn(s, "[");
841     if (s[name_len] != '[') {
842         ovs_fatal(0, "%s: missing [ looking for field name", full_s);
843     }
844
845     header = parse_nxm_field_name(name, name_len);
846     if (!header) {
847         ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
848     }
849     width = nxm_field_bits(header);
850
851     s += name_len;
852     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
853         /* Nothing to do. */
854     } else if (sscanf(s, "[%d]", &start) == 1) {
855         end = start;
856     } else if (!strncmp(s, "[]", 2)) {
857         start = 0;
858         end = width - 1;
859     } else {
860         ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
861                   "[<start>..<end>]", full_s);
862     }
863     s = strchr(s, ']') + 1;
864
865     if (start > end) {
866         ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
867                   full_s, start, end);
868     } else if (start >= width) {
869         ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
870                   "%d bits wide", full_s, start, width);
871     } else if (end >= width){
872         ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
873                   "%d bits wide", full_s, end, width);
874     }
875
876     *headerp = header;
877     *ofsp = start;
878     *n_bitsp = end - start + 1;
879
880     return s;
881 }
882
883 void
884 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
885 {
886     const char *full_s = s;
887     uint32_t src, dst;
888     int src_ofs, dst_ofs;
889     int src_n_bits, dst_n_bits;
890
891     s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
892     if (strncmp(s, "->", 2)) {
893         ovs_fatal(0, "%s: missing `->' following source", full_s);
894     }
895     s += 2;
896     s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
897     if (*s != '\0') {
898         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
899     }
900
901     if (src_n_bits != dst_n_bits) {
902         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
903                   "%d bits wide", full_s, src_n_bits, dst_n_bits);
904     }
905
906     move->type = htons(OFPAT_VENDOR);
907     move->len = htons(sizeof *move);
908     move->vendor = htonl(NX_VENDOR_ID);
909     move->subtype = htons(NXAST_REG_MOVE);
910     move->n_bits = htons(src_n_bits);
911     move->src_ofs = htons(src_ofs);
912     move->dst_ofs = htons(dst_ofs);
913     move->src = htonl(src);
914     move->dst = htonl(dst);
915 }
916
917 void
918 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
919 {
920     const char *full_s = s;
921     uint32_t dst;
922     int ofs, n_bits;
923     uint64_t value;
924
925     value = strtoull(s, (char **) &s, 0);
926     if (strncmp(s, "->", 2)) {
927         ovs_fatal(0, "%s: missing `->' following value", full_s);
928     }
929     s += 2;
930     s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
931     if (*s != '\0') {
932         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
933     }
934
935     if (n_bits < 64 && (value >> n_bits) != 0) {
936         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
937                   full_s, value, n_bits);
938     }
939
940     load->type = htons(OFPAT_VENDOR);
941     load->len = htons(sizeof *load);
942     load->vendor = htonl(NX_VENDOR_ID);
943     load->subtype = htons(NXAST_REG_LOAD);
944     load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
945     load->dst = htonl(dst);
946     load->value = htonll(value);
947 }
948 \f
949 /* nxm_format_reg_move(), nxm_format_reg_load(). */
950
951 void
952 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
953 {
954     format_nxm_field_name(s, header);
955     if (ofs == 0 && n_bits == nxm_field_bits(header)) {
956         ds_put_cstr(s, "[]");
957     } else if (n_bits == 1) {
958         ds_put_format(s, "[%d]", ofs);
959     } else {
960         ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
961     }
962 }
963
964 void
965 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
966 {
967     int n_bits = ntohs(move->n_bits);
968     int src_ofs = ntohs(move->src_ofs);
969     int dst_ofs = ntohs(move->dst_ofs);
970     uint32_t src = ntohl(move->src);
971     uint32_t dst = ntohl(move->dst);
972
973     ds_put_format(s, "move:");
974     nxm_format_field_bits(s, src, src_ofs, n_bits);
975     ds_put_cstr(s, "->");
976     nxm_format_field_bits(s, dst, dst_ofs, n_bits);
977 }
978
979 void
980 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
981 {
982     int ofs = nxm_decode_ofs(load->ofs_nbits);
983     int n_bits = nxm_decode_n_bits(load->ofs_nbits);
984     uint32_t dst = ntohl(load->dst);
985     uint64_t value = ntohll(load->value);
986
987     ds_put_format(s, "load:%#"PRIx64"->", value);
988     nxm_format_field_bits(s, dst, ofs, n_bits);
989 }
990 \f
991 /* nxm_check_reg_move(), nxm_check_reg_load(). */
992
993 static bool
994 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
995 {
996     return (f && !NXM_HASMASK(f->header)
997             && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
998 }
999
1000 int
1001 nxm_check_reg_move(const struct nx_action_reg_move *action,
1002                    const struct flow *flow)
1003 {
1004     const struct nxm_field *src;
1005     const struct nxm_field *dst;
1006
1007     if (action->n_bits == htons(0)) {
1008         return BAD_ARGUMENT;
1009     }
1010
1011     src = nxm_field_lookup(ntohl(action->src));
1012     if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
1013         return BAD_ARGUMENT;
1014     }
1015
1016     dst = nxm_field_lookup(ntohl(action->dst));
1017     if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
1018         return BAD_ARGUMENT;
1019     }
1020
1021     if (!dst->writable) {
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 (!dst->writable) {
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 static void
1142 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1143                 uint64_t new_value)
1144 {
1145     switch (dst->index) {
1146     case NFI_NXM_OF_VLAN_TCI:
1147         flow->vlan_tci = htons(new_value);
1148         break;
1149
1150     case NFI_NXM_NX_TUN_ID:
1151         flow->tun_id = htonll(new_value);
1152         break;
1153
1154 #define NXM_WRITE_REGISTER(IDX)                 \
1155     case NFI_NXM_NX_REG##IDX:                   \
1156         flow->regs[IDX] = new_value;            \
1157         break;                                  \
1158     case NFI_NXM_NX_REG##IDX##_W:               \
1159         NOT_REACHED();
1160
1161     NXM_WRITE_REGISTER(0);
1162 #if FLOW_N_REGS >= 2
1163     NXM_WRITE_REGISTER(1);
1164 #endif
1165 #if FLOW_N_REGS >= 3
1166     NXM_WRITE_REGISTER(2);
1167 #endif
1168 #if FLOW_N_REGS >= 4
1169     NXM_WRITE_REGISTER(3);
1170 #endif
1171 #if FLOW_N_REGS > 4
1172 #error
1173 #endif
1174
1175     case NFI_NXM_OF_IN_PORT:
1176     case NFI_NXM_OF_ETH_DST:
1177     case NFI_NXM_OF_ETH_SRC:
1178     case NFI_NXM_OF_ETH_TYPE:
1179     case NFI_NXM_OF_IP_TOS:
1180     case NFI_NXM_OF_IP_PROTO:
1181     case NFI_NXM_OF_ARP_OP:
1182     case NFI_NXM_OF_IP_SRC:
1183     case NFI_NXM_OF_ARP_SPA:
1184     case NFI_NXM_OF_IP_DST:
1185     case NFI_NXM_OF_ARP_TPA:
1186     case NFI_NXM_OF_TCP_SRC:
1187     case NFI_NXM_OF_UDP_SRC:
1188     case NFI_NXM_OF_TCP_DST:
1189     case NFI_NXM_OF_UDP_DST:
1190     case NFI_NXM_OF_ICMP_TYPE:
1191     case NFI_NXM_OF_ICMP_CODE:
1192     case NFI_NXM_OF_ETH_DST_W:
1193     case NFI_NXM_OF_VLAN_TCI_W:
1194     case NFI_NXM_OF_IP_SRC_W:
1195     case NFI_NXM_OF_IP_DST_W:
1196     case NFI_NXM_OF_ARP_SPA_W:
1197     case NFI_NXM_OF_ARP_TPA_W:
1198     case N_NXM_FIELDS:
1199         NOT_REACHED();
1200     }
1201 }
1202
1203 void
1204 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1205                      struct flow *flow)
1206 {
1207     /* Preparation. */
1208     int n_bits = ntohs(action->n_bits);
1209     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1210
1211     /* Get the interesting bits of the source field. */
1212     const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
1213     int src_ofs = ntohs(action->src_ofs);
1214     uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
1215
1216     /* Get the remaining bits of the destination field. */
1217     const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1218     int dst_ofs = ntohs(action->dst_ofs);
1219     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1220
1221     /* Get the final value. */
1222     uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
1223
1224     nxm_write_field(dst, flow, new_data);
1225 }
1226
1227 void
1228 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1229                      struct flow *flow)
1230 {
1231     /* Preparation. */
1232     int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1233     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1234
1235     /* Get source data. */
1236     uint64_t src_data = ntohll(action->value);
1237
1238     /* Get remaining bits of the destination field. */
1239     const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
1240     int dst_ofs = nxm_decode_ofs(action->ofs_nbits);
1241     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1242
1243     /* Get the final value. */
1244     uint64_t new_data = dst_data | (src_data << dst_ofs);
1245
1246     nxm_write_field(dst, flow, new_data);
1247 }