Style fix: f(x) is better than f((x))
[sliver-openvswitch.git] / lib / flow.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33 #include <config.h>
34 #include <sys/types.h>
35 #include "flow.h"
36 #include <inttypes.h>
37 #include <netinet/in.h>
38 #include <string.h>
39 #include "hash.h"
40 #include "ofpbuf.h"
41 #include "openflow/openflow.h"
42 #include "packets.h"
43
44 #include "vlog.h"
45 #define THIS_MODULE VLM_flow
46
47 static struct ip_header *
48 pull_ip(struct ofpbuf *packet)
49 {
50     if (packet->size >= IP_HEADER_LEN) {
51         struct ip_header *ip = packet->data;
52         int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
53         if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
54             return ofpbuf_pull(packet, ip_len);
55         }
56     }
57     return NULL;
58 }
59
60 static struct tcp_header *
61 pull_tcp(struct ofpbuf *packet) 
62 {
63     if (packet->size >= TCP_HEADER_LEN) {
64         struct tcp_header *tcp = packet->data;
65         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
66         if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
67             return ofpbuf_pull(packet, tcp_len);
68         }
69     }
70     return NULL;
71 }
72
73 static struct udp_header *
74 pull_udp(struct ofpbuf *packet) 
75 {
76     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
77 }
78
79 static struct icmp_header *
80 pull_icmp(struct ofpbuf *packet) 
81 {
82     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
83 }
84
85 static struct eth_header *
86 pull_eth(struct ofpbuf *packet) 
87 {
88     return ofpbuf_try_pull(packet, ETH_HEADER_LEN);
89 }
90
91 static struct vlan_header *
92 pull_vlan(struct ofpbuf *packet)
93 {
94     return ofpbuf_try_pull(packet, VLAN_HEADER_LEN);
95 }
96
97 /* Returns 1 if 'packet' is an IP fragment, 0 otherwise. */
98 int
99 flow_extract(struct ofpbuf *packet, uint16_t in_port, struct flow *flow)
100 {
101     struct ofpbuf b = *packet;
102     struct eth_header *eth;
103     int retval = 0;
104
105     if (b.size < ETH_TOTAL_MIN) {
106         /* This message is not too useful since there are various ways that we
107          * can end up with runt frames, e.g. frames that only ever passed
108          * through virtual network devices and never touched a physical
109          * Ethernet. */
110         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
111         VLOG_DBG_RL(&rl, "packet length %zu less than minimum size %d",
112                     b.size, ETH_TOTAL_MIN);
113     }
114
115     memset(flow, 0, sizeof *flow);
116     flow->dl_vlan = htons(OFP_VLAN_NONE);
117     flow->in_port = htons(in_port);
118
119     packet->l2 = b.data;
120     packet->l3 = NULL;
121     packet->l4 = NULL;
122     packet->l7 = NULL;
123
124     eth = pull_eth(&b);
125     if (eth) {
126         if (ntohs(eth->eth_type) >= OFP_DL_TYPE_ETH2_CUTOFF) {
127             /* This is an Ethernet II frame */
128             flow->dl_type = eth->eth_type;
129         } else {
130             /* This is an 802.2 frame */
131             struct llc_snap_header *h = ofpbuf_at(&b, 0, sizeof *h);
132             if (h == NULL) {
133                 return 0;
134             }
135             if (h->llc.llc_dsap == LLC_DSAP_SNAP
136                 && h->llc.llc_ssap == LLC_SSAP_SNAP
137                 && h->llc.llc_cntl == LLC_CNTL_SNAP
138                 && !memcmp(h->snap.snap_org, SNAP_ORG_ETHERNET,
139                            sizeof h->snap.snap_org)) {
140                 flow->dl_type = h->snap.snap_type;
141                 ofpbuf_pull(&b, sizeof *h);
142             } else {
143                 flow->dl_type = htons(OFP_DL_TYPE_NOT_ETH_TYPE);
144                 ofpbuf_pull(&b, sizeof(struct llc_header));
145             }
146         }
147
148         /* Check for a VLAN tag */
149         if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
150             struct vlan_header *vh = pull_vlan(&b);
151             if (vh) {
152                 flow->dl_type = vh->vlan_next_type;
153                 flow->dl_vlan = vh->vlan_tci & htons(VLAN_VID_MASK);
154             }
155         }
156         memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
157         memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
158
159         packet->l3 = b.data;
160         if (flow->dl_type == htons(ETH_TYPE_IP)) {
161             const struct ip_header *nh = pull_ip(&b);
162             if (nh) {
163                 flow->nw_src = nh->ip_src;
164                 flow->nw_dst = nh->ip_dst;
165                 flow->nw_proto = nh->ip_proto;
166                 packet->l4 = b.data;
167                 if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
168                     if (flow->nw_proto == IP_TYPE_TCP) {
169                         const struct tcp_header *tcp = pull_tcp(&b);
170                         if (tcp) {
171                             flow->tp_src = tcp->tcp_src;
172                             flow->tp_dst = tcp->tcp_dst;
173                             packet->l7 = b.data;
174                         } else {
175                             /* Avoid tricking other code into thinking that
176                              * this packet has an L4 header. */
177                             flow->nw_proto = 0;
178                         }
179                     } else if (flow->nw_proto == IP_TYPE_UDP) {
180                         const struct udp_header *udp = pull_udp(&b);
181                         if (udp) {
182                             flow->tp_src = udp->udp_src;
183                             flow->tp_dst = udp->udp_dst;
184                             packet->l7 = b.data;
185                         } else {
186                             /* Avoid tricking other code into thinking that
187                              * this packet has an L4 header. */
188                             flow->nw_proto = 0;
189                         }
190                     } else if (flow->nw_proto == IP_TYPE_ICMP) {
191                         const struct icmp_header *icmp = pull_icmp(&b);
192                         if (icmp) {
193                             flow->icmp_type = htons(icmp->icmp_type);
194                             flow->icmp_code = htons(icmp->icmp_code);
195                             packet->l7 = b.data;
196                         } else {
197                             /* Avoid tricking other code into thinking that
198                              * this packet has an L4 header. */
199                             flow->nw_proto = 0;
200                         }
201                     }
202                 } else {
203                     retval = 1;
204                 }
205             }
206         }
207     }
208     return retval;
209 }
210
211 void
212 flow_print(FILE *stream, const struct flow *flow) 
213 {
214     fprintf(stream,
215             "port%04x:vlan%04x mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" "
216             "proto%04x ip"IP_FMT"->"IP_FMT" port%d->%d",
217             ntohs(flow->in_port), ntohs(flow->dl_vlan),
218             ETH_ADDR_ARGS(flow->dl_src), ETH_ADDR_ARGS(flow->dl_dst),
219             ntohs(flow->dl_type),
220             IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst),
221             ntohs(flow->tp_src), ntohs(flow->tp_dst));
222 }
223
224 int
225 flow_compare(const struct flow *a, const struct flow *b)
226 {
227     return memcmp(a, b, sizeof *a);
228 }
229
230 unsigned long int
231 flow_hash(const struct flow *flow, uint32_t basis) 
232 {
233     return hash_fnv(flow, sizeof *flow, basis);
234 }