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