]> git.proxmox.com Git - mirror_ovs.git/blob - lib/match.c
Process RARP packets with ethertype 0x8035 similar to ARP packets.
[mirror_ovs.git] / lib / match.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "match.h"
19 #include <assert.h>
20 #include <stdlib.h>
21 #include "byte-order.h"
22 #include "dynamic-string.h"
23 #include "packets.h"
24 #include "vlog.h"
25
26 VLOG_DEFINE_THIS_MODULE(match);
27
28
29 /* Converts the flow in 'flow' into a match in 'match', with the given
30 * 'wildcards'. */
31 void
32 match_init(struct match *match,
33 const struct flow *flow, const struct flow_wildcards *wc)
34 {
35 match->flow = *flow;
36 match->wc = *wc;
37 match_zero_wildcarded_fields(match);
38 }
39
40 /* Converts a flow into a match. It sets the wildcard masks based on
41 * the packet contents. It will not set the mask for fields that do not
42 * make sense for the packet type. */
43 void
44 match_wc_init(struct match *match, const struct flow *flow)
45 {
46 struct flow_wildcards *wc;
47 int i;
48
49 match->flow = *flow;
50 wc = &match->wc;
51 memset(&wc->masks, 0x0, sizeof wc->masks);
52
53 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
54
55 if (flow->nw_proto) {
56 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
57 }
58
59 for (i = 0; i < FLOW_N_REGS; i++) {
60 if (flow->regs[i]) {
61 memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
62 }
63 }
64
65 if (flow->tunnel.ip_dst || flow->tunnel.tun_id) {
66 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
67 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
68 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
69 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
70 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
71 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
72 }
73 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
74 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
75 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
76 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
77 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
78
79 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
80 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
81 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
82 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
83 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
84 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
85 (flow->dl_type == htons(ETH_TYPE_RARP))) {
86 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
87 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
88 }
89
90 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
91 flow->dl_type == htons(ETH_TYPE_RARP)) {
92 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
93 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
94 }
95
96 if (flow->dl_type == htons(ETH_TYPE_IPV6) ||
97 flow->dl_type == htons(ETH_TYPE_IP)) {
98 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
99 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
100 }
101
102 if (flow->nw_frag) {
103 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
104 }
105
106 if (flow->nw_proto == IPPROTO_ICMP || flow->nw_proto == IPPROTO_ICMPV6 ||
107 (flow->tp_src || flow->tp_dst)) {
108 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
109 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
110 }
111
112 if (flow->nw_proto == IPPROTO_ICMPV6) {
113 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
114 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
115 }
116
117 return;
118 }
119
120 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
121 void
122 match_init_exact(struct match *match, const struct flow *flow)
123 {
124 ovs_be64 tun_id = flow->tunnel.tun_id;
125
126 match->flow = *flow;
127 match->flow.skb_priority = 0;
128 memset(&match->flow.tunnel, 0, sizeof match->flow.tunnel);
129 match->flow.tunnel.tun_id = tun_id;
130 flow_wildcards_init_exact(&match->wc);
131 }
132
133 /* Initializes 'match' as a "catch-all" match that matches every packet. */
134 void
135 match_init_catchall(struct match *match)
136 {
137 memset(&match->flow, 0, sizeof match->flow);
138 flow_wildcards_init_catchall(&match->wc);
139 }
140
141 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
142 * field in 'flow' to all-0-bits. It is important to maintain this invariant
143 * in a match that might be inserted into a classifier.
144 *
145 * It is never necessary to call this function directly for a match that is
146 * initialized or modified only by match_*() functions. It is useful to
147 * restore the invariant in a match whose 'wc' member is modified by hand.
148 */
149 void
150 match_zero_wildcarded_fields(struct match *match)
151 {
152 flow_zero_wildcards(&match->flow, &match->wc);
153 }
154
155 void
156 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
157 {
158 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
159 }
160
161 void
162 match_set_reg_masked(struct match *match, unsigned int reg_idx,
163 uint32_t value, uint32_t mask)
164 {
165 assert(reg_idx < FLOW_N_REGS);
166 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
167 match->flow.regs[reg_idx] = value & mask;
168 }
169
170 void
171 match_set_metadata(struct match *match, ovs_be64 metadata)
172 {
173 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
174 }
175
176 void
177 match_set_metadata_masked(struct match *match,
178 ovs_be64 metadata, ovs_be64 mask)
179 {
180 match->wc.masks.metadata = mask;
181 match->flow.metadata = metadata & mask;
182 }
183
184 void
185 match_set_tun_id(struct match *match, ovs_be64 tun_id)
186 {
187 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
188 }
189
190 void
191 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
192 {
193 match->wc.masks.tunnel.tun_id = mask;
194 match->flow.tunnel.tun_id = tun_id & mask;
195 }
196
197 void
198 match_set_in_port(struct match *match, uint16_t ofp_port)
199 {
200 match->wc.masks.in_port = UINT16_MAX;
201 match->flow.in_port = ofp_port;
202 }
203
204 void
205 match_set_dl_type(struct match *match, ovs_be16 dl_type)
206 {
207 match->wc.masks.dl_type = htons(UINT16_MAX);
208 match->flow.dl_type = dl_type;
209 }
210
211 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
212 * exactly. 'mask_dst' is set to all 1s. */
213 static void
214 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
215 uint8_t value_dst[ETH_ADDR_LEN],
216 uint8_t mask_dst[ETH_ADDR_LEN])
217 {
218 memcpy(value_dst, value_src, ETH_ADDR_LEN);
219 memset(mask_dst, 0xff, ETH_ADDR_LEN);
220 }
221
222 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
223 * after each byte is ANDed with the appropriate byte in 'mask_src'.
224 * 'mask_dst' is set to 'mask_src' */
225 static void
226 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
227 const uint8_t mask_src[ETH_ADDR_LEN],
228 uint8_t value_dst[ETH_ADDR_LEN],
229 uint8_t mask_dst[ETH_ADDR_LEN])
230 {
231 size_t i;
232
233 for (i = 0; i < ETH_ADDR_LEN; i++) {
234 value_dst[i] = value_src[i] & mask_src[i];
235 mask_dst[i] = mask_src[i];
236 }
237 }
238
239 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
240 * exactly. */
241 void
242 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
243 {
244 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
245 }
246
247 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
248 * after each byte is ANDed with the appropriate byte in 'mask'. */
249 void
250 match_set_dl_src_masked(struct match *match,
251 const uint8_t dl_src[ETH_ADDR_LEN],
252 const uint8_t mask[ETH_ADDR_LEN])
253 {
254 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
255 }
256
257 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
258 * exactly. */
259 void
260 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
261 {
262 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
263 }
264
265 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
266 * byte is ANDed with the appropriate byte in 'mask'.
267 *
268 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
269 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
270 void
271 match_set_dl_dst_masked(struct match *match,
272 const uint8_t dl_dst[ETH_ADDR_LEN],
273 const uint8_t mask[ETH_ADDR_LEN])
274 {
275 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
276 }
277
278 void
279 match_set_dl_tci(struct match *match, ovs_be16 tci)
280 {
281 match_set_dl_tci_masked(match, tci, htons(0xffff));
282 }
283
284 void
285 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
286 {
287 match->flow.vlan_tci = tci & mask;
288 match->wc.masks.vlan_tci = mask;
289 }
290
291 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
292 * wildcarded, then 'match' will match a packet regardless of whether it has an
293 * 802.1Q header or not. */
294 void
295 match_set_any_vid(struct match *match)
296 {
297 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
298 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
299 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
300 } else {
301 match_set_dl_tci_masked(match, htons(0), htons(0));
302 }
303 }
304
305 /* Modifies 'match' depending on 'dl_vlan':
306 *
307 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
308 * without an 802.1Q header.
309 *
310 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
311 * VID equals the low 12 bits of 'dl_vlan'.
312 */
313 void
314 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
315 {
316 flow_set_dl_vlan(&match->flow, dl_vlan);
317 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
318 match->wc.masks.vlan_tci = htons(UINT16_MAX);
319 } else {
320 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
321 }
322 }
323
324 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
325 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
326 * plus CFI). */
327 void
328 match_set_vlan_vid(struct match *match, ovs_be16 vid)
329 {
330 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
331 }
332
333
334 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
335 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
336 * plus CFI), with the corresponding 'mask'. */
337 void
338 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
339 {
340 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
341 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
342
343 mask &= vid_mask;
344 flow_set_vlan_vid(&match->flow, vid & mask);
345 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
346 }
347
348 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
349 * wildcarded, then 'match' will match a packet regardless of whether it has an
350 * 802.1Q header or not. */
351 void
352 match_set_any_pcp(struct match *match)
353 {
354 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
355 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
356 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
357 } else {
358 match_set_dl_tci_masked(match, htons(0), htons(0));
359 }
360 }
361
362 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
363 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
364 void
365 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
366 {
367 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
368 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
369 }
370
371 void
372 match_set_tp_src(struct match *match, ovs_be16 tp_src)
373 {
374 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
375 }
376
377 void
378 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
379 {
380 match->flow.tp_src = port & mask;
381 match->wc.masks.tp_src = mask;
382 }
383
384 void
385 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
386 {
387 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
388 }
389
390 void
391 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
392 {
393 match->flow.tp_dst = port & mask;
394 match->wc.masks.tp_dst = mask;
395 }
396
397 void
398 match_set_nw_proto(struct match *match, uint8_t nw_proto)
399 {
400 match->flow.nw_proto = nw_proto;
401 match->wc.masks.nw_proto = UINT8_MAX;
402 }
403
404 void
405 match_set_nw_src(struct match *match, ovs_be32 nw_src)
406 {
407 match->flow.nw_src = nw_src;
408 match->wc.masks.nw_src = htonl(UINT32_MAX);
409 }
410
411 void
412 match_set_nw_src_masked(struct match *match,
413 ovs_be32 nw_src, ovs_be32 mask)
414 {
415 match->flow.nw_src = nw_src & mask;
416 match->wc.masks.nw_src = mask;
417 }
418
419 void
420 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
421 {
422 match->flow.nw_dst = nw_dst;
423 match->wc.masks.nw_dst = htonl(UINT32_MAX);
424 }
425
426 void
427 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
428 {
429 match->flow.nw_dst = ip & mask;
430 match->wc.masks.nw_dst = mask;
431 }
432
433 void
434 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
435 {
436 match->wc.masks.nw_tos |= IP_DSCP_MASK;
437 match->flow.nw_tos &= ~IP_DSCP_MASK;
438 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
439 }
440
441 void
442 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
443 {
444 match->wc.masks.nw_tos |= IP_ECN_MASK;
445 match->flow.nw_tos &= ~IP_ECN_MASK;
446 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
447 }
448
449 void
450 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
451 {
452 match->wc.masks.nw_ttl = UINT8_MAX;
453 match->flow.nw_ttl = nw_ttl;
454 }
455
456 void
457 match_set_nw_frag(struct match *match, uint8_t nw_frag)
458 {
459 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
460 match->flow.nw_frag = nw_frag;
461 }
462
463 void
464 match_set_nw_frag_masked(struct match *match,
465 uint8_t nw_frag, uint8_t mask)
466 {
467 match->flow.nw_frag = nw_frag & mask;
468 match->wc.masks.nw_frag = mask;
469 }
470
471 void
472 match_set_icmp_type(struct match *match, uint8_t icmp_type)
473 {
474 match_set_tp_src(match, htons(icmp_type));
475 }
476
477 void
478 match_set_icmp_code(struct match *match, uint8_t icmp_code)
479 {
480 match_set_tp_dst(match, htons(icmp_code));
481 }
482
483 void
484 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
485 {
486 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
487 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
488 }
489
490 void
491 match_set_arp_sha_masked(struct match *match,
492 const uint8_t arp_sha[ETH_ADDR_LEN],
493 const uint8_t mask[ETH_ADDR_LEN])
494 {
495 set_eth_masked(arp_sha, mask,
496 match->flow.arp_sha, match->wc.masks.arp_sha);
497 }
498
499 void
500 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
501 {
502 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
503 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
504 }
505
506 void
507 match_set_arp_tha_masked(struct match *match,
508 const uint8_t arp_tha[ETH_ADDR_LEN],
509 const uint8_t mask[ETH_ADDR_LEN])
510 {
511 set_eth_masked(arp_tha, mask,
512 match->flow.arp_tha, match->wc.masks.arp_tha);
513 }
514
515 void
516 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
517 {
518 match->flow.ipv6_src = *src;
519 match->wc.masks.ipv6_src = in6addr_exact;
520 }
521
522 void
523 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
524 const struct in6_addr *mask)
525 {
526 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
527 match->wc.masks.ipv6_src = *mask;
528 }
529
530 void
531 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
532 {
533 match->flow.ipv6_dst = *dst;
534 match->wc.masks.ipv6_dst = in6addr_exact;
535 }
536
537 void
538 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
539 const struct in6_addr *mask)
540 {
541 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
542 match->wc.masks.ipv6_dst = *mask;
543 }
544
545 void
546 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
547 {
548 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
549 match->flow.ipv6_label = ipv6_label;
550 }
551
552
553 void
554 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
555 ovs_be32 mask)
556 {
557 match->flow.ipv6_label = ipv6_label & mask;
558 match->wc.masks.ipv6_label = mask;
559 }
560
561 void
562 match_set_nd_target(struct match *match, const struct in6_addr *target)
563 {
564 match->flow.nd_target = *target;
565 match->wc.masks.nd_target = in6addr_exact;
566 }
567
568 void
569 match_set_nd_target_masked(struct match *match,
570 const struct in6_addr *target,
571 const struct in6_addr *mask)
572 {
573 match->flow.nd_target = ipv6_addr_bitand(target, mask);
574 match->wc.masks.nd_target = *mask;
575 }
576
577 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
578 * values for fixed fields, otherwise false. */
579 bool
580 match_equal(const struct match *a, const struct match *b)
581 {
582 return (flow_wildcards_equal(&a->wc, &b->wc)
583 && flow_equal(&a->flow, &b->flow));
584 }
585
586 /* Returns a hash value for the flow and wildcards in 'match', starting from
587 * 'basis'. */
588 uint32_t
589 match_hash(const struct match *match, uint32_t basis)
590 {
591 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
592 }
593
594 static void
595 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
596 const uint8_t mask[6])
597 {
598 if (!eth_addr_is_zero(mask)) {
599 ds_put_format(s, "%s=", name);
600 eth_format_masked(eth, mask, s);
601 ds_put_char(s, ',');
602 }
603 }
604
605 static void
606 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
607 ovs_be32 netmask)
608 {
609 if (netmask) {
610 ds_put_format(s, "%s=", name);
611 ip_format_masked(ip, netmask, s);
612 ds_put_char(s, ',');
613 }
614 }
615
616 static void
617 format_ipv6_netmask(struct ds *s, const char *name,
618 const struct in6_addr *addr,
619 const struct in6_addr *netmask)
620 {
621 if (!ipv6_mask_is_any(netmask)) {
622 ds_put_format(s, "%s=", name);
623 print_ipv6_masked(s, addr, netmask);
624 ds_put_char(s, ',');
625 }
626 }
627
628
629 static void
630 format_be16_masked(struct ds *s, const char *name,
631 ovs_be16 value, ovs_be16 mask)
632 {
633 if (mask != htons(0)) {
634 ds_put_format(s, "%s=", name);
635 if (mask == htons(UINT16_MAX)) {
636 ds_put_format(s, "%"PRIu16, ntohs(value));
637 } else {
638 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
639 ntohs(value), ntohs(mask));
640 }
641 ds_put_char(s, ',');
642 }
643 }
644
645 /* Appends a string representation of 'match' to 's'. If 'priority' is
646 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
647 void
648 match_format(const struct match *match, struct ds *s, unsigned int priority)
649 {
650 const struct flow_wildcards *wc = &match->wc;
651 size_t start_len = s->length;
652 const struct flow *f = &match->flow;
653 bool skip_type = false;
654 bool skip_proto = false;
655
656 int i;
657
658 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
659
660 if (priority != OFP_DEFAULT_PRIORITY) {
661 ds_put_format(s, "priority=%u,", priority);
662 }
663
664 if (wc->masks.dl_type) {
665 skip_type = true;
666 if (f->dl_type == htons(ETH_TYPE_IP)) {
667 if (wc->masks.nw_proto) {
668 skip_proto = true;
669 if (f->nw_proto == IPPROTO_ICMP) {
670 ds_put_cstr(s, "icmp,");
671 } else if (f->nw_proto == IPPROTO_TCP) {
672 ds_put_cstr(s, "tcp,");
673 } else if (f->nw_proto == IPPROTO_UDP) {
674 ds_put_cstr(s, "udp,");
675 } else {
676 ds_put_cstr(s, "ip,");
677 skip_proto = false;
678 }
679 } else {
680 ds_put_cstr(s, "ip,");
681 }
682 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
683 if (wc->masks.nw_proto) {
684 skip_proto = true;
685 if (f->nw_proto == IPPROTO_ICMPV6) {
686 ds_put_cstr(s, "icmp6,");
687 } else if (f->nw_proto == IPPROTO_TCP) {
688 ds_put_cstr(s, "tcp6,");
689 } else if (f->nw_proto == IPPROTO_UDP) {
690 ds_put_cstr(s, "udp6,");
691 } else {
692 ds_put_cstr(s, "ipv6,");
693 skip_proto = false;
694 }
695 } else {
696 ds_put_cstr(s, "ipv6,");
697 }
698 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
699 ds_put_cstr(s, "arp,");
700 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
701 ds_put_cstr(s, "rarp,");
702 } else {
703 skip_type = false;
704 }
705 }
706 for (i = 0; i < FLOW_N_REGS; i++) {
707 switch (wc->masks.regs[i]) {
708 case 0:
709 break;
710 case UINT32_MAX:
711 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
712 break;
713 default:
714 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
715 i, f->regs[i], wc->masks.regs[i]);
716 break;
717 }
718 }
719 switch (wc->masks.tunnel.tun_id) {
720 case 0:
721 break;
722 case CONSTANT_HTONLL(UINT64_MAX):
723 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tunnel.tun_id));
724 break;
725 default:
726 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
727 ntohll(f->tunnel.tun_id),
728 ntohll(wc->masks.tunnel.tun_id));
729 break;
730 }
731 switch (wc->masks.metadata) {
732 case 0:
733 break;
734 case CONSTANT_HTONLL(UINT64_MAX):
735 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
736 break;
737 default:
738 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
739 ntohll(f->metadata), ntohll(wc->masks.metadata));
740 break;
741 }
742 if (wc->masks.in_port) {
743 ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
744 }
745 if (wc->masks.vlan_tci) {
746 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
747 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
748 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
749
750 if (cfi && f->vlan_tci & htons(VLAN_CFI)
751 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
752 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
753 && (vid_mask || pcp_mask)) {
754 if (vid_mask) {
755 ds_put_format(s, "dl_vlan=%"PRIu16",",
756 vlan_tci_to_vid(f->vlan_tci));
757 }
758 if (pcp_mask) {
759 ds_put_format(s, "dl_vlan_pcp=%d,",
760 vlan_tci_to_pcp(f->vlan_tci));
761 }
762 } else if (wc->masks.vlan_tci == htons(0xffff)) {
763 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
764 } else {
765 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
766 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
767 }
768 }
769 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
770 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
771 if (!skip_type && wc->masks.dl_type) {
772 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
773 }
774 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
775 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
776 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
777 if (wc->masks.ipv6_label) {
778 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
779 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
780 ntohl(f->ipv6_label));
781 } else {
782 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
783 ntohl(f->ipv6_label),
784 ntohl(wc->masks.ipv6_label));
785 }
786 }
787 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
788 f->dl_type == htons(ETH_TYPE_RARP)) {
789 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
790 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
791 } else {
792 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
793 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
794 }
795 if (!skip_proto && wc->masks.nw_proto) {
796 if (f->dl_type == htons(ETH_TYPE_ARP) ||
797 f->dl_type == htons(ETH_TYPE_RARP)) {
798 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
799 } else {
800 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
801 }
802 }
803 if (f->dl_type == htons(ETH_TYPE_ARP) ||
804 f->dl_type == htons(ETH_TYPE_RARP)) {
805 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
806 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
807 }
808 if (wc->masks.nw_tos & IP_DSCP_MASK) {
809 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
810 }
811 if (wc->masks.nw_tos & IP_ECN_MASK) {
812 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
813 }
814 if (wc->masks.nw_ttl) {
815 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
816 }
817 switch (wc->masks.nw_frag) {
818 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
819 ds_put_format(s, "nw_frag=%s,",
820 f->nw_frag & FLOW_NW_FRAG_ANY
821 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
822 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
823 break;
824
825 case FLOW_NW_FRAG_ANY:
826 ds_put_format(s, "nw_frag=%s,",
827 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
828 break;
829
830 case FLOW_NW_FRAG_LATER:
831 ds_put_format(s, "nw_frag=%s,",
832 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
833 break;
834 }
835 if (f->dl_type == htons(ETH_TYPE_IP) &&
836 f->nw_proto == IPPROTO_ICMP) {
837 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
838 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
839 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
840 f->nw_proto == IPPROTO_ICMPV6) {
841 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
842 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
843 format_ipv6_netmask(s, "nd_target", &f->nd_target,
844 &wc->masks.nd_target);
845 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
846 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
847 } else {
848 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
849 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
850 }
851
852 if (s->length > start_len && ds_last(s) == ',') {
853 s->length--;
854 }
855 }
856
857 /* Converts 'match' to a string and returns the string. If 'priority' is
858 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
859 * must free the string (with free()). */
860 char *
861 match_to_string(const struct match *match, unsigned int priority)
862 {
863 struct ds s = DS_EMPTY_INITIALIZER;
864 match_format(match, &s, priority);
865 return ds_steal_cstr(&s);
866 }
867
868 void
869 match_print(const struct match *match)
870 {
871 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
872 puts(s);
873 free(s);
874 }
875 \f
876 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
877 * with minimatch_destroy(). */
878 void
879 minimatch_init(struct minimatch *dst, const struct match *src)
880 {
881 miniflow_init(&dst->flow, &src->flow);
882 minimask_init(&dst->mask, &src->wc);
883 }
884
885 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
886 * with minimatch_destroy(). */
887 void
888 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
889 {
890 miniflow_clone(&dst->flow, &src->flow);
891 minimask_clone(&dst->mask, &src->mask);
892 }
893
894 /* Frees any memory owned by 'match'. Does not free the storage in which
895 * 'match' itself resides; the caller is responsible for that. */
896 void
897 minimatch_destroy(struct minimatch *match)
898 {
899 miniflow_destroy(&match->flow);
900 minimask_destroy(&match->mask);
901 }
902
903 /* Initializes 'dst' as a copy of 'src'. */
904 void
905 minimatch_expand(const struct minimatch *src, struct match *dst)
906 {
907 miniflow_expand(&src->flow, &dst->flow);
908 minimask_expand(&src->mask, &dst->wc);
909 }
910
911 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
912 bool
913 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
914 {
915 return (miniflow_equal(&a->flow, &b->flow)
916 && minimask_equal(&a->mask, &b->mask));
917 }
918
919 /* Returns a hash value for 'match', given 'basis'. */
920 uint32_t
921 minimatch_hash(const struct minimatch *match, uint32_t basis)
922 {
923 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
924 }
925
926 /* Appends a string representation of 'match' to 's'. If 'priority' is
927 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
928 void
929 minimatch_format(const struct minimatch *match, struct ds *s,
930 unsigned int priority)
931 {
932 struct match megamatch;
933
934 minimatch_expand(match, &megamatch);
935 match_format(&megamatch, s, priority);
936 }
937
938 /* Converts 'match' to a string and returns the string. If 'priority' is
939 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
940 * must free the string (with free()). */
941 char *
942 minimatch_to_string(const struct minimatch *match, unsigned int priority)
943 {
944 struct match megamatch;
945
946 minimatch_expand(match, &megamatch);
947 return match_to_string(&megamatch, priority);
948 }