6f08f3a2312c28d743b92829b09b21d0e85a53d0
[cascardo/ovs.git] / include / openvswitch / flow.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef OPENVSWITCH_FLOW_H
17 #define OPENVSWITCH_FLOW_H 1
18
19 #include "openflow/nicira-ext.h"
20 #include "openvswitch/packets.h"
21 #include "openvswitch/util.h"
22
23 /* This sequence number should be incremented whenever anything involving flows
24  * or the wildcarding of flows changes.  This will cause build assertion
25  * failures in places which likely need to be updated. */
26 #define FLOW_WC_SEQ 35
27
28 /* Number of Open vSwitch extension 32-bit registers. */
29 #define FLOW_N_REGS 8
30 BUILD_ASSERT_DECL(FLOW_N_REGS <= NXM_NX_MAX_REGS);
31 BUILD_ASSERT_DECL(FLOW_N_REGS % 2 == 0); /* Even. */
32
33 /* Number of OpenFlow 1.5+ 64-bit registers.
34  *
35  * Each of these overlays a pair of Open vSwitch 32-bit registers, so there
36  * are half as many of them.*/
37 #define FLOW_N_XREGS (FLOW_N_REGS / 2)
38
39 /* Used for struct flow's dl_type member for frames that have no Ethernet
40  * type, that is, pure 802.2 frames. */
41 #define FLOW_DL_TYPE_NONE 0x5ff
42
43 /* Fragment bits, used for IPv4 and IPv6, always zero for non-IP flows. */
44 #define FLOW_NW_FRAG_ANY   (1 << 0) /* Set for any IP frag. */
45 #define FLOW_NW_FRAG_LATER (1 << 1) /* Set for IP frag with nonzero offset. */
46 #define FLOW_NW_FRAG_MASK  (FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER)
47
48 BUILD_ASSERT_DECL(FLOW_NW_FRAG_ANY == NX_IP_FRAG_ANY);
49 BUILD_ASSERT_DECL(FLOW_NW_FRAG_LATER == NX_IP_FRAG_LATER);
50
51 BUILD_ASSERT_DECL(FLOW_TNL_F_OAM == NX_TUN_FLAG_OAM);
52
53 const char *flow_tun_flag_to_string(uint32_t flags);
54
55 /* Maximum number of supported MPLS labels. */
56 #define FLOW_MAX_MPLS_LABELS 3
57
58 /*
59  * A flow in the network.
60  *
61  * Must be initialized to all zeros to make any compiler-induced padding
62  * zeroed.  Helps also in keeping unused fields (such as mutually exclusive
63  * IPv4 and IPv6 addresses) zeroed out.
64  *
65  * The meaning of 'in_port' is context-dependent.  In most cases, it is a
66  * 16-bit OpenFlow 1.0 port number.  In the software datapath interface (dpif)
67  * layer and its implementations (e.g. dpif-netlink, dpif-netdev), it is
68  * instead a 32-bit datapath port number.
69  *
70  * The fields are organized in four segments to facilitate staged lookup, where
71  * lower layer fields are first used to determine if the later fields need to
72  * be looked at.  This enables better wildcarding for datapath flows.
73  *
74  * NOTE: Order of the fields is significant, any change in the order must be
75  * reflected in miniflow_extract()!
76  */
77 struct flow {
78     /* Metadata */
79     struct flow_tnl tunnel;     /* Encapsulating tunnel parameters. */
80     ovs_be64 metadata;          /* OpenFlow Metadata. */
81     uint32_t regs[FLOW_N_REGS]; /* Registers. */
82     uint32_t skb_priority;      /* Packet priority for QoS. */
83     uint32_t pkt_mark;          /* Packet mark. */
84     uint32_t dp_hash;           /* Datapath computed hash value. The exact
85                                  * computation is opaque to the user space. */
86     union flow_in_port in_port; /* Input port.*/
87     uint32_t recirc_id;         /* Must be exact match. */
88     uint16_t ct_state;          /* Connection tracking state. */
89     uint16_t ct_zone;           /* Connection tracking zone. */
90     uint32_t ct_mark;           /* Connection mark.*/
91     uint8_t pad1[4];            /* Pad to 64 bits. */
92     ovs_u128 ct_label;          /* Connection label. */
93     uint32_t conj_id;           /* Conjunction ID. */
94     ofp_port_t actset_output;   /* Output port in action set. */
95     uint8_t pad2[2];            /* Pad to 64 bits. */
96
97     /* L2, Order the same as in the Ethernet header! (64-bit aligned) */
98     struct eth_addr dl_dst;     /* Ethernet destination address. */
99     struct eth_addr dl_src;     /* Ethernet source address. */
100     ovs_be16 dl_type;           /* Ethernet frame type. */
101     ovs_be16 vlan_tci;          /* If 802.1Q, TCI | VLAN_CFI; otherwise 0. */
102     ovs_be32 mpls_lse[ROUND_UP(FLOW_MAX_MPLS_LABELS, 2)]; /* MPLS label stack
103                                                              (with padding). */
104     /* L3 (64-bit aligned) */
105     ovs_be32 nw_src;            /* IPv4 source address. */
106     ovs_be32 nw_dst;            /* IPv4 destination address. */
107     struct in6_addr ipv6_src;   /* IPv6 source address. */
108     struct in6_addr ipv6_dst;   /* IPv6 destination address. */
109     ovs_be32 ipv6_label;        /* IPv6 flow label. */
110     uint8_t nw_frag;            /* FLOW_FRAG_* flags. */
111     uint8_t nw_tos;             /* IP ToS (including DSCP and ECN). */
112     uint8_t nw_ttl;             /* IP TTL/Hop Limit. */
113     uint8_t nw_proto;           /* IP protocol or low 8 bits of ARP opcode. */
114     struct in6_addr nd_target;  /* IPv6 neighbor discovery (ND) target. */
115     struct eth_addr arp_sha;    /* ARP/ND source hardware address. */
116     struct eth_addr arp_tha;    /* ARP/ND target hardware address. */
117     ovs_be16 tcp_flags;         /* TCP flags. With L3 to avoid matching L4. */
118     ovs_be16 pad3;              /* Pad to 64 bits. */
119
120     /* L4 (64-bit aligned) */
121     ovs_be16 tp_src;            /* TCP/UDP/SCTP source port/ICMP type. */
122     ovs_be16 tp_dst;            /* TCP/UDP/SCTP destination port/ICMP code. */
123     ovs_be32 igmp_group_ip4;    /* IGMP group IPv4 address.
124                                  * Keep last for BUILD_ASSERT_DECL below. */
125 };
126 BUILD_ASSERT_DECL(sizeof(struct flow) % sizeof(uint64_t) == 0);
127 BUILD_ASSERT_DECL(sizeof(struct flow_tnl) % sizeof(uint64_t) == 0);
128
129 #define FLOW_U64S (sizeof(struct flow) / sizeof(uint64_t))
130
131 /* Remember to update FLOW_WC_SEQ when changing 'struct flow'. */
132 BUILD_ASSERT_DECL(offsetof(struct flow, igmp_group_ip4) + sizeof(uint32_t)
133                   == sizeof(struct flow_tnl) + 216
134                   && FLOW_WC_SEQ == 35);
135
136 /* Incremental points at which flow classification may be performed in
137  * segments.
138  * This is located here since this is dependent on the structure of the
139  * struct flow defined above:
140  * Each offset must be on a distinct, successive U64 boundary strictly
141  * within the struct flow. */
142 enum {
143     FLOW_SEGMENT_1_ENDS_AT = offsetof(struct flow, dl_dst),
144     FLOW_SEGMENT_2_ENDS_AT = offsetof(struct flow, nw_src),
145     FLOW_SEGMENT_3_ENDS_AT = offsetof(struct flow, tp_src),
146 };
147 BUILD_ASSERT_DECL(FLOW_SEGMENT_1_ENDS_AT % sizeof(uint64_t) == 0);
148 BUILD_ASSERT_DECL(FLOW_SEGMENT_2_ENDS_AT % sizeof(uint64_t) == 0);
149 BUILD_ASSERT_DECL(FLOW_SEGMENT_3_ENDS_AT % sizeof(uint64_t) == 0);
150 BUILD_ASSERT_DECL(                     0 < FLOW_SEGMENT_1_ENDS_AT);
151 BUILD_ASSERT_DECL(FLOW_SEGMENT_1_ENDS_AT < FLOW_SEGMENT_2_ENDS_AT);
152 BUILD_ASSERT_DECL(FLOW_SEGMENT_2_ENDS_AT < FLOW_SEGMENT_3_ENDS_AT);
153 BUILD_ASSERT_DECL(FLOW_SEGMENT_3_ENDS_AT < sizeof(struct flow));
154
155 /* Wildcards for a flow.
156  *
157  * A 1-bit in each bit in 'masks' indicates that the corresponding bit of
158  * the flow is significant (must match).  A 0-bit indicates that the
159  * corresponding bit of the flow is wildcarded (need not match). */
160 struct flow_wildcards {
161     struct flow masks;
162 };
163
164 #define WC_MASK_FIELD(WC, FIELD) \
165     memset(&(WC)->masks.FIELD, 0xff, sizeof (WC)->masks.FIELD)
166 #define WC_MASK_FIELD_MASK(WC, FIELD, MASK)     \
167     ((WC)->masks.FIELD |= (MASK))
168 #define WC_UNMASK_FIELD(WC, FIELD) \
169     memset(&(WC)->masks.FIELD, 0, sizeof (WC)->masks.FIELD)
170
171 void flow_wildcards_init_catchall(struct flow_wildcards *);
172
173 void flow_wildcards_init_for_packet(struct flow_wildcards *,
174                                     const struct flow *);
175
176 void flow_wildcards_clear_non_packet_fields(struct flow_wildcards *);
177
178 bool flow_wildcards_is_catchall(const struct flow_wildcards *);
179
180 void flow_wildcards_set_reg_mask(struct flow_wildcards *,
181                                  int idx, uint32_t mask);
182 void flow_wildcards_set_xreg_mask(struct flow_wildcards *,
183                                   int idx, uint64_t mask);
184
185 void flow_wildcards_and(struct flow_wildcards *dst,
186                         const struct flow_wildcards *src1,
187                         const struct flow_wildcards *src2);
188 void flow_wildcards_or(struct flow_wildcards *dst,
189                        const struct flow_wildcards *src1,
190                        const struct flow_wildcards *src2);
191 bool flow_wildcards_has_extra(const struct flow_wildcards *,
192                               const struct flow_wildcards *);
193 uint32_t flow_wildcards_hash(const struct flow_wildcards *, uint32_t basis);
194 bool flow_wildcards_equal(const struct flow_wildcards *,
195                           const struct flow_wildcards *);
196
197 #endif /* flow.h */