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