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