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