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