]> git.proxmox.com Git - mirror_ovs.git/blame - lib/flow.c
pktbuf: Directly use pointers in pktbuf_save().
[mirror_ovs.git] / lib / flow.c
CommitLineData
064af421 1/*
8368c090 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16#include <config.h>
17#include <sys/types.h>
18#include "flow.h"
db7f8281 19#include <assert.h>
d31f1109 20#include <errno.h>
064af421
BP
21#include <inttypes.h>
22#include <netinet/in.h>
d31f1109
JP
23#include <netinet/icmp6.h>
24#include <netinet/ip6.h>
064af421
BP
25#include <stdlib.h>
26#include <string.h>
10a24935 27#include "byte-order.h"
064af421
BP
28#include "coverage.h"
29#include "dynamic-string.h"
30#include "hash.h"
31#include "ofpbuf.h"
32#include "openflow/openflow.h"
064af421 33#include "packets.h"
176aaa65 34#include "unaligned.h"
5136ce49 35#include "vlog.h"
064af421 36
d98e6007 37VLOG_DEFINE_THIS_MODULE(flow);
064af421 38
d76f09ea
BP
39COVERAGE_DEFINE(flow_extract);
40
a26ef517
JP
41static struct arp_eth_header *
42pull_arp(struct ofpbuf *packet)
43{
44 return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
45}
46
064af421
BP
47static struct ip_header *
48pull_ip(struct ofpbuf *packet)
49{
50 if (packet->size >= IP_HEADER_LEN) {
51 struct ip_header *ip = packet->data;
52 int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
53 if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
54 return ofpbuf_pull(packet, ip_len);
55 }
56 }
57 return NULL;
58}
59
60static struct tcp_header *
d295e8e9 61pull_tcp(struct ofpbuf *packet)
064af421
BP
62{
63 if (packet->size >= TCP_HEADER_LEN) {
64 struct tcp_header *tcp = packet->data;
65 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
66 if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
67 return ofpbuf_pull(packet, tcp_len);
68 }
69 }
70 return NULL;
71}
72
73static struct udp_header *
d295e8e9 74pull_udp(struct ofpbuf *packet)
064af421
BP
75{
76 return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
77}
78
79static struct icmp_header *
d295e8e9 80pull_icmp(struct ofpbuf *packet)
064af421
BP
81{
82 return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
83}
84
d31f1109
JP
85static struct icmp6_hdr *
86pull_icmpv6(struct ofpbuf *packet)
87{
88 return ofpbuf_try_pull(packet, sizeof(struct icmp6_hdr));
89}
90
50f06e16 91static void
ae412e7d 92parse_vlan(struct ofpbuf *b, struct flow *flow)
064af421 93{
50f06e16 94 struct qtag_prefix {
0b3e77bb
BP
95 ovs_be16 eth_type; /* ETH_TYPE_VLAN */
96 ovs_be16 tci;
50f06e16
BP
97 };
98
0b3e77bb 99 if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
50f06e16 100 struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
66642cb4 101 flow->vlan_tci = qp->tci | htons(VLAN_CFI);
50f06e16 102 }
064af421
BP
103}
104
0b3e77bb 105static ovs_be16
50f06e16 106parse_ethertype(struct ofpbuf *b)
064af421 107{
50f06e16 108 struct llc_snap_header *llc;
0b3e77bb 109 ovs_be16 proto;
50f06e16 110
0b3e77bb 111 proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
36956a7d 112 if (ntohs(proto) >= ETH_TYPE_MIN) {
50f06e16
BP
113 return proto;
114 }
115
116 if (b->size < sizeof *llc) {
36956a7d 117 return htons(FLOW_DL_TYPE_NONE);
50f06e16
BP
118 }
119
120 llc = b->data;
121 if (llc->llc.llc_dsap != LLC_DSAP_SNAP
122 || llc->llc.llc_ssap != LLC_SSAP_SNAP
123 || llc->llc.llc_cntl != LLC_CNTL_SNAP
124 || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
125 sizeof llc->snap.snap_org)) {
36956a7d 126 return htons(FLOW_DL_TYPE_NONE);
50f06e16
BP
127 }
128
129 ofpbuf_pull(b, sizeof *llc);
130 return llc->snap.snap_type;
064af421
BP
131}
132
d31f1109
JP
133static int
134parse_ipv6(struct ofpbuf *packet, struct flow *flow)
135{
88366484 136 const struct ip6_hdr *nh;
d31f1109
JP
137 ovs_be32 tc_flow;
138 int nexthdr;
139
88366484
JG
140 nh = ofpbuf_try_pull(packet, sizeof *nh);
141 if (!nh) {
142 return EINVAL;
d31f1109
JP
143 }
144
d31f1109 145 nexthdr = nh->ip6_nxt;
d31f1109
JP
146
147 flow->ipv6_src = nh->ip6_src;
148 flow->ipv6_dst = nh->ip6_dst;
149
150 tc_flow = get_unaligned_be32(&nh->ip6_flow);
25cfd5ca 151 flow->nw_tos = ntohl(tc_flow) >> 20;
fa8223b7 152 flow->ipv6_label = tc_flow & htonl(IPV6_LABEL_MASK);
a61680c6 153 flow->nw_ttl = nh->ip6_hlim;
d31f1109
JP
154 flow->nw_proto = IPPROTO_NONE;
155
d31f1109
JP
156 while (1) {
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'. */
165 break;
166 }
167
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
88366484 171 * bytes. All extension headers are required to be at least 8
d31f1109 172 * bytes. */
88366484
JG
173 if (packet->size < 8) {
174 return EINVAL;
d31f1109
JP
175 }
176
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. */
88366484 182 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
d31f1109 183 nexthdr = ext_hdr->ip6e_nxt;
88366484
JG
184 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 1) * 8)) {
185 return EINVAL;
186 }
d31f1109
JP
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
191 * differently. */
88366484 192 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
d31f1109 193 nexthdr = ext_hdr->ip6e_nxt;
88366484
JG
194 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 2) * 4)) {
195 return EINVAL;
196 }
d31f1109 197 } else if (nexthdr == IPPROTO_FRAGMENT) {
88366484 198 const struct ip6_frag *frag_hdr = (struct ip6_frag *)packet->data;
d31f1109
JP
199
200 nexthdr = frag_hdr->ip6f_nxt;
88366484
JG
201 if (!ofpbuf_try_pull(packet, sizeof *frag_hdr)) {
202 return EINVAL;
203 }
d31f1109
JP
204
205 /* We only process the first fragment. */
0fd0d083
JG
206 if (frag_hdr->ip6f_offlg != htons(0)) {
207 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) == htons(0)) {
208 flow->nw_frag = FLOW_NW_FRAG_ANY;
209 } else {
210 flow->nw_frag |= FLOW_NW_FRAG_LATER;
211 nexthdr = IPPROTO_FRAGMENT;
212 break;
213 }
d31f1109
JP
214 }
215 }
216 }
217
d31f1109 218 flow->nw_proto = nexthdr;
88366484 219 return 0;
d31f1109
JP
220}
221
88366484
JG
222static void
223parse_tcp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
224{
225 const struct tcp_header *tcp = pull_tcp(b);
226 if (tcp) {
227 flow->tp_src = tcp->tcp_src;
228 flow->tp_dst = tcp->tcp_dst;
229 packet->l7 = b->data;
230 }
231}
232
233static void
234parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
235{
236 const struct udp_header *udp = pull_udp(b);
237 if (udp) {
238 flow->tp_src = udp->udp_src;
239 flow->tp_dst = udp->udp_dst;
240 packet->l7 = b->data;
241 }
242}
685a51a5
JP
243
244static bool
88366484 245parse_icmpv6(struct ofpbuf *b, struct flow *flow)
685a51a5
JP
246{
247 const struct icmp6_hdr *icmp = pull_icmpv6(b);
248
249 if (!icmp) {
250 return false;
251 }
252
253 /* The ICMPv6 type and code fields use the 16-bit transport port
254 * fields, so we need to store them in 16-bit network byte order. */
3ee8a9f0
BP
255 flow->tp_src = htons(icmp->icmp6_type);
256 flow->tp_dst = htons(icmp->icmp6_code);
685a51a5 257
88366484
JG
258 if (icmp->icmp6_code == 0 &&
259 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
260 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
261 const struct in6_addr *nd_target;
685a51a5 262
88366484
JG
263 nd_target = ofpbuf_try_pull(b, sizeof *nd_target);
264 if (!nd_target) {
685a51a5
JP
265 return false;
266 }
88366484 267 flow->nd_target = *nd_target;
685a51a5 268
88366484 269 while (b->size >= 8) {
685a51a5
JP
270 /* The minimum size of an option is 8 bytes, which also is
271 * the size of Ethernet link-layer options. */
88366484
JG
272 const struct nd_opt_hdr *nd_opt = b->data;
273 int opt_len = nd_opt->nd_opt_len * 8;
274
275 if (!opt_len || opt_len > b->size) {
685a51a5
JP
276 goto invalid;
277 }
685a51a5
JP
278
279 /* Store the link layer address if the appropriate option is
280 * provided. It is considered an error if the same link
281 * layer option is specified twice. */
282 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
283 && opt_len == 8) {
284 if (eth_addr_is_zero(flow->arp_sha)) {
88366484 285 memcpy(flow->arp_sha, nd_opt + 1, ETH_ADDR_LEN);
685a51a5
JP
286 } else {
287 goto invalid;
288 }
289 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
290 && opt_len == 8) {
291 if (eth_addr_is_zero(flow->arp_tha)) {
88366484 292 memcpy(flow->arp_tha, nd_opt + 1, ETH_ADDR_LEN);
685a51a5
JP
293 } else {
294 goto invalid;
295 }
296 }
297
88366484 298 if (!ofpbuf_try_pull(b, opt_len)) {
685a51a5
JP
299 goto invalid;
300 }
685a51a5
JP
301 }
302 }
303
304 return true;
305
306invalid:
88366484
JG
307 memset(&flow->nd_target, 0, sizeof(flow->nd_target));
308 memset(flow->arp_sha, 0, sizeof(flow->arp_sha));
309 memset(flow->arp_tha, 0, sizeof(flow->arp_tha));
685a51a5
JP
310
311 return false;
312
313}
314
deedf7e7
BP
315/* Initializes 'flow' members from 'packet', 'skb_priority', 'tun_id', and
316 * 'ofp_in_port'.
317 *
0b3e77bb 318 * Initializes 'packet' header pointers as follows:
ca78c6b6
BP
319 *
320 * - packet->l2 to the start of the Ethernet header.
321 *
322 * - packet->l3 to just past the Ethernet header, or just past the
323 * vlan_header if one is present, to the first byte of the payload of the
324 * Ethernet frame.
325 *
326 * - packet->l4 to just past the IPv4 header, if one is present and has a
327 * correct length, and otherwise NULL.
328 *
329 * - packet->l7 to just past the TCP or UDP or ICMP header, if one is
330 * present and has a correct length, and otherwise NULL.
331 */
7257b535 332void
deedf7e7 333flow_extract(struct ofpbuf *packet, uint32_t skb_priority, ovs_be64 tun_id,
abff858b 334 uint16_t ofp_in_port, struct flow *flow)
064af421
BP
335{
336 struct ofpbuf b = *packet;
337 struct eth_header *eth;
064af421
BP
338
339 COVERAGE_INC(flow_extract);
340
341 memset(flow, 0, sizeof *flow);
659586ef 342 flow->tun_id = tun_id;
abe529af 343 flow->in_port = ofp_in_port;
deedf7e7 344 flow->skb_priority = skb_priority;
064af421
BP
345
346 packet->l2 = b.data;
347 packet->l3 = NULL;
348 packet->l4 = NULL;
349 packet->l7 = NULL;
350
50f06e16 351 if (b.size < sizeof *eth) {
7257b535 352 return;
50f06e16 353 }
064af421 354
50f06e16
BP
355 /* Link layer. */
356 eth = b.data;
357 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
358 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
359
66642cb4 360 /* dl_type, vlan_tci. */
50f06e16
BP
361 ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
362 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
363 parse_vlan(&b, flow);
364 }
365 flow->dl_type = parse_ethertype(&b);
366
367 /* Network layer. */
368 packet->l3 = b.data;
369 if (flow->dl_type == htons(ETH_TYPE_IP)) {
370 const struct ip_header *nh = pull_ip(&b);
371 if (nh) {
7257b535
BP
372 packet->l4 = b.data;
373
9ea5d2d5
BP
374 flow->nw_src = get_unaligned_be32(&nh->ip_src);
375 flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
50f06e16 376 flow->nw_proto = nh->ip_proto;
7257b535 377
eadef313 378 flow->nw_tos = nh->ip_tos;
7257b535 379 if (IP_IS_FRAGMENT(nh->ip_frag_off)) {
eadef313 380 flow->nw_frag = FLOW_NW_FRAG_ANY;
7257b535 381 if (nh->ip_frag_off & htons(IP_FRAG_OFF_MASK)) {
eadef313 382 flow->nw_frag |= FLOW_NW_FRAG_LATER;
7257b535
BP
383 }
384 }
a61680c6 385 flow->nw_ttl = nh->ip_ttl;
7257b535
BP
386
387 if (!(nh->ip_frag_off & htons(IP_FRAG_OFF_MASK))) {
6767a2cc 388 if (flow->nw_proto == IPPROTO_TCP) {
88366484 389 parse_tcp(packet, &b, flow);
6767a2cc 390 } else if (flow->nw_proto == IPPROTO_UDP) {
88366484 391 parse_udp(packet, &b, flow);
6767a2cc 392 } else if (flow->nw_proto == IPPROTO_ICMP) {
50f06e16
BP
393 const struct icmp_header *icmp = pull_icmp(&b);
394 if (icmp) {
3ee8a9f0
BP
395 flow->tp_src = htons(icmp->icmp_type);
396 flow->tp_dst = htons(icmp->icmp_code);
50f06e16 397 packet->l7 = b.data;
064af421 398 }
064af421 399 }
50f06e16
BP
400 }
401 }
d31f1109 402 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
7257b535
BP
403 if (parse_ipv6(&b, flow)) {
404 return;
d31f1109
JP
405 }
406
88366484
JG
407 packet->l4 = b.data;
408 if (flow->nw_proto == IPPROTO_TCP) {
409 parse_tcp(packet, &b, flow);
410 } else if (flow->nw_proto == IPPROTO_UDP) {
411 parse_udp(packet, &b, flow);
412 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
413 if (parse_icmpv6(&b, flow)) {
414 packet->l7 = b.data;
d31f1109
JP
415 }
416 }
50f06e16
BP
417 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
418 const struct arp_eth_header *arp = pull_arp(&b);
419 if (arp && arp->ar_hrd == htons(1)
d295e8e9 420 && arp->ar_pro == htons(ETH_TYPE_IP)
50f06e16
BP
421 && arp->ar_hln == ETH_ADDR_LEN
422 && arp->ar_pln == 4) {
423 /* We only match on the lower 8 bits of the opcode. */
424 if (ntohs(arp->ar_op) <= 0xff) {
425 flow->nw_proto = ntohs(arp->ar_op);
064af421 426 }
a26ef517 427
d295e8e9 428 if ((flow->nw_proto == ARP_OP_REQUEST)
50f06e16
BP
429 || (flow->nw_proto == ARP_OP_REPLY)) {
430 flow->nw_src = arp->ar_spa;
431 flow->nw_dst = arp->ar_tpa;
bad68a99
JP
432 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
433 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
a26ef517 434 }
064af421
BP
435 }
436 }
064af421
BP
437}
438
993410fb
BP
439/* For every bit of a field that is wildcarded in 'wildcards', sets the
440 * corresponding bit in 'flow' to zero. */
441void
442flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
443{
444 const flow_wildcards_t wc = wildcards->wildcards;
445 int i;
446
2486e66a 447 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
993410fb
BP
448
449 for (i = 0; i < FLOW_N_REGS; i++) {
450 flow->regs[i] &= wildcards->reg_masks[i];
451 }
452 flow->tun_id &= wildcards->tun_id_mask;
453 flow->nw_src &= wildcards->nw_src_mask;
454 flow->nw_dst &= wildcards->nw_dst_mask;
455 if (wc & FWW_IN_PORT) {
456 flow->in_port = 0;
457 }
458 flow->vlan_tci &= wildcards->vlan_tci_mask;
459 if (wc & FWW_DL_TYPE) {
cb9457e2 460 flow->dl_type = htons(0);
993410fb
BP
461 }
462 if (wc & FWW_TP_SRC) {
cb9457e2 463 flow->tp_src = htons(0);
993410fb
BP
464 }
465 if (wc & FWW_TP_DST) {
cb9457e2 466 flow->tp_dst = htons(0);
993410fb
BP
467 }
468 if (wc & FWW_DL_SRC) {
469 memset(flow->dl_src, 0, sizeof flow->dl_src);
470 }
471 if (wc & FWW_DL_DST) {
472 flow->dl_dst[0] &= 0x01;
473 memset(&flow->dl_dst[1], 0, 5);
474 }
475 if (wc & FWW_ETH_MCAST) {
476 flow->dl_dst[0] &= 0xfe;
477 }
478 if (wc & FWW_NW_PROTO) {
479 flow->nw_proto = 0;
480 }
fa8223b7
JP
481 if (wc & FWW_IPV6_LABEL) {
482 flow->ipv6_label = htonl(0);
483 }
2486e66a
JP
484 if (wc & FWW_NW_DSCP) {
485 flow->nw_tos &= ~IP_DSCP_MASK;
486 }
487 if (wc & FWW_NW_ECN) {
488 flow->nw_tos &= ~IP_ECN_MASK;
489 }
a61680c6
JP
490 if (wc & FWW_NW_TTL) {
491 flow->nw_ttl = 0;
492 }
eadef313 493 flow->nw_frag &= wildcards->nw_frag_mask;
993410fb
BP
494 if (wc & FWW_ARP_SHA) {
495 memset(flow->arp_sha, 0, sizeof flow->arp_sha);
496 }
497 if (wc & FWW_ARP_THA) {
498 memset(flow->arp_tha, 0, sizeof flow->arp_tha);
499 }
500 flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
501 &wildcards->ipv6_src_mask);
502 flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
503 &wildcards->ipv6_dst_mask);
504 if (wc & FWW_ND_TARGET) {
505 memset(&flow->nd_target, 0, sizeof flow->nd_target);
506 }
deedf7e7 507 flow->skb_priority = 0;
993410fb
BP
508}
509
064af421 510char *
ae412e7d 511flow_to_string(const struct flow *flow)
064af421
BP
512{
513 struct ds ds = DS_EMPTY_INITIALIZER;
514 flow_format(&ds, flow);
515 return ds_cstr(&ds);
516}
517
518void
ae412e7d 519flow_format(struct ds *ds, const struct flow *flow)
064af421 520{
2bcf7df6
EJ
521 ds_put_format(ds, "priority:%"PRIu32
522 ",tunnel:%#"PRIx64
523 ",in_port:%04"PRIx16,
deedf7e7 524 flow->skb_priority,
abff858b
PS
525 ntohll(flow->tun_id),
526 flow->in_port);
527
2bcf7df6 528 ds_put_format(ds, ",tci(");
66642cb4 529 if (flow->vlan_tci) {
2bcf7df6 530 ds_put_format(ds, "vlan:%"PRIu16",pcp:%d",
66642cb4
BP
531 vlan_tci_to_vid(flow->vlan_tci),
532 vlan_tci_to_pcp(flow->vlan_tci));
533 } else {
534 ds_put_char(ds, '0');
535 }
2bcf7df6
EJ
536 ds_put_format(ds, ") mac("ETH_ADDR_FMT"->"ETH_ADDR_FMT
537 ") type:%04"PRIx16,
659586ef
JG
538 ETH_ADDR_ARGS(flow->dl_src),
539 ETH_ADDR_ARGS(flow->dl_dst),
d31f1109
JP
540 ntohs(flow->dl_type));
541
542 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2bcf7df6
EJ
543 ds_put_format(ds, " label:%#"PRIx32" proto:%"PRIu8" tos:%#"PRIx8
544 " ttl:%"PRIu8" ipv6(",
a61680c6 545 ntohl(flow->ipv6_label), flow->nw_proto,
eadef313 546 flow->nw_tos, flow->nw_ttl);
d31f1109
JP
547 print_ipv6_addr(ds, &flow->ipv6_src);
548 ds_put_cstr(ds, "->");
549 print_ipv6_addr(ds, &flow->ipv6_dst);
2bcf7df6 550 ds_put_char(ds, ')');
d31f1109 551 } else {
2bcf7df6
EJ
552 ds_put_format(ds, " proto:%"PRIu8" tos:%#"PRIx8" ttl:%"PRIu8
553 " ip("IP_FMT"->"IP_FMT")",
554 flow->nw_proto, flow->nw_tos, flow->nw_ttl,
555 IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst));
d31f1109 556 }
eadef313 557 if (flow->nw_frag) {
7257b535 558 ds_put_format(ds, " frag(%s)",
eadef313
JP
559 flow->nw_frag == FLOW_NW_FRAG_ANY ? "first"
560 : flow->nw_frag == (FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER)
9e44d715 561 ? "later" : "<error>");
7257b535 562 }
bad68a99 563 if (flow->tp_src || flow->tp_dst) {
2bcf7df6 564 ds_put_format(ds, " port(%"PRIu16"->%"PRIu16")",
bad68a99
JP
565 ntohs(flow->tp_src), ntohs(flow->tp_dst));
566 }
567 if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
2bcf7df6 568 ds_put_format(ds, " arp_ha("ETH_ADDR_FMT"->"ETH_ADDR_FMT")",
bad68a99
JP
569 ETH_ADDR_ARGS(flow->arp_sha),
570 ETH_ADDR_ARGS(flow->arp_tha));
571 }
064af421
BP
572}
573
574void
ae412e7d 575flow_print(FILE *stream, const struct flow *flow)
064af421
BP
576{
577 char *s = flow_to_string(flow);
578 fputs(s, stream);
579 free(s);
580}
54363004
BP
581\f
582/* flow_wildcards functions. */
583
d8ae4d67 584/* Initializes 'wc' as a set of wildcards that matches every packet. */
54363004 585void
d8ae4d67 586flow_wildcards_init_catchall(struct flow_wildcards *wc)
54363004 587{
2486e66a 588 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
7257b535 589
d8ae4d67 590 wc->wildcards = FWW_ALL;
8368c090 591 wc->tun_id_mask = htonll(0);
d8ae4d67
BP
592 wc->nw_src_mask = htonl(0);
593 wc->nw_dst_mask = htonl(0);
d31f1109
JP
594 wc->ipv6_src_mask = in6addr_any;
595 wc->ipv6_dst_mask = in6addr_any;
b6c9e612 596 memset(wc->reg_masks, 0, sizeof wc->reg_masks);
66642cb4 597 wc->vlan_tci_mask = htons(0);
eadef313 598 wc->nw_frag_mask = 0;
dc56021d 599 memset(wc->zeros, 0, sizeof wc->zeros);
54363004
BP
600}
601
494e43a5
BP
602/* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
603 * wildcard any bits or fields. */
604void
605flow_wildcards_init_exact(struct flow_wildcards *wc)
606{
2486e66a 607 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
7257b535 608
b6c9e612 609 wc->wildcards = 0;
8368c090 610 wc->tun_id_mask = htonll(UINT64_MAX);
b6c9e612
BP
611 wc->nw_src_mask = htonl(UINT32_MAX);
612 wc->nw_dst_mask = htonl(UINT32_MAX);
d31f1109
JP
613 wc->ipv6_src_mask = in6addr_exact;
614 wc->ipv6_dst_mask = in6addr_exact;
b6c9e612 615 memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
66642cb4 616 wc->vlan_tci_mask = htons(UINT16_MAX);
eadef313 617 wc->nw_frag_mask = UINT8_MAX;
dc56021d 618 memset(wc->zeros, 0, sizeof wc->zeros);
494e43a5
BP
619}
620
00561f41
BP
621/* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
622 * fields. */
623bool
624flow_wildcards_is_exact(const struct flow_wildcards *wc)
625{
d8ae4d67 626 int i;
00561f41 627
2486e66a 628 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
ecf1e7ac 629
d8ae4d67 630 if (wc->wildcards
8368c090 631 || wc->tun_id_mask != htonll(UINT64_MAX)
d8ae4d67 632 || wc->nw_src_mask != htonl(UINT32_MAX)
66642cb4 633 || wc->nw_dst_mask != htonl(UINT32_MAX)
d31f1109
JP
634 || wc->vlan_tci_mask != htons(UINT16_MAX)
635 || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
7257b535 636 || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)
eadef313 637 || wc->nw_frag_mask != UINT8_MAX) {
d8ae4d67
BP
638 return false;
639 }
640
641 for (i = 0; i < FLOW_N_REGS; i++) {
d84d4b88 642 if (wc->reg_masks[i] != UINT32_MAX) {
d8ae4d67
BP
643 return false;
644 }
645 }
646
647 return true;
b5d97350
BP
648}
649
ecf1e7ac
BP
650/* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
651 * fields. */
652bool
653flow_wildcards_is_catchall(const struct flow_wildcards *wc)
654{
655 int i;
656
2486e66a 657 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
ecf1e7ac
BP
658
659 if (wc->wildcards != FWW_ALL
660 || wc->tun_id_mask != htonll(0)
661 || wc->nw_src_mask != htonl(0)
662 || wc->nw_dst_mask != htonl(0)
663 || wc->vlan_tci_mask != htons(0)
664 || !ipv6_mask_is_any(&wc->ipv6_src_mask)
7257b535 665 || !ipv6_mask_is_any(&wc->ipv6_dst_mask)
eadef313 666 || wc->nw_frag_mask != 0) {
ecf1e7ac
BP
667 return false;
668 }
669
670 for (i = 0; i < FLOW_N_REGS; i++) {
671 if (wc->reg_masks[i] != 0) {
672 return false;
673 }
674 }
675
676 return true;
677}
678
b5d97350
BP
679/* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
680 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
681 * 'src1' or 'src2' or both. */
682void
683flow_wildcards_combine(struct flow_wildcards *dst,
684 const struct flow_wildcards *src1,
685 const struct flow_wildcards *src2)
686{
b6c9e612 687 int i;
b5d97350 688
d8ae4d67 689 dst->wildcards = src1->wildcards | src2->wildcards;
8368c090 690 dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
b5d97350
BP
691 dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
692 dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
d31f1109
JP
693 dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
694 &src2->ipv6_src_mask);
695 dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
696 &src2->ipv6_dst_mask);
b6c9e612
BP
697 for (i = 0; i < FLOW_N_REGS; i++) {
698 dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
699 }
66642cb4 700 dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
b5d97350
BP
701}
702
703/* Returns a hash of the wildcards in 'wc'. */
704uint32_t
1006cda6 705flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
b5d97350 706{
d8ae4d67
BP
707 /* If you change struct flow_wildcards and thereby trigger this
708 * assertion, please check that the new struct flow_wildcards has no holes
709 * in it before you update the assertion. */
dc56021d 710 BUILD_ASSERT_DECL(sizeof *wc == 60 + FLOW_N_REGS * 4);
1006cda6 711 return hash_bytes(wc, sizeof *wc, basis);
b5d97350
BP
712}
713
714/* Returns true if 'a' and 'b' represent the same wildcards, false if they are
715 * different. */
716bool
717flow_wildcards_equal(const struct flow_wildcards *a,
718 const struct flow_wildcards *b)
719{
b6c9e612
BP
720 int i;
721
d8ae4d67 722 if (a->wildcards != b->wildcards
8368c090 723 || a->tun_id_mask != b->tun_id_mask
d8ae4d67 724 || a->nw_src_mask != b->nw_src_mask
66642cb4 725 || a->nw_dst_mask != b->nw_dst_mask
b53055f4 726 || a->vlan_tci_mask != b->vlan_tci_mask
d31f1109
JP
727 || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
728 || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
b6c9e612
BP
729 return false;
730 }
731
732 for (i = 0; i < FLOW_N_REGS; i++) {
733 if (a->reg_masks[i] != b->reg_masks[i]) {
734 return false;
735 }
736 }
737
738 return true;
b5d97350
BP
739}
740
741/* Returns true if at least one bit or field is wildcarded in 'a' but not in
742 * 'b', false otherwise. */
743bool
744flow_wildcards_has_extra(const struct flow_wildcards *a,
745 const struct flow_wildcards *b)
746{
b6c9e612 747 int i;
d31f1109 748 struct in6_addr ipv6_masked;
b6c9e612
BP
749
750 for (i = 0; i < FLOW_N_REGS; i++) {
751 if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
752 return true;
753 }
754 }
755
d31f1109
JP
756 ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
757 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
758 return true;
759 }
760
761 ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
762 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
763 return true;
764 }
765
d8ae4d67 766 return (a->wildcards & ~b->wildcards
8368c090 767 || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
b5d97350 768 || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
66642cb4
BP
769 || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
770 || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
b5d97350
BP
771}
772
b6c9e612
BP
773/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
774 * (A 0-bit indicates a wildcard bit.) */
775void
776flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
777{
d8ae4d67 778 wc->reg_masks[idx] = mask;
b6c9e612 779}
ff55ea1f 780
db7f8281
BP
781/* Returns the wildcard bitmask for the Ethernet destination address
782 * that 'wc' specifies. The bitmask has a 0 in each bit that is wildcarded
783 * and a 1 in each bit that must match. */
784const uint8_t *
785flow_wildcards_to_dl_dst_mask(flow_wildcards_t wc)
786{
787 static const uint8_t no_wild[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
788 static const uint8_t addr_wild[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
789 static const uint8_t mcast_wild[] = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
790 static const uint8_t all_wild[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
791
792 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
793 case 0: return no_wild;
794 case FWW_DL_DST: return addr_wild;
795 case FWW_ETH_MCAST: return mcast_wild;
796 case FWW_DL_DST | FWW_ETH_MCAST: return all_wild;
797 }
798 NOT_REACHED();
799}
800
801/* Returns true if 'mask' is a valid wildcard bitmask for the Ethernet
802 * destination address. Valid bitmasks are either all-bits-0 or all-bits-1,
803 * except that the multicast bit may differ from the rest of the bits. So,
804 * there are four possible valid bitmasks:
805 *
806 * - 00:00:00:00:00:00
807 * - 01:00:00:00:00:00
808 * - fe:ff:ff:ff:ff:ff
809 * - ff:ff:ff:ff:ff:ff
810 *
811 * All other bitmasks are invalid. */
812bool
813flow_wildcards_is_dl_dst_mask_valid(const uint8_t mask[ETH_ADDR_LEN])
814{
815 switch (mask[0]) {
816 case 0x00:
817 case 0x01:
818 return (mask[1] | mask[2] | mask[3] | mask[4] | mask[5]) == 0x00;
819
820 case 0xfe:
821 case 0xff:
822 return (mask[1] & mask[2] & mask[3] & mask[4] & mask[5]) == 0xff;
823
824 default:
825 return false;
826 }
827}
828
829/* Returns 'wc' with the FWW_DL_DST and FWW_ETH_MCAST bits modified
830 * appropriately to match 'mask'.
831 *
832 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
833 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
834flow_wildcards_t
835flow_wildcards_set_dl_dst_mask(flow_wildcards_t wc,
836 const uint8_t mask[ETH_ADDR_LEN])
837{
838 assert(flow_wildcards_is_dl_dst_mask_valid(mask));
839
840 switch (mask[0]) {
841 case 0x00:
842 return wc | FWW_DL_DST | FWW_ETH_MCAST;
843
844 case 0x01:
845 return (wc | FWW_DL_DST) & ~FWW_ETH_MCAST;
846
847 case 0xfe:
848 return (wc & ~FWW_DL_DST) | FWW_ETH_MCAST;
849
850 case 0xff:
851 return wc & ~(FWW_DL_DST | FWW_ETH_MCAST);
852
853 default:
854 NOT_REACHED();
855 }
856}
857
ff55ea1f
EJ
858/* Hashes 'flow' based on its L2 through L4 protocol information. */
859uint32_t
860flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
861{
862 struct {
d31f1109
JP
863 union {
864 ovs_be32 ipv4_addr;
865 struct in6_addr ipv6_addr;
866 };
ff55ea1f
EJ
867 ovs_be16 eth_type;
868 ovs_be16 vlan_tci;
869 ovs_be16 tp_addr;
870 uint8_t eth_addr[ETH_ADDR_LEN];
871 uint8_t ip_proto;
872 } fields;
873
874 int i;
875
876 memset(&fields, 0, sizeof fields);
877 for (i = 0; i < ETH_ADDR_LEN; i++) {
878 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
879 }
880 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
881 fields.eth_type = flow->dl_type;
3e3eda95
EJ
882
883 /* UDP source and destination port are not taken into account because they
884 * will not necessarily be symmetric in a bidirectional flow. */
ff55ea1f 885 if (fields.eth_type == htons(ETH_TYPE_IP)) {
d31f1109
JP
886 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
887 fields.ip_proto = flow->nw_proto;
3e3eda95 888 if (fields.ip_proto == IPPROTO_TCP) {
d31f1109
JP
889 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
890 }
891 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
892 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
893 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
894 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
895
896 for (i=0; i<16; i++) {
897 ipv6_addr[i] = a[i] ^ b[i];
898 }
ff55ea1f 899 fields.ip_proto = flow->nw_proto;
3e3eda95 900 if (fields.ip_proto == IPPROTO_TCP) {
ff55ea1f 901 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
ff55ea1f 902 }
ff55ea1f
EJ
903 }
904 return hash_bytes(&fields, sizeof fields, basis);
905}
520e9a2a
EJ
906
907/* Hashes the portions of 'flow' designated by 'fields'. */
908uint32_t
909flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
910 uint16_t basis)
911{
912 switch (fields) {
913
914 case NX_HASH_FIELDS_ETH_SRC:
915 return hash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
916
917 case NX_HASH_FIELDS_SYMMETRIC_L4:
918 return flow_hash_symmetric_l4(flow, basis);
919 }
920
921 NOT_REACHED();
922}
923
924/* Returns a string representation of 'fields'. */
925const char *
926flow_hash_fields_to_str(enum nx_hash_fields fields)
927{
928 switch (fields) {
929 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
930 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
931 default: return "<unknown>";
932 }
933}
934
935/* Returns true if the value of 'fields' is supported. Otherwise false. */
936bool
937flow_hash_fields_valid(enum nx_hash_fields fields)
938{
939 return fields == NX_HASH_FIELDS_ETH_SRC
940 || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
941}
8b3b8dd1 942
3719455c
BP
943/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
944 * OpenFlow 1.0 "dl_vlan" value:
945 *
946 * - If it is in the range 0...4095, 'flow->vlan_tci' is set to match
947 * that VLAN. Any existing PCP match is unchanged (it becomes 0 if
948 * 'flow' previously matched packets without a VLAN header).
949 *
950 * - If it is OFP_VLAN_NONE, 'flow->vlan_tci' is set to match a packet
951 * without a VLAN tag.
952 *
953 * - Other values of 'vid' should not be used. */
954void
955flow_set_vlan_vid(struct flow *flow, ovs_be16 vid)
956{
957 if (vid == htons(OFP_VLAN_NONE)) {
958 flow->vlan_tci = htons(0);
959 } else {
960 vid &= htons(VLAN_VID_MASK);
961 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
962 flow->vlan_tci |= htons(VLAN_CFI) | vid;
963 }
964}
965
966/* Sets the VLAN PCP that 'flow' matches to 'pcp', which should be in the
967 * range 0...7.
968 *
969 * This function has no effect on the VLAN ID that 'flow' matches.
970 *
971 * After calling this function, 'flow' will not match packets without a VLAN
972 * header. */
973void
974flow_set_vlan_pcp(struct flow *flow, uint8_t pcp)
975{
976 pcp &= 0x07;
977 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
978 flow->vlan_tci |= htons((pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
979}
980
8b3b8dd1
BP
981/* Puts into 'b' a packet that flow_extract() would parse as having the given
982 * 'flow'.
983 *
984 * (This is useful only for testing, obviously, and the packet isn't really
985 * valid. It hasn't got any checksums filled in, for one, and lots of fields
986 * are just zeroed.) */
987void
988flow_compose(struct ofpbuf *b, const struct flow *flow)
989{
990 eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
991 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
992 struct eth_header *eth = b->l2;
993 eth->eth_type = htons(b->size);
994 return;
995 }
996
997 if (flow->vlan_tci & htons(VLAN_CFI)) {
998 eth_push_vlan(b, flow->vlan_tci & ~htons(VLAN_CFI));
999 }
1000
1001 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1002 struct ip_header *ip;
1003
1004 b->l3 = ip = ofpbuf_put_zeros(b, sizeof *ip);
1005 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
eadef313 1006 ip->ip_tos = flow->nw_tos;
8b3b8dd1
BP
1007 ip->ip_proto = flow->nw_proto;
1008 ip->ip_src = flow->nw_src;
1009 ip->ip_dst = flow->nw_dst;
1010
eadef313 1011 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
7257b535 1012 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
eadef313 1013 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
7257b535
BP
1014 ip->ip_frag_off |= htons(100);
1015 }
1016 }
eadef313
JP
1017 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
1018 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
7257b535
BP
1019 if (flow->nw_proto == IPPROTO_TCP) {
1020 struct tcp_header *tcp;
1021
1022 b->l4 = tcp = ofpbuf_put_zeros(b, sizeof *tcp);
1023 tcp->tcp_src = flow->tp_src;
1024 tcp->tcp_dst = flow->tp_dst;
df9b6612 1025 tcp->tcp_ctl = TCP_CTL(0, 5);
7257b535
BP
1026 } else if (flow->nw_proto == IPPROTO_UDP) {
1027 struct udp_header *udp;
1028
1029 b->l4 = udp = ofpbuf_put_zeros(b, sizeof *udp);
1030 udp->udp_src = flow->tp_src;
1031 udp->udp_dst = flow->tp_dst;
1032 } else if (flow->nw_proto == IPPROTO_ICMP) {
1033 struct icmp_header *icmp;
1034
1035 b->l4 = icmp = ofpbuf_put_zeros(b, sizeof *icmp);
1036 icmp->icmp_type = ntohs(flow->tp_src);
1037 icmp->icmp_code = ntohs(flow->tp_dst);
1038 }
8b3b8dd1 1039 }
df9b6612
BP
1040
1041 ip->ip_tot_len = htons((uint8_t *) b->data + b->size
1042 - (uint8_t *) b->l3);
8b3b8dd1
BP
1043 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1044 /* XXX */
1045 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1046 struct arp_eth_header *arp;
1047
1048 b->l3 = arp = ofpbuf_put_zeros(b, sizeof *arp);
1049 arp->ar_hrd = htons(1);
1050 arp->ar_pro = htons(ETH_TYPE_IP);
1051 arp->ar_hln = ETH_ADDR_LEN;
1052 arp->ar_pln = 4;
1053 arp->ar_op = htons(flow->nw_proto);
1054
1055 if (flow->nw_proto == ARP_OP_REQUEST ||
1056 flow->nw_proto == ARP_OP_REPLY) {
1057 arp->ar_spa = flow->nw_src;
1058 arp->ar_tpa = flow->nw_dst;
1059 memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1060 memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);
1061 }
1062 }
1063}