]> git.proxmox.com Git - mirror_ovs.git/blob - lib/match.c
ofp-util: Add SCTP support
[mirror_ovs.git] / lib / match.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 <stdlib.h>
20 #include "byte-order.h"
21 #include "dynamic-string.h"
22 #include "ofp-util.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 if (flow->skb_priority) {
60 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
61 }
62
63 if (flow->pkt_mark) {
64 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
65 }
66
67 for (i = 0; i < FLOW_N_REGS; i++) {
68 if (flow->regs[i]) {
69 memset(&wc->masks.regs[i], 0xff, sizeof wc->masks.regs[i]);
70 }
71 }
72
73 if (flow->tunnel.ip_dst) {
74 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
75 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
76 }
77 memset(&wc->masks.tunnel.ip_src, 0xff, sizeof wc->masks.tunnel.ip_src);
78 memset(&wc->masks.tunnel.ip_dst, 0xff, sizeof wc->masks.tunnel.ip_dst);
79 memset(&wc->masks.tunnel.flags, 0xff, sizeof wc->masks.tunnel.flags);
80 memset(&wc->masks.tunnel.ip_tos, 0xff, sizeof wc->masks.tunnel.ip_tos);
81 memset(&wc->masks.tunnel.ip_ttl, 0xff, sizeof wc->masks.tunnel.ip_ttl);
82 } else if (flow->tunnel.tun_id) {
83 memset(&wc->masks.tunnel.tun_id, 0xff, sizeof wc->masks.tunnel.tun_id);
84 }
85
86 memset(&wc->masks.metadata, 0xff, sizeof wc->masks.metadata);
87 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
88 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
89 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
90 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
91
92 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
93 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
94 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
95 memset(&wc->masks.ipv6_label, 0xff, sizeof wc->masks.ipv6_label);
96 } else if (flow->dl_type == htons(ETH_TYPE_IP) ||
97 (flow->dl_type == htons(ETH_TYPE_ARP)) ||
98 (flow->dl_type == htons(ETH_TYPE_RARP))) {
99 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
100 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
101 } else if (eth_type_mpls(flow->dl_type)) {
102 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
103 }
104
105 if (flow->dl_type == htons(ETH_TYPE_ARP) ||
106 flow->dl_type == htons(ETH_TYPE_RARP)) {
107 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
108 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
109 }
110
111 if (is_ip_any(flow)) {
112 memset(&wc->masks.nw_tos, 0xff, sizeof wc->masks.nw_tos);
113 memset(&wc->masks.nw_ttl, 0xff, sizeof wc->masks.nw_ttl);
114
115 if (flow->nw_frag) {
116 memset(&wc->masks.nw_frag, 0xff, sizeof wc->masks.nw_frag);
117 }
118
119 if (flow->nw_proto == IPPROTO_ICMP ||
120 flow->nw_proto == IPPROTO_ICMPV6 ||
121 (flow->tp_src || flow->tp_dst)) {
122 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
123 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
124 }
125
126 if (flow->nw_proto == IPPROTO_ICMPV6) {
127 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
128 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
129 }
130 }
131
132 return;
133 }
134
135 /* Converts the flow in 'flow' into an exact-match match in 'match'. */
136 void
137 match_init_exact(struct match *match, const struct flow *flow)
138 {
139 match->flow = *flow;
140 match->flow.skb_priority = 0;
141 flow_wildcards_init_exact(&match->wc);
142 }
143
144 /* Initializes 'match' as a "catch-all" match that matches every packet. */
145 void
146 match_init_catchall(struct match *match)
147 {
148 memset(&match->flow, 0, sizeof match->flow);
149 flow_wildcards_init_catchall(&match->wc);
150 }
151
152 /* For each bit or field wildcarded in 'match', sets the corresponding bit or
153 * field in 'flow' to all-0-bits. It is important to maintain this invariant
154 * in a match that might be inserted into a classifier.
155 *
156 * It is never necessary to call this function directly for a match that is
157 * initialized or modified only by match_*() functions. It is useful to
158 * restore the invariant in a match whose 'wc' member is modified by hand.
159 */
160 void
161 match_zero_wildcarded_fields(struct match *match)
162 {
163 flow_zero_wildcards(&match->flow, &match->wc);
164 }
165
166 void
167 match_set_reg(struct match *match, unsigned int reg_idx, uint32_t value)
168 {
169 match_set_reg_masked(match, reg_idx, value, UINT32_MAX);
170 }
171
172 void
173 match_set_reg_masked(struct match *match, unsigned int reg_idx,
174 uint32_t value, uint32_t mask)
175 {
176 ovs_assert(reg_idx < FLOW_N_REGS);
177 flow_wildcards_set_reg_mask(&match->wc, reg_idx, mask);
178 match->flow.regs[reg_idx] = value & mask;
179 }
180
181 void
182 match_set_metadata(struct match *match, ovs_be64 metadata)
183 {
184 match_set_metadata_masked(match, metadata, htonll(UINT64_MAX));
185 }
186
187 void
188 match_set_metadata_masked(struct match *match,
189 ovs_be64 metadata, ovs_be64 mask)
190 {
191 match->wc.masks.metadata = mask;
192 match->flow.metadata = metadata & mask;
193 }
194
195 void
196 match_set_tun_id(struct match *match, ovs_be64 tun_id)
197 {
198 match_set_tun_id_masked(match, tun_id, htonll(UINT64_MAX));
199 }
200
201 void
202 match_set_tun_id_masked(struct match *match, ovs_be64 tun_id, ovs_be64 mask)
203 {
204 match->wc.masks.tunnel.tun_id = mask;
205 match->flow.tunnel.tun_id = tun_id & mask;
206 }
207
208 void
209 match_set_tun_src(struct match *match, ovs_be32 src)
210 {
211 match_set_tun_src_masked(match, src, htonl(UINT32_MAX));
212 }
213
214 void
215 match_set_tun_src_masked(struct match *match, ovs_be32 src, ovs_be32 mask)
216 {
217 match->wc.masks.tunnel.ip_src = mask;
218 match->flow.tunnel.ip_src = src & mask;
219 }
220
221 void
222 match_set_tun_dst(struct match *match, ovs_be32 dst)
223 {
224 match_set_tun_dst_masked(match, dst, htonl(UINT32_MAX));
225 }
226
227 void
228 match_set_tun_dst_masked(struct match *match, ovs_be32 dst, ovs_be32 mask)
229 {
230 match->wc.masks.tunnel.ip_dst = mask;
231 match->flow.tunnel.ip_dst = dst & mask;
232 }
233
234 void
235 match_set_tun_ttl(struct match *match, uint8_t ttl)
236 {
237 match_set_tun_ttl_masked(match, ttl, UINT8_MAX);
238 }
239
240 void
241 match_set_tun_ttl_masked(struct match *match, uint8_t ttl, uint8_t mask)
242 {
243 match->wc.masks.tunnel.ip_ttl = mask;
244 match->flow.tunnel.ip_ttl = ttl & mask;
245 }
246
247 void
248 match_set_tun_tos(struct match *match, uint8_t tos)
249 {
250 match_set_tun_tos_masked(match, tos, UINT8_MAX);
251 }
252
253 void
254 match_set_tun_tos_masked(struct match *match, uint8_t tos, uint8_t mask)
255 {
256 match->wc.masks.tunnel.ip_tos = mask;
257 match->flow.tunnel.ip_tos = tos & mask;
258 }
259
260 void
261 match_set_tun_flags(struct match *match, uint16_t flags)
262 {
263 match_set_tun_flags_masked(match, flags, UINT16_MAX);
264 }
265
266 void
267 match_set_tun_flags_masked(struct match *match, uint16_t flags, uint16_t mask)
268 {
269 match->wc.masks.tunnel.flags = mask;
270 match->flow.tunnel.flags = flags & mask;
271 }
272
273 void
274 match_set_in_port(struct match *match, ofp_port_t ofp_port)
275 {
276 match->wc.masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
277 match->flow.in_port.ofp_port = ofp_port;
278 }
279
280 void
281 match_set_skb_priority(struct match *match, uint32_t skb_priority)
282 {
283 match->wc.masks.skb_priority = UINT32_MAX;
284 match->flow.skb_priority = skb_priority;
285 }
286
287 void
288 match_set_pkt_mark(struct match *match, uint32_t pkt_mark)
289 {
290 match_set_pkt_mark_masked(match, pkt_mark, UINT32_MAX);
291 }
292
293 void
294 match_set_pkt_mark_masked(struct match *match, uint32_t pkt_mark, uint32_t mask)
295 {
296 match->flow.pkt_mark = pkt_mark & mask;
297 match->wc.masks.pkt_mark = mask;
298 }
299
300 void
301 match_set_dl_type(struct match *match, ovs_be16 dl_type)
302 {
303 match->wc.masks.dl_type = htons(UINT16_MAX);
304 match->flow.dl_type = dl_type;
305 }
306
307 /* Modifies 'value_src' so that the Ethernet address must match 'value_dst'
308 * exactly. 'mask_dst' is set to all 1s. */
309 static void
310 set_eth(const uint8_t value_src[ETH_ADDR_LEN],
311 uint8_t value_dst[ETH_ADDR_LEN],
312 uint8_t mask_dst[ETH_ADDR_LEN])
313 {
314 memcpy(value_dst, value_src, ETH_ADDR_LEN);
315 memset(mask_dst, 0xff, ETH_ADDR_LEN);
316 }
317
318 /* Modifies 'value_src' so that the Ethernet address must match 'value_src'
319 * after each byte is ANDed with the appropriate byte in 'mask_src'.
320 * 'mask_dst' is set to 'mask_src' */
321 static void
322 set_eth_masked(const uint8_t value_src[ETH_ADDR_LEN],
323 const uint8_t mask_src[ETH_ADDR_LEN],
324 uint8_t value_dst[ETH_ADDR_LEN],
325 uint8_t mask_dst[ETH_ADDR_LEN])
326 {
327 size_t i;
328
329 for (i = 0; i < ETH_ADDR_LEN; i++) {
330 value_dst[i] = value_src[i] & mask_src[i];
331 mask_dst[i] = mask_src[i];
332 }
333 }
334
335 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
336 * exactly. */
337 void
338 match_set_dl_src(struct match *match, const uint8_t dl_src[ETH_ADDR_LEN])
339 {
340 set_eth(dl_src, match->flow.dl_src, match->wc.masks.dl_src);
341 }
342
343 /* Modifies 'rule' so that the source Ethernet address must match 'dl_src'
344 * after each byte is ANDed with the appropriate byte in 'mask'. */
345 void
346 match_set_dl_src_masked(struct match *match,
347 const uint8_t dl_src[ETH_ADDR_LEN],
348 const uint8_t mask[ETH_ADDR_LEN])
349 {
350 set_eth_masked(dl_src, mask, match->flow.dl_src, match->wc.masks.dl_src);
351 }
352
353 /* Modifies 'match' so that the Ethernet address must match 'dl_dst'
354 * exactly. */
355 void
356 match_set_dl_dst(struct match *match, const uint8_t dl_dst[ETH_ADDR_LEN])
357 {
358 set_eth(dl_dst, match->flow.dl_dst, match->wc.masks.dl_dst);
359 }
360
361 /* Modifies 'match' so that the Ethernet address must match 'dl_dst' after each
362 * byte is ANDed with the appropriate byte in 'mask'.
363 *
364 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
365 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
366 void
367 match_set_dl_dst_masked(struct match *match,
368 const uint8_t dl_dst[ETH_ADDR_LEN],
369 const uint8_t mask[ETH_ADDR_LEN])
370 {
371 set_eth_masked(dl_dst, mask, match->flow.dl_dst, match->wc.masks.dl_dst);
372 }
373
374 void
375 match_set_dl_tci(struct match *match, ovs_be16 tci)
376 {
377 match_set_dl_tci_masked(match, tci, htons(0xffff));
378 }
379
380 void
381 match_set_dl_tci_masked(struct match *match, ovs_be16 tci, ovs_be16 mask)
382 {
383 match->flow.vlan_tci = tci & mask;
384 match->wc.masks.vlan_tci = mask;
385 }
386
387 /* Modifies 'match' so that the VLAN VID is wildcarded. If the PCP is already
388 * wildcarded, then 'match' will match a packet regardless of whether it has an
389 * 802.1Q header or not. */
390 void
391 match_set_any_vid(struct match *match)
392 {
393 if (match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK)) {
394 match->wc.masks.vlan_tci &= ~htons(VLAN_VID_MASK);
395 match->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
396 } else {
397 match_set_dl_tci_masked(match, htons(0), htons(0));
398 }
399 }
400
401 /* Modifies 'match' depending on 'dl_vlan':
402 *
403 * - If 'dl_vlan' is htons(OFP_VLAN_NONE), makes 'match' match only packets
404 * without an 802.1Q header.
405 *
406 * - Otherwise, makes 'match' match only packets with an 802.1Q header whose
407 * VID equals the low 12 bits of 'dl_vlan'.
408 */
409 void
410 match_set_dl_vlan(struct match *match, ovs_be16 dl_vlan)
411 {
412 flow_set_dl_vlan(&match->flow, dl_vlan);
413 if (dl_vlan == htons(OFP10_VLAN_NONE)) {
414 match->wc.masks.vlan_tci = htons(UINT16_MAX);
415 } else {
416 match->wc.masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
417 }
418 }
419
420 /* Sets the VLAN VID that 'match' matches to 'vid', which is interpreted as an
421 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
422 * plus CFI). */
423 void
424 match_set_vlan_vid(struct match *match, ovs_be16 vid)
425 {
426 match_set_vlan_vid_masked(match, vid, htons(VLAN_VID_MASK | VLAN_CFI));
427 }
428
429
430 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
431 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
432 * plus CFI), with the corresponding 'mask'. */
433 void
434 match_set_vlan_vid_masked(struct match *match, ovs_be16 vid, ovs_be16 mask)
435 {
436 ovs_be16 pcp_mask = htons(VLAN_PCP_MASK);
437 ovs_be16 vid_mask = htons(VLAN_VID_MASK | VLAN_CFI);
438
439 mask &= vid_mask;
440 flow_set_vlan_vid(&match->flow, vid & mask);
441 match->wc.masks.vlan_tci = mask | (match->wc.masks.vlan_tci & pcp_mask);
442 }
443
444 /* Modifies 'match' so that the VLAN PCP is wildcarded. If the VID is already
445 * wildcarded, then 'match' will match a packet regardless of whether it has an
446 * 802.1Q header or not. */
447 void
448 match_set_any_pcp(struct match *match)
449 {
450 if (match->wc.masks.vlan_tci & htons(VLAN_VID_MASK)) {
451 match->wc.masks.vlan_tci &= ~htons(VLAN_PCP_MASK);
452 match->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
453 } else {
454 match_set_dl_tci_masked(match, htons(0), htons(0));
455 }
456 }
457
458 /* Modifies 'match' so that it matches only packets with an 802.1Q header whose
459 * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
460 void
461 match_set_dl_vlan_pcp(struct match *match, uint8_t dl_vlan_pcp)
462 {
463 flow_set_vlan_pcp(&match->flow, dl_vlan_pcp);
464 match->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_PCP_MASK);
465 }
466
467 /* Modifies 'match' so that the MPLS label is wildcarded. */
468 void
469 match_set_any_mpls_label(struct match *match)
470 {
471 match->wc.masks.mpls_lse &= ~htonl(MPLS_LABEL_MASK);
472 flow_set_mpls_label(&match->flow, htonl(0));
473 }
474
475 /* Modifies 'match' so that it matches only packets with an MPLS header whose
476 * label equals the low 20 bits of 'mpls_label'. */
477 void
478 match_set_mpls_label(struct match *match, ovs_be32 mpls_label)
479 {
480 match->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
481 flow_set_mpls_label(&match->flow, mpls_label);
482 }
483
484 /* Modifies 'match' so that the MPLS TC is wildcarded. */
485 void
486 match_set_any_mpls_tc(struct match *match)
487 {
488 match->wc.masks.mpls_lse &= ~htonl(MPLS_TC_MASK);
489 flow_set_mpls_tc(&match->flow, 0);
490 }
491
492 /* Modifies 'match' so that it matches only packets with an MPLS header whose
493 * Traffic Class equals the low 3 bits of 'mpls_tc'. */
494 void
495 match_set_mpls_tc(struct match *match, uint8_t mpls_tc)
496 {
497 match->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
498 flow_set_mpls_tc(&match->flow, mpls_tc);
499 }
500
501 /* Modifies 'match' so that the MPLS stack flag is wildcarded. */
502 void
503 match_set_any_mpls_bos(struct match *match)
504 {
505 match->wc.masks.mpls_lse &= ~htonl(MPLS_BOS_MASK);
506 flow_set_mpls_bos(&match->flow, 0);
507 }
508
509 /* Modifies 'match' so that it matches only packets with an MPLS header whose
510 * Stack Flag equals the lower bit of 'mpls_bos' */
511 void
512 match_set_mpls_bos(struct match *match, uint8_t mpls_bos)
513 {
514 match->wc.masks.mpls_lse |= htonl(MPLS_BOS_MASK);
515 flow_set_mpls_bos(&match->flow, mpls_bos);
516 }
517
518 void
519 match_set_tp_src(struct match *match, ovs_be16 tp_src)
520 {
521 match_set_tp_src_masked(match, tp_src, htons(UINT16_MAX));
522 }
523
524 void
525 match_set_tp_src_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
526 {
527 match->flow.tp_src = port & mask;
528 match->wc.masks.tp_src = mask;
529 }
530
531 void
532 match_set_tp_dst(struct match *match, ovs_be16 tp_dst)
533 {
534 match_set_tp_dst_masked(match, tp_dst, htons(UINT16_MAX));
535 }
536
537 void
538 match_set_tp_dst_masked(struct match *match, ovs_be16 port, ovs_be16 mask)
539 {
540 match->flow.tp_dst = port & mask;
541 match->wc.masks.tp_dst = mask;
542 }
543
544 void
545 match_set_nw_proto(struct match *match, uint8_t nw_proto)
546 {
547 match->flow.nw_proto = nw_proto;
548 match->wc.masks.nw_proto = UINT8_MAX;
549 }
550
551 void
552 match_set_nw_src(struct match *match, ovs_be32 nw_src)
553 {
554 match->flow.nw_src = nw_src;
555 match->wc.masks.nw_src = htonl(UINT32_MAX);
556 }
557
558 void
559 match_set_nw_src_masked(struct match *match,
560 ovs_be32 nw_src, ovs_be32 mask)
561 {
562 match->flow.nw_src = nw_src & mask;
563 match->wc.masks.nw_src = mask;
564 }
565
566 void
567 match_set_nw_dst(struct match *match, ovs_be32 nw_dst)
568 {
569 match->flow.nw_dst = nw_dst;
570 match->wc.masks.nw_dst = htonl(UINT32_MAX);
571 }
572
573 void
574 match_set_nw_dst_masked(struct match *match, ovs_be32 ip, ovs_be32 mask)
575 {
576 match->flow.nw_dst = ip & mask;
577 match->wc.masks.nw_dst = mask;
578 }
579
580 void
581 match_set_nw_dscp(struct match *match, uint8_t nw_dscp)
582 {
583 match->wc.masks.nw_tos |= IP_DSCP_MASK;
584 match->flow.nw_tos &= ~IP_DSCP_MASK;
585 match->flow.nw_tos |= nw_dscp & IP_DSCP_MASK;
586 }
587
588 void
589 match_set_nw_ecn(struct match *match, uint8_t nw_ecn)
590 {
591 match->wc.masks.nw_tos |= IP_ECN_MASK;
592 match->flow.nw_tos &= ~IP_ECN_MASK;
593 match->flow.nw_tos |= nw_ecn & IP_ECN_MASK;
594 }
595
596 void
597 match_set_nw_ttl(struct match *match, uint8_t nw_ttl)
598 {
599 match->wc.masks.nw_ttl = UINT8_MAX;
600 match->flow.nw_ttl = nw_ttl;
601 }
602
603 void
604 match_set_nw_frag(struct match *match, uint8_t nw_frag)
605 {
606 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
607 match->flow.nw_frag = nw_frag;
608 }
609
610 void
611 match_set_nw_frag_masked(struct match *match,
612 uint8_t nw_frag, uint8_t mask)
613 {
614 match->flow.nw_frag = nw_frag & mask;
615 match->wc.masks.nw_frag = mask;
616 }
617
618 void
619 match_set_icmp_type(struct match *match, uint8_t icmp_type)
620 {
621 match_set_tp_src(match, htons(icmp_type));
622 }
623
624 void
625 match_set_icmp_code(struct match *match, uint8_t icmp_code)
626 {
627 match_set_tp_dst(match, htons(icmp_code));
628 }
629
630 void
631 match_set_arp_sha(struct match *match, const uint8_t sha[ETH_ADDR_LEN])
632 {
633 memcpy(match->flow.arp_sha, sha, ETH_ADDR_LEN);
634 memset(match->wc.masks.arp_sha, UINT8_MAX, ETH_ADDR_LEN);
635 }
636
637 void
638 match_set_arp_sha_masked(struct match *match,
639 const uint8_t arp_sha[ETH_ADDR_LEN],
640 const uint8_t mask[ETH_ADDR_LEN])
641 {
642 set_eth_masked(arp_sha, mask,
643 match->flow.arp_sha, match->wc.masks.arp_sha);
644 }
645
646 void
647 match_set_arp_tha(struct match *match, const uint8_t tha[ETH_ADDR_LEN])
648 {
649 memcpy(match->flow.arp_tha, tha, ETH_ADDR_LEN);
650 memset(match->wc.masks.arp_tha, UINT8_MAX, ETH_ADDR_LEN);
651 }
652
653 void
654 match_set_arp_tha_masked(struct match *match,
655 const uint8_t arp_tha[ETH_ADDR_LEN],
656 const uint8_t mask[ETH_ADDR_LEN])
657 {
658 set_eth_masked(arp_tha, mask,
659 match->flow.arp_tha, match->wc.masks.arp_tha);
660 }
661
662 void
663 match_set_ipv6_src(struct match *match, const struct in6_addr *src)
664 {
665 match->flow.ipv6_src = *src;
666 match->wc.masks.ipv6_src = in6addr_exact;
667 }
668
669 void
670 match_set_ipv6_src_masked(struct match *match, const struct in6_addr *src,
671 const struct in6_addr *mask)
672 {
673 match->flow.ipv6_src = ipv6_addr_bitand(src, mask);
674 match->wc.masks.ipv6_src = *mask;
675 }
676
677 void
678 match_set_ipv6_dst(struct match *match, const struct in6_addr *dst)
679 {
680 match->flow.ipv6_dst = *dst;
681 match->wc.masks.ipv6_dst = in6addr_exact;
682 }
683
684 void
685 match_set_ipv6_dst_masked(struct match *match, const struct in6_addr *dst,
686 const struct in6_addr *mask)
687 {
688 match->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
689 match->wc.masks.ipv6_dst = *mask;
690 }
691
692 void
693 match_set_ipv6_label(struct match *match, ovs_be32 ipv6_label)
694 {
695 match->wc.masks.ipv6_label = htonl(UINT32_MAX);
696 match->flow.ipv6_label = ipv6_label;
697 }
698
699
700 void
701 match_set_ipv6_label_masked(struct match *match, ovs_be32 ipv6_label,
702 ovs_be32 mask)
703 {
704 match->flow.ipv6_label = ipv6_label & mask;
705 match->wc.masks.ipv6_label = mask;
706 }
707
708 void
709 match_set_nd_target(struct match *match, const struct in6_addr *target)
710 {
711 match->flow.nd_target = *target;
712 match->wc.masks.nd_target = in6addr_exact;
713 }
714
715 void
716 match_set_nd_target_masked(struct match *match,
717 const struct in6_addr *target,
718 const struct in6_addr *mask)
719 {
720 match->flow.nd_target = ipv6_addr_bitand(target, mask);
721 match->wc.masks.nd_target = *mask;
722 }
723
724 /* Returns true if 'a' and 'b' wildcard the same fields and have the same
725 * values for fixed fields, otherwise false. */
726 bool
727 match_equal(const struct match *a, const struct match *b)
728 {
729 return (flow_wildcards_equal(&a->wc, &b->wc)
730 && flow_equal(&a->flow, &b->flow));
731 }
732
733 /* Returns a hash value for the flow and wildcards in 'match', starting from
734 * 'basis'. */
735 uint32_t
736 match_hash(const struct match *match, uint32_t basis)
737 {
738 return flow_wildcards_hash(&match->wc, flow_hash(&match->flow, basis));
739 }
740
741 static void
742 format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
743 const uint8_t mask[6])
744 {
745 if (!eth_addr_is_zero(mask)) {
746 ds_put_format(s, "%s=", name);
747 eth_format_masked(eth, mask, s);
748 ds_put_char(s, ',');
749 }
750 }
751
752 static void
753 format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
754 ovs_be32 netmask)
755 {
756 if (netmask) {
757 ds_put_format(s, "%s=", name);
758 ip_format_masked(ip, netmask, s);
759 ds_put_char(s, ',');
760 }
761 }
762
763 static void
764 format_ipv6_netmask(struct ds *s, const char *name,
765 const struct in6_addr *addr,
766 const struct in6_addr *netmask)
767 {
768 if (!ipv6_mask_is_any(netmask)) {
769 ds_put_format(s, "%s=", name);
770 print_ipv6_masked(s, addr, netmask);
771 ds_put_char(s, ',');
772 }
773 }
774
775
776 static void
777 format_be16_masked(struct ds *s, const char *name,
778 ovs_be16 value, ovs_be16 mask)
779 {
780 if (mask != htons(0)) {
781 ds_put_format(s, "%s=", name);
782 if (mask == htons(UINT16_MAX)) {
783 ds_put_format(s, "%"PRIu16, ntohs(value));
784 } else {
785 ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
786 ntohs(value), ntohs(mask));
787 }
788 ds_put_char(s, ',');
789 }
790 }
791
792 static void
793 format_flow_tunnel(struct ds *s, const struct match *match)
794 {
795 const struct flow_wildcards *wc = &match->wc;
796 const struct flow_tnl *tnl = &match->flow.tunnel;
797
798 switch (wc->masks.tunnel.tun_id) {
799 case 0:
800 break;
801 case CONSTANT_HTONLL(UINT64_MAX):
802 ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(tnl->tun_id));
803 break;
804 default:
805 ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
806 ntohll(tnl->tun_id),
807 ntohll(wc->masks.tunnel.tun_id));
808 break;
809 }
810 format_ip_netmask(s, "tun_src", tnl->ip_src, wc->masks.tunnel.ip_src);
811 format_ip_netmask(s, "tun_dst", tnl->ip_dst, wc->masks.tunnel.ip_dst);
812
813 if (wc->masks.tunnel.ip_tos) {
814 ds_put_format(s, "tun_tos=%"PRIx8",", tnl->ip_tos);
815 }
816 if (wc->masks.tunnel.ip_ttl) {
817 ds_put_format(s, "tun_ttl=%"PRIu8",", tnl->ip_ttl);
818 }
819 if (wc->masks.tunnel.flags) {
820 format_flags(s, flow_tun_flag_to_string, tnl->flags, '|');
821 ds_put_char(s, ',');
822 }
823 }
824
825 /* Appends a string representation of 'match' to 's'. If 'priority' is
826 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
827 void
828 match_format(const struct match *match, struct ds *s, unsigned int priority)
829 {
830 const struct flow_wildcards *wc = &match->wc;
831 size_t start_len = s->length;
832 const struct flow *f = &match->flow;
833 bool skip_type = false;
834 bool skip_proto = false;
835
836 int i;
837
838 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20);
839
840 if (priority != OFP_DEFAULT_PRIORITY) {
841 ds_put_format(s, "priority=%u,", priority);
842 }
843
844 switch (wc->masks.pkt_mark) {
845 case 0:
846 break;
847 case UINT32_MAX:
848 ds_put_format(s, "pkt_mark=%#"PRIx32",", f->pkt_mark);
849 break;
850 default:
851 ds_put_format(s, "pkt_mark=%#"PRIx32"/%#"PRIx32",",
852 f->pkt_mark, wc->masks.pkt_mark);
853 break;
854 }
855
856 if (wc->masks.skb_priority) {
857 ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority);
858 }
859
860 if (wc->masks.dl_type) {
861 skip_type = true;
862 if (f->dl_type == htons(ETH_TYPE_IP)) {
863 if (wc->masks.nw_proto) {
864 skip_proto = true;
865 if (f->nw_proto == IPPROTO_ICMP) {
866 ds_put_cstr(s, "icmp,");
867 } else if (f->nw_proto == IPPROTO_TCP) {
868 ds_put_cstr(s, "tcp,");
869 } else if (f->nw_proto == IPPROTO_UDP) {
870 ds_put_cstr(s, "udp,");
871 } else if (f->nw_proto == IPPROTO_SCTP) {
872 ds_put_cstr(s, "sctp,");
873 } else {
874 ds_put_cstr(s, "ip,");
875 skip_proto = false;
876 }
877 } else {
878 ds_put_cstr(s, "ip,");
879 }
880 } else if (f->dl_type == htons(ETH_TYPE_IPV6)) {
881 if (wc->masks.nw_proto) {
882 skip_proto = true;
883 if (f->nw_proto == IPPROTO_ICMPV6) {
884 ds_put_cstr(s, "icmp6,");
885 } else if (f->nw_proto == IPPROTO_TCP) {
886 ds_put_cstr(s, "tcp6,");
887 } else if (f->nw_proto == IPPROTO_UDP) {
888 ds_put_cstr(s, "udp6,");
889 } else if (f->nw_proto == IPPROTO_SCTP) {
890 ds_put_cstr(s, "sctp6,");
891 } else {
892 ds_put_cstr(s, "ipv6,");
893 skip_proto = false;
894 }
895 } else {
896 ds_put_cstr(s, "ipv6,");
897 }
898 } else if (f->dl_type == htons(ETH_TYPE_ARP)) {
899 ds_put_cstr(s, "arp,");
900 } else if (f->dl_type == htons(ETH_TYPE_RARP)) {
901 ds_put_cstr(s, "rarp,");
902 } else if (f->dl_type == htons(ETH_TYPE_MPLS)) {
903 ds_put_cstr(s, "mpls,");
904 } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
905 ds_put_cstr(s, "mplsm,");
906 } else {
907 skip_type = false;
908 }
909 }
910 for (i = 0; i < FLOW_N_REGS; i++) {
911 switch (wc->masks.regs[i]) {
912 case 0:
913 break;
914 case UINT32_MAX:
915 ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]);
916 break;
917 default:
918 ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",",
919 i, f->regs[i], wc->masks.regs[i]);
920 break;
921 }
922 }
923
924 format_flow_tunnel(s, match);
925
926 switch (wc->masks.metadata) {
927 case 0:
928 break;
929 case CONSTANT_HTONLL(UINT64_MAX):
930 ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata));
931 break;
932 default:
933 ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",",
934 ntohll(f->metadata), ntohll(wc->masks.metadata));
935 break;
936 }
937 if (wc->masks.in_port.ofp_port) {
938 ds_put_cstr(s, "in_port=");
939 ofputil_format_port(f->in_port.ofp_port, s);
940 ds_put_char(s, ',');
941 }
942 if (wc->masks.vlan_tci) {
943 ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
944 ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK);
945 ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI);
946
947 if (cfi && f->vlan_tci & htons(VLAN_CFI)
948 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
949 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
950 && (vid_mask || pcp_mask)) {
951 if (vid_mask) {
952 ds_put_format(s, "dl_vlan=%"PRIu16",",
953 vlan_tci_to_vid(f->vlan_tci));
954 }
955 if (pcp_mask) {
956 ds_put_format(s, "dl_vlan_pcp=%d,",
957 vlan_tci_to_pcp(f->vlan_tci));
958 }
959 } else if (wc->masks.vlan_tci == htons(0xffff)) {
960 ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci));
961 } else {
962 ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",",
963 ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci));
964 }
965 }
966 format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src);
967 format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst);
968 if (!skip_type && wc->masks.dl_type) {
969 ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type));
970 }
971 if (f->dl_type == htons(ETH_TYPE_IPV6)) {
972 format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src);
973 format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst);
974 if (wc->masks.ipv6_label) {
975 if (wc->masks.ipv6_label == htonl(UINT32_MAX)) {
976 ds_put_format(s, "ipv6_label=0x%05"PRIx32",",
977 ntohl(f->ipv6_label));
978 } else {
979 ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",",
980 ntohl(f->ipv6_label),
981 ntohl(wc->masks.ipv6_label));
982 }
983 }
984 } else if (f->dl_type == htons(ETH_TYPE_ARP) ||
985 f->dl_type == htons(ETH_TYPE_RARP)) {
986 format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src);
987 format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst);
988 } else {
989 format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src);
990 format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst);
991 }
992 if (!skip_proto && wc->masks.nw_proto) {
993 if (f->dl_type == htons(ETH_TYPE_ARP) ||
994 f->dl_type == htons(ETH_TYPE_RARP)) {
995 ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto);
996 } else {
997 ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto);
998 }
999 }
1000 if (f->dl_type == htons(ETH_TYPE_ARP) ||
1001 f->dl_type == htons(ETH_TYPE_RARP)) {
1002 format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha);
1003 format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha);
1004 }
1005 if (wc->masks.nw_tos & IP_DSCP_MASK) {
1006 ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
1007 }
1008 if (wc->masks.nw_tos & IP_ECN_MASK) {
1009 ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
1010 }
1011 if (wc->masks.nw_ttl) {
1012 ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
1013 }
1014 if (wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK)) {
1015 ds_put_format(s, "mpls_label=%"PRIu32",",
1016 mpls_lse_to_label(f->mpls_lse));
1017 }
1018 if (wc->masks.mpls_lse & htonl(MPLS_TC_MASK)) {
1019 ds_put_format(s, "mpls_tc=%"PRIu8",",
1020 mpls_lse_to_tc(f->mpls_lse));
1021 }
1022 if (wc->masks.mpls_lse & htonl(MPLS_TTL_MASK)) {
1023 ds_put_format(s, "mpls_ttl=%"PRIu8",",
1024 mpls_lse_to_ttl(f->mpls_lse));
1025 }
1026 if (wc->masks.mpls_lse & htonl(MPLS_BOS_MASK)) {
1027 ds_put_format(s, "mpls_bos=%"PRIu8",",
1028 mpls_lse_to_bos(f->mpls_lse));
1029 }
1030 switch (wc->masks.nw_frag) {
1031 case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER:
1032 ds_put_format(s, "nw_frag=%s,",
1033 f->nw_frag & FLOW_NW_FRAG_ANY
1034 ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first")
1035 : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no"));
1036 break;
1037
1038 case FLOW_NW_FRAG_ANY:
1039 ds_put_format(s, "nw_frag=%s,",
1040 f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no");
1041 break;
1042
1043 case FLOW_NW_FRAG_LATER:
1044 ds_put_format(s, "nw_frag=%s,",
1045 f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later");
1046 break;
1047 }
1048 if (f->dl_type == htons(ETH_TYPE_IP) &&
1049 f->nw_proto == IPPROTO_ICMP) {
1050 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1051 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1052 } else if (f->dl_type == htons(ETH_TYPE_IPV6) &&
1053 f->nw_proto == IPPROTO_ICMPV6) {
1054 format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src);
1055 format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst);
1056 format_ipv6_netmask(s, "nd_target", &f->nd_target,
1057 &wc->masks.nd_target);
1058 format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha);
1059 format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha);
1060 } else {
1061 format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src);
1062 format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst);
1063 }
1064
1065 if (s->length > start_len && ds_last(s) == ',') {
1066 s->length--;
1067 }
1068 }
1069
1070 /* Converts 'match' to a string and returns the string. If 'priority' is
1071 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1072 * must free the string (with free()). */
1073 char *
1074 match_to_string(const struct match *match, unsigned int priority)
1075 {
1076 struct ds s = DS_EMPTY_INITIALIZER;
1077 match_format(match, &s, priority);
1078 return ds_steal_cstr(&s);
1079 }
1080
1081 void
1082 match_print(const struct match *match)
1083 {
1084 char *s = match_to_string(match, OFP_DEFAULT_PRIORITY);
1085 puts(s);
1086 free(s);
1087 }
1088 \f
1089 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1090 * with minimatch_destroy(). */
1091 void
1092 minimatch_init(struct minimatch *dst, const struct match *src)
1093 {
1094 miniflow_init(&dst->flow, &src->flow);
1095 minimask_init(&dst->mask, &src->wc);
1096 }
1097
1098 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1099 * with minimatch_destroy(). */
1100 void
1101 minimatch_clone(struct minimatch *dst, const struct minimatch *src)
1102 {
1103 miniflow_clone(&dst->flow, &src->flow);
1104 minimask_clone(&dst->mask, &src->mask);
1105 }
1106
1107 /* Frees any memory owned by 'match'. Does not free the storage in which
1108 * 'match' itself resides; the caller is responsible for that. */
1109 void
1110 minimatch_destroy(struct minimatch *match)
1111 {
1112 miniflow_destroy(&match->flow);
1113 minimask_destroy(&match->mask);
1114 }
1115
1116 /* Initializes 'dst' as a copy of 'src'. */
1117 void
1118 minimatch_expand(const struct minimatch *src, struct match *dst)
1119 {
1120 miniflow_expand(&src->flow, &dst->flow);
1121 minimask_expand(&src->mask, &dst->wc);
1122 }
1123
1124 /* Returns true if 'a' and 'b' match the same packets, false otherwise. */
1125 bool
1126 minimatch_equal(const struct minimatch *a, const struct minimatch *b)
1127 {
1128 return (miniflow_equal(&a->flow, &b->flow)
1129 && minimask_equal(&a->mask, &b->mask));
1130 }
1131
1132 /* Returns a hash value for 'match', given 'basis'. */
1133 uint32_t
1134 minimatch_hash(const struct minimatch *match, uint32_t basis)
1135 {
1136 return miniflow_hash(&match->flow, minimask_hash(&match->mask, basis));
1137 }
1138
1139 /* Appends a string representation of 'match' to 's'. If 'priority' is
1140 * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */
1141 void
1142 minimatch_format(const struct minimatch *match, struct ds *s,
1143 unsigned int priority)
1144 {
1145 struct match megamatch;
1146
1147 minimatch_expand(match, &megamatch);
1148 match_format(&megamatch, s, priority);
1149 }
1150
1151 /* Converts 'match' to a string and returns the string. If 'priority' is
1152 * different from OFP_DEFAULT_PRIORITY, includes it in the string. The caller
1153 * must free the string (with free()). */
1154 char *
1155 minimatch_to_string(const struct minimatch *match, unsigned int priority)
1156 {
1157 struct match megamatch;
1158
1159 minimatch_expand(match, &megamatch);
1160 return match_to_string(&megamatch, priority);
1161 }