Don't overload IP TOS with the frag matching 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 (!(wc & FWW_NW_PROTO)) {
496             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
497             switch (flow->nw_proto) {
498                 /* TCP. */
499             case IPPROTO_TCP:
500                 if (!(wc & FWW_TP_SRC)) {
501                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
502                 }
503                 if (!(wc & FWW_TP_DST)) {
504                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
505                 }
506                 break;
507
508                 /* UDP. */
509             case IPPROTO_UDP:
510                 if (!(wc & FWW_TP_SRC)) {
511                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
512                 }
513                 if (!(wc & FWW_TP_DST)) {
514                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
515                 }
516                 break;
517
518                 /* ICMP. */
519             case IPPROTO_ICMP:
520                 if (!(wc & FWW_TP_SRC)) {
521                     nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
522                 }
523                 if (!(wc & FWW_TP_DST)) {
524                     nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
525                 }
526                 break;
527             }
528         }
529     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IPV6)) {
530         /* IPv6. */
531         nxm_put_ipv6(b, NXM_NX_IPV6_SRC, &flow->ipv6_src,
532                 &cr->wc.ipv6_src_mask);
533         nxm_put_ipv6(b, NXM_NX_IPV6_DST, &flow->ipv6_dst,
534                 &cr->wc.ipv6_dst_mask);
535         nxm_put_frag(b, cr);
536
537         if (!(wc & FWW_IPV6_LABEL)) {
538             nxm_put_32(b, NXM_NX_IPV6_LABEL, flow->ipv6_label);
539         }
540
541         if (cr->wc.tos_mask & IP_DSCP_MASK) {
542             nxm_put_8(b, NXM_OF_IP_TOS, flow->tos & IP_DSCP_MASK);
543         }
544
545         if (!(wc & FWW_NW_PROTO)) {
546             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
547             switch (flow->nw_proto) {
548                 /* TCP. */
549             case IPPROTO_TCP:
550                 if (!(wc & FWW_TP_SRC)) {
551                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
552                 }
553                 if (!(wc & FWW_TP_DST)) {
554                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
555                 }
556                 break;
557
558                 /* UDP. */
559             case IPPROTO_UDP:
560                 if (!(wc & FWW_TP_SRC)) {
561                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
562                 }
563                 if (!(wc & FWW_TP_DST)) {
564                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
565                 }
566                 break;
567
568                 /* ICMPv6. */
569             case IPPROTO_ICMPV6:
570                 if (!(wc & FWW_TP_SRC)) {
571                     nxm_put_8(b, NXM_NX_ICMPV6_TYPE, ntohs(flow->tp_src));
572
573                     if (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
574                         flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
575                         if (!(wc & FWW_ND_TARGET)) {
576                             nxm_put_ipv6(b, NXM_NX_ND_TARGET, &flow->nd_target,
577                                          &in6addr_exact);
578                         }
579                         if (!(wc & FWW_ARP_SHA)
580                             && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
581                             nxm_put_eth(b, NXM_NX_ND_SLL, flow->arp_sha);
582                         }
583                         if (!(wc & FWW_ARP_THA)
584                             && flow->tp_src == htons(ND_NEIGHBOR_ADVERT)) {
585                             nxm_put_eth(b, NXM_NX_ND_TLL, flow->arp_tha);
586                         }
587                     }
588                 }
589                 if (!(wc & FWW_TP_DST)) {
590                     nxm_put_8(b, NXM_NX_ICMPV6_CODE, ntohs(flow->tp_dst));
591                 }
592                 break;
593             }
594         }
595     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
596         /* ARP. */
597         if (!(wc & FWW_NW_PROTO)) {
598             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
599         }
600         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
601         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
602         if (!(wc & FWW_ARP_SHA)) {
603             nxm_put_eth(b, NXM_NX_ARP_SHA, flow->arp_sha);
604         }
605         if (!(wc & FWW_ARP_THA)) {
606             nxm_put_eth(b, NXM_NX_ARP_THA, flow->arp_tha);
607         }
608     }
609
610     /* Tunnel ID. */
611     nxm_put_64m(b, NXM_NX_TUN_ID, flow->tun_id, cr->wc.tun_id_mask);
612
613     /* Registers. */
614     for (i = 0; i < FLOW_N_REGS; i++) {
615         nxm_put_32m(b, NXM_NX_REG(i),
616                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
617     }
618
619     match_len = b->size - start_len;
620     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
621     return match_len;
622 }
623 \f
624 /* nx_match_to_string() and helpers. */
625
626 static void format_nxm_field_name(struct ds *, uint32_t header);
627
628 char *
629 nx_match_to_string(const uint8_t *p, unsigned int match_len)
630 {
631     uint32_t header;
632     struct ds s;
633
634     if (!match_len) {
635         return xstrdup("<any>");
636     }
637
638     ds_init(&s);
639     while ((header = nx_entry_ok(p, match_len)) != 0) {
640         unsigned int length = NXM_LENGTH(header);
641         unsigned int value_len = nxm_field_bytes(header);
642         const uint8_t *value = p + 4;
643         const uint8_t *mask = value + value_len;
644         unsigned int i;
645
646         if (s.length) {
647             ds_put_cstr(&s, ", ");
648         }
649
650         format_nxm_field_name(&s, header);
651         ds_put_char(&s, '(');
652
653         for (i = 0; i < value_len; i++) {
654             ds_put_format(&s, "%02x", value[i]);
655         }
656         if (NXM_HASMASK(header)) {
657             ds_put_char(&s, '/');
658             for (i = 0; i < value_len; i++) {
659                 ds_put_format(&s, "%02x", mask[i]);
660             }
661         }
662         ds_put_char(&s, ')');
663
664         p += 4 + length;
665         match_len -= 4 + length;
666     }
667
668     if (match_len) {
669         if (s.length) {
670             ds_put_cstr(&s, ", ");
671         }
672
673         ds_put_format(&s, "<%u invalid bytes>", match_len);
674     }
675
676     return ds_steal_cstr(&s);
677 }
678
679 static void
680 format_nxm_field_name(struct ds *s, uint32_t header)
681 {
682     const struct nxm_field *f = nxm_field_lookup(header);
683     if (f) {
684         ds_put_cstr(s, f->name);
685     } else {
686         ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
687     }
688 }
689
690 static uint32_t
691 parse_nxm_field_name(const char *name, int name_len)
692 {
693     const struct nxm_field *f;
694
695     /* Check whether it's a field name. */
696     for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
697         if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
698             return f->header;
699         }
700     }
701
702     /* Check whether it's a 32-bit field header value as hex.
703      * (This isn't ordinarily useful except for testing error behavior.) */
704     if (name_len == 8) {
705         uint32_t header = hexits_value(name, name_len, NULL);
706         if (header != UINT_MAX) {
707             return header;
708         }
709     }
710
711     return 0;
712 }
713 \f
714 /* nx_match_from_string(). */
715
716 int
717 nx_match_from_string(const char *s, struct ofpbuf *b)
718 {
719     const char *full_s = s;
720     const size_t start_len = b->size;
721     int match_len;
722
723     if (!strcmp(s, "<any>")) {
724         /* Ensure that 'b->data' isn't actually null. */
725         ofpbuf_prealloc_tailroom(b, 1);
726         return 0;
727     }
728
729     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
730         const char *name;
731         uint32_t header;
732         int name_len;
733         size_t n;
734
735         name = s;
736         name_len = strcspn(s, "(");
737         if (s[name_len] != '(') {
738             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
739         }
740
741         header = parse_nxm_field_name(name, name_len);
742         if (!header) {
743             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
744         }
745
746         s += name_len + 1;
747
748         nxm_put_header(b, header);
749         s = ofpbuf_put_hex(b, s, &n);
750         if (n != nxm_field_bytes(header)) {
751             ovs_fatal(0, "%.2s: hex digits expected", s);
752         }
753         if (NXM_HASMASK(header)) {
754             s += strspn(s, " ");
755             if (*s != '/') {
756                 ovs_fatal(0, "%s: missing / in masked field %.*s",
757                           full_s, name_len, name);
758             }
759             s = ofpbuf_put_hex(b, s + 1, &n);
760             if (n != nxm_field_bytes(header)) {
761                 ovs_fatal(0, "%.2s: hex digits expected", s);
762             }
763         }
764
765         s += strspn(s, " ");
766         if (*s != ')') {
767             ovs_fatal(0, "%s: missing ) following field %.*s",
768                       full_s, name_len, name);
769         }
770         s++;
771     }
772
773     match_len = b->size - start_len;
774     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
775     return match_len;
776 }
777 \f
778 const char *
779 nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
780 {
781     const char *full_s = s;
782     const char *name;
783     uint32_t header;
784     int start, end;
785     int name_len;
786     int width;
787
788     name = s;
789     name_len = strcspn(s, "[");
790     if (s[name_len] != '[') {
791         ovs_fatal(0, "%s: missing [ looking for field name", full_s);
792     }
793
794     header = parse_nxm_field_name(name, name_len);
795     if (!header) {
796         ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
797     }
798     width = nxm_field_bits(header);
799
800     s += name_len;
801     if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
802         /* Nothing to do. */
803     } else if (sscanf(s, "[%d]", &start) == 1) {
804         end = start;
805     } else if (!strncmp(s, "[]", 2)) {
806         start = 0;
807         end = width - 1;
808     } else {
809         ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
810                   "[<start>..<end>]", full_s);
811     }
812     s = strchr(s, ']') + 1;
813
814     if (start > end) {
815         ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
816                   full_s, start, end);
817     } else if (start >= width) {
818         ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
819                   "%d bits wide", full_s, start, width);
820     } else if (end >= width){
821         ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
822                   "%d bits wide", full_s, end, width);
823     }
824
825     *headerp = header;
826     *ofsp = start;
827     *n_bitsp = end - start + 1;
828
829     return s;
830 }
831
832 void
833 nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
834 {
835     const char *full_s = s;
836     uint32_t src, dst;
837     int src_ofs, dst_ofs;
838     int src_n_bits, dst_n_bits;
839
840     s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
841     if (strncmp(s, "->", 2)) {
842         ovs_fatal(0, "%s: missing `->' following source", full_s);
843     }
844     s += 2;
845     s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
846     if (*s != '\0') {
847         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
848     }
849
850     if (src_n_bits != dst_n_bits) {
851         ovs_fatal(0, "%s: source field is %d bits wide but destination is "
852                   "%d bits wide", full_s, src_n_bits, dst_n_bits);
853     }
854
855     ofputil_init_NXAST_REG_MOVE(move);
856     move->n_bits = htons(src_n_bits);
857     move->src_ofs = htons(src_ofs);
858     move->dst_ofs = htons(dst_ofs);
859     move->src = htonl(src);
860     move->dst = htonl(dst);
861 }
862
863 void
864 nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
865 {
866     const char *full_s = s;
867     uint32_t dst;
868     int ofs, n_bits;
869     uint64_t value;
870
871     value = strtoull(s, (char **) &s, 0);
872     if (strncmp(s, "->", 2)) {
873         ovs_fatal(0, "%s: missing `->' following value", full_s);
874     }
875     s += 2;
876     s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
877     if (*s != '\0') {
878         ovs_fatal(0, "%s: trailing garbage following destination", full_s);
879     }
880
881     if (n_bits < 64 && (value >> n_bits) != 0) {
882         ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
883                   full_s, value, n_bits);
884     }
885
886     ofputil_init_NXAST_REG_LOAD(load);
887     load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
888     load->dst = htonl(dst);
889     load->value = htonll(value);
890 }
891 \f
892 /* nxm_format_reg_move(), nxm_format_reg_load(). */
893
894 void
895 nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
896 {
897     format_nxm_field_name(s, header);
898     if (ofs == 0 && n_bits == nxm_field_bits(header)) {
899         ds_put_cstr(s, "[]");
900     } else if (n_bits == 1) {
901         ds_put_format(s, "[%d]", ofs);
902     } else {
903         ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
904     }
905 }
906
907 void
908 nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
909 {
910     int n_bits = ntohs(move->n_bits);
911     int src_ofs = ntohs(move->src_ofs);
912     int dst_ofs = ntohs(move->dst_ofs);
913     uint32_t src = ntohl(move->src);
914     uint32_t dst = ntohl(move->dst);
915
916     ds_put_format(s, "move:");
917     nxm_format_field_bits(s, src, src_ofs, n_bits);
918     ds_put_cstr(s, "->");
919     nxm_format_field_bits(s, dst, dst_ofs, n_bits);
920 }
921
922 void
923 nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
924 {
925     int ofs = nxm_decode_ofs(load->ofs_nbits);
926     int n_bits = nxm_decode_n_bits(load->ofs_nbits);
927     uint32_t dst = ntohl(load->dst);
928     uint64_t value = ntohll(load->value);
929
930     ds_put_format(s, "load:%#"PRIx64"->", value);
931     nxm_format_field_bits(s, dst, ofs, n_bits);
932 }
933 \f
934 /* nxm_check_reg_move(), nxm_check_reg_load(). */
935
936 static bool
937 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
938 {
939     return (f
940             && !NXM_HASMASK(f->header)
941             && mf_are_prereqs_ok(f->mf, flow)
942             && size <= nxm_field_bits(f->header));
943 }
944
945 int
946 nxm_check_reg_move(const struct nx_action_reg_move *action,
947                    const struct flow *flow)
948 {
949     int src_ofs, dst_ofs, n_bits;
950     int error;
951
952     n_bits = ntohs(action->n_bits);
953     src_ofs = ntohs(action->src_ofs);
954     dst_ofs = ntohs(action->dst_ofs);
955
956     error = nxm_src_check(action->src, src_ofs, n_bits, flow);
957     if (error) {
958         return error;
959     }
960
961     return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
962 }
963
964 /* Given a flow, checks that the source field represented by 'src_header'
965  * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
966 int
967 nxm_src_check(ovs_be32 src_header, unsigned int ofs, unsigned int n_bits,
968               const struct flow *flow)
969 {
970     const struct nxm_field *src = nxm_field_lookup(ntohl(src_header));
971
972     if (!n_bits) {
973         VLOG_WARN_RL(&rl, "zero bit source field");
974     } else if (!field_ok(src, flow, ofs + n_bits)) {
975         VLOG_WARN_RL(&rl, "invalid source field");
976     } else {
977         return 0;
978     }
979
980     return BAD_ARGUMENT;
981 }
982
983 /* Given a flow, checks that the destination field represented by 'dst_header'
984  * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
985 int
986 nxm_dst_check(ovs_be32 dst_header, unsigned int ofs, unsigned int n_bits,
987               const struct flow *flow)
988 {
989     const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
990
991     if (!n_bits) {
992         VLOG_WARN_RL(&rl, "zero bit destination field");
993     } else if (!field_ok(dst, flow, ofs + n_bits)) {
994         VLOG_WARN_RL(&rl, "invalid destination field");
995     } else if (!dst->writable) {
996         VLOG_WARN_RL(&rl, "destination field is not writable");
997     } else {
998         return 0;
999     }
1000
1001     return BAD_ARGUMENT;
1002 }
1003
1004 int
1005 nxm_check_reg_load(const struct nx_action_reg_load *action,
1006                    const struct flow *flow)
1007 {
1008     unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
1009     unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
1010     int error;
1011
1012     error = nxm_dst_check(action->dst, ofs, n_bits, flow);
1013     if (error) {
1014         return error;
1015     }
1016
1017     /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
1018      * action->value. */
1019     if (n_bits < 64 && ntohll(action->value) >> n_bits) {
1020         return BAD_ARGUMENT;
1021     }
1022
1023     return 0;
1024 }
1025 \f
1026 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
1027
1028 static uint64_t
1029 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
1030 {
1031     switch (src->index) {
1032     case NFI_NXM_OF_IN_PORT:
1033         return flow->in_port;
1034
1035     case NFI_NXM_OF_ETH_DST:
1036         return eth_addr_to_uint64(flow->dl_dst);
1037
1038     case NFI_NXM_OF_ETH_SRC:
1039         return eth_addr_to_uint64(flow->dl_src);
1040
1041     case NFI_NXM_OF_ETH_TYPE:
1042         return ntohs(ofputil_dl_type_to_openflow(flow->dl_type));
1043
1044     case NFI_NXM_OF_VLAN_TCI:
1045         return ntohs(flow->vlan_tci);
1046
1047     case NFI_NXM_OF_IP_TOS:
1048         return flow->tos & IP_DSCP_MASK;
1049
1050     case NFI_NXM_NX_IP_FRAG:
1051         return flow->frag;
1052
1053     case NFI_NXM_OF_IP_PROTO:
1054     case NFI_NXM_OF_ARP_OP:
1055         return flow->nw_proto;
1056
1057     case NFI_NXM_OF_IP_SRC:
1058     case NFI_NXM_OF_ARP_SPA:
1059         return ntohl(flow->nw_src);
1060
1061     case NFI_NXM_OF_IP_DST:
1062     case NFI_NXM_OF_ARP_TPA:
1063         return ntohl(flow->nw_dst);
1064
1065     case NFI_NXM_OF_TCP_SRC:
1066     case NFI_NXM_OF_UDP_SRC:
1067         return ntohs(flow->tp_src);
1068
1069     case NFI_NXM_OF_TCP_DST:
1070     case NFI_NXM_OF_UDP_DST:
1071         return ntohs(flow->tp_dst);
1072
1073     case NFI_NXM_OF_ICMP_TYPE:
1074     case NFI_NXM_NX_ICMPV6_TYPE:
1075         return ntohs(flow->tp_src) & 0xff;
1076
1077     case NFI_NXM_OF_ICMP_CODE:
1078     case NFI_NXM_NX_ICMPV6_CODE:
1079         return ntohs(flow->tp_dst) & 0xff;
1080
1081     case NFI_NXM_NX_TUN_ID:
1082         return ntohll(flow->tun_id);
1083
1084     case NFI_NXM_NX_IPV6_LABEL:
1085         return ntohl(flow->ipv6_label);
1086
1087 #define NXM_READ_REGISTER(IDX)                  \
1088     case NFI_NXM_NX_REG##IDX:                   \
1089         return flow->regs[IDX];                 \
1090     case NFI_NXM_NX_REG##IDX##_W:               \
1091         NOT_REACHED();
1092
1093     NXM_READ_REGISTER(0);
1094 #if FLOW_N_REGS >= 2
1095     NXM_READ_REGISTER(1);
1096 #endif
1097 #if FLOW_N_REGS >= 3
1098     NXM_READ_REGISTER(2);
1099 #endif
1100 #if FLOW_N_REGS >= 4
1101     NXM_READ_REGISTER(3);
1102 #endif
1103 #if FLOW_N_REGS >= 5
1104     NXM_READ_REGISTER(4);
1105 #endif
1106 #if FLOW_N_REGS > 5
1107 #error
1108 #endif
1109
1110     case NFI_NXM_NX_ARP_SHA:
1111     case NFI_NXM_NX_ND_SLL:
1112         return eth_addr_to_uint64(flow->arp_sha);
1113
1114     case NFI_NXM_NX_ARP_THA:
1115     case NFI_NXM_NX_ND_TLL:
1116         return eth_addr_to_uint64(flow->arp_tha);
1117
1118     case NFI_NXM_NX_TUN_ID_W:
1119     case NFI_NXM_OF_ETH_DST_W:
1120     case NFI_NXM_OF_VLAN_TCI_W:
1121     case NFI_NXM_OF_IP_SRC_W:
1122     case NFI_NXM_OF_IP_DST_W:
1123     case NFI_NXM_OF_ARP_SPA_W:
1124     case NFI_NXM_OF_ARP_TPA_W:
1125     case NFI_NXM_NX_IPV6_SRC:
1126     case NFI_NXM_NX_IPV6_SRC_W:
1127     case NFI_NXM_NX_IPV6_DST:
1128     case NFI_NXM_NX_IPV6_DST_W:
1129     case NFI_NXM_NX_IP_FRAG_W:
1130     case NFI_NXM_NX_ND_TARGET:
1131     case N_NXM_FIELDS:
1132         NOT_REACHED();
1133     }
1134
1135     NOT_REACHED();
1136 }
1137
1138 /* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
1139  * in 'flow'. */
1140 uint64_t
1141 nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
1142                     const struct flow *flow)
1143 {
1144     int n_bits = nxm_decode_n_bits(ofs_nbits);
1145     int ofs = nxm_decode_ofs(ofs_nbits);
1146     uint64_t mask, data;
1147
1148     mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1149     data = nxm_read_field(nxm_field_lookup(ntohl(header)), flow);
1150     data = (data >> ofs) & mask;
1151
1152     return data;
1153 }
1154
1155 static void
1156 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
1157                 uint64_t new_value)
1158 {
1159     switch (dst->index) {
1160     case NFI_NXM_OF_ETH_DST:
1161         eth_addr_from_uint64(new_value, flow->dl_dst);
1162         break;
1163
1164     case NFI_NXM_OF_ETH_SRC:
1165         eth_addr_from_uint64(new_value, flow->dl_src);
1166         break;
1167
1168     case NFI_NXM_OF_VLAN_TCI:
1169         flow->vlan_tci = htons(new_value);
1170         break;
1171
1172     case NFI_NXM_NX_TUN_ID:
1173         flow->tun_id = htonll(new_value);
1174         break;
1175
1176 #define NXM_WRITE_REGISTER(IDX)                 \
1177     case NFI_NXM_NX_REG##IDX:                   \
1178         flow->regs[IDX] = new_value;            \
1179         break;                                  \
1180     case NFI_NXM_NX_REG##IDX##_W:               \
1181         NOT_REACHED();
1182
1183     NXM_WRITE_REGISTER(0);
1184 #if FLOW_N_REGS >= 2
1185     NXM_WRITE_REGISTER(1);
1186 #endif
1187 #if FLOW_N_REGS >= 3
1188     NXM_WRITE_REGISTER(2);
1189 #endif
1190 #if FLOW_N_REGS >= 4
1191     NXM_WRITE_REGISTER(3);
1192 #endif
1193 #if FLOW_N_REGS >= 5
1194     NXM_WRITE_REGISTER(4);
1195 #endif
1196 #if FLOW_N_REGS > 5
1197 #error
1198 #endif
1199
1200     case NFI_NXM_OF_IP_TOS:
1201         flow->tos &= ~IP_DSCP_MASK;
1202         flow->tos |= new_value & IP_DSCP_MASK;
1203         break;
1204
1205     case NFI_NXM_NX_IP_FRAG:
1206         flow->frag = new_value;
1207         break;
1208
1209     case NFI_NXM_OF_IP_SRC:
1210         flow->nw_src = htonl(new_value);
1211         break;
1212
1213     case NFI_NXM_OF_IP_DST:
1214         flow->nw_dst = htonl(new_value);
1215         break;
1216
1217     case NFI_NXM_NX_IPV6_LABEL:
1218         flow->ipv6_label = htonl(new_value);
1219         break;
1220
1221     case NFI_NXM_OF_TCP_SRC:
1222     case NFI_NXM_OF_UDP_SRC:
1223         flow->tp_src = htons(new_value);
1224         break;
1225
1226     case NFI_NXM_OF_TCP_DST:
1227     case NFI_NXM_OF_UDP_DST:
1228         flow->tp_dst = htons(new_value);
1229         break;
1230
1231     case NFI_NXM_OF_IN_PORT:
1232     case NFI_NXM_OF_ETH_TYPE:
1233     case NFI_NXM_OF_IP_PROTO:
1234     case NFI_NXM_OF_ARP_OP:
1235     case NFI_NXM_OF_ARP_SPA:
1236     case NFI_NXM_OF_ARP_TPA:
1237     case NFI_NXM_OF_ICMP_TYPE:
1238     case NFI_NXM_OF_ICMP_CODE:
1239     case NFI_NXM_NX_TUN_ID_W:
1240     case NFI_NXM_OF_ETH_DST_W:
1241     case NFI_NXM_OF_VLAN_TCI_W:
1242     case NFI_NXM_OF_IP_SRC_W:
1243     case NFI_NXM_OF_IP_DST_W:
1244     case NFI_NXM_OF_ARP_SPA_W:
1245     case NFI_NXM_OF_ARP_TPA_W:
1246     case NFI_NXM_NX_ARP_SHA:
1247     case NFI_NXM_NX_ARP_THA:
1248     case NFI_NXM_NX_IPV6_SRC:
1249     case NFI_NXM_NX_IPV6_SRC_W:
1250     case NFI_NXM_NX_IPV6_DST:
1251     case NFI_NXM_NX_IPV6_DST_W:
1252     case NFI_NXM_NX_IP_FRAG_W:
1253     case NFI_NXM_NX_ICMPV6_TYPE:
1254     case NFI_NXM_NX_ICMPV6_CODE:
1255     case NFI_NXM_NX_ND_TARGET:
1256     case NFI_NXM_NX_ND_SLL:
1257     case NFI_NXM_NX_ND_TLL:
1258     case N_NXM_FIELDS:
1259         NOT_REACHED();
1260     }
1261 }
1262
1263 void
1264 nxm_execute_reg_move(const struct nx_action_reg_move *action,
1265                      struct flow *flow)
1266 {
1267     ovs_be16 src_ofs_nbits, dst_ofs_nbits;
1268     uint64_t src_data;
1269     int n_bits;
1270
1271     n_bits = ntohs(action->n_bits);
1272     src_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->src_ofs), n_bits);
1273     dst_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits);
1274
1275     src_data = nxm_read_field_bits(action->src, src_ofs_nbits, flow);
1276     nxm_reg_load(action->dst, dst_ofs_nbits, src_data, flow);
1277 }
1278
1279 void
1280 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1281                      struct flow *flow)
1282 {
1283     nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
1284 }
1285
1286 /* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
1287  * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
1288 void
1289 nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
1290              struct flow *flow)
1291 {
1292     int n_bits = nxm_decode_n_bits(ofs_nbits);
1293     int dst_ofs = nxm_decode_ofs(ofs_nbits);
1294     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
1295
1296     /* Get remaining bits of the destination field. */
1297     const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
1298     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
1299
1300     /* Get the final value. */
1301     uint64_t new_data = dst_data | (src_data << dst_ofs);
1302
1303     nxm_write_field(dst, flow, new_data);
1304 }