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