meta-flow: New library for working with fields by id.
[sliver-openvswitch.git] / 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 <netinet/icmp6.h>
22
23 #include "classifier.h"
24 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "ofp-util.h"
27 #include "ofpbuf.h"
28 #include "openflow/nicira-ext.h"
29 #include "packets.h"
30 #include "unaligned.h"
31 #include "vlog.h"
32
33 VLOG_DEFINE_THIS_MODULE(nx_match);
34
35 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
36  * peer and so there's not much point in showing a lot of them. */
37 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
38
39 enum {
40     NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
41     NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
42     NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
43     NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
44     NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
45     NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
46     BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
47 };
48
49 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
50  * zero. */
51 enum nxm_field_index {
52 #define DEFINE_FIELD(HEADER, MFF_ID, WRITABLE)  \
53         NFI_NXM_##HEADER,
54 #include "nx-match.def"
55     N_NXM_FIELDS
56 };
57
58 struct nxm_field {
59     struct hmap_node hmap_node;
60     enum nxm_field_index index;       /* NFI_* value. */
61     uint32_t header;                  /* NXM_* value. */
62     enum mf_field_id mf_id;           /* MFF_* value. */
63     const struct mf_field *mf;
64     const char *name;                 /* "NXM_*" string. */
65     bool writable;                    /* Writable with NXAST_REG_{MOVE,LOAD}? */
66 };
67
68 /* All the known fields. */
69 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
70 #define DEFINE_FIELD(HEADER, MFF_ID, WRITABLE)                            \
71     { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, \
72       MFF_ID, NULL, "NXM_" #HEADER, WRITABLE },
73 #include "nx-match.def"
74 };
75
76 /* Hash table of 'nxm_fields'. */
77 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
78
79 static void
80 nxm_init(void)
81 {
82     if (hmap_is_empty(&all_nxm_fields)) {
83         int i;
84
85         for (i = 0; i < N_NXM_FIELDS; i++) {
86             struct nxm_field *f = &nxm_fields[i];
87             hmap_insert(&all_nxm_fields, &f->hmap_node,
88                         hash_int(f->header, 0));
89             f->mf = mf_from_id(f->mf_id);
90         }
91
92         /* Verify that the header values are unique (duplicate "case" values
93          * cause a compile error). */
94         switch (0) {
95 #define DEFINE_FIELD(HEADER, MFF_ID, WRITABLE)  \
96         case NXM_##HEADER: break;
97 #include "nx-match.def"
98         }
99     }
100 }
101
102 static const struct nxm_field *
103 nxm_field_lookup(uint32_t header)
104 {
105     struct nxm_field *f;
106
107     nxm_init();
108
109     HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
110                              &all_nxm_fields) {
111         if (f->header == header) {
112             return f;
113         }
114     }
115
116     return NULL;
117 }
118
119 /* Returns the width of the data for a field with the given 'header', in
120  * bytes. */
121 int
122 nxm_field_bytes(uint32_t header)
123 {
124     unsigned int length = NXM_LENGTH(header);
125     return NXM_HASMASK(header) ? length / 2 : length;
126 }
127
128 /* Returns the width of the data for a field with the given 'header', in
129  * bits. */
130 int
131 nxm_field_bits(uint32_t header)
132 {
133     return nxm_field_bytes(header) * 8;
134 }
135
136 const struct mf_field *
137 nxm_field_to_mf_field(uint32_t header)
138 {
139     const struct nxm_field *f = nxm_field_lookup(header);
140     return f ? f->mf : NULL;
141 }
142 \f
143 /* nx_pull_match() and helpers. */
144
145 static uint32_t
146 nx_entry_ok(const void *p, unsigned int match_len)
147 {
148     unsigned int payload_len;
149     ovs_be32 header_be;
150     uint32_t header;
151
152     if (match_len < 4) {
153         if (match_len) {
154             VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
155         }
156         return 0;
157     }
158     memcpy(&header_be, p, 4);
159     header = ntohl(header_be);
160
161     payload_len = NXM_LENGTH(header);
162     if (!payload_len) {
163         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
164                     "length 0", header);
165         return 0;
166     }
167     if (match_len < payload_len + 4) {
168         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
169                     "%u bytes left in nx_match", payload_len + 4, match_len);
170         return 0;
171     }
172
173     return header;
174 }
175
176 int
177 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
178               struct cls_rule *rule)
179 {
180     uint32_t header;
181     uint8_t *p;
182
183     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
184     if (!p) {
185         VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
186                     "multiple of 8, is longer than space in message (max "
187                     "length %zu)", match_len, b->size);
188         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
189     }
190
191     cls_rule_init_catchall(rule, priority);
192     while ((header = nx_entry_ok(p, match_len)) != 0) {
193         unsigned length = NXM_LENGTH(header);
194         const struct nxm_field *f;
195         int error;
196
197         f = nxm_field_lookup(header);
198         if (!f) {
199             error = NXM_BAD_TYPE;
200         } else if (!mf_are_prereqs_ok(f->mf, &rule->flow)) {
201             error = NXM_BAD_PREREQ;
202         } else if (!mf_is_all_wild(f->mf, &rule->wc)) {
203             error = NXM_DUP_TYPE;
204         } else {
205             unsigned int width = f->mf->n_bytes;
206             union mf_value value;
207
208             memcpy(&value, p + 4, width);
209             if (!mf_is_value_valid(f->mf, &value)) {
210                 error = NXM_BAD_VALUE;
211             } else if (!NXM_HASMASK(header)) {
212                 error = 0;
213                 mf_set_value(f->mf, &value, rule);
214             } else {
215                 union mf_value mask;
216
217                 memcpy(&mask, p + 4 + width, width);
218                 if (!mf_is_mask_valid(f->mf, &mask)) {
219                     error = NXM_BAD_MASK;
220                 } else {
221                     error = 0;
222                     mf_set(f->mf, &value, &mask, rule);
223                 }
224             }
225         }
226
227         if (error) {
228             VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
229                         "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
230                         "(error %x)",
231                         NXM_VENDOR(header), NXM_FIELD(header),
232                         NXM_HASMASK(header), NXM_TYPE(header),
233                         error);
234             return error;
235         }
236
237         p += 4 + length;
238         match_len -= 4 + length;
239     }
240
241     return match_len ? NXM_INVALID : 0;
242 }
243 \f
244 /* nx_put_match() and helpers.
245  *
246  * 'put' functions whose names end in 'w' add a wildcarded field.
247  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
248  * Other 'put' functions add exact-match fields.
249  */
250
251 static void
252 nxm_put_header(struct ofpbuf *b, uint32_t header)
253 {
254     ovs_be32 n_header = htonl(header);
255     ofpbuf_put(b, &n_header, sizeof n_header);
256 }
257
258 static void
259 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
260 {
261     nxm_put_header(b, header);
262     ofpbuf_put(b, &value, sizeof value);
263 }
264
265 static void
266 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
267 {
268     nxm_put_header(b, header);
269     ofpbuf_put(b, &value, sizeof value);
270 }
271
272 static void
273 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
274 {
275     nxm_put_header(b, header);
276     ofpbuf_put(b, &value, sizeof value);
277     ofpbuf_put(b, &mask, sizeof mask);
278 }
279
280 static void
281 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
282 {
283     switch (mask) {
284     case 0:
285         break;
286
287     case CONSTANT_HTONS(UINT16_MAX):
288         nxm_put_16(b, header, value);
289         break;
290
291     default:
292         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
293         break;
294     }
295 }
296
297 static void
298 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
299 {
300     nxm_put_header(b, header);
301     ofpbuf_put(b, &value, sizeof value);
302 }
303
304 static void
305 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
306 {
307     nxm_put_header(b, header);
308     ofpbuf_put(b, &value, sizeof value);
309     ofpbuf_put(b, &mask, sizeof mask);
310 }
311
312 static void
313 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
314 {
315     switch (mask) {
316     case 0:
317         break;
318
319     case CONSTANT_HTONL(UINT32_MAX):
320         nxm_put_32(b, header, value);
321         break;
322
323     default:
324         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
325         break;
326     }
327 }
328
329 static void
330 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
331 {
332     nxm_put_header(b, header);
333     ofpbuf_put(b, &value, sizeof value);
334 }
335
336 static void
337 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
338 {
339     nxm_put_header(b, header);
340     ofpbuf_put(b, &value, sizeof value);
341     ofpbuf_put(b, &mask, sizeof mask);
342 }
343
344 static void
345 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
346 {
347     switch (mask) {
348     case 0:
349         break;
350
351     case CONSTANT_HTONLL(UINT64_MAX):
352         nxm_put_64(b, header, value);
353         break;
354
355     default:
356         nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
357         break;
358     }
359 }
360
361 static void
362 nxm_put_eth(struct ofpbuf *b, uint32_t header,
363             const uint8_t value[ETH_ADDR_LEN])
364 {
365     nxm_put_header(b, header);
366     ofpbuf_put(b, value, ETH_ADDR_LEN);
367 }
368
369 static void
370 nxm_put_eth_dst(struct ofpbuf *b,
371                 flow_wildcards_t wc, const uint8_t value[ETH_ADDR_LEN])
372 {
373     switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
374     case FWW_DL_DST | FWW_ETH_MCAST:
375         break;
376     default:
377         nxm_put_header(b, NXM_OF_ETH_DST_W);
378         ofpbuf_put(b, value, ETH_ADDR_LEN);
379         ofpbuf_put(b, flow_wildcards_to_dl_dst_mask(wc), ETH_ADDR_LEN);
380         break;
381     case 0:
382         nxm_put_eth(b, NXM_OF_ETH_DST, value);
383         break;
384     }
385 }
386
387 static void
388 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
389              const struct in6_addr *value, const struct in6_addr *mask)
390 {
391     if (ipv6_mask_is_any(mask)) {
392         return;
393     } else if (ipv6_mask_is_exact(mask)) {
394         nxm_put_header(b, header);
395         ofpbuf_put(b, value, sizeof *value);
396     } else {
397         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
398         ofpbuf_put(b, value, sizeof *value);
399         ofpbuf_put(b, mask, sizeof *mask);
400     }
401 }
402
403 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
404  * 'cr->priority', because priority is not part of nx_match), plus enough
405  * zero bytes to pad the nx_match out to a multiple of 8.
406  *
407  * This function can cause 'b''s data to be reallocated.
408  *
409  * Returns the number of bytes appended to 'b', excluding padding.
410  *
411  * If 'cr' is a catch-all rule that matches every packet, then this function
412  * appends nothing to 'b' and returns 0. */
413 int
414 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
415 {
416     const flow_wildcards_t wc = cr->wc.wildcards;
417     const struct flow *flow = &cr->flow;
418     const size_t start_len = b->size;
419     int match_len;
420     int i;
421
422     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
423
424     /* Metadata. */
425     if (!(wc & FWW_IN_PORT)) {
426         uint16_t in_port = flow->in_port;
427         nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
428     }
429
430     /* Ethernet. */
431     nxm_put_eth_dst(b, wc, flow->dl_dst);
432     if (!(wc & FWW_DL_SRC)) {
433         nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
434     }
435     if (!(wc & FWW_DL_TYPE)) {
436         nxm_put_16(b, NXM_OF_ETH_TYPE,
437                    ofputil_dl_type_to_openflow(flow->dl_type));
438     }
439
440     /* 802.1Q. */
441     nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
442
443     /* L3. */
444     if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
445         /* IP. */
446         if (!(wc & FWW_NW_TOS)) {
447             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
448         }
449         nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
450         nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
451
452         if (!(wc & FWW_NW_PROTO)) {
453             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
454             switch (flow->nw_proto) {
455                 /* TCP. */
456             case IPPROTO_TCP:
457                 if (!(wc & FWW_TP_SRC)) {
458                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
459                 }
460                 if (!(wc & FWW_TP_DST)) {
461                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
462                 }
463                 break;
464
465                 /* UDP. */
466             case IPPROTO_UDP:
467                 if (!(wc & FWW_TP_SRC)) {
468                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
469                 }
470                 if (!(wc & FWW_TP_DST)) {
471                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
472                 }
473                 break;
474
475                 /* ICMP. */
476             case IPPROTO_ICMP:
477                 if (!(wc & FWW_TP_SRC)) {
478                     nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
479                 }
480                 if (!(wc & FWW_TP_DST)) {
481                     nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
482                 }
483                 break;
484             }
485         }
486     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
487         /* IPv6. */
488
489         if (!(wc & FWW_NW_TOS)) {
490             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
491         }
492         nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
493                 &cr->wc.ipv6_src_mask);
494         nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
495                 &cr->wc.ipv6_dst_mask);
496
497         if (!(wc & FWW_NW_PROTO)) {
498             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
499             switch (flow->nw_proto) {
500                 /* TCP. */
501             case IPPROTO_TCP:
502                 if (!(wc & FWW_TP_SRC)) {
503                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
504                 }
505                 if (!(wc & FWW_TP_DST)) {
506                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
507                 }
508                 break;
509
510                 /* UDP. */
511             case IPPROTO_UDP:
512                 if (!(wc & FWW_TP_SRC)) {
513                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
514                 }
515                 if (!(wc & FWW_TP_DST)) {
516                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
517                 }
518                 break;
519
520                 /* ICMPv6. */
521             case IPPROTO_ICMPV6:
522                 if (!(wc & FWW_TP_SRC)) {
523                     nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
524
525                     if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
526                         flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
527                         if (!(wc & FWW_ND_TARGET)) {
528                             nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
529                                          &in6addr_exact);
530                         }
531                         if (!(wc & FWW_ARP_SHA)
532                             && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
533                             nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
534                         }
535                         if (!(wc & FWW_ARP_THA)
536                             && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
537                             nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
538                         }
539                     }
540                 }
541                 if (!(wc & FWW_TP_DST)) {
542                     nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
543                 }
544                 break;
545             }
546         }
547     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
548         /* ARP. */
549         if (!(wc & FWW_NW_PROTO)) {
550             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
551         }
552         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
553         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
554         if (!(wc & FWW_ARP_SHA)) {
555             nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
556         }
557         if (!(wc & FWW_ARP_THA)) {
558             nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
559         }
560     }
561
562     /* Tunnel ID. */
563     nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
564
565     /* Registers. */
566     for (i = 0; i < FLOW_N_REGS; i++) {
567         nxm_put_32m(b, NXM_NX_REG(i),
568                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
569     }
570
571     match_len = b->size - start_len;
572     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
573     return match_len;
574 }
575 \f
576 /* nx_match_to_string() and helpers. */
577
578 static void format_nxm_field_name(struct ds *, uint32_t header);
579
580 char *
581 nx_match_to_string(const uint8_t *p, unsigned int match_len)
582 {
583     uint32_t header;
584     struct ds s;
585
586     if (!match_len) {
587         return xstrdup("<any>");
588     }
589
590     ds_init(&s);
591     while ((header = nx_entry_ok(p, match_len)) != 0) {
592         unsigned int length = NXM_LENGTH(header);
593         unsigned int value_len = nxm_field_bytes(header);
594         const uint8_t *value = p + 4;
595         const uint8_t *mask = value + value_len;
596         unsigned int i;
597
598         if (s.length) {
599             ds_put_cstr(&s, ", ");
600         }
601
602         format_nxm_field_name(&s, header);
603         ds_put_char(&s, '(');
604
605         for (i = 0; i < value_len; i++) {
606             ds_put_format(&s, "%02x", value[i]);
607         }
608         if (NXM_HASMASK(header)) {
609             ds_put_char(&s, '/');
610             for (i = 0; i < value_len; i++) {
611                 ds_put_format(&s, "%02x", mask[i]);
612             }
613         }
614         ds_put_char(&s, ')');
615
616         p += 4 + length;
617         match_len -= 4 + length;
618     }
619
620     if (match_len) {
621         if (s.length) {
622             ds_put_cstr(&s, ", ");
623         }
624
625         ds_put_format(&s, "<%u invalid bytes>", match_len);
626     }
627
628     return ds_steal_cstr(&s);
629 }
630
631 static void
632 format_nxm_field_name(struct ds *s, uint32_t header)
633 {
634     const struct nxm_field *f = nxm_field_lookup(header);
635     if (f) {
636         ds_put_cstr(s, f->name);
637     } else {
638         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
639     }
640 }
641
642 static uint32_t
643 parse_nxm_field_name(const char *name, int name_len)
644 {
645     const struct nxm_field *f;
646
647     /* Check whether it's a field name. */
648     for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
649         if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
650             return f->header;
651         }
652     }
653
654     /* Check whether it's a 32-bit field header value as hex.
655      * (This isn't ordinarily useful except for testing error behavior.) */
656     if (name_len == 8) {
657         uint32_t header = hexits_value(name, name_len, NULL);
658         if (header != UINT_MAX) {
659             return header;
660         }
661     }
662
663     return 0;
664 }
665 \f
666 /* nx_match_from_string(). */
667
668 int
669 nx_match_from_string(const char *s, struct ofpbuf *b)
670 {
671     const char *full_s = s;
672     const size_t start_len = b->size;
673     int match_len;
674
675     if (!strcmp(s, "<any>")) {
676         /* Ensure that 'b->data' isn't actually null. */
677         ofpbuf_prealloc_tailroom(b, 1);
678         return 0;
679     }
680
681     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
682         const char *name;
683         uint32_t header;
684         int name_len;
685         size_t n;
686
687         name = s;
688         name_len = strcspn(s, "(");
689         if (s[name_len] != '(') {
690             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
691         }
692
693         header = parse_nxm_field_name(name, name_len);
694         if (!header) {
695             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
696         }
697
698         s += name_len + 1;
699
700         nxm_put_header(b, header);
701         s = ofpbuf_put_hex(b, s, &n);
702         if (n != nxm_field_bytes(header)) {
703             ovs_fatal(0, "%.2s: hex digits expected", s);
704         }
705         if (NXM_HASMASK(header)) {
706             s += strspn(s, " ");
707             if (*s != '/') {
708                 ovs_fatal(0, "%s: missing / in masked field %.*s",
709                           full_s, name_len, name);
710             }
711             s = ofpbuf_put_hex(b, s + 1, &n);
712             if (n != nxm_field_bytes(header)) {
713                 ovs_fatal(0, "%.2s: hex digits expected", s);
714             }
715         }
716
717         s += strspn(s, " ");
718         if (*s != ')') {
719             ovs_fatal(0, "%s: missing ) following field %.*s",
720                       full_s, name_len, name);
721         }
722         s++;
723     }
724
725     match_len = b->size - start_len;
726     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
727     return match_len;
728 }
729 \f
730 const char *
731 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
732 {
733     const char *full_s = s;
734     const char *name;
735     uint32_t header;
736     int start, end;
737     int name_len;
738     int width;
739
740     name = s;
741     name_len = strcspn(s, "[");
742     if (s[name_len] != '[') {
743         ovs_fatal(0, "%s: missing [ looking for field name", full_s);
744     }
745
746     header = parse_nxm_field_name(name, name_len);
747     if (!header) {
748         ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
749     }
750     width = nxm_field_bits(header);
751
752     s += name_len;
753     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
754         /* Nothing to do. */
755     } else if (sscanf(s, "[%d]", &start) == 1) {
756         end = start;
757     } else if (!strncmp(s, "[]", 2)) {
758         start = 0;
759         end = width - 1;
760     } else {
761         ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
762                   "[<start>..<end>]", full_s);
763     }
764     s = strchr(s, ']') + 1;
765
766     if (start > end) {
767         ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
768                   full_s, start, end);
769     } else if (start >= width) {
770         ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
771                   "%d bits wide", full_s, start, width);
772     } else if (end >= width){
773         ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
774                   "%d bits wide", full_s, end, width);
775     }
776
777     *headerp = header;
778     *ofsp = start;
779     *n_bitsp = end - start + 1;
780
781     return s;
782 }
783
784 void
785 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
786 {
787     const char *full_s = s;
788     uint32_t src, dst;
789     int src_ofs, dst_ofs;
790     int src_n_bits, dst_n_bits;
791
792     s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
793     if (strncmp(s, "->", 2)) {
794         ovs_fatal(0, "%s: missing `->' following source", full_s);
795     }
796     s += 2;
797     s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
798     if (*s != '\0') {
799         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
800     }
801
802     if (src_n_bits != dst_n_bits) {
803         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
804                   "%d bits wide", full_s, src_n_bits, dst_n_bits);
805     }
806
807     ofputil_init_NXAST_REG_MOVE(move);
808     move->n_bits = htons(src_n_bits);
809     move->src_ofs = htons(src_ofs);
810     move->dst_ofs = htons(dst_ofs);
811     move->src = htonl(src);
812     move->dst = htonl(dst);
813 }
814
815 void
816 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
817 {
818     const char *full_s = s;
819     uint32_t dst;
820     int ofs, n_bits;
821     uint64_t value;
822
823     value = strtoull(s, (char **) &s, 0);
824     if (strncmp(s, "->", 2)) {
825         ovs_fatal(0, "%s: missing `->' following value", full_s);
826     }
827     s += 2;
828     s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
829     if (*s != '\0') {
830         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
831     }
832
833     if (n_bits < 64 && (value >> n_bits) != 0) {
834         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
835                   full_s, value, n_bits);
836     }
837
838     ofputil_init_NXAST_REG_LOAD(load);
839     load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
840     load->dst = htonl(dst);
841     load->value = htonll(value);
842 }
843 \f
844 /* nxm_format_reg_move(), nxm_format_reg_load(). */
845
846 void
847 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
848 {
849     format_nxm_field_name(s, header);
850     if (ofs == 0 && n_bits == nxm_field_bits(header)) {
851         ds_put_cstr(s, "[]");
852     } else if (n_bits == 1) {
853         ds_put_format(s, "[%d]", ofs);
854     } else {
855         ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
856     }
857 }
858
859 void
860 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
861 {
862     int n_bits = ntohs(move->n_bits);
863     int src_ofs = ntohs(move->src_ofs);
864     int dst_ofs = ntohs(move->dst_ofs);
865     uint32_t src = ntohl(move->src);
866     uint32_t dst = ntohl(move->dst);
867
868     ds_put_format(s, "move:");
869     nxm_format_field_bits(s, src, src_ofs, n_bits);
870     ds_put_cstr(s, "->");
871     nxm_format_field_bits(s, dst, dst_ofs, n_bits);
872 }
873
874 void
875 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
876 {
877     int ofs = nxm_decode_ofs(load->ofs_nbits);
878     int n_bits = nxm_decode_n_bits(load->ofs_nbits);
879     uint32_t dst = ntohl(load->dst);
880     uint64_t value = ntohll(load->value);
881
882     ds_put_format(s, "load:%#"PRIx64"->", value);
883     nxm_format_field_bits(s, dst, ofs, n_bits);
884 }
885 \f
886 /* nxm_check_reg_move(), nxm_check_reg_load(). */
887
888 static bool
889 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
890 {
891     return (f
892             && !NXM_HASMASK(f->header)
893             && mf_are_prereqs_ok(f->mf, flow)
894             && size <= nxm_field_bits(f->header));
895 }
896
897 int
898 nxm_check_reg_move(const struct nx_action_reg_move *action,
899                    const struct flow *flow)
900 {
901     int src_ofs, dst_ofs, n_bits;
902     int error;
903
904     n_bits = ntohs(action->n_bits);
905     src_ofs = ntohs(action->src_ofs);
906     dst_ofs = ntohs(action->dst_ofs);
907
908     error = nxm_src_check(action->src, src_ofs, n_bits, flow);
909     if (error) {
910         return error;
911     }
912
913     return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
914 }
915
916 /* Given a flow, checks that the source field represented by 'src_header'
917  * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
918 int
919 nxm_src_check(ovs_be32 src_header, unsigned int ofs, unsigned int n_bits,
920               const struct flow *flow)
921 {
922     const struct nxm_field *src = nxm_field_lookup(ntohl(src_header));
923
924     if (!n_bits) {
925         VLOG_WARN_RL(&rl, "zero bit source field");
926     } else if (!field_ok(src, flow, ofs + n_bits)) {
927         VLOG_WARN_RL(&rl, "invalid source field");
928     } else {
929         return 0;
930     }
931
932     return BAD_ARGUMENT;
933 }
934
935 /* Given a flow, checks that the destination field represented by 'dst_header'
936  * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
937 int
938 nxm_dst_check(ovs_be32 dst_header, unsigned int ofs, unsigned int n_bits,
939               const struct flow *flow)
940 {
941     const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
942
943     if (!n_bits) {
944         VLOG_WARN_RL(&rl, "zero bit destination field");
945     } else if (!field_ok(dst, flow, ofs + n_bits)) {
946         VLOG_WARN_RL(&rl, "invalid destination field");
947     } else if (!dst->writable) {
948         VLOG_WARN_RL(&rl, "destination field is not writable");
949     } else {
950         return 0;
951     }
952
953     return BAD_ARGUMENT;
954 }
955
956 int
957 nxm_check_reg_load(const struct nx_action_reg_load *action,
958                    const struct flow *flow)
959 {
960     unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
961     unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
962     int error;
963
964     error = nxm_dst_check(action->dst, ofs, n_bits, flow);
965     if (error) {
966         return error;
967     }
968
969     /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
970      * action->value. */
971     if (n_bits < 64 && ntohll(action->value) >> n_bits) {
972         return BAD_ARGUMENT;
973     }
974
975     return 0;
976 }
977 \f
978 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
979
980 static uint64_t
981 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
982 {
983     switch (src->index) {
984     case NFI_NXM_OF_IN_PORT:
985         return flow->in_port;
986
987     case NFI_NXM_OF_ETH_DST:
988         return eth_addr_to_uint64(flow->dl_dst);
989
990     case NFI_NXM_OF_ETH_SRC:
991         return eth_addr_to_uint64(flow->dl_src);
992
993     case NFI_NXM_OF_ETH_TYPE:
994         return ntohs(ofputil_dl_type_to_openflow(flow->dl_type));
995
996     case NFI_NXM_OF_VLAN_TCI:
997         return ntohs(flow->vlan_tci);
998
999     case NFI_NXM_OF_IP_TOS:
1000         return flow->nw_tos;
1001
1002     case NFI_NXM_OF_IP_PROTO:
1003     case NFI_NXM_OF_ARP_OP:
1004         return flow->nw_proto;
1005
1006     case NFI_NXM_OF_IP_SRC:
1007     case NFI_NXM_OF_ARP_SPA:
1008         return ntohl(flow->nw_src);
1009
1010     case NFI_NXM_OF_IP_DST:
1011     case NFI_NXM_OF_ARP_TPA:
1012         return ntohl(flow->nw_dst);
1013
1014     case NFI_NXM_OF_TCP_SRC:
1015     case NFI_NXM_OF_UDP_SRC:
1016         return ntohs(flow->tp_src);
1017
1018     case NFI_NXM_OF_TCP_DST:
1019     case NFI_NXM_OF_UDP_DST:
1020         return ntohs(flow->tp_dst);
1021
1022     case NFI_NXM_OF_ICMP_TYPE:
1023     case NFI_NXM_NX_ICMPV6_TYPE:
1024         return ntohs(flow->tp_src) & 0xff;
1025
1026     case NFI_NXM_OF_ICMP_CODE:
1027     case NFI_NXM_NX_ICMPV6_CODE:
1028         return ntohs(flow->tp_dst) & 0xff;
1029
1030     case NFI_NXM_NX_TUN_ID:
1031         return ntohll(flow->tun_id);
1032
1033 #define NXM_READ_REGISTER(IDX)                  \
1034     case NFI_NXM_NX_REG##IDX:                   \
1035         return flow->regs[IDX];                 \
1036     case NFI_NXM_NX_REG##IDX##_W:               \
1037         NOT_REACHED();
1038
1039     NXM_READ_REGISTER(0);
1040 #if FLOW_N_REGS >= 2
1041     NXM_READ_REGISTER(1);
1042 #endif
1043 #if FLOW_N_REGS >= 3
1044     NXM_READ_REGISTER(2);
1045 #endif
1046 #if FLOW_N_REGS >= 4
1047     NXM_READ_REGISTER(3);
1048 #endif
1049 #if FLOW_N_REGS > 4
1050 #error
1051 #endif
1052
1053     case NFI_NXM_NX_ARP_SHA:
1054     case NFI_NXM_NX_ND_SLL:
1055         return eth_addr_to_uint64(flow->arp_sha);
1056
1057     case NFI_NXM_NX_ARP_THA:
1058     case NFI_NXM_NX_ND_TLL:
1059         return eth_addr_to_uint64(flow->arp_tha);
1060
1061     case NFI_NXM_NX_TUN_ID_W:
1062     case NFI_NXM_OF_ETH_DST_W:
1063     case NFI_NXM_OF_VLAN_TCI_W:
1064     case NFI_NXM_OF_IP_SRC_W:
1065     case NFI_NXM_OF_IP_DST_W:
1066     case NFI_NXM_OF_ARP_SPA_W:
1067     case NFI_NXM_OF_ARP_TPA_W:
1068     case NFI_NXM_NX_IPV6_SRC:
1069     case NFI_NXM_NX_IPV6_SRC_W:
1070     case NFI_NXM_NX_IPV6_DST:
1071     case NFI_NXM_NX_IPV6_DST_W:
1072     case NFI_NXM_NX_ND_TARGET:
1073     case N_NXM_FIELDS:
1074         NOT_REACHED();
1075     }
1076
1077     NOT_REACHED();
1078 }
1079
1080 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1081  * in 'flow'. */
1082 uint64_t
1083 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1084                     const struct flow *flow)
1085 {
1086     int n_bits = nxm_decode_n_bits(ofs_nbits);
1087     int ofs = nxm_decode_ofs(ofs_nbits);
1088     uint64_t mask, data;
1089
1090     mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1091     data = nxm_read_field(nxm_field_lookup(ntohl(header)), flow);
1092     data = (data >> ofs) & mask;
1093
1094     return data;
1095 }
1096
1097 static void
1098 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1099                 uint64_t new_value)
1100 {
1101     switch (dst->index) {
1102     case NFI_NXM_OF_ETH_DST:
1103         eth_addr_from_uint64(new_value, flow->dl_dst);
1104         break;
1105
1106     case NFI_NXM_OF_ETH_SRC:
1107         eth_addr_from_uint64(new_value, flow->dl_src);
1108         break;
1109
1110     case NFI_NXM_OF_VLAN_TCI:
1111         flow->vlan_tci = htons(new_value);
1112         break;
1113
1114     case NFI_NXM_NX_TUN_ID:
1115         flow->tun_id = htonll(new_value);
1116         break;
1117
1118 #define NXM_WRITE_REGISTER(IDX)                 \
1119     case NFI_NXM_NX_REG##IDX:                   \
1120         flow->regs[IDX] = new_value;            \
1121         break;                                  \
1122     case NFI_NXM_NX_REG##IDX##_W:               \
1123         NOT_REACHED();
1124
1125     NXM_WRITE_REGISTER(0);
1126 #if FLOW_N_REGS >= 2
1127     NXM_WRITE_REGISTER(1);
1128 #endif
1129 #if FLOW_N_REGS >= 3
1130     NXM_WRITE_REGISTER(2);
1131 #endif
1132 #if FLOW_N_REGS >= 4
1133     NXM_WRITE_REGISTER(3);
1134 #endif
1135 #if FLOW_N_REGS > 4
1136 #error
1137 #endif
1138
1139     case NFI_NXM_OF_IP_TOS:
1140         flow->nw_tos = new_value & IP_DSCP_MASK;
1141         break;
1142
1143     case NFI_NXM_OF_IP_SRC:
1144         flow->nw_src = htonl(new_value);
1145         break;
1146
1147     case NFI_NXM_OF_IP_DST:
1148         flow->nw_dst = htonl(new_value);
1149         break;
1150
1151     case NFI_NXM_OF_TCP_SRC:
1152     case NFI_NXM_OF_UDP_SRC:
1153         flow->tp_src = htons(new_value);
1154         break;
1155
1156     case NFI_NXM_OF_TCP_DST:
1157     case NFI_NXM_OF_UDP_DST:
1158         flow->tp_dst = htons(new_value);
1159         break;
1160
1161     case NFI_NXM_OF_IN_PORT:
1162     case NFI_NXM_OF_ETH_TYPE:
1163     case NFI_NXM_OF_IP_PROTO:
1164     case NFI_NXM_OF_ARP_OP:
1165     case NFI_NXM_OF_ARP_SPA:
1166     case NFI_NXM_OF_ARP_TPA:
1167     case NFI_NXM_OF_ICMP_TYPE:
1168     case NFI_NXM_OF_ICMP_CODE:
1169     case NFI_NXM_NX_TUN_ID_W:
1170     case NFI_NXM_OF_ETH_DST_W:
1171     case NFI_NXM_OF_VLAN_TCI_W:
1172     case NFI_NXM_OF_IP_SRC_W:
1173     case NFI_NXM_OF_IP_DST_W:
1174     case NFI_NXM_OF_ARP_SPA_W:
1175     case NFI_NXM_OF_ARP_TPA_W:
1176     case NFI_NXM_NX_ARP_SHA:
1177     case NFI_NXM_NX_ARP_THA:
1178     case NFI_NXM_NX_IPV6_SRC:
1179     case NFI_NXM_NX_IPV6_SRC_W:
1180     case NFI_NXM_NX_IPV6_DST:
1181     case NFI_NXM_NX_IPV6_DST_W:
1182     case NFI_NXM_NX_ICMPV6_TYPE:
1183     case NFI_NXM_NX_ICMPV6_CODE:
1184     case NFI_NXM_NX_ND_TARGET:
1185     case NFI_NXM_NX_ND_SLL:
1186     case NFI_NXM_NX_ND_TLL:
1187     case N_NXM_FIELDS:
1188         NOT_REACHED();
1189     }
1190 }
1191
1192 void
1193 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1194                      struct flow *flow)
1195 {
1196     ovs_be16 src_ofs_nbits, dst_ofs_nbits;
1197     uint64_t src_data;
1198     int n_bits;
1199
1200     n_bits = ntohs(action->n_bits);
1201     src_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->src_ofs), n_bits);
1202     dst_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits);
1203
1204     src_data = nxm_read_field_bits(action->src, src_ofs_nbits, flow);
1205     nxm_reg_load(action->dst, dst_ofs_nbits, src_data, flow);
1206 }
1207
1208 void
1209 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1210                      struct flow *flow)
1211 {
1212     nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1213 }
1214
1215 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1216  * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1217 void
1218 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1219              struct flow *flow)
1220 {
1221     int n_bits = nxm_decode_n_bits(ofs_nbits);
1222     int dst_ofs = nxm_decode_ofs(ofs_nbits);
1223     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1224
1225     /* Get remaining bits of the destination field. */
1226     const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
1227     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1228
1229     /* Get the final value. */
1230     uint64_t new_data = dst_data | (src_data << dst_ofs);
1231
1232     nxm_write_field(dst, flow, new_data);
1233 }