]>
git.proxmox.com Git - ovs.git/blob - lib/flow.c
2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
27 #include "byte-order.h"
30 #include "dynamic-string.h"
33 #include "openflow/openflow.h"
34 #include "openvswitch/datapath-protocol.h"
36 #include "unaligned.h"
39 VLOG_DEFINE_THIS_MODULE(flow
);
41 COVERAGE_DEFINE(flow_extract
);
43 static struct arp_eth_header
*
44 pull_arp(struct ofpbuf
*packet
)
46 return ofpbuf_try_pull(packet
, ARP_ETH_HEADER_LEN
);
49 static struct ip_header
*
50 pull_ip(struct ofpbuf
*packet
)
52 if (packet
->size
>= IP_HEADER_LEN
) {
53 struct ip_header
*ip
= packet
->data
;
54 int ip_len
= IP_IHL(ip
->ip_ihl_ver
) * 4;
55 if (ip_len
>= IP_HEADER_LEN
&& packet
->size
>= ip_len
) {
56 return ofpbuf_pull(packet
, ip_len
);
62 static struct tcp_header
*
63 pull_tcp(struct ofpbuf
*packet
)
65 if (packet
->size
>= TCP_HEADER_LEN
) {
66 struct tcp_header
*tcp
= packet
->data
;
67 int tcp_len
= TCP_OFFSET(tcp
->tcp_ctl
) * 4;
68 if (tcp_len
>= TCP_HEADER_LEN
&& packet
->size
>= tcp_len
) {
69 return ofpbuf_pull(packet
, tcp_len
);
75 static struct udp_header
*
76 pull_udp(struct ofpbuf
*packet
)
78 return ofpbuf_try_pull(packet
, UDP_HEADER_LEN
);
81 static struct icmp_header
*
82 pull_icmp(struct ofpbuf
*packet
)
84 return ofpbuf_try_pull(packet
, ICMP_HEADER_LEN
);
87 static struct icmp6_hdr
*
88 pull_icmpv6(struct ofpbuf
*packet
)
90 return ofpbuf_try_pull(packet
, sizeof(struct icmp6_hdr
));
94 parse_vlan(struct ofpbuf
*b
, struct flow
*flow
)
97 ovs_be16 eth_type
; /* ETH_TYPE_VLAN */
101 if (b
->size
>= sizeof(struct qtag_prefix
) + sizeof(ovs_be16
)) {
102 struct qtag_prefix
*qp
= ofpbuf_pull(b
, sizeof *qp
);
103 flow
->vlan_tci
= qp
->tci
| htons(VLAN_CFI
);
108 parse_ethertype(struct ofpbuf
*b
)
110 struct llc_snap_header
*llc
;
113 proto
= *(ovs_be16
*) ofpbuf_pull(b
, sizeof proto
);
114 if (ntohs(proto
) >= ETH_TYPE_MIN
) {
118 if (b
->size
< sizeof *llc
) {
119 return htons(FLOW_DL_TYPE_NONE
);
123 if (llc
->llc
.llc_dsap
!= LLC_DSAP_SNAP
124 || llc
->llc
.llc_ssap
!= LLC_SSAP_SNAP
125 || llc
->llc
.llc_cntl
!= LLC_CNTL_SNAP
126 || memcmp(llc
->snap
.snap_org
, SNAP_ORG_ETHERNET
,
127 sizeof llc
->snap
.snap_org
)) {
128 return htons(FLOW_DL_TYPE_NONE
);
131 ofpbuf_pull(b
, sizeof *llc
);
132 return llc
->snap
.snap_type
;
136 parse_ipv6(struct ofpbuf
*packet
, struct flow
*flow
)
138 const struct ip6_hdr
*nh
;
142 nh
= ofpbuf_try_pull(packet
, sizeof *nh
);
147 nexthdr
= nh
->ip6_nxt
;
149 flow
->ipv6_src
= nh
->ip6_src
;
150 flow
->ipv6_dst
= nh
->ip6_dst
;
152 tc_flow
= get_unaligned_be32(&nh
->ip6_flow
);
153 flow
->nw_tos
= (ntohl(tc_flow
) >> 4) & IP_DSCP_MASK
;
154 flow
->nw_proto
= IPPROTO_NONE
;
157 if ((nexthdr
!= IPPROTO_HOPOPTS
)
158 && (nexthdr
!= IPPROTO_ROUTING
)
159 && (nexthdr
!= IPPROTO_DSTOPTS
)
160 && (nexthdr
!= IPPROTO_AH
)
161 && (nexthdr
!= IPPROTO_FRAGMENT
)) {
162 /* It's either a terminal header (e.g., TCP, UDP) or one we
163 * don't understand. In either case, we're done with the
164 * packet, so use it to fill in 'nw_proto'. */
168 /* We only verify that at least 8 bytes of the next header are
169 * available, but many of these headers are longer. Ensure that
170 * accesses within the extension header are within those first 8
171 * bytes. All extension headers are required to be at least 8
173 if (packet
->size
< 8) {
177 if ((nexthdr
== IPPROTO_HOPOPTS
)
178 || (nexthdr
== IPPROTO_ROUTING
)
179 || (nexthdr
== IPPROTO_DSTOPTS
)) {
180 /* These headers, while different, have the fields we care about
181 * in the same location and with the same interpretation. */
182 const struct ip6_ext
*ext_hdr
= (struct ip6_ext
*)packet
->data
;
183 nexthdr
= ext_hdr
->ip6e_nxt
;
184 if (!ofpbuf_try_pull(packet
, (ext_hdr
->ip6e_len
+ 1) * 8)) {
187 } else if (nexthdr
== IPPROTO_AH
) {
188 /* A standard AH definition isn't available, but the fields
189 * we care about are in the same location as the generic
190 * option header--only the header length is calculated
192 const struct ip6_ext
*ext_hdr
= (struct ip6_ext
*)packet
->data
;
193 nexthdr
= ext_hdr
->ip6e_nxt
;
194 if (!ofpbuf_try_pull(packet
, (ext_hdr
->ip6e_len
+ 2) * 4)) {
197 } else if (nexthdr
== IPPROTO_FRAGMENT
) {
198 const struct ip6_frag
*frag_hdr
= (struct ip6_frag
*)packet
->data
;
200 nexthdr
= frag_hdr
->ip6f_nxt
;
201 if (!ofpbuf_try_pull(packet
, sizeof *frag_hdr
)) {
205 /* We only process the first fragment. */
206 if ((frag_hdr
->ip6f_offlg
& IP6F_OFF_MASK
) != htons(0)) {
207 nexthdr
= IPPROTO_FRAGMENT
;
213 flow
->nw_proto
= nexthdr
;
218 parse_tcp(struct ofpbuf
*packet
, struct ofpbuf
*b
, struct flow
*flow
)
220 const struct tcp_header
*tcp
= pull_tcp(b
);
222 flow
->tp_src
= tcp
->tcp_src
;
223 flow
->tp_dst
= tcp
->tcp_dst
;
224 packet
->l7
= b
->data
;
229 parse_udp(struct ofpbuf
*packet
, struct ofpbuf
*b
, struct flow
*flow
)
231 const struct udp_header
*udp
= pull_udp(b
);
233 flow
->tp_src
= udp
->udp_src
;
234 flow
->tp_dst
= udp
->udp_dst
;
235 packet
->l7
= b
->data
;
240 parse_icmpv6(struct ofpbuf
*b
, struct flow
*flow
)
242 const struct icmp6_hdr
*icmp
= pull_icmpv6(b
);
248 /* The ICMPv6 type and code fields use the 16-bit transport port
249 * fields, so we need to store them in 16-bit network byte order. */
250 flow
->icmp_type
= htons(icmp
->icmp6_type
);
251 flow
->icmp_code
= htons(icmp
->icmp6_code
);
253 if (icmp
->icmp6_code
== 0 &&
254 (icmp
->icmp6_type
== ND_NEIGHBOR_SOLICIT
||
255 icmp
->icmp6_type
== ND_NEIGHBOR_ADVERT
)) {
256 const struct in6_addr
*nd_target
;
258 nd_target
= ofpbuf_try_pull(b
, sizeof *nd_target
);
262 flow
->nd_target
= *nd_target
;
264 while (b
->size
>= 8) {
265 /* The minimum size of an option is 8 bytes, which also is
266 * the size of Ethernet link-layer options. */
267 const struct nd_opt_hdr
*nd_opt
= b
->data
;
268 int opt_len
= nd_opt
->nd_opt_len
* 8;
270 if (!opt_len
|| opt_len
> b
->size
) {
274 /* Store the link layer address if the appropriate option is
275 * provided. It is considered an error if the same link
276 * layer option is specified twice. */
277 if (nd_opt
->nd_opt_type
== ND_OPT_SOURCE_LINKADDR
279 if (eth_addr_is_zero(flow
->arp_sha
)) {
280 memcpy(flow
->arp_sha
, nd_opt
+ 1, ETH_ADDR_LEN
);
284 } else if (nd_opt
->nd_opt_type
== ND_OPT_TARGET_LINKADDR
286 if (eth_addr_is_zero(flow
->arp_tha
)) {
287 memcpy(flow
->arp_tha
, nd_opt
+ 1, ETH_ADDR_LEN
);
293 if (!ofpbuf_try_pull(b
, opt_len
)) {
302 memset(&flow
->nd_target
, 0, sizeof(flow
->nd_target
));
303 memset(flow
->arp_sha
, 0, sizeof(flow
->arp_sha
));
304 memset(flow
->arp_tha
, 0, sizeof(flow
->arp_tha
));
310 /* Initializes 'flow' members from 'packet', 'tun_id', and 'ofp_in_port'.
311 * Initializes 'packet' header pointers as follows:
313 * - packet->l2 to the start of the Ethernet header.
315 * - packet->l3 to just past the Ethernet header, or just past the
316 * vlan_header if one is present, to the first byte of the payload of the
319 * - packet->l4 to just past the IPv4 header, if one is present and has a
320 * correct length, and otherwise NULL.
322 * - packet->l7 to just past the TCP or UDP or ICMP header, if one is
323 * present and has a correct length, and otherwise NULL.
326 flow_extract(struct ofpbuf
*packet
, ovs_be64 tun_id
, uint16_t ofp_in_port
,
329 struct ofpbuf b
= *packet
;
330 struct eth_header
*eth
;
333 COVERAGE_INC(flow_extract
);
335 memset(flow
, 0, sizeof *flow
);
336 flow
->tun_id
= tun_id
;
337 flow
->in_port
= ofp_in_port
;
344 if (b
.size
< sizeof *eth
) {
350 memcpy(flow
->dl_src
, eth
->eth_src
, ETH_ADDR_LEN
);
351 memcpy(flow
->dl_dst
, eth
->eth_dst
, ETH_ADDR_LEN
);
353 /* dl_type, vlan_tci. */
354 ofpbuf_pull(&b
, ETH_ADDR_LEN
* 2);
355 if (eth
->eth_type
== htons(ETH_TYPE_VLAN
)) {
356 parse_vlan(&b
, flow
);
358 flow
->dl_type
= parse_ethertype(&b
);
362 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
363 const struct ip_header
*nh
= pull_ip(&b
);
365 flow
->nw_src
= get_unaligned_be32(&nh
->ip_src
);
366 flow
->nw_dst
= get_unaligned_be32(&nh
->ip_dst
);
367 flow
->nw_tos
= nh
->ip_tos
& IP_DSCP_MASK
;
368 flow
->nw_proto
= nh
->ip_proto
;
370 if (!IP_IS_FRAGMENT(nh
->ip_frag_off
)) {
371 if (flow
->nw_proto
== IPPROTO_TCP
) {
372 parse_tcp(packet
, &b
, flow
);
373 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
374 parse_udp(packet
, &b
, flow
);
375 } else if (flow
->nw_proto
== IPPROTO_ICMP
) {
376 const struct icmp_header
*icmp
= pull_icmp(&b
);
378 flow
->icmp_type
= htons(icmp
->icmp_type
);
379 flow
->icmp_code
= htons(icmp
->icmp_code
);
387 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
389 retval
= parse_ipv6(&b
, flow
);
395 if (flow
->nw_proto
== IPPROTO_TCP
) {
396 parse_tcp(packet
, &b
, flow
);
397 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
398 parse_udp(packet
, &b
, flow
);
399 } else if (flow
->nw_proto
== IPPROTO_ICMPV6
) {
400 if (parse_icmpv6(&b
, flow
)) {
404 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
)) {
405 const struct arp_eth_header
*arp
= pull_arp(&b
);
406 if (arp
&& arp
->ar_hrd
== htons(1)
407 && arp
->ar_pro
== htons(ETH_TYPE_IP
)
408 && arp
->ar_hln
== ETH_ADDR_LEN
409 && arp
->ar_pln
== 4) {
410 /* We only match on the lower 8 bits of the opcode. */
411 if (ntohs(arp
->ar_op
) <= 0xff) {
412 flow
->nw_proto
= ntohs(arp
->ar_op
);
415 if ((flow
->nw_proto
== ARP_OP_REQUEST
)
416 || (flow
->nw_proto
== ARP_OP_REPLY
)) {
417 flow
->nw_src
= arp
->ar_spa
;
418 flow
->nw_dst
= arp
->ar_tpa
;
419 memcpy(flow
->arp_sha
, arp
->ar_sha
, ETH_ADDR_LEN
);
420 memcpy(flow
->arp_tha
, arp
->ar_tha
, ETH_ADDR_LEN
);
428 /* Extracts the flow stats for a packet. The 'flow' and 'packet'
429 * arguments must have been initialized through a call to flow_extract().
432 flow_extract_stats(const struct flow
*flow
, struct ofpbuf
*packet
,
433 struct dpif_flow_stats
*stats
)
435 memset(stats
, 0, sizeof(*stats
));
437 if ((flow
->dl_type
== htons(ETH_TYPE_IP
)) && packet
->l4
) {
438 if ((flow
->nw_proto
== IPPROTO_TCP
) && packet
->l7
) {
439 struct tcp_header
*tcp
= packet
->l4
;
440 stats
->tcp_flags
= TCP_FLAGS(tcp
->tcp_ctl
);
444 stats
->n_bytes
= packet
->size
;
445 stats
->n_packets
= 1;
449 flow_to_string(const struct flow
*flow
)
451 struct ds ds
= DS_EMPTY_INITIALIZER
;
452 flow_format(&ds
, flow
);
457 flow_format(struct ds
*ds
, const struct flow
*flow
)
459 ds_put_format(ds
, "tunnel%#"PRIx64
":in_port%04"PRIx16
":tci(",
460 ntohll(flow
->tun_id
), flow
->in_port
);
461 if (flow
->vlan_tci
) {
462 ds_put_format(ds
, "vlan%"PRIu16
",pcp%d",
463 vlan_tci_to_vid(flow
->vlan_tci
),
464 vlan_tci_to_pcp(flow
->vlan_tci
));
466 ds_put_char(ds
, '0');
468 ds_put_format(ds
, ") mac"ETH_ADDR_FMT
"->"ETH_ADDR_FMT
470 ETH_ADDR_ARGS(flow
->dl_src
),
471 ETH_ADDR_ARGS(flow
->dl_dst
),
472 ntohs(flow
->dl_type
));
474 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
475 ds_put_format(ds
, " proto%"PRIu8
" tos%"PRIu8
" ipv6",
476 flow
->nw_proto
, flow
->nw_tos
);
477 print_ipv6_addr(ds
, &flow
->ipv6_src
);
478 ds_put_cstr(ds
, "->");
479 print_ipv6_addr(ds
, &flow
->ipv6_dst
);
482 ds_put_format(ds
, " proto%"PRIu8
484 " ip"IP_FMT
"->"IP_FMT
,
487 IP_ARGS(&flow
->nw_src
),
488 IP_ARGS(&flow
->nw_dst
));
490 if (flow
->tp_src
|| flow
->tp_dst
) {
491 ds_put_format(ds
, " port%"PRIu16
"->%"PRIu16
,
492 ntohs(flow
->tp_src
), ntohs(flow
->tp_dst
));
494 if (!eth_addr_is_zero(flow
->arp_sha
) || !eth_addr_is_zero(flow
->arp_tha
)) {
495 ds_put_format(ds
, " arp_ha"ETH_ADDR_FMT
"->"ETH_ADDR_FMT
,
496 ETH_ADDR_ARGS(flow
->arp_sha
),
497 ETH_ADDR_ARGS(flow
->arp_tha
));
502 flow_print(FILE *stream
, const struct flow
*flow
)
504 char *s
= flow_to_string(flow
);
509 /* flow_wildcards functions. */
511 /* Initializes 'wc' as a set of wildcards that matches every packet. */
513 flow_wildcards_init_catchall(struct flow_wildcards
*wc
)
515 wc
->wildcards
= FWW_ALL
;
516 wc
->tun_id_mask
= htonll(0);
517 wc
->nw_src_mask
= htonl(0);
518 wc
->nw_dst_mask
= htonl(0);
519 wc
->ipv6_src_mask
= in6addr_any
;
520 wc
->ipv6_dst_mask
= in6addr_any
;
521 memset(wc
->reg_masks
, 0, sizeof wc
->reg_masks
);
522 wc
->vlan_tci_mask
= htons(0);
526 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
527 * wildcard any bits or fields. */
529 flow_wildcards_init_exact(struct flow_wildcards
*wc
)
532 wc
->tun_id_mask
= htonll(UINT64_MAX
);
533 wc
->nw_src_mask
= htonl(UINT32_MAX
);
534 wc
->nw_dst_mask
= htonl(UINT32_MAX
);
535 wc
->ipv6_src_mask
= in6addr_exact
;
536 wc
->ipv6_dst_mask
= in6addr_exact
;
537 memset(wc
->reg_masks
, 0xff, sizeof wc
->reg_masks
);
538 wc
->vlan_tci_mask
= htons(UINT16_MAX
);
542 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
545 flow_wildcards_is_exact(const struct flow_wildcards
*wc
)
550 || wc
->tun_id_mask
!= htonll(UINT64_MAX
)
551 || wc
->nw_src_mask
!= htonl(UINT32_MAX
)
552 || wc
->nw_dst_mask
!= htonl(UINT32_MAX
)
553 || wc
->vlan_tci_mask
!= htons(UINT16_MAX
)
554 || !ipv6_mask_is_exact(&wc
->ipv6_src_mask
)
555 || !ipv6_mask_is_exact(&wc
->ipv6_dst_mask
)) {
559 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
560 if (wc
->reg_masks
[i
] != UINT32_MAX
) {
568 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
569 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
570 * 'src1' or 'src2' or both. */
572 flow_wildcards_combine(struct flow_wildcards
*dst
,
573 const struct flow_wildcards
*src1
,
574 const struct flow_wildcards
*src2
)
578 dst
->wildcards
= src1
->wildcards
| src2
->wildcards
;
579 dst
->tun_id_mask
= src1
->tun_id_mask
& src2
->tun_id_mask
;
580 dst
->nw_src_mask
= src1
->nw_src_mask
& src2
->nw_src_mask
;
581 dst
->nw_dst_mask
= src1
->nw_dst_mask
& src2
->nw_dst_mask
;
582 dst
->ipv6_src_mask
= ipv6_addr_bitand(&src1
->ipv6_src_mask
,
583 &src2
->ipv6_src_mask
);
584 dst
->ipv6_dst_mask
= ipv6_addr_bitand(&src1
->ipv6_dst_mask
,
585 &src2
->ipv6_dst_mask
);
586 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
587 dst
->reg_masks
[i
] = src1
->reg_masks
[i
] & src2
->reg_masks
[i
];
589 dst
->vlan_tci_mask
= src1
->vlan_tci_mask
& src2
->vlan_tci_mask
;
592 /* Returns a hash of the wildcards in 'wc'. */
594 flow_wildcards_hash(const struct flow_wildcards
*wc
, uint32_t basis
)
596 /* If you change struct flow_wildcards and thereby trigger this
597 * assertion, please check that the new struct flow_wildcards has no holes
598 * in it before you update the assertion. */
599 BUILD_ASSERT_DECL(sizeof *wc
== 56 + FLOW_N_REGS
* 4);
600 return hash_bytes(wc
, sizeof *wc
, basis
);
603 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
606 flow_wildcards_equal(const struct flow_wildcards
*a
,
607 const struct flow_wildcards
*b
)
611 if (a
->wildcards
!= b
->wildcards
612 || a
->tun_id_mask
!= b
->tun_id_mask
613 || a
->nw_src_mask
!= b
->nw_src_mask
614 || a
->nw_dst_mask
!= b
->nw_dst_mask
615 || a
->vlan_tci_mask
!= b
->vlan_tci_mask
616 || !ipv6_addr_equals(&a
->ipv6_src_mask
, &b
->ipv6_src_mask
)
617 || !ipv6_addr_equals(&a
->ipv6_dst_mask
, &b
->ipv6_dst_mask
)) {
621 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
622 if (a
->reg_masks
[i
] != b
->reg_masks
[i
]) {
630 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
631 * 'b', false otherwise. */
633 flow_wildcards_has_extra(const struct flow_wildcards
*a
,
634 const struct flow_wildcards
*b
)
637 struct in6_addr ipv6_masked
;
639 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
640 if ((a
->reg_masks
[i
] & b
->reg_masks
[i
]) != b
->reg_masks
[i
]) {
645 ipv6_masked
= ipv6_addr_bitand(&a
->ipv6_src_mask
, &b
->ipv6_src_mask
);
646 if (!ipv6_addr_equals(&ipv6_masked
, &b
->ipv6_src_mask
)) {
650 ipv6_masked
= ipv6_addr_bitand(&a
->ipv6_dst_mask
, &b
->ipv6_dst_mask
);
651 if (!ipv6_addr_equals(&ipv6_masked
, &b
->ipv6_dst_mask
)) {
655 return (a
->wildcards
& ~b
->wildcards
656 || (a
->tun_id_mask
& b
->tun_id_mask
) != b
->tun_id_mask
657 || (a
->nw_src_mask
& b
->nw_src_mask
) != b
->nw_src_mask
658 || (a
->nw_dst_mask
& b
->nw_dst_mask
) != b
->nw_dst_mask
659 || (a
->vlan_tci_mask
& b
->vlan_tci_mask
) != b
->vlan_tci_mask
);
663 set_nw_mask(ovs_be32
*maskp
, ovs_be32 mask
)
665 if (ip_is_cidr(mask
)) {
673 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
674 * high-order 1-bit and 32-N low-order 0-bits). Returns true if successful,
675 * false if 'mask' is not a CIDR mask. */
677 flow_wildcards_set_nw_src_mask(struct flow_wildcards
*wc
, ovs_be32 mask
)
679 return set_nw_mask(&wc
->nw_src_mask
, mask
);
682 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
683 * N high-order 1-bit and 32-N low-order 0-bits). Returns true if successful,
684 * false if 'mask' is not a CIDR mask. */
686 flow_wildcards_set_nw_dst_mask(struct flow_wildcards
*wc
, ovs_be32 mask
)
688 return set_nw_mask(&wc
->nw_dst_mask
, mask
);
692 set_ipv6_mask(struct in6_addr
*maskp
, const struct in6_addr
*mask
)
694 if (ipv6_is_cidr(mask
)) {
702 /* Sets the IPv6 source wildcard mask to CIDR 'mask' (consisting of N
703 * high-order 1-bit and 128-N low-order 0-bits). Returns true if successful,
704 * false if 'mask' is not a CIDR mask. */
706 flow_wildcards_set_ipv6_src_mask(struct flow_wildcards
*wc
,
707 const struct in6_addr
*mask
)
709 return set_ipv6_mask(&wc
->ipv6_src_mask
, mask
);
712 /* Sets the IPv6 destination wildcard mask to CIDR 'mask' (consisting of
713 * N high-order 1-bit and 128-N low-order 0-bits). Returns true if
714 * successful, false if 'mask' is not a CIDR mask. */
716 flow_wildcards_set_ipv6_dst_mask(struct flow_wildcards
*wc
,
717 const struct in6_addr
*mask
)
719 return set_ipv6_mask(&wc
->ipv6_dst_mask
, mask
);
722 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
723 * (A 0-bit indicates a wildcard bit.) */
725 flow_wildcards_set_reg_mask(struct flow_wildcards
*wc
, int idx
, uint32_t mask
)
727 wc
->reg_masks
[idx
] = mask
;
730 /* Returns the wildcard bitmask for the Ethernet destination address
731 * that 'wc' specifies. The bitmask has a 0 in each bit that is wildcarded
732 * and a 1 in each bit that must match. */
734 flow_wildcards_to_dl_dst_mask(flow_wildcards_t wc
)
736 static const uint8_t no_wild
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
737 static const uint8_t addr_wild
[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
738 static const uint8_t mcast_wild
[] = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
739 static const uint8_t all_wild
[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
741 switch (wc
& (FWW_DL_DST
| FWW_ETH_MCAST
)) {
742 case 0: return no_wild
;
743 case FWW_DL_DST
: return addr_wild
;
744 case FWW_ETH_MCAST
: return mcast_wild
;
745 case FWW_DL_DST
| FWW_ETH_MCAST
: return all_wild
;
750 /* Returns true if 'mask' is a valid wildcard bitmask for the Ethernet
751 * destination address. Valid bitmasks are either all-bits-0 or all-bits-1,
752 * except that the multicast bit may differ from the rest of the bits. So,
753 * there are four possible valid bitmasks:
755 * - 00:00:00:00:00:00
756 * - 01:00:00:00:00:00
757 * - fe:ff:ff:ff:ff:ff
758 * - ff:ff:ff:ff:ff:ff
760 * All other bitmasks are invalid. */
762 flow_wildcards_is_dl_dst_mask_valid(const uint8_t mask
[ETH_ADDR_LEN
])
767 return (mask
[1] | mask
[2] | mask
[3] | mask
[4] | mask
[5]) == 0x00;
771 return (mask
[1] & mask
[2] & mask
[3] & mask
[4] & mask
[5]) == 0xff;
778 /* Returns 'wc' with the FWW_DL_DST and FWW_ETH_MCAST bits modified
779 * appropriately to match 'mask'.
781 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
782 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
784 flow_wildcards_set_dl_dst_mask(flow_wildcards_t wc
,
785 const uint8_t mask
[ETH_ADDR_LEN
])
787 assert(flow_wildcards_is_dl_dst_mask_valid(mask
));
791 return wc
| FWW_DL_DST
| FWW_ETH_MCAST
;
794 return (wc
| FWW_DL_DST
) & ~FWW_ETH_MCAST
;
797 return (wc
& ~FWW_DL_DST
) | FWW_ETH_MCAST
;
800 return wc
& ~(FWW_DL_DST
| FWW_ETH_MCAST
);
807 /* Hashes 'flow' based on its L2 through L4 protocol information. */
809 flow_hash_symmetric_l4(const struct flow
*flow
, uint32_t basis
)
814 struct in6_addr ipv6_addr
;
819 uint8_t eth_addr
[ETH_ADDR_LEN
];
825 memset(&fields
, 0, sizeof fields
);
826 for (i
= 0; i
< ETH_ADDR_LEN
; i
++) {
827 fields
.eth_addr
[i
] = flow
->dl_src
[i
] ^ flow
->dl_dst
[i
];
829 fields
.vlan_tci
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
830 fields
.eth_type
= flow
->dl_type
;
832 /* UDP source and destination port are not taken into account because they
833 * will not necessarily be symmetric in a bidirectional flow. */
834 if (fields
.eth_type
== htons(ETH_TYPE_IP
)) {
835 fields
.ipv4_addr
= flow
->nw_src
^ flow
->nw_dst
;
836 fields
.ip_proto
= flow
->nw_proto
;
837 if (fields
.ip_proto
== IPPROTO_TCP
) {
838 fields
.tp_addr
= flow
->tp_src
^ flow
->tp_dst
;
840 } else if (fields
.eth_type
== htons(ETH_TYPE_IPV6
)) {
841 const uint8_t *a
= &flow
->ipv6_src
.s6_addr
[0];
842 const uint8_t *b
= &flow
->ipv6_dst
.s6_addr
[0];
843 uint8_t *ipv6_addr
= &fields
.ipv6_addr
.s6_addr
[0];
845 for (i
=0; i
<16; i
++) {
846 ipv6_addr
[i
] = a
[i
] ^ b
[i
];
848 fields
.ip_proto
= flow
->nw_proto
;
849 if (fields
.ip_proto
== IPPROTO_TCP
) {
850 fields
.tp_addr
= flow
->tp_src
^ flow
->tp_dst
;
853 return hash_bytes(&fields
, sizeof fields
, basis
);