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