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