flow: Fully separate flow_wildcards from OpenFlow wildcard bits.
[sliver-openvswitch.git] / lib / nx-match.c
1 /*
2  * Copyright (c) 2010 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 "classifier.h"
22 #include "dynamic-string.h"
23 #include "ofp-util.h"
24 #include "ofpbuf.h"
25 #include "openflow/nicira-ext.h"
26 #include "packets.h"
27 #include "unaligned.h"
28 #include "vlog.h"
29
30 VLOG_DEFINE_THIS_MODULE(nx_match);
31
32 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
33  * peer and so there's not much point in showing a lot of them. */
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35
36 enum {
37     NXM_INVALID = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
38     NXM_BAD_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
39     NXM_BAD_VALUE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
40     NXM_BAD_MASK = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
41     NXM_BAD_PREREQ = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
42     NXM_DUP_TYPE = OFP_MKERR_NICIRA(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
43     BAD_ARGUMENT = OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)
44 };
45
46 /* For each NXM_* field, define NFI_NXM_* as consecutive integers starting from
47  * zero. */
48 enum nxm_field_index {
49 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) NFI_NXM_##HEADER,
50 #include "nx-match.def"
51     N_NXM_FIELDS
52 };
53
54 struct nxm_field {
55     struct hmap_node hmap_node;
56     enum nxm_field_index index; /* NFI_* value. */
57     uint32_t header;            /* NXM_* value. */
58     flow_wildcards_t wildcard;  /* FWW_* bit, if exactly one. */
59     ovs_be16 dl_type;           /* dl_type prerequisite, if nonzero. */
60     uint8_t nw_proto;           /* nw_proto prerequisite, if nonzero. */
61     const char *name;           /* "NXM_*" string. */
62 };
63
64 /* All the known fields. */
65 static struct nxm_field nxm_fields[N_NXM_FIELDS] = {
66 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
67     { HMAP_NODE_NULL_INITIALIZER, NFI_NXM_##HEADER, NXM_##HEADER, WILDCARD, \
68       CONSTANT_HTONS(DL_TYPE), NW_PROTO, "NXM_" #HEADER },
69 #include "nx-match.def"
70 };
71
72 /* Hash table of 'nxm_fields'. */
73 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
74
75 /* Possible masks for NXM_OF_ETH_DST_W. */
76 static const uint8_t eth_all_0s[ETH_ADDR_LEN]
77     = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
78 static const uint8_t eth_all_1s[ETH_ADDR_LEN]
79     = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
80 static const uint8_t eth_mcast_1[ETH_ADDR_LEN]
81     = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
82 static const uint8_t eth_mcast_0[ETH_ADDR_LEN]
83     = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
84
85 static void
86 nxm_init(void)
87 {
88     if (hmap_is_empty(&all_nxm_fields)) {
89         int i;
90
91         for (i = 0; i < N_NXM_FIELDS; i++) {
92             struct nxm_field *f = &nxm_fields[i];
93             hmap_insert(&all_nxm_fields, &f->hmap_node,
94                         hash_int(f->header, 0));
95         }
96
97         /* Verify that the header values are unique (duplicate "case" values
98          * cause a compile error). */
99         switch (0) {
100 #define DEFINE_FIELD(HEADER, WILDCARD, DL_TYPE, NW_PROTO) \
101         case NXM_##HEADER: break;
102 #include "nx-match.def"
103         }
104     }
105 }
106
107 static const struct nxm_field *
108 nxm_field_lookup(uint32_t header)
109 {
110     struct nxm_field *f;
111
112     nxm_init();
113
114     HMAP_FOR_EACH_WITH_HASH (f, hmap_node, hash_int(header, 0),
115                              &all_nxm_fields) {
116         if (f->header == header) {
117             return f;
118         }
119     }
120
121     return NULL;
122 }
123
124 /* Returns the width of the data for a field with the given 'header', in
125  * bytes. */
126 static int
127 nxm_field_bytes(uint32_t header)
128 {
129     unsigned int length = NXM_LENGTH(header);
130     return NXM_HASMASK(header) ? length / 2 : length;
131 }
132
133 /* Returns the width of the data for a field with the given 'header', in
134  * bits. */
135 static int
136 nxm_field_bits(uint32_t header)
137 {
138     return nxm_field_bytes(header) * 8;
139 }
140 \f
141 /* nx_pull_match() and helpers. */
142
143 static int
144 parse_tci(struct cls_rule *rule, ovs_be16 tci, ovs_be16 mask)
145 {
146     const flow_wildcards_t FWW_DL_TCI = FWW_DL_VLAN | FWW_DL_VLAN_PCP;
147
148     if ((rule->wc.wildcards & FWW_DL_TCI) != FWW_DL_TCI) {
149         return NXM_DUP_TYPE;
150     } else {
151         return cls_rule_set_dl_tci_masked(rule, tci, mask) ? 0 : NXM_INVALID;
152     }
153 }
154
155 static int
156 parse_nx_reg(const struct nxm_field *f,
157              struct flow *flow, struct flow_wildcards *wc,
158              const void *value, const void *maskp)
159 {
160     int idx = NXM_NX_REG_IDX(f->header);
161     if (wc->reg_masks[idx]) {
162         return NXM_DUP_TYPE;
163     } else {
164         flow_wildcards_set_reg_mask(wc, idx,
165                                     (NXM_HASMASK(f->header)
166                                      ? ntohl(get_unaligned_u32(maskp))
167                                      : UINT32_MAX));
168         flow->regs[idx] = ntohl(get_unaligned_u32(value));
169         flow->regs[idx] &= wc->reg_masks[idx];
170         return 0;
171     }
172 }
173
174 static int
175 parse_nxm_entry(struct cls_rule *rule, const struct nxm_field *f,
176                 const void *value, const void *mask)
177 {
178     struct flow_wildcards *wc = &rule->wc;
179     struct flow *flow = &rule->flow;
180
181     switch (f->index) {
182         /* Metadata. */
183     case NFI_NXM_OF_IN_PORT:
184         flow->in_port = ntohs(get_unaligned_u16(value));
185         if (flow->in_port == OFPP_LOCAL) {
186             flow->in_port = ODPP_LOCAL;
187         }
188         return 0;
189
190         /* Ethernet header. */
191     case NFI_NXM_OF_ETH_DST:
192         if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
193             != (FWW_DL_DST | FWW_ETH_MCAST)) {
194             return NXM_DUP_TYPE;
195         } else {
196             wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
197             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
198             return 0;
199         }
200     case NFI_NXM_OF_ETH_DST_W:
201         if ((wc->wildcards & (FWW_DL_DST | FWW_ETH_MCAST))
202             != (FWW_DL_DST | FWW_ETH_MCAST)) {
203             return NXM_DUP_TYPE;
204         } else if (eth_addr_equals(mask, eth_mcast_1)) {
205             wc->wildcards &= ~FWW_ETH_MCAST;
206             flow->dl_dst[0] = *(uint8_t *) value & 0x01;
207         } else if (eth_addr_equals(mask, eth_mcast_0)) {
208             wc->wildcards &= ~FWW_DL_DST;
209             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
210             flow->dl_dst[0] &= 0xfe;
211         } else if (eth_addr_equals(mask, eth_all_0s)) {
212             return 0;
213         } else if (eth_addr_equals(mask, eth_all_1s)) {
214             wc->wildcards &= ~(FWW_DL_DST | FWW_ETH_MCAST);
215             memcpy(flow->dl_dst, value, ETH_ADDR_LEN);
216             return 0;
217         } else {
218             return NXM_BAD_MASK;
219         }
220     case NFI_NXM_OF_ETH_SRC:
221         memcpy(flow->dl_src, value, ETH_ADDR_LEN);
222         return 0;
223     case NFI_NXM_OF_ETH_TYPE:
224         flow->dl_type = get_unaligned_u16(value);
225         return 0;
226
227         /* 802.1Q header. */
228     case NFI_NXM_OF_VLAN_TCI:
229         return parse_tci(rule, get_unaligned_u16(value), htons(UINT16_MAX));
230
231     case NFI_NXM_OF_VLAN_TCI_W:
232         return parse_tci(rule, get_unaligned_u16(value),
233                          get_unaligned_u16(mask));
234
235         /* IP header. */
236     case NFI_NXM_OF_IP_TOS:
237         if (*(uint8_t *) value & 0x03) {
238             return NXM_BAD_VALUE;
239         } else {
240             flow->nw_tos = *(uint8_t *) value;
241             return 0;
242         }
243     case NFI_NXM_OF_IP_PROTO:
244         flow->nw_proto = *(uint8_t *) value;
245         return 0;
246
247         /* IP addresses in IP and ARP headers. */
248     case NFI_NXM_OF_IP_SRC:
249     case NFI_NXM_OF_ARP_SPA:
250         if (wc->nw_src_mask) {
251             return NXM_DUP_TYPE;
252         } else {
253             cls_rule_set_nw_src(rule, get_unaligned_u32(value));
254             return 0;
255         }
256     case NFI_NXM_OF_IP_SRC_W:
257     case NFI_NXM_OF_ARP_SPA_W:
258         if (wc->nw_src_mask) {
259             return NXM_DUP_TYPE;
260         } else {
261             ovs_be32 ip = get_unaligned_u32(value);
262             ovs_be32 netmask = get_unaligned_u32(mask);
263             if (!cls_rule_set_nw_src_masked(rule, ip, netmask)) {
264                 return NXM_BAD_MASK;
265             }
266             return 0;
267         }
268     case NFI_NXM_OF_IP_DST:
269     case NFI_NXM_OF_ARP_TPA:
270         if (wc->nw_dst_mask) {
271             return NXM_DUP_TYPE;
272         } else {
273             cls_rule_set_nw_dst(rule, get_unaligned_u32(value));
274             return 0;
275         }
276     case NFI_NXM_OF_IP_DST_W:
277     case NFI_NXM_OF_ARP_TPA_W:
278         if (wc->nw_dst_mask) {
279             return NXM_DUP_TYPE;
280         } else {
281             ovs_be32 ip = get_unaligned_u32(value);
282             ovs_be32 netmask = get_unaligned_u32(mask);
283             if (!cls_rule_set_nw_dst_masked(rule, ip, netmask)) {
284                 return NXM_BAD_MASK;
285             }
286             return 0;
287         }
288
289         /* TCP header. */
290     case NFI_NXM_OF_TCP_SRC:
291         flow->tp_src = get_unaligned_u16(value);
292         return 0;
293     case NFI_NXM_OF_TCP_DST:
294         flow->tp_dst = get_unaligned_u16(value);
295         return 0;
296
297         /* UDP header. */
298     case NFI_NXM_OF_UDP_SRC:
299         flow->tp_src = get_unaligned_u16(value);
300         return 0;
301     case NFI_NXM_OF_UDP_DST:
302         flow->tp_dst = get_unaligned_u16(value);
303         return 0;
304
305         /* ICMP header. */
306     case NFI_NXM_OF_ICMP_TYPE:
307         flow->tp_src = htons(*(uint8_t *) value);
308         return 0;
309     case NFI_NXM_OF_ICMP_CODE:
310         flow->tp_dst = htons(*(uint8_t *) value);
311         return 0;
312
313         /* ARP header. */
314     case NFI_NXM_OF_ARP_OP:
315         if (ntohs(get_unaligned_u16(value)) > 255) {
316             return NXM_BAD_VALUE;
317         } else {
318             flow->nw_proto = ntohs(get_unaligned_u16(value));
319             return 0;
320         }
321
322         /* Tunnel ID. */
323     case NFI_NXM_NX_TUN_ID:
324         flow->tun_id = htonl(ntohll(get_unaligned_u64(value)));
325         return 0;
326
327         /* Registers. */
328     case NFI_NXM_NX_REG0:
329     case NFI_NXM_NX_REG0_W:
330 #if FLOW_N_REGS >= 2
331     case NFI_NXM_NX_REG1:
332     case NFI_NXM_NX_REG1_W:
333 #endif
334 #if FLOW_N_REGS >= 3
335     case NFI_NXM_NX_REG2:
336     case NFI_NXM_NX_REG2_W:
337 #endif
338 #if FLOW_N_REGS >= 4
339     case NFI_NXM_NX_REG3:
340     case NFI_NXM_NX_REG3_W:
341 #endif
342 #if FLOW_N_REGS > 4
343 #error
344 #endif
345         return parse_nx_reg(f, flow, wc, value, mask);
346
347     case N_NXM_FIELDS:
348         NOT_REACHED();
349     }
350     NOT_REACHED();
351 }
352
353 static bool
354 nxm_prereqs_ok(const struct nxm_field *field, const struct flow *flow)
355 {
356     return (!field->dl_type
357             || (field->dl_type == flow->dl_type
358                 && (!field->nw_proto || field->nw_proto == flow->nw_proto)));
359 }
360
361 static uint32_t
362 nx_entry_ok(const void *p, unsigned int match_len)
363 {
364     unsigned int payload_len;
365     ovs_be32 header_be;
366     uint32_t header;
367
368     if (match_len < 4) {
369         if (match_len) {
370             VLOG_DBG_RL(&rl, "nx_match ends with partial nxm_header");
371         }
372         return 0;
373     }
374     memcpy(&header_be, p, 4);
375     header = ntohl(header_be);
376
377     payload_len = NXM_LENGTH(header);
378     if (!payload_len) {
379         VLOG_DBG_RL(&rl, "nxm_entry %08"PRIx32" has invalid payload "
380                     "length 0", header);
381         return 0;
382     }
383     if (match_len < payload_len + 4) {
384         VLOG_DBG_RL(&rl, "%"PRIu32"-byte nxm_entry but only "
385                     "%u bytes left in nx_match", payload_len + 4, match_len);
386         return 0;
387     }
388
389     return header;
390 }
391
392 int
393 nx_pull_match(struct ofpbuf *b, unsigned int match_len, uint16_t priority,
394               struct cls_rule *rule)
395 {
396     uint32_t header;
397     uint8_t *p;
398
399     p = ofpbuf_try_pull(b, ROUND_UP(match_len, 8));
400     if (!p) {
401         VLOG_DBG_RL(&rl, "nx_match length %zu, rounded up to a "
402                     "multiple of 8, is longer than space in message (max "
403                     "length %zu)", match_len, b->size);
404         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
405     }
406
407     cls_rule_init_catchall(rule, priority);
408     while ((header = nx_entry_ok(p, match_len)) != 0) {
409         unsigned length = NXM_LENGTH(header);
410         const struct nxm_field *f;
411         int error;
412
413         f = nxm_field_lookup(header);
414         if (!f) {
415             error = NXM_BAD_TYPE;
416         } else if (!nxm_prereqs_ok(f, &rule->flow)) {
417             error = NXM_BAD_PREREQ;
418         } else if (f->wildcard && !(rule->wc.wildcards & f->wildcard)) {
419             error = NXM_DUP_TYPE;
420         } else {
421             /* 'hasmask' and 'length' are known to be correct at this point
422              * because they are included in 'header' and nxm_field_lookup()
423              * checked them already. */
424             rule->wc.wildcards &= ~f->wildcard;
425             error = parse_nxm_entry(rule, f, p + 4, p + 4 + length / 2);
426         }
427         if (error) {
428             VLOG_DBG_RL(&rl, "bad nxm_entry with vendor=%"PRIu32", "
429                         "field=%"PRIu32", hasmask=%"PRIu32", type=%"PRIu32" "
430                         "(error %x)",
431                         NXM_VENDOR(header), NXM_FIELD(header),
432                         NXM_HASMASK(header), NXM_TYPE(header),
433                         error);
434             return error;
435         }
436
437
438         p += 4 + length;
439         match_len -= 4 + length;
440     }
441
442     return match_len ? NXM_INVALID : 0;
443 }
444 \f
445 /* nx_put_match() and helpers.
446  *
447  * 'put' functions whose names end in 'w' add a wildcarded field.
448  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
449  * Other 'put' functions add exact-match fields.
450  */
451
452 static void
453 nxm_put_header(struct ofpbuf *b, uint32_t header)
454 {
455     ovs_be32 n_header = htonl(header);
456     ofpbuf_put(b, &n_header, sizeof n_header);
457 }
458
459 static void
460 nxm_put_8(struct ofpbuf *b, uint32_t header, uint8_t value)
461 {
462     nxm_put_header(b, header);
463     ofpbuf_put(b, &value, sizeof value);
464 }
465
466 static void
467 nxm_put_16(struct ofpbuf *b, uint32_t header, ovs_be16 value)
468 {
469     nxm_put_header(b, header);
470     ofpbuf_put(b, &value, sizeof value);
471 }
472
473 static void
474 nxm_put_16w(struct ofpbuf *b, uint32_t header, ovs_be16 value, ovs_be16 mask)
475 {
476     nxm_put_header(b, header);
477     ofpbuf_put(b, &value, sizeof value);
478     ofpbuf_put(b, &mask, sizeof mask);
479 }
480
481 static void
482 nxm_put_32(struct ofpbuf *b, uint32_t header, ovs_be32 value)
483 {
484     nxm_put_header(b, header);
485     ofpbuf_put(b, &value, sizeof value);
486 }
487
488 static void
489 nxm_put_32w(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
490 {
491     nxm_put_header(b, header);
492     ofpbuf_put(b, &value, sizeof value);
493     ofpbuf_put(b, &mask, sizeof mask);
494 }
495
496 static void
497 nxm_put_32m(struct ofpbuf *b, uint32_t header, ovs_be32 value, ovs_be32 mask)
498 {
499     switch (mask) {
500     case 0:
501         break;
502
503     case UINT32_MAX:
504         nxm_put_32(b, header, value);
505         break;
506
507     default:
508         nxm_put_32w(b, NXM_MAKE_WILD_HEADER(header), value, mask);
509         break;
510     }
511 }
512
513 static void
514 nxm_put_64(struct ofpbuf *b, uint32_t header, ovs_be64 value)
515 {
516     nxm_put_header(b, header);
517     ofpbuf_put(b, &value, sizeof value);
518 }
519
520 static void
521 nxm_put_eth(struct ofpbuf *b, uint32_t header,
522             const uint8_t value[ETH_ADDR_LEN])
523 {
524     nxm_put_header(b, header);
525     ofpbuf_put(b, value, ETH_ADDR_LEN);
526 }
527
528 static void
529 nxm_put_eth_dst(struct ofpbuf *b,
530                 uint32_t wc, const uint8_t value[ETH_ADDR_LEN])
531 {
532     switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
533     case FWW_DL_DST | FWW_ETH_MCAST:
534         break;
535     case FWW_DL_DST:
536         nxm_put_header(b, NXM_OF_ETH_DST_W);
537         ofpbuf_put(b, value, ETH_ADDR_LEN);
538         ofpbuf_put(b, eth_mcast_1, ETH_ADDR_LEN);
539         break;
540     case FWW_ETH_MCAST:
541         nxm_put_header(b, NXM_OF_ETH_DST_W);
542         ofpbuf_put(b, value, ETH_ADDR_LEN);
543         ofpbuf_put(b, eth_mcast_0, ETH_ADDR_LEN);
544         break;
545     case 0:
546         nxm_put_eth(b, NXM_OF_ETH_DST, value);
547         break;
548     }
549 }
550
551 int
552 nx_put_match(struct ofpbuf *b, const struct cls_rule *cr)
553 {
554     const flow_wildcards_t wc = cr->wc.wildcards;
555     const struct flow *flow = &cr->flow;
556     const size_t start_len = b->size;
557     ovs_be16 vid, pcp;
558     int match_len;
559     int i;
560
561     /* Metadata. */
562     if (!(wc & FWW_IN_PORT)) {
563         uint16_t in_port = flow->in_port;
564         if (in_port == ODPP_LOCAL) {
565             in_port = OFPP_LOCAL;
566         }
567         nxm_put_16(b, NXM_OF_IN_PORT, htons(in_port));
568     }
569
570     /* Ethernet. */
571     nxm_put_eth_dst(b, wc, flow->dl_dst);
572     if (!(wc & FWW_DL_SRC)) {
573         nxm_put_eth(b, NXM_OF_ETH_SRC, flow->dl_src);
574     }
575     if (!(wc & FWW_DL_TYPE)) {
576         nxm_put_16(b, NXM_OF_ETH_TYPE, flow->dl_type);
577     }
578
579     /* 802.1Q. */
580     vid = flow->dl_vlan & htons(VLAN_VID_MASK);
581     pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
582     switch (wc & (FWW_DL_VLAN | FWW_DL_VLAN_PCP)) {
583     case FWW_DL_VLAN | FWW_DL_VLAN_PCP:
584         break;
585     case FWW_DL_VLAN:
586         nxm_put_16w(b, NXM_OF_VLAN_TCI_W, pcp | htons(VLAN_CFI),
587                      htons(VLAN_PCP_MASK | VLAN_CFI));
588         break;
589     case FWW_DL_VLAN_PCP:
590         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
591             nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
592         } else {
593             nxm_put_16w(b, NXM_OF_VLAN_TCI_W, vid | htons(VLAN_CFI),
594                          htons(VLAN_VID_MASK | VLAN_CFI));
595         }
596         break;
597     case 0:
598         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
599             nxm_put_16(b, NXM_OF_VLAN_TCI, 0);
600         } else {
601             nxm_put_16(b, NXM_OF_VLAN_TCI, vid | pcp | htons(VLAN_CFI));
602         }
603         break;
604     }
605
606     if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_IP)) {
607         /* IP. */
608         if (!(wc & FWW_NW_TOS)) {
609             nxm_put_8(b, NXM_OF_IP_TOS, flow->nw_tos & 0xfc);
610         }
611         nxm_put_32m(b, NXM_OF_IP_SRC, flow->nw_src, cr->wc.nw_src_mask);
612         nxm_put_32m(b, NXM_OF_IP_DST, flow->nw_dst, cr->wc.nw_dst_mask);
613
614         if (!(wc & FWW_NW_PROTO)) {
615             nxm_put_8(b, NXM_OF_IP_PROTO, flow->nw_proto);
616             switch (flow->nw_proto) {
617                 /* TCP. */
618             case IP_TYPE_TCP:
619                 if (!(wc & FWW_TP_SRC)) {
620                     nxm_put_16(b, NXM_OF_TCP_SRC, flow->tp_src);
621                 }
622                 if (!(wc & FWW_TP_DST)) {
623                     nxm_put_16(b, NXM_OF_TCP_DST, flow->tp_dst);
624                 }
625                 break;
626
627                 /* UDP. */
628             case IP_TYPE_UDP:
629                 if (!(wc & FWW_TP_SRC)) {
630                     nxm_put_16(b, NXM_OF_UDP_SRC, flow->tp_src);
631                 }
632                 if (!(wc & FWW_TP_DST)) {
633                     nxm_put_16(b, NXM_OF_UDP_DST, flow->tp_dst);
634                 }
635                 break;
636
637                 /* ICMP. */
638             case IP_TYPE_ICMP:
639                 if (!(wc & FWW_TP_SRC)) {
640                     nxm_put_8(b, NXM_OF_ICMP_TYPE, ntohs(flow->tp_src));
641                 }
642                 if (!(wc & FWW_TP_DST)) {
643                     nxm_put_8(b, NXM_OF_ICMP_CODE, ntohs(flow->tp_dst));
644                 }
645                 break;
646             }
647         }
648     } else if (!(wc & FWW_DL_TYPE) && flow->dl_type == htons(ETH_TYPE_ARP)) {
649         /* ARP. */
650         if (!(wc & FWW_NW_PROTO)) {
651             nxm_put_16(b, NXM_OF_ARP_OP, htons(flow->nw_proto));
652         }
653         nxm_put_32m(b, NXM_OF_ARP_SPA, flow->nw_src, cr->wc.nw_src_mask);
654         nxm_put_32m(b, NXM_OF_ARP_TPA, flow->nw_dst, cr->wc.nw_dst_mask);
655     }
656
657     /* Tunnel ID. */
658     if (!(wc & FWW_TUN_ID)) {
659         nxm_put_64(b, NXM_NX_TUN_ID, htonll(ntohl(flow->tun_id)));
660     }
661
662     /* Registers. */
663     for (i = 0; i < FLOW_N_REGS; i++) {
664         nxm_put_32m(b, NXM_NX_REG(i),
665                     htonl(flow->regs[i]), htonl(cr->wc.reg_masks[i]));
666     }
667
668     match_len = b->size - start_len;
669     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
670     return match_len;
671 }
672 \f
673 /* nx_match_to_string() and helpers. */
674
675 char *
676 nx_match_to_string(const uint8_t *p, unsigned int match_len)
677 {
678     uint32_t header;
679     struct ds s;
680
681     if (!match_len) {
682         return xstrdup("<any>");
683     }
684
685     ds_init(&s);
686     while ((header = nx_entry_ok(p, match_len)) != 0) {
687         unsigned int length = NXM_LENGTH(header);
688         unsigned int value_len = nxm_field_bytes(header);
689         const uint8_t *value = p + 4;
690         const uint8_t *mask = value + value_len;
691         const struct nxm_field *f;
692         unsigned int i;
693
694         if (s.length) {
695             ds_put_cstr(&s, ", ");
696         }
697
698         f = nxm_field_lookup(header);
699         if (f) {
700             ds_put_cstr(&s, f->name);
701         } else {
702             ds_put_format(&s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
703         }
704
705         ds_put_char(&s, '(');
706
707         for (i = 0; i < value_len; i++) {
708             ds_put_format(&s, "%02x", value[i]);
709         }
710         if (NXM_HASMASK(header)) {
711             ds_put_char(&s, '/');
712             for (i = 0; i < value_len; i++) {
713                 ds_put_format(&s, "%02x", mask[i]);
714             }
715         }
716         ds_put_char(&s, ')');
717
718         p += 4 + length;
719         match_len -= 4 + length;
720     }
721
722     if (match_len) {
723         if (s.length) {
724             ds_put_cstr(&s, ", ");
725         }
726
727         ds_put_format(&s, "<%u invalid bytes>", match_len);
728     }
729
730     return ds_steal_cstr(&s);
731 }
732
733 static const struct nxm_field *
734 lookup_nxm_field(const char *name, int name_len)
735 {
736     const struct nxm_field *f;
737
738     for (f = nxm_fields; f < &nxm_fields[ARRAY_SIZE(nxm_fields)]; f++) {
739         if (!strncmp(f->name, name, name_len) && f->name[name_len] == '\0') {
740             return f;
741         }
742     }
743
744     return NULL;
745 }
746
747 static const char *
748 parse_hex_bytes(struct ofpbuf *b, const char *s, unsigned int n)
749 {
750     while (n--) {
751         uint8_t byte;
752         bool ok;
753
754         s += strspn(s, " ");
755         byte = hexits_value(s, 2, &ok);
756         if (!ok) {
757             ovs_fatal(0, "%.2s: hex digits expected", s);
758         }
759
760         ofpbuf_put(b, &byte, 1);
761         s += 2;
762     }
763     return s;
764 }
765 \f
766 /* nx_match_from_string(). */
767
768 int
769 nx_match_from_string(const char *s, struct ofpbuf *b)
770 {
771     const char *full_s = s;
772     const size_t start_len = b->size;
773     int match_len;
774
775     if (!strcmp(s, "<any>")) {
776         /* Ensure that 'b->data' isn't actually null. */
777         ofpbuf_prealloc_tailroom(b, 1);
778         return 0;
779     }
780
781     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
782         const struct nxm_field *f;
783         int name_len;
784
785         name_len = strcspn(s, "(");
786         if (s[name_len] != '(') {
787             ovs_fatal(0, "%s: missing ( at end of nx_match", full_s);
788         }
789
790         f = lookup_nxm_field(s, name_len);
791         if (!f) {
792             ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
793         }
794
795         s += name_len + 1;
796
797         nxm_put_header(b, f->header);
798         s = parse_hex_bytes(b, s, nxm_field_bytes(f->header));
799         if (NXM_HASMASK(f->header)) {
800             s += strspn(s, " ");
801             if (*s != '/') {
802                 ovs_fatal(0, "%s: missing / in masked field %s",
803                           full_s, f->name);
804             }
805             s = parse_hex_bytes(b, s + 1, nxm_field_bytes(f->header));
806         }
807
808         s += strspn(s, " ");
809         if (*s != ')') {
810             ovs_fatal(0, "%s: missing ) following field %s", full_s, f->name);
811         }
812         s++;
813     }
814
815     match_len = b->size - start_len;
816     ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
817     return match_len;
818 }
819 \f
820 /* nxm_check_reg_move(), nxm_check_reg_load(). */
821
822 static bool
823 field_ok(const struct nxm_field *f, const struct flow *flow, int size)
824 {
825     return (f && !NXM_HASMASK(f->header)
826             && nxm_prereqs_ok(f, flow) && size <= nxm_field_bits(f->header));
827 }
828
829 int
830 nxm_check_reg_move(const struct nx_action_reg_move *action,
831                    const struct flow *flow)
832 {
833     const struct nxm_field *src;
834     const struct nxm_field *dst;
835
836     if (action->n_bits == htons(0)) {
837         return BAD_ARGUMENT;
838     }
839
840     src = nxm_field_lookup(ntohl(action->src));
841     if (!field_ok(src, flow, ntohs(action->src_ofs) + ntohs(action->n_bits))) {
842         return BAD_ARGUMENT;
843     }
844
845     dst = nxm_field_lookup(ntohl(action->dst));
846     if (!field_ok(dst, flow, ntohs(action->dst_ofs) + ntohs(action->n_bits))) {
847         return BAD_ARGUMENT;
848     }
849
850     if (!NXM_IS_NX_REG(dst->header)
851         && dst->header != NXM_OF_VLAN_TCI
852         && dst->header != NXM_NX_TUN_ID) {
853         return BAD_ARGUMENT;
854     }
855
856     return 0;
857 }
858
859 int
860 nxm_check_reg_load(const struct nx_action_reg_load *action,
861                    const struct flow *flow)
862 {
863     const struct nxm_field *dst;
864     int ofs, n_bits;
865
866     ofs = ntohs(action->ofs_nbits) >> 6;
867     n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
868     dst = nxm_field_lookup(ntohl(action->dst));
869     if (!field_ok(dst, flow, ofs + n_bits)) {
870         return BAD_ARGUMENT;
871     }
872
873     /* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
874      * action->value. */
875     if (n_bits < 64 && ntohll(action->value) >> n_bits) {
876         return BAD_ARGUMENT;
877     }
878
879     if (!NXM_IS_NX_REG(dst->header)) {
880         return BAD_ARGUMENT;
881     }
882
883     return 0;
884 }
885 \f
886 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
887
888 static uint64_t
889 nxm_read_field(const struct nxm_field *src, const struct flow *flow)
890 {
891     switch (src->index) {
892     case NFI_NXM_OF_IN_PORT:
893         return flow->in_port == ODPP_LOCAL ? OFPP_LOCAL : flow->in_port;
894
895     case NFI_NXM_OF_ETH_DST:
896         return eth_addr_to_uint64(flow->dl_dst);
897
898     case NFI_NXM_OF_ETH_SRC:
899         return eth_addr_to_uint64(flow->dl_src);
900
901     case NFI_NXM_OF_ETH_TYPE:
902         return ntohs(flow->dl_type);
903
904     case NFI_NXM_OF_VLAN_TCI:
905         if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
906             return 0;
907         } else {
908             return (ntohs(flow->dl_vlan & htons(VLAN_VID_MASK))
909                     | ((flow->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK)
910                     | VLAN_CFI);
911         }
912
913     case NFI_NXM_OF_IP_TOS:
914         return flow->nw_tos;
915
916     case NFI_NXM_OF_IP_PROTO:
917     case NFI_NXM_OF_ARP_OP:
918         return flow->nw_proto;
919
920     case NFI_NXM_OF_IP_SRC:
921     case NFI_NXM_OF_ARP_SPA:
922         return ntohl(flow->nw_src);
923
924     case NFI_NXM_OF_IP_DST:
925     case NFI_NXM_OF_ARP_TPA:
926         return ntohl(flow->nw_dst);
927
928     case NFI_NXM_OF_TCP_SRC:
929     case NFI_NXM_OF_UDP_SRC:
930         return ntohs(flow->tp_src);
931
932     case NFI_NXM_OF_TCP_DST:
933     case NFI_NXM_OF_UDP_DST:
934         return ntohs(flow->tp_dst);
935
936     case NFI_NXM_OF_ICMP_TYPE:
937         return ntohs(flow->tp_src) & 0xff;
938
939     case NFI_NXM_OF_ICMP_CODE:
940         return ntohs(flow->tp_dst) & 0xff;
941
942     case NFI_NXM_NX_TUN_ID:
943         return ntohl(flow->tun_id);
944
945 #define NXM_READ_REGISTER(IDX)                  \
946     case NFI_NXM_NX_REG##IDX:                   \
947         return flow->regs[IDX];                 \
948     case NFI_NXM_NX_REG##IDX##_W:               \
949         NOT_REACHED();
950
951     NXM_READ_REGISTER(0);
952 #if FLOW_N_REGS >= 2
953     NXM_READ_REGISTER(1);
954 #endif
955 #if FLOW_N_REGS >= 3
956     NXM_READ_REGISTER(2);
957 #endif
958 #if FLOW_N_REGS >= 4
959     NXM_READ_REGISTER(3);
960 #endif
961 #if FLOW_N_REGS > 4
962 #error
963 #endif
964
965     case NFI_NXM_OF_ETH_DST_W:
966     case NFI_NXM_OF_VLAN_TCI_W:
967     case NFI_NXM_OF_IP_SRC_W:
968     case NFI_NXM_OF_IP_DST_W:
969     case NFI_NXM_OF_ARP_SPA_W:
970     case NFI_NXM_OF_ARP_TPA_W:
971     case N_NXM_FIELDS:
972         NOT_REACHED();
973     }
974
975     NOT_REACHED();
976 }
977
978 void
979 nxm_execute_reg_move(const struct nx_action_reg_move *action,
980                      struct flow *flow)
981 {
982     /* Preparation. */
983     int n_bits = ntohs(action->n_bits);
984     uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
985
986     /* Get the interesting bits of the source field. */
987     const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
988     int src_ofs = ntohs(action->src_ofs);
989     uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
990
991     /* Get the remaining bits of the destination field. */
992     const struct nxm_field *dst = nxm_field_lookup(ntohl(action->dst));
993     int dst_ofs = ntohs(action->dst_ofs);
994     uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
995
996     /* Get the final value. */
997     uint64_t new_data = dst_data | ((src_data >> src_ofs) << dst_ofs);
998
999     /* Store the result. */
1000     if (NXM_IS_NX_REG(dst->header)) {
1001         flow->regs[NXM_NX_REG_IDX(dst->header)] = new_data;
1002     } else if (dst->header == NXM_OF_VLAN_TCI) {
1003         ovs_be16 vlan_tci = htons(new_data & VLAN_CFI ? new_data : 0);
1004         flow->dl_vlan = htons(vlan_tci_to_vid(vlan_tci));
1005         flow->dl_vlan_pcp = vlan_tci_to_pcp(vlan_tci);
1006     } else if (dst->header == NXM_NX_TUN_ID) {
1007         flow->tun_id = htonl(new_data);
1008     } else {
1009         NOT_REACHED();
1010     }
1011 }
1012
1013 void
1014 nxm_execute_reg_load(const struct nx_action_reg_load *action,
1015                      struct flow *flow)
1016 {
1017     /* Preparation. */
1018     int n_bits = (ntohs(action->ofs_nbits) & 0x3f) + 1;
1019     uint32_t mask = n_bits == 32 ? UINT32_MAX : (UINT32_C(1) << n_bits) - 1;
1020     uint32_t *reg = &flow->regs[NXM_NX_REG_IDX(ntohl(action->dst))];
1021
1022     /* Get source data. */
1023     uint32_t src_data = ntohll(action->value);
1024
1025     /* Get remaining bits of the destination field. */
1026     int dst_ofs = ntohs(action->ofs_nbits) >> 6;
1027     uint32_t dst_data = *reg & ~(mask << dst_ofs);
1028
1029     *reg = dst_data | (src_data << dst_ofs);
1030 }