ofproto: Implement support for registers in extended flow match.
[cascardo/ovs.git] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "nx-match.h"
20
21 #include "classifier.h"
22 #include "dynamic-string.h"
23 #include "ofp-util.h"
24 #include "ofpbuf.h"
25 #include "openflow/nicira-ext.h"
26 #include "packets.h"
27 #include "unaligned.h"
28 #include "vlog.h"
29
30 VLOG_DEFINE_THIS_MODULE(nx_match);
31
32 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
33  * peer and so there's not much point in showing a lot of them. */
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35
36 enum {
37     NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
38     NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
39     NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
40     NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
41     NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
42     NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
43     BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
44 };
45
46 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
47  * zero. */
48 enum nxm_field_index {
49 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) NFI_NXM_##HEADER,
50 #include "nx-match.def"
51     N_NXM_FIELDS
52 };
53
54 struct nxm_field {
55     struct hmap_node hmap_node;
56     enum nxm_field_index index; /* NFI_* value. */
57     uint32_t header;            /* NXM_* value. */
58     uint32_t wildcard;          /* Wildcard bit, if exactly one. */
59     ovs_be16 dl_type;           /* dl_type prerequisite, if nonzero. */
60     uint8_t nw_proto;           /* nw_proto prerequisite, if nonzero. */
61     const char *name;           /* "NXM_*" string. */
62 };
63
64 /* All the known fields. */
65 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
66 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
67     { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
68       CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER },
69 #include "nx-match.def"
70 };
71
72 /* Hash table of 'nxm_fields'. */
73 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
74
75 static void
76 nxm_init(void)
77 {
78     if (hmap_is_empty(&all_nxm_fields)) {
79         int i;
80
81         for (i = 0; i < N_NXM_FIELDS; i++) {
82             struct nxm_field *f = &nxm_fields[i];
83             hmap_insert(&all_nxm_fields, &f->hmap_node,
84                         hash_int(f->header, 0));
85         }
86
87         /* Verify that the header values are unique (duplicate "case" values
88          * cause a compile error). */
89         switch (0) {
90 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
91         case NXM_##HEADER: break;
92 #include "nx-match.def"
93         }
94     }
95 }
96
97 static const struct nxm_field *
98 nxm_field_lookup(uint32_t header)
99 {
100     struct nxm_field *f;
101
102     nxm_init();
103
104     HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
105                              &all_nxm_fields) {
106         if (f->header == header) {
107             return f;
108         }
109     }
110
111     return NULL;
112 }
113
114 /* Returns the width of the data for a field with the given 'header', in
115  * bytes. */
116 static int
117 nxm_field_bytes(uint32_t header)
118 {
119     unsigned int length = NXM_LENGTH(header);
120     return NXM_HASMASK(header) ? length / 2 : length;
121 }
122
123 /* Returns the width of the data for a field with the given 'header', in
124  * bits. */
125 static int
126 nxm_field_bits(uint32_t header)
127 {
128     return nxm_field_bytes(header) * 8;
129 }
130 \f
131 /* nx_pull_match() and helpers. */
132
133 static int
134 parse_tci(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
135 {
136     enum { OFPFW_DL_TCI = OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP };
137     if ((rule->wc.wildcards & OFPFW_DL_TCI) != OFPFW_DL_TCI) {
138         return NXM_DUP_TYPE;
139     } else {
140         return cls_rule_set_dl_tci_masked(rule, tci, mask) ? 0 : NXM_INVALID;
141     }
142 }
143
144 static int
145 parse_nx_reg(const struct nxm_field *f,
146              struct flow *flow, struct flow_wildcards *wc,
147              const void *value, const void *maskp)
148 {
149     int idx = NXM_NX_REG_IDX(f->header);
150     if (wc->reg_masks[idx]) {
151         return NXM_DUP_TYPE;
152     } else {
153         flow_wildcards_set_reg_mask(wc, idx,
154                                     (NXM_HASMASK(f->header)
155                                      ? ntohl(get_unaligned_u32(maskp))
156                                      : UINT32_MAX));
157         flow->regs[idx] = ntohl(get_unaligned_u32(value));
158         flow->regs[idx] &= wc->reg_masks[idx];
159         return 0;
160     }
161 }
162
163 static int
164 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
165                 const void *value, const void *mask)
166 {
167     struct flow_wildcards *wc = &rule->wc;
168     struct flow *flow = &rule->flow;
169
170     switch (f->index) {
171         /* Metadata. */
172     case NFI_NXM_OF_IN_PORT:
173         flow->in_port = ntohs(get_unaligned_u16(value));
174         if (flow->in_port == OFPP_LOCAL) {
175             flow->in_port = ODPP_LOCAL;
176         }
177         return 0;
178
179         /* Ethernet header. */
180     case NFI_NXM_OF_ETH_DST:
181         memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
182         return 0;
183     case NFI_NXM_OF_ETH_SRC:
184         memcpy(flow->dl_src, value, ETH_ADDR_LEN);
185         return 0;
186     case NFI_NXM_OF_ETH_TYPE:
187         flow->dl_type = get_unaligned_u16(value);
188         return 0;
189
190         /* 802.1Q header. */
191     case NFI_NXM_OF_VLAN_TCI:
192         return parse_tci(rule, get_unaligned_u16(value), htons(UINT16_MAX));
193
194     case NFI_NXM_OF_VLAN_TCI_W:
195         return parse_tci(rule, get_unaligned_u16(value),
196                          get_unaligned_u16(mask));
197
198         /* IP header. */
199     case NFI_NXM_OF_IP_TOS:
200         if (*(uint8_t *) value & 0x03) {
201             return NXM_BAD_VALUE;
202         } else {
203             flow->nw_tos = *(uint8_t *) value;
204             return 0;
205         }
206     case NFI_NXM_OF_IP_PROTO:
207         flow->nw_proto = *(uint8_t *) value;
208         return 0;
209
210         /* IP addresses in IP and ARP headers. */
211     case NFI_NXM_OF_IP_SRC:
212     case NFI_NXM_OF_ARP_SPA:
213         if (wc->nw_src_mask) {
214             return NXM_DUP_TYPE;
215         } else {
216             cls_rule_set_nw_src(rule, get_unaligned_u32(value));
217             return 0;
218         }
219     case NFI_NXM_OF_IP_SRC_W:
220     case NFI_NXM_OF_ARP_SPA_W:
221         if (wc->nw_src_mask) {
222             return NXM_DUP_TYPE;
223         } else {
224             ovs_be32 ip = get_unaligned_u32(value);
225             ovs_be32 netmask = get_unaligned_u32(mask);
226             if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
227                 return NXM_BAD_MASK;
228             }
229             return 0;
230         }
231     case NFI_NXM_OF_IP_DST:
232     case NFI_NXM_OF_ARP_TPA:
233         if (wc->nw_dst_mask) {
234             return NXM_DUP_TYPE;
235         } else {
236             cls_rule_set_nw_dst(rule, get_unaligned_u32(value));
237             return 0;
238         }
239     case NFI_NXM_OF_IP_DST_W:
240     case NFI_NXM_OF_ARP_TPA_W:
241         if (wc->nw_dst_mask) {
242             return NXM_DUP_TYPE;
243         } else {
244             ovs_be32 ip = get_unaligned_u32(value);
245             ovs_be32 netmask = get_unaligned_u32(mask);
246             if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
247                 return NXM_BAD_MASK;
248             }
249             return 0;
250         }
251
252         /* TCP header. */
253     case NFI_NXM_OF_TCP_SRC:
254         flow->tp_src = get_unaligned_u16(value);
255         return 0;
256     case NFI_NXM_OF_TCP_DST:
257         flow->tp_dst = get_unaligned_u16(value);
258         return 0;
259
260         /* UDP header. */
261     case NFI_NXM_OF_UDP_SRC:
262         flow->tp_src = get_unaligned_u16(value);
263         return 0;
264     case NFI_NXM_OF_UDP_DST:
265         flow->tp_dst = get_unaligned_u16(value);
266         return 0;
267
268         /* ICMP header. */
269     case NFI_NXM_OF_ICMP_TYPE:
270         flow->tp_src = htons(*(uint8_t *) value);
271         return 0;
272     case NFI_NXM_OF_ICMP_CODE:
273         flow->tp_dst = htons(*(uint8_t *) value);
274         return 0;
275
276         /* ARP header. */
277     case NFI_NXM_OF_ARP_OP:
278         if (ntohs(get_unaligned_u16(value)) > 255) {
279             return NXM_BAD_VALUE;
280         } else {
281             flow->nw_proto = ntohs(get_unaligned_u16(value));
282             return 0;
283         }
284
285         /* Tunnel ID. */
286     case NFI_NXM_NX_TUN_ID:
287         flow->tun_id = htonl(ntohll(get_unaligned_u64(value)));
288         return 0;
289
290         /* Registers. */
291     case NFI_NXM_NX_REG0:
292     case NFI_NXM_NX_REG0_W:
293 #if FLOW_N_REGS >= 2
294     case NFI_NXM_NX_REG1:
295     case NFI_NXM_NX_REG1_W:
296 #endif
297 #if FLOW_N_REGS >= 3
298     case NFI_NXM_NX_REG2:
299     case NFI_NXM_NX_REG2_W:
300 #endif
301 #if FLOW_N_REGS >= 4
302     case NFI_NXM_NX_REG3:
303     case NFI_NXM_NX_REG3_W:
304 #endif
305 #if FLOW_N_REGS > 4
306 #error
307 #endif
308         return parse_nx_reg(f, flow, wc, value, mask);
309
310     case N_NXM_FIELDS:
311         NOT_REACHED();
312     }
313     NOT_REACHED();
314 }
315
316 static bool
317 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
318 {
319     return (!field->dl_type
320             || (field->dl_type == flow->dl_type
321                 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
322 }
323
324 static uint32_t
325 nx_entry_ok(const void *p, unsigned int match_len)
326 {
327     unsigned int payload_len;
328     ovs_be32 header_be;
329     uint32_t header;
330
331     if (match_len < 4) {
332         if (match_len) {
333             VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
334         }
335         return 0;
336     }
337     memcpy(&header_be, p, 4);
338     header = ntohl(header_be);
339
340     payload_len = NXM_LENGTH(header);
341     if (!payload_len) {
342         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
343                     "length 0", header);
344         return 0;
345     }
346     if (match_len < payload_len + 4) {
347         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
348                     "%u bytes left in nx_match", payload_len + 4, match_len);
349         return 0;
350     }
351
352     return header;
353 }
354
355 int
356 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
357               struct cls_rule *rule)
358 {
359     uint32_t header;
360     uint8_t *p;
361
362     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
363     if (!p) {
364         VLOG_DBG_RL(&rl, "nx_match length %zu, rounded up to a "
365                     "multiple of 8, is longer than space in message (max "
366                     "length %zu)", match_len, b->size);
367         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
368     }
369
370     cls_rule_init_catchall(rule, priority);
371     while ((header = nx_entry_ok(p, match_len)) != 0) {
372         unsigned length = NXM_LENGTH(header);
373         const struct nxm_field *f;
374         int error;
375
376         f = nxm_field_lookup(header);
377         if (!f) {
378             error = NXM_BAD_TYPE;
379         } else if (!nxm_prereqs_ok(f, &rule->flow)) {
380             error = NXM_BAD_PREREQ;
381         } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
382             error = NXM_DUP_TYPE;
383         } else {
384             /* 'hasmask' and 'length' are known to be correct at this point
385              * because they are included in 'header' and nxm_field_lookup()
386              * checked them already. */
387             rule->wc.wildcards &= ~f->wildcard;
388             error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
389         }
390         if (error) {
391             VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
392                         "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
393                         "(error %x)",
394                         NXM_VENDOR(header), NXM_FIELD(header),
395                         NXM_HASMASK(header), NXM_TYPE(header),
396                         error);
397             return error;
398         }
399
400
401         p += 4 + length;
402         match_len -= 4 + length;
403     }
404
405     return match_len ? NXM_INVALID : 0;
406 }
407 \f
408 /* nx_put_match() and helpers.
409  *
410  * 'put' functions whose names end in 'w' add a wildcarded field.
411  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
412  * Other 'put' functions add exact-match fields.
413  */
414
415 static void
416 nxm_put_header(struct ofpbuf *b, uint32_t header)
417 {
418     ovs_be32 n_header = htonl(header);
419     ofpbuf_put(b, &n_header, sizeof n_header);
420 }
421
422 static void
423 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
424 {
425     nxm_put_header(b, header);
426     ofpbuf_put(b, &value, sizeof value);
427 }
428
429 static void
430 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
431 {
432     nxm_put_header(b, header);
433     ofpbuf_put(b, &value, sizeof value);
434 }
435
436 static void
437 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
438 {
439     nxm_put_header(b, header);
440     ofpbuf_put(b, &value, sizeof value);
441     ofpbuf_put(b, &mask, sizeof mask);
442 }
443
444 static void
445 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
446 {
447     nxm_put_header(b, header);
448     ofpbuf_put(b, &value, sizeof value);
449 }
450
451 static void
452 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
453 {
454     nxm_put_header(b, header);
455     ofpbuf_put(b, &value, sizeof value);
456     ofpbuf_put(b, &mask, sizeof mask);
457 }
458
459 static void
460 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
461 {
462     switch (mask) {
463     case 0:
464         break;
465
466     case UINT32_MAX:
467         nxm_put_32(b, header, value);
468         break;
469
470     default:
471         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
472         break;
473     }
474 }
475
476 static void
477 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
478 {
479     nxm_put_header(b, header);
480     ofpbuf_put(b, &value, sizeof value);
481 }
482
483
484 static void
485 nxm_put_eth(struct ofpbuf *b, uint32_t header,
486             const uint8_t value[ETH_ADDR_LEN])
487 {
488     nxm_put_header(b, header);
489     ofpbuf_put(b, value, ETH_ADDR_LEN);
490 }
491
492 int
493 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
494 {
495     const uint32_t wc = cr->wc.wildcards;
496     const struct flow *flow = &cr->flow;
497     const size_t start_len = b->size;
498     ovs_be16 vid, pcp;
499     int match_len;
500     int i;
501
502     /* Metadata. */
503     if (!(wc & OFPFW_IN_PORT)) {
504         uint16_t in_port = flow->in_port;
505         if (in_port == ODPP_LOCAL) {
506             in_port = OFPP_LOCAL;
507         }
508         nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
509     }
510
511     /* Ethernet. */
512     if (!(wc & OFPFW_DL_DST)) {
513         nxm_put_eth(b, NXM_OF_ETH_DST, flow->dl_dst);
514     }
515     if (!(wc & OFPFW_DL_SRC)) {
516         nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
517     }
518     if (!(wc & OFPFW_DL_TYPE)) {
519         nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
520     }
521
522     /* 802.1Q. */
523     vid = flow->dl_vlan & htons(VLAN_VID_MASK);
524     pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
525     switch (wc & (OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP)) {
526     case OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP:
527         break;
528     case OFPFW_DL_VLAN:
529         nxm_put_16w(b, NXM_OF_VLAN_TCI_W, pcp | htons(VLAN_CFI),
530                      htons(VLAN_PCP_MASK | VLAN_CFI));
531         break;
532     case OFPFW_DL_VLAN_PCP:
533         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
534             nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
535         } else {
536             nxm_put_16w(b, NXM_OF_VLAN_TCI_W, vid | htons(VLAN_CFI),
537                          htons(VLAN_VID_MASK | VLAN_CFI));
538         }
539         break;
540     case 0:
541         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
542             nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
543         } else {
544             nxm_put_16(b, NXM_OF_VLAN_TCI, vid | pcp | htons(VLAN_CFI));
545         }
546         break;
547     }
548
549     if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
550         /* IP. */
551         if (!(wc & OFPFW_NW_TOS)) {
552             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
553         }
554         nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
555         nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
556
557         if (!(wc & OFPFW_NW_PROTO)) {
558             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
559             switch (flow->nw_proto) {
560                 /* TCP. */
561             case IP_TYPE_TCP:
562                 if (!(wc & OFPFW_TP_SRC)) {
563                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
564                 }
565                 if (!(wc & OFPFW_TP_DST)) {
566                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
567                 }
568                 break;
569
570                 /* UDP. */
571             case IP_TYPE_UDP:
572                 if (!(wc & OFPFW_TP_SRC)) {
573                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
574                 }
575                 if (!(wc & OFPFW_TP_DST)) {
576                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
577                 }
578                 break;
579
580                 /* ICMP. */
581             case IP_TYPE_ICMP:
582                 if (!(wc & OFPFW_TP_SRC)) {
583                     nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
584                 }
585                 if (!(wc & OFPFW_TP_DST)) {
586                     nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
587                 }
588                 break;
589             }
590         }
591     } else if (!(wc & OFPFW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
592         /* ARP. */
593         if (!(wc & OFPFW_NW_PROTO)) {
594             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
595         }
596         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
597         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
598     }
599
600     /* Tunnel ID. */
601     if (!(wc & NXFW_TUN_ID)) {
602         nxm_put_64(b, NXM_NX_TUN_ID, htonll(ntohl(flow->tun_id)));
603     }
604
605     /* Registers. */
606     for (i = 0; i < FLOW_N_REGS; i++) {
607         nxm_put_32m(b, NXM_NX_REG(i),
608                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
609     }
610
611     match_len = b->size - start_len;
612     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
613     return match_len;
614 }
615 \f
616 /* nx_match_to_string() and helpers. */
617
618 char *
619 nx_match_to_string(const uint8_t *p, unsigned int match_len)
620 {
621     uint32_t header;
622     struct ds s;
623
624     if (!match_len) {
625         return xstrdup("<any>");
626     }
627
628     ds_init(&s);
629     while ((header = nx_entry_ok(p, match_len)) != 0) {
630         unsigned int length = NXM_LENGTH(header);
631         unsigned int value_len = nxm_field_bytes(header);
632         const uint8_t *value = p + 4;
633         const uint8_t *mask = value + value_len;
634         const struct nxm_field *f;
635         unsigned int i;
636
637         if (s.length) {
638             ds_put_cstr(&s, ", ");
639         }
640
641         f = nxm_field_lookup(header);
642         if (f) {
643             ds_put_cstr(&s, f->name);
644         } else {
645             ds_put_format(&s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
646         }
647
648         ds_put_char(&s, '(');
649
650         for (i = 0; i < value_len; i++) {
651             ds_put_format(&s, "%02x", value[i]);
652         }
653         if (NXM_HASMASK(header)) {
654             ds_put_char(&s, '/');
655             for (i = 0; i < value_len; i++) {
656                 ds_put_format(&s, "%02x", mask[i]);
657             }
658         }
659         ds_put_char(&s, ')');
660
661         p += 4 + length;
662         match_len -= 4 + length;
663     }
664
665     if (match_len) {
666         if (s.length) {
667             ds_put_cstr(&s, ", ");
668         }
669
670         ds_put_format(&s, "<%u invalid bytes>", match_len);
671     }
672
673     return ds_steal_cstr(&s);
674 }
675
676 static const struct nxm_field *
677 lookup_nxm_field(const char *name, int name_len)
678 {
679     const struct nxm_field *f;
680
681     for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
682         if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
683             return f;
684         }
685     }
686
687     return NULL;
688 }
689
690 static const char *
691 parse_hex_bytes(struct ofpbuf *b, const char *s, unsigned int n)
692 {
693     while (n--) {
694         int low, high;
695         uint8_t byte;
696
697         s += strspn(s, " ");
698         low = hexit_value(*s);
699         high = low < 0 ? low : hexit_value(s[1]);
700         if (low < 0 || high < 0) {
701             ovs_fatal(0, "%.2s: hex digits expected", s);
702         }
703
704         byte = 16 * low + high;
705         ofpbuf_put(b, &byte, 1);
706         s += 2;
707     }
708     return s;
709 }
710 \f
711 /* nx_match_from_string(). */
712
713 int
714 nx_match_from_string(const char *s, struct ofpbuf *b)
715 {
716     const char *full_s = s;
717     const size_t start_len = b->size;
718     int match_len;
719
720     if (!strcmp(s, "<any>")) {
721         /* Ensure that 'b->data' isn't actually null. */
722         ofpbuf_prealloc_tailroom(b, 1);
723         return 0;
724     }
725
726     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
727         const struct nxm_field *f;
728         int name_len;
729
730         name_len = strcspn(s, "(");
731         if (s[name_len] != '(') {
732             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
733         }
734
735         f = lookup_nxm_field(s, name_len);
736         if (!f) {
737             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
738         }
739
740         s += name_len + 1;
741
742         nxm_put_header(b, f->header);
743         s = parse_hex_bytes(b, s, nxm_field_bytes(f->header));
744         if (NXM_HASMASK(f->header)) {
745             s += strspn(s, " ");
746             if (*s != '/') {
747                 ovs_fatal(0, "%s: missing / in masked field %s",
748                           full_s, f->name);
749             }
750             s = parse_hex_bytes(b, s + 1, nxm_field_bytes(f->header));
751         }
752
753         s += strspn(s, " ");
754         if (*s != ')') {
755             ovs_fatal(0, "%s: missing ) following field %s", full_s, f->name);
756         }
757         s++;
758     }
759
760     match_len = b->size - start_len;
761     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
762     return match_len;
763 }
764 \f
765 /* nxm_check_reg_move(), nxm_check_reg_load(). */
766
767 static bool
768 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
769 {
770     return (f && !NXM_HASMASK(f->header)
771             && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
772 }
773
774 int
775 nxm_check_reg_move(const struct nx_action_reg_move *action,
776                    const struct flow *flow)
777 {
778     const struct nxm_field *src;
779     const struct nxm_field *dst;
780
781     if (action->n_bits == htons(0)) {
782         return BAD_ARGUMENT;
783     }
784
785     src = nxm_field_lookup(ntohl(action->src));
786     if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
787         return BAD_ARGUMENT;
788     }
789
790     dst = nxm_field_lookup(ntohl(action->dst));
791     if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
792         return BAD_ARGUMENT;
793     }
794
795     if (!NXM_IS_NX_REG(dst->header)
796         && dst->header != NXM_OF_VLAN_TCI
797         && dst->header != NXM_NX_TUN_ID) {
798         return BAD_ARGUMENT;
799     }
800
801     return 0;
802 }
803
804 int
805 nxm_check_reg_load(const struct nx_action_reg_load *action,
806                    const struct flow *flow)
807 {
808     const struct nxm_field *dst;
809     int ofs, n_bits;
810
811     ofs = ntohs(action->ofs_nbits) >> 6;
812     n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
813     dst = nxm_field_lookup(ntohl(action->dst));
814     if (!field_ok(dst, flow, ofs + n_bits)) {
815         return BAD_ARGUMENT;
816     }
817
818     /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
819      * action->value. */
820     if (n_bits < 64 && ntohll(action->value) >> n_bits) {
821         return BAD_ARGUMENT;
822     }
823
824     if (!NXM_IS_NX_REG(dst->header)) {
825         return BAD_ARGUMENT;
826     }
827
828     return 0;
829 }
830 \f
831 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
832
833 static uint64_t
834 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
835 {
836     switch (src->index) {
837     case NFI_NXM_OF_IN_PORT:
838         return flow->in_port == ODPP_LOCAL ? OFPP_LOCAL : flow->in_port;
839
840     case NFI_NXM_OF_ETH_DST:
841         return eth_addr_to_uint64(flow->dl_dst);
842
843     case NFI_NXM_OF_ETH_SRC:
844         return eth_addr_to_uint64(flow->dl_src);
845
846     case NFI_NXM_OF_ETH_TYPE:
847         return ntohs(flow->dl_type);
848
849     case NFI_NXM_OF_VLAN_TCI:
850         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
851             return 0;
852         } else {
853             return (ntohs(flow->dl_vlan & htons(VLAN_VID_MASK))
854                     | ((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK)
855                     | VLAN_CFI);
856         }
857
858     case NFI_NXM_OF_IP_TOS:
859         return flow->nw_tos;
860
861     case NFI_NXM_OF_IP_PROTO:
862     case NFI_NXM_OF_ARP_OP:
863         return flow->nw_proto;
864
865     case NFI_NXM_OF_IP_SRC:
866     case NFI_NXM_OF_ARP_SPA:
867         return ntohl(flow->nw_src);
868
869     case NFI_NXM_OF_IP_DST:
870     case NFI_NXM_OF_ARP_TPA:
871         return ntohl(flow->nw_dst);
872
873     case NFI_NXM_OF_TCP_SRC:
874     case NFI_NXM_OF_UDP_SRC:
875         return ntohs(flow->tp_src);
876
877     case NFI_NXM_OF_TCP_DST:
878     case NFI_NXM_OF_UDP_DST:
879         return ntohs(flow->tp_dst);
880
881     case NFI_NXM_OF_ICMP_TYPE:
882         return ntohs(flow->tp_src) & 0xff;
883
884     case NFI_NXM_OF_ICMP_CODE:
885         return ntohs(flow->tp_dst) & 0xff;
886
887     case NFI_NXM_NX_TUN_ID:
888         return ntohl(flow->tun_id);
889
890 #define NXM_READ_REGISTER(IDX)                  \
891     case NFI_NXM_NX_REG##IDX:                   \
892         return flow->regs[IDX];                 \
893     case NFI_NXM_NX_REG##IDX##_W:               \
894         NOT_REACHED();
895
896     NXM_READ_REGISTER(0);
897 #if FLOW_N_REGS >= 2
898     NXM_READ_REGISTER(1);
899 #endif
900 #if FLOW_N_REGS >= 3
901     NXM_READ_REGISTER(2);
902 #endif
903 #if FLOW_N_REGS >= 4
904     NXM_READ_REGISTER(3);
905 #endif
906 #if FLOW_N_REGS > 4
907 #error
908 #endif
909
910     case NFI_NXM_OF_VLAN_TCI_W:
911     case NFI_NXM_OF_IP_SRC_W:
912     case NFI_NXM_OF_IP_DST_W:
913     case NFI_NXM_OF_ARP_SPA_W:
914     case NFI_NXM_OF_ARP_TPA_W:
915     case N_NXM_FIELDS:
916         NOT_REACHED();
917     }
918
919     NOT_REACHED();
920 }
921
922 void
923 nxm_execute_reg_move(const struct nx_action_reg_move *action,
924                      struct flow *flow)
925 {
926     /* Preparation. */
927     int n_bits = ntohs(action->n_bits);
928     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
929
930     /* Get the interesting bits of the source field. */
931     const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
932     int src_ofs = ntohs(action->src_ofs);
933     uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
934
935     /* Get the remaining bits of the destination field. */
936     const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
937     int dst_ofs = ntohs(action->dst_ofs);
938     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
939
940     /* Get the final value. */
941     uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
942
943     /* Store the result. */
944     if (NXM_IS_NX_REG(dst->header)) {
945         flow->regs[NXM_NX_REG_IDX(dst->header)] = new_data;
946     } else if (dst->header == NXM_OF_VLAN_TCI) {
947         ovs_be16 vlan_tci = htons(new_data & VLAN_CFI ? new_data : 0);
948         flow->dl_vlan = htons(vlan_tci_to_vid(vlan_tci));
949         flow->dl_vlan_pcp = vlan_tci_to_pcp(vlan_tci);
950     } else if (dst->header == NXM_NX_TUN_ID) {
951         flow->tun_id = htonl(new_data);
952     } else {
953         NOT_REACHED();
954     }
955 }
956
957 void
958 nxm_execute_reg_load(const struct nx_action_reg_load *action,
959                      struct flow *flow)
960 {
961     /* Preparation. */
962     int n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
963     uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
964     uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(action->dst))];
965
966     /* Get source data. */
967     uint32_t src_data = ntohll(action->value);
968
969     /* Get remaining bits of the destination field. */
970     int dst_ofs = ntohs(action->ofs_nbits) >> 6;
971     uint32_t dst_data = *reg & ~(mask << dst_ofs);
972
973     *reg = dst_data | (src_data << dst_ofs);
974 }