flow: Fully separate flow_wildcards from OpenFlow wildcard bits.
[sliver-openvswitch.git] / lib / flow.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 #include <config.h>
17 #include <sys/types.h>
18 #include "flow.h"
19 #include <inttypes.h>
20 #include <netinet/in.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "byte-order.h"
24 #include "coverage.h"
25 #include "dynamic-string.h"
26 #include "hash.h"
27 #include "ofp-util.h"
28 #include "ofpbuf.h"
29 #include "openflow/openflow.h"
30 #include "openvswitch/datapath-protocol.h"
31 #include "packets.h"
32 #include "unaligned.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(flow);
36
37 static struct arp_eth_header *
38 pull_arp(struct ofpbuf *packet)
39 {
40     return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
41 }
42
43 static struct ip_header *
44 pull_ip(struct ofpbuf *packet)
45 {
46     if (packet->size >= IP_HEADER_LEN) {
47         struct ip_header *ip = packet->data;
48         int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
49         if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
50             return ofpbuf_pull(packet, ip_len);
51         }
52     }
53     return NULL;
54 }
55
56 static struct tcp_header *
57 pull_tcp(struct ofpbuf *packet)
58 {
59     if (packet->size >= TCP_HEADER_LEN) {
60         struct tcp_header *tcp = packet->data;
61         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
62         if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
63             return ofpbuf_pull(packet, tcp_len);
64         }
65     }
66     return NULL;
67 }
68
69 static struct udp_header *
70 pull_udp(struct ofpbuf *packet)
71 {
72     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
73 }
74
75 static struct icmp_header *
76 pull_icmp(struct ofpbuf *packet)
77 {
78     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
79 }
80
81 static void
82 parse_vlan(struct ofpbuf *b, struct flow *flow)
83 {
84     struct qtag_prefix {
85         ovs_be16 eth_type;      /* ETH_TYPE_VLAN */
86         ovs_be16 tci;
87     };
88
89     if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
90         struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
91         flow->dl_vlan = qp->tci & htons(VLAN_VID_MASK);
92         flow->dl_vlan_pcp = vlan_tci_to_pcp(qp->tci);
93     }
94 }
95
96 static ovs_be16
97 parse_ethertype(struct ofpbuf *b)
98 {
99     struct llc_snap_header *llc;
100     ovs_be16 proto;
101
102     proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
103     if (ntohs(proto) >= ODP_DL_TYPE_ETH2_CUTOFF) {
104         return proto;
105     }
106
107     if (b->size < sizeof *llc) {
108         return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
109     }
110
111     llc = b->data;
112     if (llc->llc.llc_dsap != LLC_DSAP_SNAP
113         || llc->llc.llc_ssap != LLC_SSAP_SNAP
114         || llc->llc.llc_cntl != LLC_CNTL_SNAP
115         || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
116                   sizeof llc->snap.snap_org)) {
117         return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
118     }
119
120     ofpbuf_pull(b, sizeof *llc);
121     return llc->snap.snap_type;
122 }
123
124 /* Initializes 'flow' members from 'packet', 'tun_id', and 'in_port.
125  * Initializes 'packet' header pointers as follows:
126  *
127  *    - packet->l2 to the start of the Ethernet header.
128  *
129  *    - packet->l3 to just past the Ethernet header, or just past the
130  *      vlan_header if one is present, to the first byte of the payload of the
131  *      Ethernet frame.
132  *
133  *    - packet->l4 to just past the IPv4 header, if one is present and has a
134  *      correct length, and otherwise NULL.
135  *
136  *    - packet->l7 to just past the TCP or UDP or ICMP header, if one is
137  *      present and has a correct length, and otherwise NULL.
138  */
139 int
140 flow_extract(struct ofpbuf *packet, ovs_be32 tun_id, uint16_t in_port,
141              struct flow *flow)
142 {
143     struct ofpbuf b = *packet;
144     struct eth_header *eth;
145     int retval = 0;
146
147     COVERAGE_INC(flow_extract);
148
149     memset(flow, 0, sizeof *flow);
150     flow->tun_id = tun_id;
151     flow->in_port = in_port;
152     flow->dl_vlan = htons(OFP_VLAN_NONE);
153
154     packet->l2 = b.data;
155     packet->l3 = NULL;
156     packet->l4 = NULL;
157     packet->l7 = NULL;
158
159     if (b.size < sizeof *eth) {
160         return 0;
161     }
162
163     /* Link layer. */
164     eth = b.data;
165     memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
166     memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
167
168     /* dl_type, dl_vlan, dl_vlan_pcp. */
169     ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
170     if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
171         parse_vlan(&b, flow);
172     }
173     flow->dl_type = parse_ethertype(&b);
174
175     /* Network layer. */
176     packet->l3 = b.data;
177     if (flow->dl_type == htons(ETH_TYPE_IP)) {
178         const struct ip_header *nh = pull_ip(&b);
179         if (nh) {
180             flow->nw_src = get_unaligned_u32(&nh->ip_src);
181             flow->nw_dst = get_unaligned_u32(&nh->ip_dst);
182             flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
183             flow->nw_proto = nh->ip_proto;
184             packet->l4 = b.data;
185             if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
186                 if (flow->nw_proto == IP_TYPE_TCP) {
187                     const struct tcp_header *tcp = pull_tcp(&b);
188                     if (tcp) {
189                         flow->tp_src = tcp->tcp_src;
190                         flow->tp_dst = tcp->tcp_dst;
191                         packet->l7 = b.data;
192                     }
193                 } else if (flow->nw_proto == IP_TYPE_UDP) {
194                     const struct udp_header *udp = pull_udp(&b);
195                     if (udp) {
196                         flow->tp_src = udp->udp_src;
197                         flow->tp_dst = udp->udp_dst;
198                         packet->l7 = b.data;
199                     }
200                 } else if (flow->nw_proto == IP_TYPE_ICMP) {
201                     const struct icmp_header *icmp = pull_icmp(&b);
202                     if (icmp) {
203                         flow->icmp_type = htons(icmp->icmp_type);
204                         flow->icmp_code = htons(icmp->icmp_code);
205                         packet->l7 = b.data;
206                     }
207                 }
208             } else {
209                 retval = 1;
210             }
211         }
212     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
213         const struct arp_eth_header *arp = pull_arp(&b);
214         if (arp && arp->ar_hrd == htons(1)
215             && arp->ar_pro == htons(ETH_TYPE_IP)
216             && arp->ar_hln == ETH_ADDR_LEN
217             && arp->ar_pln == 4) {
218             /* We only match on the lower 8 bits of the opcode. */
219             if (ntohs(arp->ar_op) <= 0xff) {
220                 flow->nw_proto = ntohs(arp->ar_op);
221             }
222
223             if ((flow->nw_proto == ARP_OP_REQUEST)
224                 || (flow->nw_proto == ARP_OP_REPLY)) {
225                 flow->nw_src = arp->ar_spa;
226                 flow->nw_dst = arp->ar_tpa;
227             }
228         }
229     }
230     return retval;
231 }
232
233 /* Extracts the flow stats for a packet.  The 'flow' and 'packet'
234  * arguments must have been initialized through a call to flow_extract().
235  */
236 void
237 flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
238         struct odp_flow_stats *stats)
239 {
240     memset(stats, '\0', sizeof(*stats));
241
242     if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
243         if ((flow->nw_proto == IP_TYPE_TCP) && packet->l7) {
244             struct tcp_header *tcp = packet->l4;
245             stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
246         }
247     }
248
249     stats->n_bytes = packet->size;
250     stats->n_packets = 1;
251 }
252
253 char *
254 flow_to_string(const struct flow *flow)
255 {
256     struct ds ds = DS_EMPTY_INITIALIZER;
257     flow_format(&ds, flow);
258     return ds_cstr(&ds);
259 }
260
261 void
262 flow_format(struct ds *ds, const struct flow *flow)
263 {
264     ds_put_format(ds, "tunnel%08"PRIx32":in_port%04"PRIx16
265                       ":vlan%"PRIu16":pcp%"PRIu8
266                       " mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
267                       " type%04"PRIx16
268                       " proto%"PRIu8
269                       " tos%"PRIu8
270                       " ip"IP_FMT"->"IP_FMT
271                       " port%"PRIu16"->%"PRIu16,
272                   ntohl(flow->tun_id),
273                   flow->in_port,
274                   ntohs(flow->dl_vlan),
275                   flow->dl_vlan_pcp,
276                   ETH_ADDR_ARGS(flow->dl_src),
277                   ETH_ADDR_ARGS(flow->dl_dst),
278                   ntohs(flow->dl_type),
279                   flow->nw_proto,
280                   flow->nw_tos,
281                   IP_ARGS(&flow->nw_src),
282                   IP_ARGS(&flow->nw_dst),
283                   ntohs(flow->tp_src),
284                   ntohs(flow->tp_dst));
285 }
286
287 void
288 flow_print(FILE *stream, const struct flow *flow)
289 {
290     char *s = flow_to_string(flow);
291     fputs(s, stream);
292     free(s);
293 }
294 \f
295 /* flow_wildcards functions. */
296
297 /* Initializes 'wc' as a set of wildcards that matches every packet. */
298 void
299 flow_wildcards_init_catchall(struct flow_wildcards *wc)
300 {
301     wc->wildcards = FWW_ALL;
302     wc->nw_src_mask = htonl(0);
303     wc->nw_dst_mask = htonl(0);
304     memset(wc->reg_masks, 0, sizeof wc->reg_masks);
305 }
306
307 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
308  * wildcard any bits or fields. */
309 void
310 flow_wildcards_init_exact(struct flow_wildcards *wc)
311 {
312     wc->wildcards = 0;
313     wc->nw_src_mask = htonl(UINT32_MAX);
314     wc->nw_dst_mask = htonl(UINT32_MAX);
315     memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
316 }
317
318 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
319  * fields. */
320 bool
321 flow_wildcards_is_exact(const struct flow_wildcards *wc)
322 {
323     int i;
324
325     if (wc->wildcards
326         || wc->nw_src_mask != htonl(UINT32_MAX)
327         || wc->nw_dst_mask != htonl(UINT32_MAX)) {
328         return false;
329     }
330
331     for (i = 0; i < FLOW_N_REGS; i++) {
332         if (wc->reg_masks[i] != htonl(UINT32_MAX)) {
333             return false;
334         }
335     }
336
337     return true;
338 }
339
340 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
341  * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
342  * 'src1' or 'src2' or both.  */
343 void
344 flow_wildcards_combine(struct flow_wildcards *dst,
345                        const struct flow_wildcards *src1,
346                        const struct flow_wildcards *src2)
347 {
348     int i;
349
350     dst->wildcards = src1->wildcards | src2->wildcards;
351     dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
352     dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
353     for (i = 0; i < FLOW_N_REGS; i++) {
354         dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
355     }
356 }
357
358 /* Returns a hash of the wildcards in 'wc'. */
359 uint32_t
360 flow_wildcards_hash(const struct flow_wildcards *wc)
361 {
362     /* If you change struct flow_wildcards and thereby trigger this
363      * assertion, please check that the new struct flow_wildcards has no holes
364      * in it before you update the assertion. */
365     BUILD_ASSERT_DECL(sizeof *wc == 12 + FLOW_N_REGS * 4);
366     return hash_bytes(wc, sizeof *wc, 0);
367 }
368
369 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
370  * different. */
371 bool
372 flow_wildcards_equal(const struct flow_wildcards *a,
373                      const struct flow_wildcards *b)
374 {
375     int i;
376
377     if (a->wildcards != b->wildcards
378         || a->nw_src_mask != b->nw_src_mask
379         || a->nw_dst_mask != b->nw_dst_mask) {
380         return false;
381     }
382
383     for (i = 0; i < FLOW_N_REGS; i++) {
384         if (a->reg_masks[i] != b->reg_masks[i]) {
385             return false;
386         }
387     }
388
389     return true;
390 }
391
392 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
393  * 'b', false otherwise. */
394 bool
395 flow_wildcards_has_extra(const struct flow_wildcards *a,
396                          const struct flow_wildcards *b)
397 {
398     int i;
399
400     for (i = 0; i < FLOW_N_REGS; i++) {
401         if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
402             return true;
403         }
404     }
405
406     return (a->wildcards & ~b->wildcards
407             || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
408             || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask);
409 }
410
411 static bool
412 set_nw_mask(ovs_be32 *maskp, ovs_be32 mask)
413 {
414     if (ip_is_cidr(mask)) {
415         *maskp = mask;
416         return true;
417     } else {
418         return false;
419     }
420 }
421
422 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
423  * high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
424  * false if 'mask' is not a CIDR mask.  */
425 bool
426 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
427 {
428     return set_nw_mask(&wc->nw_src_mask, mask);
429 }
430
431 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
432  * N high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
433  * false if 'mask' is not a CIDR mask.  */
434 bool
435 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
436 {
437     return set_nw_mask(&wc->nw_dst_mask, mask);
438 }
439
440 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
441  * (A 0-bit indicates a wildcard bit.) */
442 void
443 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
444 {
445     wc->reg_masks[idx] = mask;
446 }