]> git.proxmox.com Git - mirror_ovs.git/blame - lib/flow.c
AUTHORS: Add Dongdong and Chunhe Li.
[mirror_ovs.git] / lib / flow.c
CommitLineData
064af421 1/*
8bfd0fda 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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"
d31f1109 19#include <errno.h>
064af421 20#include <inttypes.h>
5cb7a798 21#include <limits.h>
064af421 22#include <netinet/in.h>
d31f1109
JP
23#include <netinet/icmp6.h>
24#include <netinet/ip6.h>
5cb7a798 25#include <stdint.h>
064af421
BP
26#include <stdlib.h>
27#include <string.h>
10a24935 28#include "byte-order.h"
064af421 29#include "coverage.h"
dc5a7ce7 30#include "csum.h"
064af421
BP
31#include "dynamic-string.h"
32#include "hash.h"
c49d1dd1 33#include "jhash.h"
aa6c9932 34#include "match.h"
064af421
BP
35#include "ofpbuf.h"
36#include "openflow/openflow.h"
064af421 37#include "packets.h"
b5e7e61a 38#include "odp-util.h"
94639963 39#include "random.h"
176aaa65 40#include "unaligned.h"
064af421 41
d76f09ea 42COVERAGE_DEFINE(flow_extract);
5cb7a798 43COVERAGE_DEFINE(miniflow_malloc);
d76f09ea 44
476f36e8
JR
45/* U32 indices for segmented flow classification. */
46const uint8_t flow_segment_u32s[4] = {
47 FLOW_SEGMENT_1_ENDS_AT / 4,
48 FLOW_SEGMENT_2_ENDS_AT / 4,
49 FLOW_SEGMENT_3_ENDS_AT / 4,
50 FLOW_U32S
51};
52
419681da
JR
53/* miniflow_extract() assumes the following to be true to optimize the
54 * extraction process. */
55BUILD_ASSERT_DECL(offsetof(struct flow, dl_type) + 2
56 == offsetof(struct flow, vlan_tci) &&
57 offsetof(struct flow, dl_type) / 4
58 == offsetof(struct flow, vlan_tci) / 4 );
59
60BUILD_ASSERT_DECL(offsetof(struct flow, nw_frag) + 3
61 == offsetof(struct flow, nw_proto) &&
62 offsetof(struct flow, nw_tos) + 2
63 == offsetof(struct flow, nw_proto) &&
64 offsetof(struct flow, nw_ttl) + 1
65 == offsetof(struct flow, nw_proto) &&
66 offsetof(struct flow, nw_frag) / 4
67 == offsetof(struct flow, nw_tos) / 4 &&
68 offsetof(struct flow, nw_ttl) / 4
69 == offsetof(struct flow, nw_tos) / 4 &&
70 offsetof(struct flow, nw_proto) / 4
71 == offsetof(struct flow, nw_tos) / 4);
72
73/* TCP flags in the first half of a BE32, zeroes in the other half. */
74BUILD_ASSERT_DECL(offsetof(struct flow, tcp_flags) + 2
75 == offsetof(struct flow, pad) &&
76 offsetof(struct flow, tcp_flags) / 4
77 == offsetof(struct flow, pad) / 4);
78#if WORDS_BIGENDIAN
79#define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl) \
80 << 16)
81#else
82#define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl))
83#endif
84
85BUILD_ASSERT_DECL(offsetof(struct flow, tp_src) + 2
86 == offsetof(struct flow, tp_dst) &&
87 offsetof(struct flow, tp_src) / 4
88 == offsetof(struct flow, tp_dst) / 4);
89
90/* Removes 'size' bytes from the head end of '*datap', of size '*sizep', which
91 * must contain at least 'size' bytes of data. Returns the first byte of data
92 * removed. */
93static inline const void *
94data_pull(void **datap, size_t *sizep, size_t size)
a26ef517 95{
419681da
JR
96 char *data = (char *)*datap;
97 *datap = data + size;
98 *sizep -= size;
99 return data;
a26ef517
JP
100}
101
419681da
JR
102/* If '*datap' has at least 'size' bytes of data, removes that many bytes from
103 * the head end of '*datap' and returns the first byte removed. Otherwise,
104 * returns a null pointer without modifying '*datap'. */
105static inline const void *
106data_try_pull(void **datap, size_t *sizep, size_t size)
064af421 107{
419681da 108 return OVS_LIKELY(*sizep >= size) ? data_pull(datap, sizep, size) : NULL;
064af421
BP
109}
110
419681da
JR
111/* Context for pushing data to a miniflow. */
112struct mf_ctx {
113 uint64_t map;
114 uint32_t *data;
115 uint32_t * const end;
116};
064af421 117
419681da
JR
118/* miniflow_push_* macros allow filling in a miniflow data values in order.
119 * Assertions are needed only when the layout of the struct flow is modified.
120 * 'ofs' is a compile-time constant, which allows most of the code be optimized
694ffecc 121 * away. Some GCC versions gave warnings on ALWAYS_INLINE, so these are
419681da
JR
122 * defined as macros. */
123
694ffecc 124#if (FLOW_WC_SEQ != 27)
419681da 125#define MINIFLOW_ASSERT(X) ovs_assert(X)
dce96af8
DDP
126BUILD_MESSAGE("FLOW_WC_SEQ changed: miniflow_extract() will have runtime "
127 "assertions enabled. Consider updating FLOW_WC_SEQ after "
128 "testing")
419681da
JR
129#else
130#define MINIFLOW_ASSERT(X)
131#endif
132
133#define miniflow_push_uint32_(MF, OFS, VALUE) \
134{ \
135 MINIFLOW_ASSERT(MF.data < MF.end && (OFS) % 4 == 0 \
136 && !(MF.map & (UINT64_MAX << (OFS) / 4))); \
137 *MF.data++ = VALUE; \
138 MF.map |= UINT64_C(1) << (OFS) / 4; \
d31f1109
JP
139}
140
419681da
JR
141#define miniflow_push_be32_(MF, OFS, VALUE) \
142 miniflow_push_uint32_(MF, OFS, (OVS_FORCE uint32_t)(VALUE))
143
144#define miniflow_push_uint16_(MF, OFS, VALUE) \
145{ \
146 MINIFLOW_ASSERT(MF.data < MF.end && \
147 (((OFS) % 4 == 0 && !(MF.map & (UINT64_MAX << (OFS) / 4))) \
148 || ((OFS) % 4 == 2 && MF.map & (UINT64_C(1) << (OFS) / 4) \
149 && !(MF.map & (UINT64_MAX << ((OFS) / 4 + 1)))))); \
150 \
151 if ((OFS) % 4 == 0) { \
152 *(uint16_t *)MF.data = VALUE; \
153 MF.map |= UINT64_C(1) << (OFS) / 4; \
154 } else if ((OFS) % 4 == 2) { \
155 *((uint16_t *)MF.data + 1) = VALUE; \
156 MF.data++; \
157 } \
b02475c5
SH
158}
159
419681da
JR
160#define miniflow_push_be16_(MF, OFS, VALUE) \
161 miniflow_push_uint16_(MF, OFS, (OVS_FORCE uint16_t)VALUE);
162
163/* Data at 'valuep' may be unaligned. */
164#define miniflow_push_words_(MF, OFS, VALUEP, N_WORDS) \
165{ \
166 int ofs32 = (OFS) / 4; \
167 \
168 MINIFLOW_ASSERT(MF.data + (N_WORDS) <= MF.end && (OFS) % 4 == 0 \
169 && !(MF.map & (UINT64_MAX << ofs32))); \
170 \
171 memcpy(MF.data, (VALUEP), (N_WORDS) * sizeof *MF.data); \
172 MF.data += (N_WORDS); \
173 MF.map |= ((UINT64_MAX >> (64 - (N_WORDS))) << ofs32); \
064af421
BP
174}
175
419681da
JR
176#define miniflow_push_uint32(MF, FIELD, VALUE) \
177 miniflow_push_uint32_(MF, offsetof(struct flow, FIELD), VALUE)
50f06e16 178
419681da
JR
179#define miniflow_push_be32(MF, FIELD, VALUE) \
180 miniflow_push_be32_(MF, offsetof(struct flow, FIELD), VALUE)
50f06e16 181
419681da
JR
182#define miniflow_push_uint32_check(MF, FIELD, VALUE) \
183 { if (OVS_LIKELY(VALUE)) { \
184 miniflow_push_uint32_(MF, offsetof(struct flow, FIELD), VALUE); \
185 } \
50f06e16
BP
186 }
187
419681da
JR
188#define miniflow_push_be32_check(MF, FIELD, VALUE) \
189 { if (OVS_LIKELY(VALUE)) { \
190 miniflow_push_be32_(MF, offsetof(struct flow, FIELD), VALUE); \
191 } \
50f06e16
BP
192 }
193
419681da
JR
194#define miniflow_push_uint16(MF, FIELD, VALUE) \
195 miniflow_push_uint16_(MF, offsetof(struct flow, FIELD), VALUE)
9e69bc5f 196
419681da
JR
197#define miniflow_push_be16(MF, FIELD, VALUE) \
198 miniflow_push_be16_(MF, offsetof(struct flow, FIELD), VALUE)
9e69bc5f 199
419681da
JR
200#define miniflow_push_words(MF, FIELD, VALUEP, N_WORDS) \
201 miniflow_push_words_(MF, offsetof(struct flow, FIELD), VALUEP, N_WORDS)
064af421 202
419681da
JR
203/* Pulls the MPLS headers at '*datap' and returns the count of them. */
204static inline int
205parse_mpls(void **datap, size_t *sizep)
d31f1109 206{
419681da
JR
207 const struct mpls_hdr *mh;
208 int count = 0;
d31f1109 209
419681da
JR
210 while ((mh = data_try_pull(datap, sizep, sizeof *mh))) {
211 count++;
212 if (mh->mpls_lse.lo & htons(1 << MPLS_BOS_SHIFT)) {
d31f1109
JP
213 break;
214 }
419681da 215 }
ba8561c6 216 return MIN(count, FLOW_MAX_MPLS_LABELS);
419681da 217}
d31f1109 218
419681da
JR
219static inline ovs_be16
220parse_vlan(void **datap, size_t *sizep)
221{
222 const struct eth_header *eth = *datap;
d31f1109 223
419681da
JR
224 struct qtag_prefix {
225 ovs_be16 eth_type; /* ETH_TYPE_VLAN */
226 ovs_be16 tci;
227 };
d31f1109 228
419681da 229 data_pull(datap, sizep, ETH_ADDR_LEN * 2);
d31f1109 230
419681da
JR
231 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
232 if (OVS_LIKELY(*sizep
233 >= sizeof(struct qtag_prefix) + sizeof(ovs_be16))) {
234 const struct qtag_prefix *qp = data_pull(datap, sizep, sizeof *qp);
235 return qp->tci | htons(VLAN_CFI);
d31f1109
JP
236 }
237 }
88366484 238 return 0;
d31f1109
JP
239}
240
419681da
JR
241static inline ovs_be16
242parse_ethertype(void **datap, size_t *sizep)
88366484 243{
419681da
JR
244 const struct llc_snap_header *llc;
245 ovs_be16 proto;
5a51b2cd 246
419681da
JR
247 proto = *(ovs_be16 *) data_pull(datap, sizep, sizeof proto);
248 if (OVS_LIKELY(ntohs(proto) >= ETH_TYPE_MIN)) {
249 return proto;
88366484 250 }
5a51b2cd 251
419681da
JR
252 if (OVS_UNLIKELY(*sizep < sizeof *llc)) {
253 return htons(FLOW_DL_TYPE_NONE);
88366484 254 }
5a51b2cd 255
419681da
JR
256 llc = *datap;
257 if (OVS_UNLIKELY(llc->llc.llc_dsap != LLC_DSAP_SNAP
258 || llc->llc.llc_ssap != LLC_SSAP_SNAP
259 || llc->llc.llc_cntl != LLC_CNTL_SNAP
260 || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
261 sizeof llc->snap.snap_org))) {
262 return htons(FLOW_DL_TYPE_NONE);
c6bcb685 263 }
c6bcb685 264
419681da 265 data_pull(datap, sizep, sizeof *llc);
685a51a5 266
419681da
JR
267 if (OVS_LIKELY(ntohs(llc->snap.snap_type) >= ETH_TYPE_MIN)) {
268 return llc->snap.snap_type;
685a51a5
JP
269 }
270
419681da
JR
271 return htons(FLOW_DL_TYPE_NONE);
272}
685a51a5 273
419681da
JR
274static inline bool
275parse_icmpv6(void **datap, size_t *sizep, const struct icmp6_hdr *icmp,
276 const struct in6_addr **nd_target,
277 uint8_t arp_buf[2][ETH_ADDR_LEN])
278{
88366484
JG
279 if (icmp->icmp6_code == 0 &&
280 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
281 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
685a51a5 282
b0e2ec32 283 *nd_target = data_try_pull(datap, sizep, sizeof **nd_target);
419681da
JR
284 if (OVS_UNLIKELY(!*nd_target)) {
285 return false;
685a51a5 286 }
685a51a5 287
419681da 288 while (*sizep >= 8) {
685a51a5
JP
289 /* The minimum size of an option is 8 bytes, which also is
290 * the size of Ethernet link-layer options. */
419681da 291 const struct nd_opt_hdr *nd_opt = *datap;
88366484
JG
292 int opt_len = nd_opt->nd_opt_len * 8;
293
419681da 294 if (!opt_len || opt_len > *sizep) {
685a51a5
JP
295 goto invalid;
296 }
685a51a5
JP
297
298 /* Store the link layer address if the appropriate option is
299 * provided. It is considered an error if the same link
300 * layer option is specified twice. */
301 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
302 && opt_len == 8) {
419681da
JR
303 if (OVS_LIKELY(eth_addr_is_zero(arp_buf[0]))) {
304 memcpy(arp_buf[0], nd_opt + 1, ETH_ADDR_LEN);
685a51a5
JP
305 } else {
306 goto invalid;
307 }
308 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
309 && opt_len == 8) {
419681da
JR
310 if (OVS_LIKELY(eth_addr_is_zero(arp_buf[1]))) {
311 memcpy(arp_buf[1], nd_opt + 1, ETH_ADDR_LEN);
685a51a5
JP
312 } else {
313 goto invalid;
314 }
315 }
316
419681da 317 if (OVS_UNLIKELY(!data_try_pull(datap, sizep, opt_len))) {
685a51a5
JP
318 goto invalid;
319 }
685a51a5
JP
320 }
321 }
322
419681da 323 return true;
685a51a5
JP
324
325invalid:
419681da 326 return false;
685a51a5
JP
327}
328
b5e7e61a 329/* Initializes 'flow' members from 'packet' and 'md'
deedf7e7 330 *
437d0d22
JR
331 * Initializes 'packet' header l2 pointer to the start of the Ethernet
332 * header, and the layer offsets as follows:
ca78c6b6 333 *
437d0d22
JR
334 * - packet->l2_5_ofs to the start of the MPLS shim header, or UINT16_MAX
335 * when there is no MPLS shim header.
ca78c6b6 336 *
437d0d22 337 * - packet->l3_ofs to just past the Ethernet header, or just past the
ca78c6b6 338 * vlan_header if one is present, to the first byte of the payload of the
437d0d22
JR
339 * Ethernet frame. UINT16_MAX if the frame is too short to contain an
340 * Ethernet header.
ca78c6b6 341 *
437d0d22
JR
342 * - packet->l4_ofs to just past the IPv4 header, if one is present and
343 * has at least the content used for the fields of interest for the flow,
344 * otherwise UINT16_MAX.
ca78c6b6 345 */
7257b535 346void
b5e7e61a 347flow_extract(struct ofpbuf *packet, const struct pkt_metadata *md,
296e07ac 348 struct flow *flow)
064af421 349{
27bbe15d
JR
350 struct {
351 struct miniflow mf;
352 uint32_t buf[FLOW_U32S];
353 } m;
064af421
BP
354
355 COVERAGE_INC(flow_extract);
356
27bbe15d
JR
357 miniflow_initialize(&m.mf, m.buf);
358 miniflow_extract(packet, md, &m.mf);
359 miniflow_expand(&m.mf, flow);
419681da 360}
296e07ac 361
27bbe15d
JR
362/* Caller is responsible for initializing 'dst' with enough storage for
363 * FLOW_U32S * 4 bytes. */
419681da
JR
364void
365miniflow_extract(struct ofpbuf *packet, const struct pkt_metadata *md,
366 struct miniflow *dst)
367{
368 void *data = ofpbuf_data(packet);
369 size_t size = ofpbuf_size(packet);
27bbe15d
JR
370 uint32_t *values = miniflow_values(dst);
371 struct mf_ctx mf = { 0, values, values + FLOW_U32S };
419681da 372 char *l2;
419681da
JR
373 ovs_be16 dl_type;
374 uint8_t nw_frag, nw_tos, nw_ttl, nw_proto;
375
376 /* Metadata. */
b5e7e61a 377 if (md) {
419681da
JR
378 if (md->tunnel.ip_dst) {
379 miniflow_push_words(mf, tunnel, &md->tunnel,
380 sizeof md->tunnel / 4);
381 }
382 miniflow_push_uint32_check(mf, skb_priority, md->skb_priority);
383 miniflow_push_uint32_check(mf, pkt_mark, md->pkt_mark);
384 miniflow_push_uint32_check(mf, recirc_id, md->recirc_id);
385 miniflow_push_uint32(mf, in_port, odp_to_u32(md->in_port.odp_port));
296e07ac 386 }
064af421 387
419681da
JR
388 /* Initialize packet's layer pointer and offsets. */
389 l2 = data;
390 ofpbuf_set_frame(packet, data);
064af421 391
419681da
JR
392 /* Must have full Ethernet header to proceed. */
393 if (OVS_UNLIKELY(size < sizeof(struct eth_header))) {
394 goto out;
395 } else {
396 ovs_be16 vlan_tci;
50f06e16 397
419681da
JR
398 /* Link layer. */
399 BUILD_ASSERT(offsetof(struct flow, dl_dst) + 6
400 == offsetof(struct flow, dl_src));
401 miniflow_push_words(mf, dl_dst, data, ETH_ADDR_LEN * 2 / 4);
402 /* dl_type, vlan_tci. */
403 vlan_tci = parse_vlan(&data, &size);
404 dl_type = parse_ethertype(&data, &size);
405 miniflow_push_be16(mf, dl_type, dl_type);
406 miniflow_push_be16(mf, vlan_tci, vlan_tci);
50f06e16 407 }
50f06e16 408
419681da
JR
409 /* Parse mpls. */
410 if (OVS_UNLIKELY(eth_type_mpls(dl_type))) {
411 int count;
412 const void *mpls = data;
413
414 packet->l2_5_ofs = (char *)data - l2;
415 count = parse_mpls(&data, &size);
416 miniflow_push_words(mf, mpls_lse, mpls, count);
b02475c5
SH
417 }
418
ad128cc1 419 /* Network layer. */
419681da
JR
420 packet->l3_ofs = (char *)data - l2;
421
422 nw_frag = 0;
423 if (OVS_LIKELY(dl_type == htons(ETH_TYPE_IP))) {
424 const struct ip_header *nh = data;
425 int ip_len;
426
427 if (OVS_UNLIKELY(size < IP_HEADER_LEN)) {
428 goto out;
429 }
430 ip_len = IP_IHL(nh->ip_ihl_ver) * 4;
431
432 if (OVS_UNLIKELY(ip_len < IP_HEADER_LEN)) {
433 goto out;
434 }
435
436 /* Push both source and destination address at once. */
437 miniflow_push_words(mf, nw_src, &nh->ip_src, 2);
438
439 nw_tos = nh->ip_tos;
440 nw_ttl = nh->ip_ttl;
441 nw_proto = nh->ip_proto;
442 if (OVS_UNLIKELY(IP_IS_FRAGMENT(nh->ip_frag_off))) {
443 nw_frag = FLOW_NW_FRAG_ANY;
444 if (nh->ip_frag_off & htons(IP_FRAG_OFF_MASK)) {
445 nw_frag |= FLOW_NW_FRAG_LATER;
446 }
447 }
448 if (OVS_UNLIKELY(size < ip_len)) {
449 goto out;
450 }
451 data_pull(&data, &size, ip_len);
452
453 } else if (dl_type == htons(ETH_TYPE_IPV6)) {
454 const struct ovs_16aligned_ip6_hdr *nh;
455 ovs_be32 tc_flow;
456
457 if (OVS_UNLIKELY(size < sizeof *nh)) {
458 goto out;
459 }
460 nh = data_pull(&data, &size, sizeof *nh);
461
462 miniflow_push_words(mf, ipv6_src, &nh->ip6_src,
463 sizeof nh->ip6_src / 4);
464 miniflow_push_words(mf, ipv6_dst, &nh->ip6_dst,
465 sizeof nh->ip6_dst / 4);
466
467 tc_flow = get_16aligned_be32(&nh->ip6_flow);
468 {
469 ovs_be32 label = tc_flow & htonl(IPV6_LABEL_MASK);
470 miniflow_push_be32_check(mf, ipv6_label, label);
471 }
472
473 nw_tos = ntohl(tc_flow) >> 20;
474 nw_ttl = nh->ip6_hlim;
475 nw_proto = nh->ip6_nxt;
476
477 while (1) {
478 if (OVS_LIKELY((nw_proto != IPPROTO_HOPOPTS)
479 && (nw_proto != IPPROTO_ROUTING)
480 && (nw_proto != IPPROTO_DSTOPTS)
481 && (nw_proto != IPPROTO_AH)
482 && (nw_proto != IPPROTO_FRAGMENT))) {
483 /* It's either a terminal header (e.g., TCP, UDP) or one we
484 * don't understand. In either case, we're done with the
485 * packet, so use it to fill in 'nw_proto'. */
486 break;
487 }
488
489 /* We only verify that at least 8 bytes of the next header are
490 * available, but many of these headers are longer. Ensure that
491 * accesses within the extension header are within those first 8
492 * bytes. All extension headers are required to be at least 8
493 * bytes. */
494 if (OVS_UNLIKELY(size < 8)) {
495 goto out;
7257b535 496 }
419681da
JR
497
498 if ((nw_proto == IPPROTO_HOPOPTS)
499 || (nw_proto == IPPROTO_ROUTING)
500 || (nw_proto == IPPROTO_DSTOPTS)) {
501 /* These headers, while different, have the fields we care
502 * about in the same location and with the same
503 * interpretation. */
504 const struct ip6_ext *ext_hdr = data;
505 nw_proto = ext_hdr->ip6e_nxt;
506 if (OVS_UNLIKELY(!data_try_pull(&data, &size,
507 (ext_hdr->ip6e_len + 1) * 8))) {
508 goto out;
509 }
510 } else if (nw_proto == IPPROTO_AH) {
511 /* A standard AH definition isn't available, but the fields
512 * we care about are in the same location as the generic
513 * option header--only the header length is calculated
514 * differently. */
515 const struct ip6_ext *ext_hdr = data;
516 nw_proto = ext_hdr->ip6e_nxt;
517 if (OVS_UNLIKELY(!data_try_pull(&data, &size,
518 (ext_hdr->ip6e_len + 2) * 4))) {
519 goto out;
520 }
521 } else if (nw_proto == IPPROTO_FRAGMENT) {
522 const struct ovs_16aligned_ip6_frag *frag_hdr = data;
523
524 nw_proto = frag_hdr->ip6f_nxt;
525 if (!data_try_pull(&data, &size, sizeof *frag_hdr)) {
526 goto out;
527 }
528
529 /* We only process the first fragment. */
530 if (frag_hdr->ip6f_offlg != htons(0)) {
531 nw_frag = FLOW_NW_FRAG_ANY;
532 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
533 nw_frag |= FLOW_NW_FRAG_LATER;
534 nw_proto = IPPROTO_FRAGMENT;
535 break;
064af421 536 }
064af421 537 }
50f06e16
BP
538 }
539 }
419681da
JR
540 } else {
541 if (dl_type == htons(ETH_TYPE_ARP) ||
542 dl_type == htons(ETH_TYPE_RARP)) {
543 uint8_t arp_buf[2][ETH_ADDR_LEN];
544 const struct arp_eth_header *arp = (const struct arp_eth_header *)
545 data_try_pull(&data, &size, ARP_ETH_HEADER_LEN);
546
547 if (OVS_LIKELY(arp) && OVS_LIKELY(arp->ar_hrd == htons(1))
548 && OVS_LIKELY(arp->ar_pro == htons(ETH_TYPE_IP))
549 && OVS_LIKELY(arp->ar_hln == ETH_ADDR_LEN)
550 && OVS_LIKELY(arp->ar_pln == 4)) {
551 miniflow_push_words(mf, nw_src, &arp->ar_spa, 1);
552 miniflow_push_words(mf, nw_dst, &arp->ar_tpa, 1);
553
554 /* We only match on the lower 8 bits of the opcode. */
555 if (OVS_LIKELY(ntohs(arp->ar_op) <= 0xff)) {
556 miniflow_push_be32(mf, nw_frag, htonl(ntohs(arp->ar_op)));
557 }
d31f1109 558
419681da
JR
559 /* Must be adjacent. */
560 BUILD_ASSERT(offsetof(struct flow, arp_sha) + 6
561 == offsetof(struct flow, arp_tha));
562
563 memcpy(arp_buf[0], arp->ar_sha, ETH_ADDR_LEN);
564 memcpy(arp_buf[1], arp->ar_tha, ETH_ADDR_LEN);
565 miniflow_push_words(mf, arp_sha, arp_buf,
566 ETH_ADDR_LEN * 2 / 4);
567 }
d31f1109 568 }
419681da
JR
569 goto out;
570 }
571
572 packet->l4_ofs = (char *)data - l2;
573 miniflow_push_be32(mf, nw_frag,
574 BYTES_TO_BE32(nw_frag, nw_tos, nw_ttl, nw_proto));
575
576 if (OVS_LIKELY(!(nw_frag & FLOW_NW_FRAG_LATER))) {
577 if (OVS_LIKELY(nw_proto == IPPROTO_TCP)) {
578 if (OVS_LIKELY(size >= TCP_HEADER_LEN)) {
579 const struct tcp_header *tcp = data;
580
581 miniflow_push_be32(mf, tcp_flags,
582 TCP_FLAGS_BE32(tcp->tcp_ctl));
583 miniflow_push_words(mf, tp_src, &tcp->tcp_src, 1);
584 }
585 } else if (OVS_LIKELY(nw_proto == IPPROTO_UDP)) {
586 if (OVS_LIKELY(size >= UDP_HEADER_LEN)) {
587 const struct udp_header *udp = data;
588
589 miniflow_push_words(mf, tp_src, &udp->udp_src, 1);
064af421 590 }
419681da
JR
591 } else if (OVS_LIKELY(nw_proto == IPPROTO_SCTP)) {
592 if (OVS_LIKELY(size >= SCTP_HEADER_LEN)) {
593 const struct sctp_header *sctp = data;
a26ef517 594
419681da
JR
595 miniflow_push_words(mf, tp_src, &sctp->sctp_src, 1);
596 }
597 } else if (OVS_LIKELY(nw_proto == IPPROTO_ICMP)) {
598 if (OVS_LIKELY(size >= ICMP_HEADER_LEN)) {
599 const struct icmp_header *icmp = data;
600
601 miniflow_push_be16(mf, tp_src, htons(icmp->icmp_type));
602 miniflow_push_be16(mf, tp_dst, htons(icmp->icmp_code));
603 }
0e612675
FL
604 } else if (OVS_LIKELY(nw_proto == IPPROTO_IGMP)) {
605 if (OVS_LIKELY(size >= IGMP_HEADER_LEN)) {
606 const struct igmp_header *igmp = data;
607
608 miniflow_push_be16(mf, tp_src, htons(igmp->igmp_type));
609 miniflow_push_be16(mf, tp_dst, htons(igmp->igmp_code));
610 miniflow_push_be32(mf, igmp_group_ip4,
611 get_16aligned_be32(&igmp->group));
612 }
419681da
JR
613 } else if (OVS_LIKELY(nw_proto == IPPROTO_ICMPV6)) {
614 if (OVS_LIKELY(size >= sizeof(struct icmp6_hdr))) {
615 const struct in6_addr *nd_target = NULL;
616 uint8_t arp_buf[2][ETH_ADDR_LEN];
617 const struct icmp6_hdr *icmp = data_pull(&data, &size,
618 sizeof *icmp);
619 memset(arp_buf, 0, sizeof arp_buf);
620 if (OVS_LIKELY(parse_icmpv6(&data, &size, icmp, &nd_target,
621 arp_buf))) {
b0e2ec32
JR
622 miniflow_push_words(mf, arp_sha, arp_buf,
623 ETH_ADDR_LEN * 2 / 4);
419681da
JR
624 if (nd_target) {
625 miniflow_push_words(mf, nd_target, nd_target,
626 sizeof *nd_target / 4);
627 }
419681da
JR
628 miniflow_push_be16(mf, tp_src, htons(icmp->icmp6_type));
629 miniflow_push_be16(mf, tp_dst, htons(icmp->icmp6_code));
630 }
631 }
064af421
BP
632 }
633 }
419681da
JR
634 if (md) {
635 miniflow_push_uint32_check(mf, dp_hash, md->dp_hash);
636 }
637 out:
638 dst->map = mf.map;
064af421
BP
639}
640
993410fb
BP
641/* For every bit of a field that is wildcarded in 'wildcards', sets the
642 * corresponding bit in 'flow' to zero. */
643void
644flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
645{
659c2346
BP
646 uint32_t *flow_u32 = (uint32_t *) flow;
647 const uint32_t *wc_u32 = (const uint32_t *) &wildcards->masks;
648 size_t i;
993410fb 649
659c2346
BP
650 for (i = 0; i < FLOW_U32S; i++) {
651 flow_u32[i] &= wc_u32[i];
26720e24 652 }
993410fb
BP
653}
654
d8d9c698
EJ
655void
656flow_unwildcard_tp_ports(const struct flow *flow, struct flow_wildcards *wc)
657{
658 if (flow->nw_proto != IPPROTO_ICMP) {
659 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
660 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
661 } else {
662 wc->masks.tp_src = htons(0xff);
663 wc->masks.tp_dst = htons(0xff);
664 }
665}
666
5d6c3af0
EJ
667/* Initializes 'fmd' with the metadata found in 'flow'. */
668void
669flow_get_metadata(const struct flow *flow, struct flow_metadata *fmd)
670{
0e612675 671 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
e9358af6 672
a79f29f2
AZ
673 fmd->dp_hash = flow->dp_hash;
674 fmd->recirc_id = flow->recirc_id;
296e07ac 675 fmd->tun_id = flow->tunnel.tun_id;
0ad90c84
JR
676 fmd->tun_src = flow->tunnel.ip_src;
677 fmd->tun_dst = flow->tunnel.ip_dst;
969fc56c 678 fmd->metadata = flow->metadata;
5d6c3af0 679 memcpy(fmd->regs, flow->regs, sizeof fmd->regs);
ac923e91 680 fmd->pkt_mark = flow->pkt_mark;
4e022ec0 681 fmd->in_port = flow->in_port.ofp_port;
5d6c3af0
EJ
682}
683
064af421 684char *
ae412e7d 685flow_to_string(const struct flow *flow)
064af421
BP
686{
687 struct ds ds = DS_EMPTY_INITIALIZER;
688 flow_format(&ds, flow);
689 return ds_cstr(&ds);
690}
691
4fe3445a
PS
692const char *
693flow_tun_flag_to_string(uint32_t flags)
694{
695 switch (flags) {
696 case FLOW_TNL_F_DONT_FRAGMENT:
697 return "df";
698 case FLOW_TNL_F_CSUM:
699 return "csum";
700 case FLOW_TNL_F_KEY:
701 return "key";
94872594
JG
702 case FLOW_TNL_F_OAM:
703 return "oam";
4fe3445a
PS
704 default:
705 return NULL;
706 }
707}
708
709void
710format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
711 uint32_t flags, char del)
712{
713 uint32_t bad = 0;
714
715 if (!flags) {
716 return;
717 }
718 while (flags) {
719 uint32_t bit = rightmost_1bit(flags);
720 const char *s;
721
722 s = bit_to_string(bit);
723 if (s) {
724 ds_put_format(ds, "%s%c", s, del);
725 } else {
726 bad |= bit;
727 }
728
729 flags &= ~bit;
730 }
731
732 if (bad) {
733 ds_put_format(ds, "0x%"PRIx32"%c", bad, del);
734 }
735 ds_chomp(ds, del);
736}
737
61bf6666
JR
738void
739format_flags_masked(struct ds *ds, const char *name,
740 const char *(*bit_to_string)(uint32_t), uint32_t flags,
741 uint32_t mask)
742{
743 if (name) {
744 ds_put_format(ds, "%s=", name);
745 }
746 while (mask) {
747 uint32_t bit = rightmost_1bit(mask);
748 const char *s = bit_to_string(bit);
749
750 ds_put_format(ds, "%s%s", (flags & bit) ? "+" : "-",
751 s ? s : "[Unknown]");
752 mask &= ~bit;
753 }
754}
755
064af421 756void
ae412e7d 757flow_format(struct ds *ds, const struct flow *flow)
064af421 758{
aa6c9932 759 struct match match;
78c9486d 760 struct flow_wildcards *wc = &match.wc;
296e07ac 761
aa6c9932 762 match_wc_init(&match, flow);
78c9486d
JR
763
764 /* As this function is most often used for formatting a packet in a
765 * packet-in message, skip formatting the packet context fields that are
766 * all-zeroes (Openflow spec encourages leaving out all-zeroes context
767 * fields from the packet-in messages). We make an exception with the
768 * 'in_port' field, which we always format, as packets usually have an
769 * in_port, and 0 is a port just like any other port. */
770 if (!flow->skb_priority) {
771 WC_UNMASK_FIELD(wc, skb_priority);
772 }
773 if (!flow->pkt_mark) {
774 WC_UNMASK_FIELD(wc, pkt_mark);
775 }
776 if (!flow->recirc_id) {
777 WC_UNMASK_FIELD(wc, recirc_id);
778 }
779 for (int i = 0; i < FLOW_N_REGS; i++) {
780 if (!flow->regs[i]) {
781 WC_UNMASK_FIELD(wc, regs[i]);
782 }
783 }
784 if (!flow->metadata) {
785 WC_UNMASK_FIELD(wc, metadata);
786 }
787
3f78c3cc 788 match_format(&match, ds, OFP_DEFAULT_PRIORITY);
064af421
BP
789}
790
791void
ae412e7d 792flow_print(FILE *stream, const struct flow *flow)
064af421
BP
793{
794 char *s = flow_to_string(flow);
795 fputs(s, stream);
796 free(s);
797}
54363004
BP
798\f
799/* flow_wildcards functions. */
800
d8ae4d67 801/* Initializes 'wc' as a set of wildcards that matches every packet. */
54363004 802void
d8ae4d67 803flow_wildcards_init_catchall(struct flow_wildcards *wc)
54363004 804{
659c2346 805 memset(&wc->masks, 0, sizeof wc->masks);
54363004
BP
806}
807
78c9486d
JR
808/* Converts a flow into flow wildcards. It sets the wildcard masks based on
809 * the packet headers extracted to 'flow'. It will not set the mask for fields
810 * that do not make sense for the packet type. OpenFlow-only metadata is
811 * wildcarded, but other metadata is unconditionally exact-matched. */
812void flow_wildcards_init_for_packet(struct flow_wildcards *wc,
813 const struct flow *flow)
814{
815 memset(&wc->masks, 0x0, sizeof wc->masks);
816
817 if (flow->tunnel.ip_dst) {
818 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
819 WC_MASK_FIELD(wc, tunnel.tun_id);
820 }
821 WC_MASK_FIELD(wc, tunnel.ip_src);
822 WC_MASK_FIELD(wc, tunnel.ip_dst);
823 WC_MASK_FIELD(wc, tunnel.flags);
824 WC_MASK_FIELD(wc, tunnel.ip_tos);
825 WC_MASK_FIELD(wc, tunnel.ip_ttl);
826 WC_MASK_FIELD(wc, tunnel.tp_src);
827 WC_MASK_FIELD(wc, tunnel.tp_dst);
828 } else if (flow->tunnel.tun_id) {
829 WC_MASK_FIELD(wc, tunnel.tun_id);
830 }
831
832 /* metadata and regs wildcarded. */
833
834 WC_MASK_FIELD(wc, skb_priority);
835 WC_MASK_FIELD(wc, pkt_mark);
836 WC_MASK_FIELD(wc, recirc_id);
837 WC_MASK_FIELD(wc, dp_hash);
838 WC_MASK_FIELD(wc, in_port);
839
840 WC_MASK_FIELD(wc, dl_dst);
841 WC_MASK_FIELD(wc, dl_src);
842 WC_MASK_FIELD(wc, dl_type);
843 WC_MASK_FIELD(wc, vlan_tci);
844
845 if (flow->dl_type == htons(ETH_TYPE_IP)) {
846 WC_MASK_FIELD(wc, nw_src);
847 WC_MASK_FIELD(wc, nw_dst);
848 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
849 WC_MASK_FIELD(wc, ipv6_src);
850 WC_MASK_FIELD(wc, ipv6_dst);
851 WC_MASK_FIELD(wc, ipv6_label);
852 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
853 flow->dl_type == htons(ETH_TYPE_RARP)) {
854 WC_MASK_FIELD(wc, nw_src);
855 WC_MASK_FIELD(wc, nw_dst);
856 WC_MASK_FIELD(wc, nw_proto);
857 WC_MASK_FIELD(wc, arp_sha);
858 WC_MASK_FIELD(wc, arp_tha);
859 return;
860 } else if (eth_type_mpls(flow->dl_type)) {
861 for (int i = 0; i < FLOW_MAX_MPLS_LABELS; i++) {
862 WC_MASK_FIELD(wc, mpls_lse[i]);
863 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
864 break;
865 }
866 }
867 return;
868 } else {
869 return; /* Unknown ethertype. */
870 }
871
872 /* IPv4 or IPv6. */
873 WC_MASK_FIELD(wc, nw_frag);
874 WC_MASK_FIELD(wc, nw_tos);
875 WC_MASK_FIELD(wc, nw_ttl);
876 WC_MASK_FIELD(wc, nw_proto);
877
878 /* No transport layer header in later fragments. */
879 if (!(flow->nw_frag & FLOW_NW_FRAG_LATER) &&
880 (flow->nw_proto == IPPROTO_ICMP ||
881 flow->nw_proto == IPPROTO_ICMPV6 ||
882 flow->nw_proto == IPPROTO_TCP ||
883 flow->nw_proto == IPPROTO_UDP ||
884 flow->nw_proto == IPPROTO_SCTP ||
885 flow->nw_proto == IPPROTO_IGMP)) {
886 WC_MASK_FIELD(wc, tp_src);
887 WC_MASK_FIELD(wc, tp_dst);
888
889 if (flow->nw_proto == IPPROTO_TCP) {
890 WC_MASK_FIELD(wc, tcp_flags);
891 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
892 WC_MASK_FIELD(wc, arp_sha);
893 WC_MASK_FIELD(wc, arp_tha);
894 WC_MASK_FIELD(wc, nd_target);
895 } else if (flow->nw_proto == IPPROTO_IGMP) {
896 WC_MASK_FIELD(wc, igmp_group_ip4);
897 }
898 }
899}
900
c11c6faa
AZ
901/* Clear the metadata and register wildcard masks. They are not packet
902 * header fields. */
903void
904flow_wildcards_clear_non_packet_fields(struct flow_wildcards *wc)
905{
906 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
907 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
908}
909
ecf1e7ac
BP
910/* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
911 * fields. */
912bool
913flow_wildcards_is_catchall(const struct flow_wildcards *wc)
914{
659c2346
BP
915 const uint32_t *wc_u32 = (const uint32_t *) &wc->masks;
916 size_t i;
ecf1e7ac 917
659c2346
BP
918 for (i = 0; i < FLOW_U32S; i++) {
919 if (wc_u32[i]) {
ecf1e7ac
BP
920 return false;
921 }
922 }
ecf1e7ac
BP
923 return true;
924}
925
368eefac
EJ
926/* Sets 'dst' as the bitwise AND of wildcards in 'src1' and 'src2'.
927 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded
928 * in 'src1' or 'src2' or both. */
b5d97350 929void
368eefac
EJ
930flow_wildcards_and(struct flow_wildcards *dst,
931 const struct flow_wildcards *src1,
932 const struct flow_wildcards *src2)
b5d97350 933{
659c2346
BP
934 uint32_t *dst_u32 = (uint32_t *) &dst->masks;
935 const uint32_t *src1_u32 = (const uint32_t *) &src1->masks;
936 const uint32_t *src2_u32 = (const uint32_t *) &src2->masks;
937 size_t i;
a79c50f3 938
659c2346
BP
939 for (i = 0; i < FLOW_U32S; i++) {
940 dst_u32[i] = src1_u32[i] & src2_u32[i];
26720e24 941 }
b5d97350
BP
942}
943
368eefac
EJ
944/* Sets 'dst' as the bitwise OR of wildcards in 'src1' and 'src2'. That
945 * is, a bit or a field is wildcarded in 'dst' if it is neither
946 * wildcarded in 'src1' nor 'src2'. */
947void
948flow_wildcards_or(struct flow_wildcards *dst,
949 const struct flow_wildcards *src1,
950 const struct flow_wildcards *src2)
951{
952 uint32_t *dst_u32 = (uint32_t *) &dst->masks;
953 const uint32_t *src1_u32 = (const uint32_t *) &src1->masks;
954 const uint32_t *src2_u32 = (const uint32_t *) &src2->masks;
955 size_t i;
956
957 for (i = 0; i < FLOW_U32S; i++) {
958 dst_u32[i] = src1_u32[i] | src2_u32[i];
959 }
960}
961
b5d97350
BP
962/* Returns a hash of the wildcards in 'wc'. */
963uint32_t
1006cda6 964flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
b5d97350 965{
ac31c5af 966 return flow_hash(&wc->masks, basis);
b5d97350
BP
967}
968
969/* Returns true if 'a' and 'b' represent the same wildcards, false if they are
970 * different. */
971bool
972flow_wildcards_equal(const struct flow_wildcards *a,
973 const struct flow_wildcards *b)
974{
659c2346 975 return flow_equal(&a->masks, &b->masks);
b5d97350
BP
976}
977
978/* Returns true if at least one bit or field is wildcarded in 'a' but not in
979 * 'b', false otherwise. */
980bool
981flow_wildcards_has_extra(const struct flow_wildcards *a,
982 const struct flow_wildcards *b)
983{
659c2346
BP
984 const uint32_t *a_u32 = (const uint32_t *) &a->masks;
985 const uint32_t *b_u32 = (const uint32_t *) &b->masks;
986 size_t i;
a79c50f3 987
659c2346
BP
988 for (i = 0; i < FLOW_U32S; i++) {
989 if ((a_u32[i] & b_u32[i]) != b_u32[i]) {
b6c9e612
BP
990 return true;
991 }
992 }
659c2346
BP
993 return false;
994}
b6c9e612 995
659c2346
BP
996/* Returns true if 'a' and 'b' are equal, except that 0-bits (wildcarded bits)
997 * in 'wc' do not need to be equal in 'a' and 'b'. */
998bool
999flow_equal_except(const struct flow *a, const struct flow *b,
1000 const struct flow_wildcards *wc)
1001{
1002 const uint32_t *a_u32 = (const uint32_t *) a;
1003 const uint32_t *b_u32 = (const uint32_t *) b;
1004 const uint32_t *wc_u32 = (const uint32_t *) &wc->masks;
1005 size_t i;
d31f1109 1006
659c2346
BP
1007 for (i = 0; i < FLOW_U32S; i++) {
1008 if ((a_u32[i] ^ b_u32[i]) & wc_u32[i]) {
1009 return false;
1010 }
47284b1f 1011 }
659c2346 1012 return true;
b5d97350
BP
1013}
1014
b6c9e612
BP
1015/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
1016 * (A 0-bit indicates a wildcard bit.) */
1017void
1018flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
1019{
26720e24 1020 wc->masks.regs[idx] = mask;
b6c9e612 1021}
ff55ea1f 1022
79fe0f46
BP
1023/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
1024 * (A 0-bit indicates a wildcard bit.) */
1025void
1026flow_wildcards_set_xreg_mask(struct flow_wildcards *wc, int idx, uint64_t mask)
1027{
1028 flow_set_xreg(&wc->masks, idx, mask);
1029}
1030
28a560d9
JR
1031/* Calculates the 5-tuple hash from the given miniflow.
1032 * This returns the same value as flow_hash_5tuple for the corresponding
1033 * flow. */
4f150744
JR
1034uint32_t
1035miniflow_hash_5tuple(const struct miniflow *flow, uint32_t basis)
1036{
28a560d9 1037 uint32_t hash = basis;
4f150744 1038
28a560d9
JR
1039 if (flow) {
1040 ovs_be16 dl_type = MINIFLOW_GET_BE16(flow, dl_type);
1041
33c6a1b9 1042 hash = hash_add(hash, MINIFLOW_GET_U8(flow, nw_proto));
28a560d9
JR
1043
1044 /* Separate loops for better optimization. */
1045 if (dl_type == htons(ETH_TYPE_IPV6)) {
1046 uint64_t map = MINIFLOW_MAP(ipv6_src) | MINIFLOW_MAP(ipv6_dst)
1047 | MINIFLOW_MAP(tp_src); /* Covers both ports */
1048 uint32_t value;
4f150744 1049
28a560d9 1050 MINIFLOW_FOR_EACH_IN_MAP(value, flow, map) {
33c6a1b9 1051 hash = hash_add(hash, value);
28a560d9
JR
1052 }
1053 } else {
1054 uint64_t map = MINIFLOW_MAP(nw_src) | MINIFLOW_MAP(nw_dst)
1055 | MINIFLOW_MAP(tp_src); /* Covers both ports */
1056 uint32_t value;
4f150744 1057
28a560d9 1058 MINIFLOW_FOR_EACH_IN_MAP(value, flow, map) {
33c6a1b9 1059 hash = hash_add(hash, value);
28a560d9
JR
1060 }
1061 }
33c6a1b9 1062 hash = hash_finish(hash, 42); /* Arbitrary number. */
28a560d9
JR
1063 }
1064 return hash;
4f150744
JR
1065}
1066
1067BUILD_ASSERT_DECL(offsetof(struct flow, tp_src) + 2
1068 == offsetof(struct flow, tp_dst) &&
1069 offsetof(struct flow, tp_src) / 4
1070 == offsetof(struct flow, tp_dst) / 4);
28a560d9
JR
1071BUILD_ASSERT_DECL(offsetof(struct flow, ipv6_src) + 16
1072 == offsetof(struct flow, ipv6_dst));
4f150744 1073
63be20be
AW
1074/* Calculates the 5-tuple hash from the given flow. */
1075uint32_t
1076flow_hash_5tuple(const struct flow *flow, uint32_t basis)
1077{
28a560d9 1078 uint32_t hash = basis;
63be20be 1079
28a560d9
JR
1080 if (flow) {
1081 const uint32_t *flow_u32 = (const uint32_t *)flow;
1082
33c6a1b9 1083 hash = hash_add(hash, flow->nw_proto);
28a560d9
JR
1084
1085 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1086 int ofs = offsetof(struct flow, ipv6_src) / 4;
1087 int end = ofs + 2 * sizeof flow->ipv6_src / 4;
63be20be 1088
28a560d9 1089 while (ofs < end) {
33c6a1b9 1090 hash = hash_add(hash, flow_u32[ofs++]);
28a560d9
JR
1091 }
1092 } else {
33c6a1b9
JR
1093 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_src);
1094 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_dst);
28a560d9 1095 }
33c6a1b9 1096 hash = hash_add(hash, flow_u32[offsetof(struct flow, tp_src) / 4]);
63be20be 1097
33c6a1b9 1098 hash = hash_finish(hash, 42); /* Arbitrary number. */
28a560d9
JR
1099 }
1100 return hash;
63be20be
AW
1101}
1102
ff55ea1f
EJ
1103/* Hashes 'flow' based on its L2 through L4 protocol information. */
1104uint32_t
1105flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
1106{
1107 struct {
d31f1109
JP
1108 union {
1109 ovs_be32 ipv4_addr;
1110 struct in6_addr ipv6_addr;
1111 };
ff55ea1f
EJ
1112 ovs_be16 eth_type;
1113 ovs_be16 vlan_tci;
5b909cbb 1114 ovs_be16 tp_port;
ff55ea1f
EJ
1115 uint8_t eth_addr[ETH_ADDR_LEN];
1116 uint8_t ip_proto;
1117 } fields;
1118
1119 int i;
1120
1121 memset(&fields, 0, sizeof fields);
1122 for (i = 0; i < ETH_ADDR_LEN; i++) {
1123 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
1124 }
1125 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
1126 fields.eth_type = flow->dl_type;
3e3eda95
EJ
1127
1128 /* UDP source and destination port are not taken into account because they
1129 * will not necessarily be symmetric in a bidirectional flow. */
ff55ea1f 1130 if (fields.eth_type == htons(ETH_TYPE_IP)) {
d31f1109
JP
1131 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
1132 fields.ip_proto = flow->nw_proto;
c6bcb685 1133 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
5b909cbb 1134 fields.tp_port = flow->tp_src ^ flow->tp_dst;
d31f1109
JP
1135 }
1136 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
1137 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
1138 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
1139 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
1140
1141 for (i=0; i<16; i++) {
1142 ipv6_addr[i] = a[i] ^ b[i];
1143 }
ff55ea1f 1144 fields.ip_proto = flow->nw_proto;
c6bcb685 1145 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
5b909cbb 1146 fields.tp_port = flow->tp_src ^ flow->tp_dst;
ff55ea1f 1147 }
ff55ea1f 1148 }
c49d1dd1 1149 return jhash_bytes(&fields, sizeof fields, basis);
ff55ea1f 1150}
520e9a2a 1151
94639963
JR
1152/* Initialize a flow with random fields that matter for nx_hash_fields. */
1153void
1154flow_random_hash_fields(struct flow *flow)
1155{
1156 uint16_t rnd = random_uint16();
1157
1158 /* Initialize to all zeros. */
1159 memset(flow, 0, sizeof *flow);
1160
1161 eth_addr_random(flow->dl_src);
1162 eth_addr_random(flow->dl_dst);
1163
1164 flow->vlan_tci = (OVS_FORCE ovs_be16) (random_uint16() & VLAN_VID_MASK);
1165
1166 /* Make most of the random flows IPv4, some IPv6, and rest random. */
1167 flow->dl_type = rnd < 0x8000 ? htons(ETH_TYPE_IP) :
1168 rnd < 0xc000 ? htons(ETH_TYPE_IPV6) : (OVS_FORCE ovs_be16)rnd;
1169
1170 if (dl_type_is_ip_any(flow->dl_type)) {
1171 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1172 flow->nw_src = (OVS_FORCE ovs_be32)random_uint32();
1173 flow->nw_dst = (OVS_FORCE ovs_be32)random_uint32();
1174 } else {
1175 random_bytes(&flow->ipv6_src, sizeof flow->ipv6_src);
1176 random_bytes(&flow->ipv6_dst, sizeof flow->ipv6_dst);
1177 }
1178 /* Make most of IP flows TCP, some UDP or SCTP, and rest random. */
1179 rnd = random_uint16();
1180 flow->nw_proto = rnd < 0x8000 ? IPPROTO_TCP :
1181 rnd < 0xc000 ? IPPROTO_UDP :
1182 rnd < 0xd000 ? IPPROTO_SCTP : (uint8_t)rnd;
1183 if (flow->nw_proto == IPPROTO_TCP ||
1184 flow->nw_proto == IPPROTO_UDP ||
1185 flow->nw_proto == IPPROTO_SCTP) {
1186 flow->tp_src = (OVS_FORCE ovs_be16)random_uint16();
1187 flow->tp_dst = (OVS_FORCE ovs_be16)random_uint16();
1188 }
1189 }
1190}
1191
bcd2633a
JP
1192/* Masks the fields in 'wc' that are used by the flow hash 'fields'. */
1193void
6cdd5145
JP
1194flow_mask_hash_fields(const struct flow *flow, struct flow_wildcards *wc,
1195 enum nx_hash_fields fields)
bcd2633a
JP
1196{
1197 switch (fields) {
1198 case NX_HASH_FIELDS_ETH_SRC:
1199 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1200 break;
1201
1202 case NX_HASH_FIELDS_SYMMETRIC_L4:
1203 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1204 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
6cdd5145
JP
1205 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1206 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1207 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
7f8a65ca 1208 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
6cdd5145
JP
1209 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
1210 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
1211 }
1212 if (is_ip_any(flow)) {
1213 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
d8d9c698 1214 flow_unwildcard_tp_ports(flow, wc);
6cdd5145 1215 }
1dd35f8a 1216 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
bcd2633a
JP
1217 break;
1218
1219 default:
428b2edd 1220 OVS_NOT_REACHED();
bcd2633a
JP
1221 }
1222}
1223
520e9a2a
EJ
1224/* Hashes the portions of 'flow' designated by 'fields'. */
1225uint32_t
1226flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
1227 uint16_t basis)
1228{
1229 switch (fields) {
1230
1231 case NX_HASH_FIELDS_ETH_SRC:
c49d1dd1 1232 return jhash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
520e9a2a
EJ
1233
1234 case NX_HASH_FIELDS_SYMMETRIC_L4:
1235 return flow_hash_symmetric_l4(flow, basis);
1236 }
1237
428b2edd 1238 OVS_NOT_REACHED();
520e9a2a
EJ
1239}
1240
1241/* Returns a string representation of 'fields'. */
1242const char *
1243flow_hash_fields_to_str(enum nx_hash_fields fields)
1244{
1245 switch (fields) {
1246 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
1247 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
1248 default: return "<unknown>";
1249 }
1250}
1251
1252/* Returns true if the value of 'fields' is supported. Otherwise false. */
1253bool
1254flow_hash_fields_valid(enum nx_hash_fields fields)
1255{
1256 return fields == NX_HASH_FIELDS_ETH_SRC
1257 || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
1258}
8b3b8dd1 1259
368eefac
EJ
1260/* Returns a hash value for the bits of 'flow' that are active based on
1261 * 'wc', given 'basis'. */
1262uint32_t
1263flow_hash_in_wildcards(const struct flow *flow,
1264 const struct flow_wildcards *wc, uint32_t basis)
1265{
1266 const uint32_t *wc_u32 = (const uint32_t *) &wc->masks;
1267 const uint32_t *flow_u32 = (const uint32_t *) flow;
1268 uint32_t hash;
1269 size_t i;
1270
1271 hash = basis;
1272 for (i = 0; i < FLOW_U32S; i++) {
33c6a1b9 1273 hash = hash_add(hash, flow_u32[i] & wc_u32[i]);
368eefac 1274 }
33c6a1b9 1275 return hash_finish(hash, 4 * FLOW_U32S);
368eefac
EJ
1276}
1277
3719455c
BP
1278/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
1279 * OpenFlow 1.0 "dl_vlan" value:
1280 *
1281 * - If it is in the range 0...4095, 'flow->vlan_tci' is set to match
1282 * that VLAN. Any existing PCP match is unchanged (it becomes 0 if
1283 * 'flow' previously matched packets without a VLAN header).
1284 *
1285 * - If it is OFP_VLAN_NONE, 'flow->vlan_tci' is set to match a packet
1286 * without a VLAN tag.
1287 *
1288 * - Other values of 'vid' should not be used. */
1289void
fb0451d9 1290flow_set_dl_vlan(struct flow *flow, ovs_be16 vid)
3719455c 1291{
0c436519 1292 if (vid == htons(OFP10_VLAN_NONE)) {
3719455c
BP
1293 flow->vlan_tci = htons(0);
1294 } else {
1295 vid &= htons(VLAN_VID_MASK);
1296 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1297 flow->vlan_tci |= htons(VLAN_CFI) | vid;
1298 }
1299}
1300
cc34bc8c
BP
1301/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
1302 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
1303 * plus CFI). */
1304void
1305flow_set_vlan_vid(struct flow *flow, ovs_be16 vid)
1306{
1307 ovs_be16 mask = htons(VLAN_VID_MASK | VLAN_CFI);
1308 flow->vlan_tci &= ~mask;
1309 flow->vlan_tci |= vid & mask;
1310}
1311
3719455c
BP
1312/* Sets the VLAN PCP that 'flow' matches to 'pcp', which should be in the
1313 * range 0...7.
1314 *
1315 * This function has no effect on the VLAN ID that 'flow' matches.
1316 *
1317 * After calling this function, 'flow' will not match packets without a VLAN
1318 * header. */
1319void
1320flow_set_vlan_pcp(struct flow *flow, uint8_t pcp)
1321{
1322 pcp &= 0x07;
1323 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1324 flow->vlan_tci |= htons((pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
1325}
1326
8bfd0fda
BP
1327/* Returns the number of MPLS LSEs present in 'flow'
1328 *
1329 * Returns 0 if the 'dl_type' of 'flow' is not an MPLS ethernet type.
1330 * Otherwise traverses 'flow''s MPLS label stack stopping at the
1331 * first entry that has the BoS bit set. If no such entry exists then
1332 * the maximum number of LSEs that can be stored in 'flow' is returned.
1333 */
1334int
1335flow_count_mpls_labels(const struct flow *flow, struct flow_wildcards *wc)
1336{
22d38fca 1337 /* dl_type is always masked. */
8bfd0fda
BP
1338 if (eth_type_mpls(flow->dl_type)) {
1339 int i;
1340 int len = FLOW_MAX_MPLS_LABELS;
1341
1342 for (i = 0; i < len; i++) {
1343 if (wc) {
1344 wc->masks.mpls_lse[i] |= htonl(MPLS_BOS_MASK);
1345 }
1346 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
1347 return i + 1;
1348 }
1349 }
1350
1351 return len;
1352 } else {
1353 return 0;
1354 }
1355}
1356
1357/* Returns the number consecutive of MPLS LSEs, starting at the
1358 * innermost LSE, that are common in 'a' and 'b'.
1359 *
1360 * 'an' must be flow_count_mpls_labels(a).
1361 * 'bn' must be flow_count_mpls_labels(b).
1362 */
1363int
1364flow_count_common_mpls_labels(const struct flow *a, int an,
1365 const struct flow *b, int bn,
1366 struct flow_wildcards *wc)
1367{
1368 int min_n = MIN(an, bn);
1369 if (min_n == 0) {
1370 return 0;
1371 } else {
1372 int common_n = 0;
1373 int a_last = an - 1;
1374 int b_last = bn - 1;
1375 int i;
1376
1377 for (i = 0; i < min_n; i++) {
1378 if (wc) {
1379 wc->masks.mpls_lse[a_last - i] = OVS_BE32_MAX;
1380 wc->masks.mpls_lse[b_last - i] = OVS_BE32_MAX;
1381 }
1382 if (a->mpls_lse[a_last - i] != b->mpls_lse[b_last - i]) {
1383 break;
1384 } else {
1385 common_n++;
1386 }
1387 }
1388
1389 return common_n;
1390 }
1391}
1392
1393/* Adds a new outermost MPLS label to 'flow' and changes 'flow''s Ethernet type
1394 * to 'mpls_eth_type', which must be an MPLS Ethertype.
1395 *
1396 * If the new label is the first MPLS label in 'flow', it is generated as;
1397 *
1398 * - label: 2, if 'flow' is IPv6, otherwise 0.
1399 *
1400 * - TTL: IPv4 or IPv6 TTL, if present and nonzero, otherwise 64.
1401 *
1402 * - TC: IPv4 or IPv6 TOS, if present, otherwise 0.
1403 *
1404 * - BoS: 1.
1405 *
22d38fca 1406 * If the new label is the second or later label MPLS label in 'flow', it is
8bfd0fda
BP
1407 * generated as;
1408 *
368fb7e6 1409 * - label: Copied from outer label.
8bfd0fda
BP
1410 *
1411 * - TTL: Copied from outer label.
1412 *
1413 * - TC: Copied from outer label.
1414 *
1415 * - BoS: 0.
1416 *
1417 * 'n' must be flow_count_mpls_labels(flow). 'n' must be less than
1418 * FLOW_MAX_MPLS_LABELS (because otherwise flow->mpls_lse[] would overflow).
1419 */
1420void
1421flow_push_mpls(struct flow *flow, int n, ovs_be16 mpls_eth_type,
1422 struct flow_wildcards *wc)
1423{
1424 ovs_assert(eth_type_mpls(mpls_eth_type));
1425 ovs_assert(n < FLOW_MAX_MPLS_LABELS);
1426
8bfd0fda
BP
1427 if (n) {
1428 int i;
1429
22d38fca
JR
1430 if (wc) {
1431 memset(&wc->masks.mpls_lse, 0xff, sizeof *wc->masks.mpls_lse * n);
1432 }
8bfd0fda
BP
1433 for (i = n; i >= 1; i--) {
1434 flow->mpls_lse[i] = flow->mpls_lse[i - 1];
1435 }
22d38fca 1436 flow->mpls_lse[0] = (flow->mpls_lse[1] & htonl(~MPLS_BOS_MASK));
8bfd0fda
BP
1437 } else {
1438 int label = 0; /* IPv4 Explicit Null. */
1439 int tc = 0;
1440 int ttl = 64;
1441
1442 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1443 label = 2;
1444 }
1445
1446 if (is_ip_any(flow)) {
1447 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
22d38fca
JR
1448 if (wc) {
1449 wc->masks.nw_tos |= IP_DSCP_MASK;
1450 wc->masks.nw_ttl = 0xff;
1451 }
8bfd0fda
BP
1452
1453 if (flow->nw_ttl) {
1454 ttl = flow->nw_ttl;
1455 }
8bfd0fda
BP
1456 }
1457
1458 flow->mpls_lse[0] = set_mpls_lse_values(ttl, tc, 1, htonl(label));
1459
1460 /* Clear all L3 and L4 fields. */
0e612675 1461 BUILD_ASSERT(FLOW_WC_SEQ == 27);
8bfd0fda
BP
1462 memset((char *) flow + FLOW_SEGMENT_2_ENDS_AT, 0,
1463 sizeof(struct flow) - FLOW_SEGMENT_2_ENDS_AT);
1464 }
1465 flow->dl_type = mpls_eth_type;
1466}
1467
1468/* Tries to remove the outermost MPLS label from 'flow'. Returns true if
1469 * successful, false otherwise. On success, sets 'flow''s Ethernet type to
1470 * 'eth_type'.
1471 *
1472 * 'n' must be flow_count_mpls_labels(flow). */
1473bool
1474flow_pop_mpls(struct flow *flow, int n, ovs_be16 eth_type,
1475 struct flow_wildcards *wc)
1476{
1477 int i;
1478
1479 if (n == 0) {
1480 /* Nothing to pop. */
1481 return false;
22d38fca
JR
1482 } else if (n == FLOW_MAX_MPLS_LABELS) {
1483 if (wc) {
1484 wc->masks.mpls_lse[n - 1] |= htonl(MPLS_BOS_MASK);
1485 }
1486 if (!(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
1487 /* Can't pop because don't know what to fill in mpls_lse[n - 1]. */
1488 return false;
1489 }
8bfd0fda
BP
1490 }
1491
22d38fca
JR
1492 if (wc) {
1493 memset(&wc->masks.mpls_lse[1], 0xff,
1494 sizeof *wc->masks.mpls_lse * (n - 1));
1495 }
8bfd0fda
BP
1496 for (i = 1; i < n; i++) {
1497 flow->mpls_lse[i - 1] = flow->mpls_lse[i];
1498 }
1499 flow->mpls_lse[n - 1] = 0;
1500 flow->dl_type = eth_type;
1501 return true;
1502}
1503
b02475c5
SH
1504/* Sets the MPLS Label that 'flow' matches to 'label', which is interpreted
1505 * as an OpenFlow 1.1 "mpls_label" value. */
1506void
8bfd0fda 1507flow_set_mpls_label(struct flow *flow, int idx, ovs_be32 label)
b02475c5 1508{
8bfd0fda 1509 set_mpls_lse_label(&flow->mpls_lse[idx], label);
b02475c5
SH
1510}
1511
b676167a
SH
1512/* Sets the MPLS TTL that 'flow' matches to 'ttl', which should be in the
1513 * range 0...255. */
1514void
8bfd0fda 1515flow_set_mpls_ttl(struct flow *flow, int idx, uint8_t ttl)
b676167a 1516{
8bfd0fda 1517 set_mpls_lse_ttl(&flow->mpls_lse[idx], ttl);
b676167a
SH
1518}
1519
b02475c5
SH
1520/* Sets the MPLS TC that 'flow' matches to 'tc', which should be in the
1521 * range 0...7. */
1522void
8bfd0fda 1523flow_set_mpls_tc(struct flow *flow, int idx, uint8_t tc)
b02475c5 1524{
8bfd0fda 1525 set_mpls_lse_tc(&flow->mpls_lse[idx], tc);
b02475c5
SH
1526}
1527
1528/* Sets the MPLS BOS bit that 'flow' matches to which should be 0 or 1. */
1529void
8bfd0fda 1530flow_set_mpls_bos(struct flow *flow, int idx, uint8_t bos)
b02475c5 1531{
8bfd0fda 1532 set_mpls_lse_bos(&flow->mpls_lse[idx], bos);
b02475c5
SH
1533}
1534
8bfd0fda
BP
1535/* Sets the entire MPLS LSE. */
1536void
1537flow_set_mpls_lse(struct flow *flow, int idx, ovs_be32 lse)
1538{
1539 flow->mpls_lse[idx] = lse;
1540}
52105b67 1541
437d0d22 1542static size_t
52105b67
JR
1543flow_compose_l4(struct ofpbuf *b, const struct flow *flow)
1544{
437d0d22
JR
1545 size_t l4_len = 0;
1546
52105b67
JR
1547 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
1548 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1549 if (flow->nw_proto == IPPROTO_TCP) {
1550 struct tcp_header *tcp;
1551
437d0d22
JR
1552 l4_len = sizeof *tcp;
1553 tcp = ofpbuf_put_zeros(b, l4_len);
52105b67
JR
1554 tcp->tcp_src = flow->tp_src;
1555 tcp->tcp_dst = flow->tp_dst;
1556 tcp->tcp_ctl = TCP_CTL(ntohs(flow->tcp_flags), 5);
52105b67
JR
1557 } else if (flow->nw_proto == IPPROTO_UDP) {
1558 struct udp_header *udp;
1559
437d0d22
JR
1560 l4_len = sizeof *udp;
1561 udp = ofpbuf_put_zeros(b, l4_len);
52105b67
JR
1562 udp->udp_src = flow->tp_src;
1563 udp->udp_dst = flow->tp_dst;
52105b67
JR
1564 } else if (flow->nw_proto == IPPROTO_SCTP) {
1565 struct sctp_header *sctp;
1566
437d0d22
JR
1567 l4_len = sizeof *sctp;
1568 sctp = ofpbuf_put_zeros(b, l4_len);
52105b67
JR
1569 sctp->sctp_src = flow->tp_src;
1570 sctp->sctp_dst = flow->tp_dst;
52105b67
JR
1571 } else if (flow->nw_proto == IPPROTO_ICMP) {
1572 struct icmp_header *icmp;
1573
437d0d22
JR
1574 l4_len = sizeof *icmp;
1575 icmp = ofpbuf_put_zeros(b, l4_len);
52105b67
JR
1576 icmp->icmp_type = ntohs(flow->tp_src);
1577 icmp->icmp_code = ntohs(flow->tp_dst);
1578 icmp->icmp_csum = csum(icmp, ICMP_HEADER_LEN);
0e612675
FL
1579 } else if (flow->nw_proto == IPPROTO_IGMP) {
1580 struct igmp_header *igmp;
1581
1582 l4_len = sizeof *igmp;
1583 igmp = ofpbuf_put_zeros(b, l4_len);
1584 igmp->igmp_type = ntohs(flow->tp_src);
1585 igmp->igmp_code = ntohs(flow->tp_dst);
1586 put_16aligned_be32(&igmp->group, flow->igmp_group_ip4);
1587 igmp->igmp_csum = csum(igmp, IGMP_HEADER_LEN);
52105b67
JR
1588 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
1589 struct icmp6_hdr *icmp;
1590
437d0d22
JR
1591 l4_len = sizeof *icmp;
1592 icmp = ofpbuf_put_zeros(b, l4_len);
52105b67
JR
1593 icmp->icmp6_type = ntohs(flow->tp_src);
1594 icmp->icmp6_code = ntohs(flow->tp_dst);
1595
1596 if (icmp->icmp6_code == 0 &&
1597 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
1598 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
1599 struct in6_addr *nd_target;
1600 struct nd_opt_hdr *nd_opt;
1601
437d0d22 1602 l4_len += sizeof *nd_target;
52105b67
JR
1603 nd_target = ofpbuf_put_zeros(b, sizeof *nd_target);
1604 *nd_target = flow->nd_target;
1605
1606 if (!eth_addr_is_zero(flow->arp_sha)) {
437d0d22 1607 l4_len += 8;
52105b67
JR
1608 nd_opt = ofpbuf_put_zeros(b, 8);
1609 nd_opt->nd_opt_len = 1;
1610 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
1611 memcpy(nd_opt + 1, flow->arp_sha, ETH_ADDR_LEN);
1612 }
1613 if (!eth_addr_is_zero(flow->arp_tha)) {
437d0d22 1614 l4_len += 8;
52105b67
JR
1615 nd_opt = ofpbuf_put_zeros(b, 8);
1616 nd_opt->nd_opt_len = 1;
1617 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1618 memcpy(nd_opt + 1, flow->arp_tha, ETH_ADDR_LEN);
1619 }
1620 }
1621 icmp->icmp6_cksum = (OVS_FORCE uint16_t)
1622 csum(icmp, (char *)ofpbuf_tail(b) - (char *)icmp);
52105b67
JR
1623 }
1624 }
437d0d22 1625 return l4_len;
52105b67
JR
1626}
1627
8b3b8dd1
BP
1628/* Puts into 'b' a packet that flow_extract() would parse as having the given
1629 * 'flow'.
1630 *
1631 * (This is useful only for testing, obviously, and the packet isn't really
dc5a7ce7 1632 * valid. It hasn't got some checksums filled in, for one, and lots of fields
8b3b8dd1
BP
1633 * are just zeroed.) */
1634void
1635flow_compose(struct ofpbuf *b, const struct flow *flow)
1636{
437d0d22
JR
1637 size_t l4_len;
1638
52105b67 1639 /* eth_compose() sets l3 pointer and makes sure it is 32-bit aligned. */
8b3b8dd1
BP
1640 eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
1641 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
cf3b7538 1642 struct eth_header *eth = ofpbuf_l2(b);
1f317cb5 1643 eth->eth_type = htons(ofpbuf_size(b));
8b3b8dd1
BP
1644 return;
1645 }
1646
1647 if (flow->vlan_tci & htons(VLAN_CFI)) {
1bf02876 1648 eth_push_vlan(b, htons(ETH_TYPE_VLAN), flow->vlan_tci);
8b3b8dd1
BP
1649 }
1650
cff78c88 1651 if (flow->dl_type == htons(ETH_TYPE_IP)) {
8b3b8dd1
BP
1652 struct ip_header *ip;
1653
52105b67 1654 ip = ofpbuf_put_zeros(b, sizeof *ip);
8b3b8dd1 1655 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
eadef313 1656 ip->ip_tos = flow->nw_tos;
aabf5352 1657 ip->ip_ttl = flow->nw_ttl;
8b3b8dd1 1658 ip->ip_proto = flow->nw_proto;
7c457c33
BP
1659 put_16aligned_be32(&ip->ip_src, flow->nw_src);
1660 put_16aligned_be32(&ip->ip_dst, flow->nw_dst);
8b3b8dd1 1661
eadef313 1662 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
7257b535 1663 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
eadef313 1664 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
7257b535
BP
1665 ip->ip_frag_off |= htons(100);
1666 }
1667 }
df9b6612 1668
437d0d22 1669 ofpbuf_set_l4(b, ofpbuf_tail(b));
52105b67 1670
437d0d22 1671 l4_len = flow_compose_l4(b, flow);
52105b67 1672
1c98d0ad 1673 ip = ofpbuf_l3(b);
437d0d22 1674 ip->ip_tot_len = htons(b->l4_ofs - b->l3_ofs + l4_len);
dc5a7ce7 1675 ip->ip_csum = csum(ip, sizeof *ip);
cff78c88 1676 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
52105b67
JR
1677 struct ovs_16aligned_ip6_hdr *nh;
1678
1679 nh = ofpbuf_put_zeros(b, sizeof *nh);
1680 put_16aligned_be32(&nh->ip6_flow, htonl(6 << 28) |
1681 htonl(flow->nw_tos << 20) | flow->ipv6_label);
1682 nh->ip6_hlim = flow->nw_ttl;
1683 nh->ip6_nxt = flow->nw_proto;
1684
1685 memcpy(&nh->ip6_src, &flow->ipv6_src, sizeof(nh->ip6_src));
1686 memcpy(&nh->ip6_dst, &flow->ipv6_dst, sizeof(nh->ip6_dst));
1687
437d0d22 1688 ofpbuf_set_l4(b, ofpbuf_tail(b));
52105b67 1689
437d0d22 1690 l4_len = flow_compose_l4(b, flow);
52105b67 1691
1c98d0ad 1692 nh = ofpbuf_l3(b);
437d0d22 1693 nh->ip6_plen = htons(l4_len);
cff78c88
SH
1694 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1695 flow->dl_type == htons(ETH_TYPE_RARP)) {
8b3b8dd1
BP
1696 struct arp_eth_header *arp;
1697
437d0d22
JR
1698 arp = ofpbuf_put_zeros(b, sizeof *arp);
1699 ofpbuf_set_l3(b, arp);
8b3b8dd1
BP
1700 arp->ar_hrd = htons(1);
1701 arp->ar_pro = htons(ETH_TYPE_IP);
1702 arp->ar_hln = ETH_ADDR_LEN;
1703 arp->ar_pln = 4;
1704 arp->ar_op = htons(flow->nw_proto);
1705
1706 if (flow->nw_proto == ARP_OP_REQUEST ||
1707 flow->nw_proto == ARP_OP_REPLY) {
7c457c33
BP
1708 put_16aligned_be32(&arp->ar_spa, flow->nw_src);
1709 put_16aligned_be32(&arp->ar_tpa, flow->nw_dst);
8b3b8dd1
BP
1710 memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1711 memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);
1712 }
1713 }
b02475c5
SH
1714
1715 if (eth_type_mpls(flow->dl_type)) {
8bfd0fda
BP
1716 int n;
1717
437d0d22 1718 b->l2_5_ofs = b->l3_ofs;
8bfd0fda
BP
1719 for (n = 1; n < FLOW_MAX_MPLS_LABELS; n++) {
1720 if (flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK)) {
1721 break;
1722 }
1723 }
1724 while (n > 0) {
1725 push_mpls(b, flow->dl_type, flow->mpls_lse[--n]);
1726 }
b02475c5 1727 }
8b3b8dd1 1728}
5cb7a798
BP
1729\f
1730/* Compressed flow. */
1731
1732static int
1733miniflow_n_values(const struct miniflow *flow)
1734{
fb9aefa3 1735 return count_1bits(flow->map);
5cb7a798
BP
1736}
1737
1738static uint32_t *
1739miniflow_alloc_values(struct miniflow *flow, int n)
1740{
3016f3e4
JR
1741 int size = MINIFLOW_VALUES_SIZE(n);
1742
1743 if (size <= sizeof flow->inline_values) {
27bbe15d 1744 flow->values_inline = true;
5cb7a798
BP
1745 return flow->inline_values;
1746 } else {
1747 COVERAGE_INC(miniflow_malloc);
27bbe15d 1748 flow->values_inline = false;
3016f3e4 1749 flow->offline_values = xmalloc(size);
27bbe15d 1750 return flow->offline_values;
5cb7a798
BP
1751 }
1752}
1753
df40c152
BP
1754/* Completes an initialization of 'dst' as a miniflow copy of 'src' begun by
1755 * the caller. The caller must have already initialized 'dst->map' properly
13751fd8
JR
1756 * to indicate the significant uint32_t elements of 'src'. 'n' must be the
1757 * number of 1-bits in 'dst->map'.
1758 *
1759 * Normally the significant elements are the ones that are non-zero. However,
1760 * when a miniflow is initialized from a (mini)mask, the values can be zeroes,
1761 * so that the flow and mask always have the same maps.
df40c152 1762 *
27bbe15d 1763 * This function initializes values (either inline if possible or with
13751fd8
JR
1764 * malloc() otherwise) and copies the uint32_t elements of 'src' indicated by
1765 * 'dst->map' into it. */
df40c152
BP
1766static void
1767miniflow_init__(struct miniflow *dst, const struct flow *src, int n)
1768{
1769 const uint32_t *src_u32 = (const uint32_t *) src;
27bbe15d 1770 uint32_t *dst_u32 = miniflow_alloc_values(dst, n);
080e28d0 1771 uint64_t map;
df40c152 1772
080e28d0 1773 for (map = dst->map; map; map = zero_rightmost_1bit(map)) {
27bbe15d 1774 *dst_u32++ = src_u32[raw_ctz(map)];
df40c152
BP
1775 }
1776}
1777
5cb7a798 1778/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
27bbe15d
JR
1779 * with miniflow_destroy().
1780 * Always allocates offline storage. */
5cb7a798
BP
1781void
1782miniflow_init(struct miniflow *dst, const struct flow *src)
1783{
1784 const uint32_t *src_u32 = (const uint32_t *) src;
5cb7a798
BP
1785 unsigned int i;
1786 int n;
1787
1788 /* Initialize dst->map, counting the number of nonzero elements. */
1789 n = 0;
080e28d0
JR
1790 dst->map = 0;
1791
5cb7a798
BP
1792 for (i = 0; i < FLOW_U32S; i++) {
1793 if (src_u32[i]) {
080e28d0 1794 dst->map |= UINT64_C(1) << i;
5cb7a798
BP
1795 n++;
1796 }
1797 }
1798
df40c152
BP
1799 miniflow_init__(dst, src, n);
1800}
5cb7a798 1801
df40c152
BP
1802/* Initializes 'dst' as a copy of 'src', using 'mask->map' as 'dst''s map. The
1803 * caller must eventually free 'dst' with miniflow_destroy(). */
1804void
1805miniflow_init_with_minimask(struct miniflow *dst, const struct flow *src,
1806 const struct minimask *mask)
1807{
080e28d0 1808 dst->map = mask->masks.map;
df40c152 1809 miniflow_init__(dst, src, miniflow_n_values(dst));
5cb7a798
BP
1810}
1811
1812/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1813 * with miniflow_destroy(). */
1814void
1815miniflow_clone(struct miniflow *dst, const struct miniflow *src)
1816{
3016f3e4
JR
1817 int size = MINIFLOW_VALUES_SIZE(miniflow_n_values(src));
1818 uint32_t *values;
1819
080e28d0 1820 dst->map = src->map;
3016f3e4
JR
1821 if (size <= sizeof dst->inline_values) {
1822 dst->values_inline = true;
1823 values = dst->inline_values;
1824 } else {
1825 dst->values_inline = false;
1826 COVERAGE_INC(miniflow_malloc);
1827 dst->offline_values = xmalloc(size);
1828 values = dst->offline_values;
1829 }
1830 memcpy(values, miniflow_get_values(src), size);
1831}
1832
1833/* Initializes 'dst' as a copy of 'src'. The caller must have allocated
1834 * 'dst' to have inline space all data in 'src'. */
1835void
1836miniflow_clone_inline(struct miniflow *dst, const struct miniflow *src,
1837 size_t n_values)
1838{
1839 dst->values_inline = true;
1840 dst->map = src->map;
1841 memcpy(dst->inline_values, miniflow_get_values(src),
1842 MINIFLOW_VALUES_SIZE(n_values));
5cb7a798
BP
1843}
1844
b2c1f00b 1845/* Initializes 'dst' with the data in 'src', destroying 'src'.
3016f3e4
JR
1846 * The caller must eventually free 'dst' with miniflow_destroy().
1847 * 'dst' must be regularly sized miniflow, but 'src' can have
3c30d111
JR
1848 * storage for more than the default MINI_N_INLINE inline
1849 * values. */
b2c1f00b
BP
1850void
1851miniflow_move(struct miniflow *dst, struct miniflow *src)
1852{
3016f3e4
JR
1853 int size = MINIFLOW_VALUES_SIZE(miniflow_n_values(src));
1854
1855 dst->map = src->map;
1856 if (size <= sizeof dst->inline_values) {
1857 dst->values_inline = true;
1858 memcpy(dst->inline_values, miniflow_get_values(src), size);
1859 miniflow_destroy(src);
097673a6
JR
1860 } else if (src->values_inline) {
1861 dst->values_inline = false;
1862 COVERAGE_INC(miniflow_malloc);
1863 dst->offline_values = xmalloc(size);
1864 memcpy(dst->offline_values, src->inline_values, size);
3016f3e4
JR
1865 } else {
1866 dst->values_inline = false;
1867 dst->offline_values = src->offline_values;
1868 }
b2c1f00b
BP
1869}
1870
5cb7a798
BP
1871/* Frees any memory owned by 'flow'. Does not free the storage in which 'flow'
1872 * itself resides; the caller is responsible for that. */
1873void
1874miniflow_destroy(struct miniflow *flow)
1875{
27bbe15d
JR
1876 if (!flow->values_inline) {
1877 free(flow->offline_values);
5cb7a798
BP
1878 }
1879}
1880
1881/* Initializes 'dst' as a copy of 'src'. */
1882void
1883miniflow_expand(const struct miniflow *src, struct flow *dst)
1884{
ad77e3c5
EJ
1885 memset(dst, 0, sizeof *dst);
1886 flow_union_with_miniflow(dst, src);
5cb7a798
BP
1887}
1888
5cb7a798
BP
1889/* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'flow'
1890 * were expanded into a "struct flow". */
b7807e4f 1891static uint32_t
5cb7a798
BP
1892miniflow_get(const struct miniflow *flow, unsigned int u32_ofs)
1893{
b7807e4f 1894 return (flow->map & UINT64_C(1) << u32_ofs)
27bbe15d 1895 ? *(miniflow_get_u32_values(flow) +
b7807e4f
JR
1896 count_1bits(flow->map & ((UINT64_C(1) << u32_ofs) - 1)))
1897 : 0;
5cb7a798
BP
1898}
1899
8e86f090 1900/* Returns true if 'a' and 'b' are the equal miniflow, false otherwise. */
5cb7a798
BP
1901bool
1902miniflow_equal(const struct miniflow *a, const struct miniflow *b)
1903{
27bbe15d
JR
1904 const uint32_t *ap = miniflow_get_u32_values(a);
1905 const uint32_t *bp = miniflow_get_u32_values(b);
080e28d0
JR
1906 const uint64_t a_map = a->map;
1907 const uint64_t b_map = b->map;
5cb7a798 1908
27bbe15d
JR
1909 if (OVS_LIKELY(a_map == b_map)) {
1910 int count = miniflow_n_values(a);
1911
8cc86801 1912 return !memcmp(ap, bp, count * sizeof *ap);
080e28d0 1913 } else {
27bbe15d
JR
1914 uint64_t map;
1915
080e28d0
JR
1916 for (map = a_map | b_map; map; map = zero_rightmost_1bit(map)) {
1917 uint64_t bit = rightmost_1bit(map);
1918 uint64_t a_value = a_map & bit ? *ap++ : 0;
1919 uint64_t b_value = b_map & bit ? *bp++ : 0;
df40c152 1920
080e28d0
JR
1921 if (a_value != b_value) {
1922 return false;
df40c152 1923 }
5cb7a798
BP
1924 }
1925 }
1926
df40c152 1927 return true;
5cb7a798
BP
1928}
1929
1930/* Returns true if 'a' and 'b' are equal at the places where there are 1-bits
1931 * in 'mask', false if they differ. */
1932bool
1933miniflow_equal_in_minimask(const struct miniflow *a, const struct miniflow *b,
1934 const struct minimask *mask)
1935{
27bbe15d 1936 const uint32_t *p = miniflow_get_u32_values(&mask->masks);
080e28d0 1937 uint64_t map;
5cb7a798 1938
080e28d0 1939 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
d43d314e 1940 int ofs = raw_ctz(map);
5cb7a798 1941
27bbe15d 1942 if ((miniflow_get(a, ofs) ^ miniflow_get(b, ofs)) & *p++) {
080e28d0 1943 return false;
5cb7a798
BP
1944 }
1945 }
1946
1947 return true;
1948}
1949
1950/* Returns true if 'a' and 'b' are equal at the places where there are 1-bits
1951 * in 'mask', false if they differ. */
1952bool
1953miniflow_equal_flow_in_minimask(const struct miniflow *a, const struct flow *b,
1954 const struct minimask *mask)
1955{
1956 const uint32_t *b_u32 = (const uint32_t *) b;
27bbe15d 1957 const uint32_t *p = miniflow_get_u32_values(&mask->masks);
080e28d0 1958 uint64_t map;
5cb7a798 1959
080e28d0 1960 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
d43d314e 1961 int ofs = raw_ctz(map);
5cb7a798 1962
27bbe15d 1963 if ((miniflow_get(a, ofs) ^ b_u32[ofs]) & *p++) {
080e28d0 1964 return false;
5cb7a798
BP
1965 }
1966 }
1967
1968 return true;
1969}
1970
5cb7a798
BP
1971\f
1972/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1973 * with minimask_destroy(). */
1974void
1975minimask_init(struct minimask *mask, const struct flow_wildcards *wc)
1976{
1977 miniflow_init(&mask->masks, &wc->masks);
1978}
1979
1980/* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1981 * with minimask_destroy(). */
1982void
1983minimask_clone(struct minimask *dst, const struct minimask *src)
1984{
1985 miniflow_clone(&dst->masks, &src->masks);
1986}
1987
b2c1f00b
BP
1988/* Initializes 'dst' with the data in 'src', destroying 'src'.
1989 * The caller must eventually free 'dst' with minimask_destroy(). */
1990void
1991minimask_move(struct minimask *dst, struct minimask *src)
1992{
a24de7ee 1993 miniflow_move(&dst->masks, &src->masks);
b2c1f00b
BP
1994}
1995
5cb7a798
BP
1996/* Initializes 'dst_' as the bit-wise "and" of 'a_' and 'b_'.
1997 *
1998 * The caller must provide room for FLOW_U32S "uint32_t"s in 'storage', for use
1999 * by 'dst_'. The caller must *not* free 'dst_' with minimask_destroy(). */
2000void
2001minimask_combine(struct minimask *dst_,
2002 const struct minimask *a_, const struct minimask *b_,
2003 uint32_t storage[FLOW_U32S])
2004{
2005 struct miniflow *dst = &dst_->masks;
27bbe15d 2006 uint32_t *dst_values = storage;
5cb7a798
BP
2007 const struct miniflow *a = &a_->masks;
2008 const struct miniflow *b = &b_->masks;
080e28d0
JR
2009 uint64_t map;
2010 int n = 0;
5cb7a798 2011
27bbe15d
JR
2012 dst->values_inline = false;
2013 dst->offline_values = storage;
080e28d0
JR
2014
2015 dst->map = 0;
2016 for (map = a->map & b->map; map; map = zero_rightmost_1bit(map)) {
d43d314e 2017 int ofs = raw_ctz(map);
080e28d0
JR
2018 uint32_t mask = miniflow_get(a, ofs) & miniflow_get(b, ofs);
2019
2020 if (mask) {
2021 dst->map |= rightmost_1bit(map);
27bbe15d 2022 dst_values[n++] = mask;
5cb7a798
BP
2023 }
2024 }
2025}
2026
2027/* Frees any memory owned by 'mask'. Does not free the storage in which 'mask'
2028 * itself resides; the caller is responsible for that. */
2029void
2030minimask_destroy(struct minimask *mask)
2031{
2032 miniflow_destroy(&mask->masks);
2033}
2034
2035/* Initializes 'dst' as a copy of 'src'. */
2036void
2037minimask_expand(const struct minimask *mask, struct flow_wildcards *wc)
2038{
2039 miniflow_expand(&mask->masks, &wc->masks);
2040}
2041
2042/* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'mask'
2043 * were expanded into a "struct flow_wildcards". */
2044uint32_t
2045minimask_get(const struct minimask *mask, unsigned int u32_ofs)
2046{
2047 return miniflow_get(&mask->masks, u32_ofs);
2048}
2049
5cb7a798
BP
2050/* Returns true if 'a' and 'b' are the same flow mask, false otherwise. */
2051bool
2052minimask_equal(const struct minimask *a, const struct minimask *b)
2053{
2054 return miniflow_equal(&a->masks, &b->masks);
2055}
2056
d4570fd8 2057/* Returns true if at least one bit matched by 'b' is wildcarded by 'a',
5cb7a798
BP
2058 * false otherwise. */
2059bool
d4570fd8 2060minimask_has_extra(const struct minimask *a, const struct minimask *b)
5cb7a798 2061{
27bbe15d 2062 const uint32_t *p = miniflow_get_u32_values(&b->masks);
080e28d0 2063 uint64_t map;
5cb7a798 2064
d4570fd8
JR
2065 for (map = b->masks.map; map; map = zero_rightmost_1bit(map)) {
2066 uint32_t a_u32 = minimask_get(a, raw_ctz(map));
2067 uint32_t b_u32 = *p++;
5cb7a798 2068
080e28d0
JR
2069 if ((a_u32 & b_u32) != b_u32) {
2070 return true;
5cb7a798
BP
2071 }
2072 }
2073
2074 return false;
2075}