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