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