Introduce ofpacts, an abstraction of OpenFlow actions.
[cascardo/ovs.git] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 Nicira, Inc.
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-actions.h"
27 #include "ofp-errors.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.h"
30 #include "openflow/nicira-ext.h"
31 #include "packets.h"
32 #include "unaligned.h"
33 #include "util.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(nx_match);
37
38 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
39  * peer and so there's not much point in showing a lot of them. */
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
41
42 /* Returns the width of the data for a field with the given 'header', in
43  * bytes. */
44 int
45 nxm_field_bytes(uint32_t header)
46 {
47     unsigned int length = NXM_LENGTH(header);
48     return NXM_HASMASK(header) ? length / 2 : length;
49 }
50
51 /* Returns the width of the data for a field with the given 'header', in
52  * bits. */
53 int
54 nxm_field_bits(uint32_t header)
55 {
56     return nxm_field_bytes(header) * 8;
57 }
58 \f
59 /* nx_pull_match() and helpers. */
60
61 static uint32_t
62 nx_entry_ok(const void *p, unsigned int match_len)
63 {
64     unsigned int payload_len;
65     ovs_be32 header_be;
66     uint32_t header;
67
68     if (match_len < 4) {
69         if (match_len) {
70             VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
71         }
72         return 0;
73     }
74     memcpy(&header_be, p, 4);
75     header = ntohl(header_be);
76
77     payload_len = NXM_LENGTH(header);
78     if (!payload_len) {
79         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
80                     "length 0", header);
81         return 0;
82     }
83     if (match_len < payload_len + 4) {
84         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
85                     "%u bytes left in nx_match", payload_len + 4, match_len);
86         return 0;
87     }
88
89     return header;
90 }
91
92 static enum ofperr
93 nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
94                 uint16_t priority, struct cls_rule *rule,
95                 ovs_be64 *cookie, ovs_be64 *cookie_mask)
96 {
97     uint32_t header;
98     uint8_t *p;
99
100     assert((cookie != NULL) == (cookie_mask != NULL));
101
102     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
103     if (!p) {
104         VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
105                     "multiple of 8, is longer than space in message (max "
106                     "length %zu)", match_len, b->size);
107         return OFPERR_OFPBMC_BAD_LEN;
108     }
109
110     cls_rule_init_catchall(rule, priority);
111     if (cookie) {
112         *cookie = *cookie_mask = htonll(0);
113     }
114     for (;
115          (header = nx_entry_ok(p, match_len)) != 0;
116          p += 4 + NXM_LENGTH(header), match_len -= 4 + NXM_LENGTH(header)) {
117         const struct mf_field *mf;
118         enum ofperr error;
119
120         mf = mf_from_nxm_header(header);
121         if (!mf) {
122             if (strict) {
123                 error = OFPERR_OFPBMC_BAD_FIELD;
124             } else {
125                 continue;
126             }
127         } else if (!mf_are_prereqs_ok(mf, &rule->flow)) {
128             error = OFPERR_OFPBMC_BAD_PREREQ;
129         } else if (!mf_is_all_wild(mf, &rule->wc)) {
130             error = OFPERR_OFPBMC_DUP_FIELD;
131         } else if (header != OXM_OF_IN_PORT) {
132             unsigned int width = mf->n_bytes;
133             union mf_value value;
134
135             memcpy(&value, p + 4, width);
136             if (!mf_is_value_valid(mf, &value)) {
137                 error = OFPERR_OFPBMC_BAD_VALUE;
138             } else if (!NXM_HASMASK(header)) {
139                 error = 0;
140                 mf_set_value(mf, &value, rule);
141             } else {
142                 union mf_value mask;
143
144                 memcpy(&mask, p + 4 + width, width);
145                 if (!mf_is_mask_valid(mf, &mask)) {
146                     error = OFPERR_OFPBMC_BAD_MASK;
147                 } else {
148                     error = 0;
149                     mf_set(mf, &value, &mask, rule);
150                 }
151             }
152         } else {
153             /* Special case for 32bit ports when using OXM,
154              * ports are 16 bits wide otherwise. */
155             ovs_be32 port_of11;
156             uint16_t port;
157
158             memcpy(&port_of11, p + 4, sizeof port_of11);
159             error = ofputil_port_from_ofp11(port_of11, &port);
160             if (!error) {
161                 cls_rule_set_in_port(rule, port);
162             }
163         }
164
165         /* Check if the match is for a cookie rather than a classifier rule. */
166         if ((header == NXM_NX_COOKIE || header == NXM_NX_COOKIE_W) && cookie) {
167             if (*cookie_mask) {
168                 error = OFPERR_OFPBMC_DUP_FIELD;
169             } else {
170                 unsigned int width = sizeof *cookie;
171
172                 memcpy(cookie, p + 4, width);
173                 if (NXM_HASMASK(header)) {
174                     memcpy(cookie_mask, p + 4 + width, width);
175                 } else {
176                     *cookie_mask = htonll(UINT64_MAX);
177                 }
178                 error = 0;
179             }
180         }
181
182         if (error) {
183             VLOG_DBG_RL(&rl, "bad nxm_entry %#08"PRIx32" (vendor=%"PRIu32", "
184                         "field=%"PRIu32", hasmask=%"PRIu32", len=%"PRIu32"), "
185                         "(%s)", header,
186                         NXM_VENDOR(header), NXM_FIELD(header),
187                         NXM_HASMASK(header), NXM_LENGTH(header),
188                         ofperr_to_string(error));
189             return error;
190         }
191     }
192
193     return match_len ? OFPERR_OFPBMC_BAD_LEN : 0;
194 }
195
196 /* Parses the nx_match formatted match description in 'b' with length
197  * 'match_len'.  The results are stored in 'rule', which is initialized with
198  * 'priority'.  If 'cookie' and 'cookie_mask' contain valid pointers, then the
199  * cookie and mask will be stored in them if a "NXM_NX_COOKIE*" match is
200  * defined.  Otherwise, 0 is stored in both.
201  *
202  * Fails with an error when encountering unknown NXM headers.
203  *
204  * Returns 0 if successful, otherwise an OpenFlow error code. */
205 enum ofperr
206 nx_pull_match(struct ofpbuf *b, unsigned int match_len,
207               uint16_t priority, struct cls_rule *rule,
208               ovs_be64 *cookie, ovs_be64 *cookie_mask)
209 {
210     return nx_pull_match__(b, match_len, true, priority, rule, cookie,
211                            cookie_mask);
212 }
213
214 /* Behaves the same as nx_pull_match() with one exception.  Skips over unknown
215  * NXM headers instead of failing with an error when they are encountered. */
216 enum ofperr
217 nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
218                     uint16_t priority, struct cls_rule *rule,
219                     ovs_be64 *cookie, ovs_be64 *cookie_mask)
220 {
221     return nx_pull_match__(b, match_len, false, priority, rule, cookie,
222                            cookie_mask);
223 }
224 \f
225 /* nx_put_match() and helpers.
226  *
227  * 'put' functions whose names end in 'w' add a wildcarded field.
228  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
229  * Other 'put' functions add exact-match fields.
230  */
231
232 static void
233 nxm_put_header(struct ofpbuf *b, uint32_t header)
234 {
235     ovs_be32 n_header = htonl(header);
236     ofpbuf_put(b, &n_header, sizeof n_header);
237 }
238
239 static void
240 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
241 {
242     nxm_put_header(b, header);
243     ofpbuf_put(b, &value, sizeof value);
244 }
245
246 static void
247 nxm_put_8m(struct ofpbuf *b, uint32_t header, uint8_t value, uint8_t mask)
248 {
249     switch (mask) {
250     case 0:
251         break;
252
253     case UINT8_MAX:
254         nxm_put_8(b, header, value);
255         break;
256
257     default:
258         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
259         ofpbuf_put(b, &value, sizeof value);
260         ofpbuf_put(b, &mask, sizeof mask);
261     }
262 }
263
264 static void
265 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
266 {
267     nxm_put_header(b, header);
268     ofpbuf_put(b, &value, sizeof value);
269 }
270
271 static void
272 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
273 {
274     nxm_put_header(b, header);
275     ofpbuf_put(b, &value, sizeof value);
276     ofpbuf_put(b, &mask, sizeof mask);
277 }
278
279 static void
280 nxm_put_16m(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
281 {
282     switch (mask) {
283     case 0:
284         break;
285
286     case CONSTANT_HTONS(UINT16_MAX):
287         nxm_put_16(b, header, value);
288         break;
289
290     default:
291         nxm_put_16w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
292         break;
293     }
294 }
295
296 static void
297 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
298 {
299     nxm_put_header(b, header);
300     ofpbuf_put(b, &value, sizeof value);
301 }
302
303 static void
304 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
305 {
306     nxm_put_header(b, header);
307     ofpbuf_put(b, &value, sizeof value);
308     ofpbuf_put(b, &mask, sizeof mask);
309 }
310
311 static void
312 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
313 {
314     switch (mask) {
315     case 0:
316         break;
317
318     case CONSTANT_HTONL(UINT32_MAX):
319         nxm_put_32(b, header, value);
320         break;
321
322     default:
323         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
324         break;
325     }
326 }
327
328 static void
329 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
330 {
331     nxm_put_header(b, header);
332     ofpbuf_put(b, &value, sizeof value);
333 }
334
335 static void
336 nxm_put_64w(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
337 {
338     nxm_put_header(b, header);
339     ofpbuf_put(b, &value, sizeof value);
340     ofpbuf_put(b, &mask, sizeof mask);
341 }
342
343 static void
344 nxm_put_64m(struct ofpbuf *b, uint32_t header, ovs_be64 value, ovs_be64 mask)
345 {
346     switch (mask) {
347     case 0:
348         break;
349
350     case CONSTANT_HTONLL(UINT64_MAX):
351         nxm_put_64(b, header, value);
352         break;
353
354     default:
355         nxm_put_64w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
356         break;
357     }
358 }
359
360 static void
361 nxm_put_eth(struct ofpbuf *b, uint32_t header,
362             const uint8_t value[ETH_ADDR_LEN])
363 {
364     nxm_put_header(b, header);
365     ofpbuf_put(b, value, ETH_ADDR_LEN);
366 }
367
368 static void
369 nxm_put_eth_masked(struct ofpbuf *b, uint32_t header,
370                    const uint8_t value[ETH_ADDR_LEN],
371                    const uint8_t mask[ETH_ADDR_LEN])
372 {
373     if (!eth_addr_is_zero(mask)) {
374         if (eth_mask_is_exact(mask)) {
375             nxm_put_eth(b, header, value);
376         } else {
377             nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
378             ofpbuf_put(b, value, ETH_ADDR_LEN);
379             ofpbuf_put(b, mask, ETH_ADDR_LEN);
380         }
381     }
382 }
383
384 static void
385 nxm_put_ipv6(struct ofpbuf *b, uint32_t header,
386              const struct in6_addr *value, const struct in6_addr *mask)
387 {
388     if (ipv6_mask_is_any(mask)) {
389         return;
390     } else if (ipv6_mask_is_exact(mask)) {
391         nxm_put_header(b, header);
392         ofpbuf_put(b, value, sizeof *value);
393     } else {
394         nxm_put_header(b, NXM_MAKE_WILD_HEADER(header));
395         ofpbuf_put(b, value, sizeof *value);
396         ofpbuf_put(b, mask, sizeof *mask);
397     }
398 }
399
400 static void
401 nxm_put_frag(struct ofpbuf *b, const struct cls_rule *cr)
402 {
403     uint8_t nw_frag = cr->flow.nw_frag;
404     uint8_t nw_frag_mask = cr->wc.nw_frag_mask;
405
406     switch (nw_frag_mask) {
407     case 0:
408         break;
409
410     case FLOW_NW_FRAG_MASK:
411         nxm_put_8(b, NXM_NX_IP_FRAG, nw_frag);
412         break;
413
414     default:
415         nxm_put_8m(b, NXM_NX_IP_FRAG, nw_frag,
416                    nw_frag_mask & FLOW_NW_FRAG_MASK);
417         break;
418     }
419 }
420
421 static void
422 nxm_put_ip(struct ofpbuf *b, const struct cls_rule *cr,
423            uint8_t icmp_proto, uint32_t icmp_type, uint32_t icmp_code,
424            bool oxm)
425 {
426     const flow_wildcards_t wc = cr->wc.wildcards;
427     const struct flow *flow = &cr->flow;
428
429     nxm_put_frag(b, cr);
430
431     if (!(wc & FWW_NW_DSCP)) {
432         nxm_put_8(b, oxm ? OXM_OF_IP_DSCP : NXM_OF_IP_TOS,
433                   flow->nw_tos & IP_DSCP_MASK);
434     }
435
436     if (!(wc & FWW_NW_ECN)) {
437         nxm_put_8(b, oxm ? OXM_OF_IP_ECN : NXM_NX_IP_ECN,
438                   flow->nw_tos & IP_ECN_MASK);
439     }
440
441     if (!oxm && !(wc & FWW_NW_TTL)) {
442         nxm_put_8(b, NXM_NX_IP_TTL, flow->nw_ttl);
443     }
444
445     if (!(wc & FWW_NW_PROTO)) {
446         nxm_put_8(b, oxm ? OXM_OF_IP_PROTO : NXM_OF_IP_PROTO, flow->nw_proto);
447
448         if (flow->nw_proto == IPPROTO_TCP) {
449             nxm_put_16m(b, oxm ? OXM_OF_TCP_SRC : NXM_OF_TCP_SRC,
450                         flow->tp_src, cr->wc.tp_src_mask);
451             nxm_put_16m(b, oxm ? OXM_OF_TCP_DST : NXM_OF_TCP_DST,
452                         flow->tp_dst, cr->wc.tp_dst_mask);
453         } else if (flow->nw_proto == IPPROTO_UDP) {
454             nxm_put_16m(b, oxm ? OXM_OF_UDP_SRC : NXM_OF_UDP_SRC,
455                         flow->tp_src, cr->wc.tp_src_mask);
456             nxm_put_16m(b, oxm ? OXM_OF_UDP_DST : NXM_OF_UDP_DST,
457                         flow->tp_dst, cr->wc.tp_dst_mask);
458         } else if (flow->nw_proto == icmp_proto) {
459             if (cr->wc.tp_src_mask) {
460                 nxm_put_8(b, icmp_type, ntohs(flow->tp_src));
461             }
462             if (cr->wc.tp_dst_mask) {
463                 nxm_put_8(b, icmp_code, ntohs(flow->tp_dst));
464             }
465         }
466     }
467 }
468
469 /* Appends to 'b' the nx_match format that expresses 'cr' (except for
470  * 'cr->priority', because priority is not part of nx_match), plus enough
471  * zero bytes to pad the nx_match out to a multiple of 8.  For Flow Mod
472  * and Flow Stats Requests messages, a 'cookie' and 'cookie_mask' may be
473  * supplied.  Otherwise, 'cookie_mask' should be zero.
474  *
475  * This function can cause 'b''s data to be reallocated.
476  *
477  * Returns the number of bytes appended to 'b', excluding padding.
478  *
479  * If 'cr' is a catch-all rule that matches every packet, then this function
480  * appends nothing to 'b' and returns 0. */
481 int
482 nx_put_match(struct ofpbuf *b, bool oxm, const struct cls_rule *cr,
483              ovs_be64 cookie, ovs_be64 cookie_mask)
484 {
485     const flow_wildcards_t wc = cr->wc.wildcards;
486     const struct flow *flow = &cr->flow;
487     const size_t start_len = b->size;
488     int match_len;
489     int i;
490
491     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 12);
492
493     /* Metadata. */
494     if (!(wc & FWW_IN_PORT)) {
495         uint16_t in_port = flow->in_port;
496         if (oxm) {
497             nxm_put_32(b, OXM_OF_IN_PORT, ofputil_port_to_ofp11(in_port));
498         } else {
499             nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
500         }
501     }
502
503     /* Ethernet. */
504     nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_SRC : NXM_OF_ETH_SRC,
505                        flow->dl_src, cr->wc.dl_src_mask);
506     nxm_put_eth_masked(b, oxm ? OXM_OF_ETH_DST : NXM_OF_ETH_DST,
507                        flow->dl_dst, cr->wc.dl_dst_mask);
508     if (!(wc & FWW_DL_TYPE)) {
509         nxm_put_16(b, oxm ? OXM_OF_ETH_TYPE : NXM_OF_ETH_TYPE,
510                    ofputil_dl_type_to_openflow(flow->dl_type));
511     }
512
513     /* 802.1Q.
514      *
515      * XXX missing OXM support */
516     nxm_put_16m(b, NXM_OF_VLAN_TCI, flow->vlan_tci, cr->wc.vlan_tci_mask);
517
518     /* L3. */
519     if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
520         /* IP. */
521         nxm_put_32m(b, oxm ? OXM_OF_IPV4_SRC : NXM_OF_IP_SRC,
522                     flow->nw_src, cr->wc.nw_src_mask);
523         nxm_put_32m(b, oxm ? OXM_OF_IPV4_DST : NXM_OF_IP_DST,
524                     flow->nw_dst, cr->wc.nw_dst_mask);
525         nxm_put_ip(b, cr, IPPROTO_ICMP,
526                    oxm ? OXM_OF_ICMPV4_TYPE : NXM_OF_ICMP_TYPE,
527                    oxm ? OXM_OF_ICMPV4_CODE : NXM_OF_ICMP_CODE, oxm);
528     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
529         /* IPv6. */
530         nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_SRC : NXM_NX_IPV6_SRC,
531                      &flow->ipv6_src, &cr->wc.ipv6_src_mask);
532         nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_DST : NXM_NX_IPV6_DST,
533                      &flow->ipv6_dst, &cr->wc.ipv6_dst_mask);
534         nxm_put_ip(b, cr, IPPROTO_ICMPV6,
535                    oxm ? OXM_OF_ICMPV6_TYPE : NXM_NX_ICMPV6_TYPE,
536                    oxm ? OXM_OF_ICMPV6_CODE : NXM_NX_ICMPV6_CODE, oxm);
537
538         if (!(wc & FWW_IPV6_LABEL)) {
539             nxm_put_32(b, oxm ? OXM_OF_IPV6_FLABEL : NXM_NX_IPV6_LABEL,
540                        flow->ipv6_label);
541         }
542
543         if (flow->nw_proto == IPPROTO_ICMPV6
544             && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
545                 flow->tp_src == htons(ND_NEIGHBOR_ADVERT))) {
546             nxm_put_ipv6(b, oxm ? OXM_OF_IPV6_ND_TARGET : NXM_NX_ND_TARGET,
547                          &flow->nd_target, &cr->wc.nd_target_mask);
548             if (!(wc & FWW_ARP_SHA)
549                 && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
550                 nxm_put_eth(b, oxm ? OXM_OF_IPV6_ND_SLL : NXM_NX_ND_SLL,
551                             flow->arp_sha);
552             }
553             if (!(wc & FWW_ARP_THA)
554                 && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
555                 nxm_put_eth(b, oxm ? OXM_OF_IPV6_ND_TLL : NXM_NX_ND_TLL,
556                             flow->arp_tha);
557             }
558         }
559     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
560         /* ARP. */
561         if (!(wc & FWW_NW_PROTO)) {
562             nxm_put_16(b, oxm ? OXM_OF_ARP_OP : NXM_OF_ARP_OP,
563                        htons(flow->nw_proto));
564         }
565         nxm_put_32m(b, oxm ? OXM_OF_ARP_SPA : NXM_OF_ARP_SPA,
566                     flow->nw_src, cr->wc.nw_src_mask);
567         nxm_put_32m(b, oxm ? OXM_OF_ARP_TPA : NXM_OF_ARP_TPA,
568                     flow->nw_dst, cr->wc.nw_dst_mask);
569         if (!(wc & FWW_ARP_SHA)) {
570             nxm_put_eth(b, oxm ? OXM_OF_ARP_SHA : NXM_NX_ARP_SHA,
571                         flow->arp_sha);
572         }
573         if (!(wc & FWW_ARP_THA)) {
574             nxm_put_eth(b, oxm ? OXM_OF_ARP_THA : NXM_NX_ARP_THA,
575                         flow->arp_tha);
576         }
577     }
578
579     /* Tunnel ID. */
580     nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
581
582     /* Registers. */
583     for (i = 0; i < FLOW_N_REGS; i++) {
584         nxm_put_32m(b, NXM_NX_REG(i),
585                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
586     }
587
588     /* OpenFlow 1.1+ Metadata. */
589     nxm_put_64m(b, OXM_OF_METADATA, flow->metadata, cr->wc.metadata_mask);
590
591     /* Cookie. */
592     nxm_put_64m(b, NXM_NX_COOKIE, cookie, cookie_mask);
593
594     match_len = b->size - start_len;
595     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
596     return match_len;
597 }
598 \f
599 /* nx_match_to_string() and helpers. */
600
601 static void format_nxm_field_name(struct ds *, uint32_t header);
602
603 char *
604 nx_match_to_string(const uint8_t *p, unsigned int match_len)
605 {
606     uint32_t header;
607     struct ds s;
608
609     if (!match_len) {
610         return xstrdup("<any>");
611     }
612
613     ds_init(&s);
614     while ((header = nx_entry_ok(p, match_len)) != 0) {
615         unsigned int length = NXM_LENGTH(header);
616         unsigned int value_len = nxm_field_bytes(header);
617         const uint8_t *value = p + 4;
618         const uint8_t *mask = value + value_len;
619         unsigned int i;
620
621         if (s.length) {
622             ds_put_cstr(&s, ", ");
623         }
624
625         format_nxm_field_name(&s, header);
626         ds_put_char(&s, '(');
627
628         for (i = 0; i < value_len; i++) {
629             ds_put_format(&s, "%02x", value[i]);
630         }
631         if (NXM_HASMASK(header)) {
632             ds_put_char(&s, '/');
633             for (i = 0; i < value_len; i++) {
634                 ds_put_format(&s, "%02x", mask[i]);
635             }
636         }
637         ds_put_char(&s, ')');
638
639         p += 4 + length;
640         match_len -= 4 + length;
641     }
642
643     if (match_len) {
644         if (s.length) {
645             ds_put_cstr(&s, ", ");
646         }
647
648         ds_put_format(&s, "<%u invalid bytes>", match_len);
649     }
650
651     return ds_steal_cstr(&s);
652 }
653
654 static void
655 format_nxm_field_name(struct ds *s, uint32_t header)
656 {
657     const struct mf_field *mf = mf_from_nxm_header(header);
658     if (mf) {
659         ds_put_cstr(s, IS_OXM_HEADER(header) ? mf->oxm_name : mf->nxm_name);
660         if (NXM_HASMASK(header)) {
661             ds_put_cstr(s, "_W");
662         }
663     } else if (header == NXM_NX_COOKIE) {
664         ds_put_cstr(s, "NXM_NX_COOKIE");
665     } else if (header == NXM_NX_COOKIE_W) {
666         ds_put_cstr(s, "NXM_NX_COOKIE_W");
667     } else {
668         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
669     }
670 }
671
672 static uint32_t
673 parse_nxm_field_name(const char *name, int name_len)
674 {
675     bool wild;
676     int i;
677
678     /* Check whether it's a field name. */
679     wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
680     if (wild) {
681         name_len -= 2;
682     }
683
684     for (i = 0; i < MFF_N_IDS; i++) {
685         const struct mf_field *mf = mf_from_id(i);
686         uint32_t header;
687
688         if (mf->nxm_name &&
689             !strncmp(mf->nxm_name, name, name_len) &&
690             mf->nxm_name[name_len] == '\0') {
691             header = mf->nxm_header;
692         } else if (mf->oxm_name &&
693                    !strncmp(mf->oxm_name, name, name_len) &&
694                    mf->oxm_name[name_len] == '\0') {
695             header = mf->oxm_header;
696         } else {
697             continue;
698         }
699
700         if (!wild) {
701             return header;
702         } else if (mf->maskable != MFM_NONE) {
703             return NXM_MAKE_WILD_HEADER(header);
704         }
705     }
706
707     if (!strncmp("NXM_NX_COOKIE", name, name_len) &&
708         (name_len == strlen("NXM_NX_COOKIE"))) {
709         if (!wild) {
710             return NXM_NX_COOKIE;
711         } else {
712             return NXM_NX_COOKIE_W;
713         }
714     }
715
716     /* Check whether it's a 32-bit field header value as hex.
717      * (This isn't ordinarily useful except for testing error behavior.) */
718     if (name_len == 8) {
719         uint32_t header = hexits_value(name, name_len, NULL);
720         if (header != UINT_MAX) {
721             return header;
722         }
723     }
724
725     return 0;
726 }
727 \f
728 /* nx_match_from_string(). */
729
730 int
731 nx_match_from_string(const char *s, struct ofpbuf *b)
732 {
733     const char *full_s = s;
734     const size_t start_len = b->size;
735     int match_len;
736
737     if (!strcmp(s, "<any>")) {
738         /* Ensure that 'b->data' isn't actually null. */
739         ofpbuf_prealloc_tailroom(b, 1);
740         return 0;
741     }
742
743     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
744         const char *name;
745         uint32_t header;
746         int name_len;
747         size_t n;
748
749         name = s;
750         name_len = strcspn(s, "(");
751         if (s[name_len] != '(') {
752             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
753         }
754
755         header = parse_nxm_field_name(name, name_len);
756         if (!header) {
757             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
758         }
759
760         s += name_len + 1;
761
762         nxm_put_header(b, header);
763         s = ofpbuf_put_hex(b, s, &n);
764         if (n != nxm_field_bytes(header)) {
765             ovs_fatal(0, "%.2s: hex digits expected", s);
766         }
767         if (NXM_HASMASK(header)) {
768             s += strspn(s, " ");
769             if (*s != '/') {
770                 ovs_fatal(0, "%s: missing / in masked field %.*s",
771                           full_s, name_len, name);
772             }
773             s = ofpbuf_put_hex(b, s + 1, &n);
774             if (n != nxm_field_bytes(header)) {
775                 ovs_fatal(0, "%.2s: hex digits expected", s);
776             }
777         }
778
779         s += strspn(s, " ");
780         if (*s != ')') {
781             ovs_fatal(0, "%s: missing ) following field %.*s",
782                       full_s, name_len, name);
783         }
784         s++;
785     }
786
787     match_len = b->size - start_len;
788     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
789     return match_len;
790 }
791 \f
792 void
793 nxm_parse_reg_move(struct ofpact_reg_move *move, const char *s)
794 {
795     const char *full_s = s;
796
797     s = mf_parse_subfield(&move->src, s);
798     if (strncmp(s, "->", 2)) {
799         ovs_fatal(0, "%s: missing `->' following source", full_s);
800     }
801     s += 2;
802     s = mf_parse_subfield(&move->dst, s);
803     if (*s != '\0') {
804         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
805     }
806
807     if (move->src.n_bits != move->dst.n_bits) {
808         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
809                   "%d bits wide", full_s,
810                   move->src.n_bits, move->dst.n_bits);
811     }
812 }
813
814 void
815 nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s)
816 {
817     const char *full_s = s;
818
819     load->value = strtoull(s, (char **) &s, 0);
820     if (strncmp(s, "->", 2)) {
821         ovs_fatal(0, "%s: missing `->' following value", full_s);
822     }
823     s += 2;
824     s = mf_parse_subfield(&load->dst, s);
825     if (*s != '\0') {
826         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
827     }
828
829     if (load->dst.n_bits < 64 && (load->value >> load->dst.n_bits) != 0) {
830         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
831                   full_s, load->value, load->dst.n_bits);
832     }
833 }
834 \f
835 /* nxm_format_reg_move(), nxm_format_reg_load(). */
836
837 void
838 nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s)
839 {
840     ds_put_format(s, "move:");
841     mf_format_subfield(&move->src, s);
842     ds_put_cstr(s, "->");
843     mf_format_subfield(&move->dst, s);
844 }
845
846 void
847 nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s)
848 {
849     ds_put_format(s, "load:%#"PRIx64"->", load->value);
850     mf_format_subfield(&load->dst, s);
851 }
852 \f
853 enum ofperr
854 nxm_reg_move_from_openflow(const struct nx_action_reg_move *narm,
855                            struct ofpbuf *ofpacts)
856 {
857     struct ofpact_reg_move *move;
858
859     move = ofpact_put_REG_MOVE(ofpacts);
860     move->src.field = mf_from_nxm_header(ntohl(narm->src));
861     move->src.ofs = ntohs(narm->src_ofs);
862     move->src.n_bits = ntohs(narm->n_bits);
863     move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
864     move->dst.ofs = ntohs(narm->dst_ofs);
865     move->dst.n_bits = ntohs(narm->n_bits);
866
867     return nxm_reg_move_check(move, NULL);
868 }
869
870 enum ofperr
871 nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl,
872                            struct ofpbuf *ofpacts)
873 {
874     struct ofpact_reg_load *load;
875
876     load = ofpact_put_REG_LOAD(ofpacts);
877     load->dst.field = mf_from_nxm_header(ntohl(narl->dst));
878     load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
879     load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
880     load->value = ntohll(narl->value);
881
882     /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
883      * narl->value. */
884     if (load->dst.n_bits < 64 && load->value >> load->dst.n_bits) {
885         return OFPERR_OFPBAC_BAD_ARGUMENT;
886     }
887
888     return nxm_reg_load_check(load, NULL);
889 }
890 \f
891 enum ofperr
892 nxm_reg_move_check(const struct ofpact_reg_move *move, const struct flow *flow)
893 {
894     enum ofperr error;
895
896     error = mf_check_src(&move->src, flow);
897     if (error) {
898         return error;
899     }
900
901     return mf_check_dst(&move->dst, NULL);
902 }
903
904 enum ofperr
905 nxm_reg_load_check(const struct ofpact_reg_load *load, const struct flow *flow)
906 {
907     return mf_check_dst(&load->dst, flow);
908 }
909 \f
910 void
911 nxm_reg_move_to_nxast(const struct ofpact_reg_move *move,
912                       struct ofpbuf *openflow)
913 {
914     struct nx_action_reg_move *narm;
915
916     narm = ofputil_put_NXAST_REG_MOVE(openflow);
917     narm->n_bits = htons(move->dst.n_bits);
918     narm->src_ofs = htons(move->src.ofs);
919     narm->dst_ofs = htons(move->dst.ofs);
920     narm->src = htonl(move->src.field->nxm_header);
921     narm->dst = htonl(move->dst.field->nxm_header);
922 }
923
924 void
925 nxm_reg_load_to_nxast(const struct ofpact_reg_load *load,
926                       struct ofpbuf *openflow)
927 {
928     struct nx_action_reg_load *narl;
929
930     narl = ofputil_put_NXAST_REG_LOAD(openflow);
931     narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits);
932     narl->dst = htonl(load->dst.field->nxm_header);
933     narl->value = htonll(load->value);
934 }
935 \f
936 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
937
938 void
939 nxm_execute_reg_move(const struct ofpact_reg_move *move,
940                      struct flow *flow)
941 {
942     union mf_value src_value;
943     union mf_value dst_value;
944
945     mf_get_value(move->dst.field, flow, &dst_value);
946     mf_get_value(move->src.field, flow, &src_value);
947     bitwise_copy(&src_value, move->src.field->n_bytes, move->src.ofs,
948                  &dst_value, move->dst.field->n_bytes, move->dst.ofs,
949                  move->src.n_bits);
950     mf_set_flow_value(move->dst.field, &dst_value, flow);
951 }
952
953 void
954 nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow)
955 {
956     nxm_reg_load(&load->dst, load->value, flow);
957 }
958
959 void
960 nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data,
961              struct flow *flow)
962 {
963     union mf_value dst_value;
964     union mf_value src_value;
965
966     mf_get_value(dst->field, flow, &dst_value);
967     src_value.be64 = htonll(src_data);
968     bitwise_copy(&src_value, sizeof src_value.be64, 0,
969                  &dst_value, dst->field->n_bytes, dst->ofs,
970                  dst->n_bits);
971     mf_set_flow_value(dst->field, &dst_value, flow);
972 }