Update primary code license to Apache 2.0.
[cascardo/ovs.git] / lib / flow.c
1 /*
2  * Copyright (c) 2008, 2009 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 "coverage.h"
24 #include "dynamic-string.h"
25 #include "hash.h"
26 #include "ofpbuf.h"
27 #include "openflow/openflow.h"
28 #include "openvswitch/datapath-protocol.h"
29 #include "packets.h"
30
31 #include "vlog.h"
32 #define THIS_MODULE VLM_flow
33
34 static struct ip_header *
35 pull_ip(struct ofpbuf *packet)
36 {
37     if (packet->size >= IP_HEADER_LEN) {
38         struct ip_header *ip = packet->data;
39         int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
40         if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
41             return ofpbuf_pull(packet, ip_len);
42         }
43     }
44     return NULL;
45 }
46
47 static struct tcp_header *
48 pull_tcp(struct ofpbuf *packet) 
49 {
50     if (packet->size >= TCP_HEADER_LEN) {
51         struct tcp_header *tcp = packet->data;
52         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
53         if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
54             return ofpbuf_pull(packet, tcp_len);
55         }
56     }
57     return NULL;
58 }
59
60 static struct udp_header *
61 pull_udp(struct ofpbuf *packet) 
62 {
63     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
64 }
65
66 static struct icmp_header *
67 pull_icmp(struct ofpbuf *packet) 
68 {
69     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
70 }
71
72 static struct eth_header *
73 pull_eth(struct ofpbuf *packet) 
74 {
75     return ofpbuf_try_pull(packet, ETH_HEADER_LEN);
76 }
77
78 static struct vlan_header *
79 pull_vlan(struct ofpbuf *packet)
80 {
81     return ofpbuf_try_pull(packet, VLAN_HEADER_LEN);
82 }
83
84 /* Returns 1 if 'packet' is an IP fragment, 0 otherwise. */
85 int
86 flow_extract(struct ofpbuf *packet, uint16_t in_port, flow_t *flow)
87 {
88     struct ofpbuf b = *packet;
89     struct eth_header *eth;
90     int retval = 0;
91
92     COVERAGE_INC(flow_extract);
93
94     memset(flow, 0, sizeof *flow);
95     flow->dl_vlan = htons(OFP_VLAN_NONE);
96     flow->in_port = in_port;
97
98     packet->l2 = b.data;
99     packet->l3 = NULL;
100     packet->l4 = NULL;
101     packet->l7 = NULL;
102
103     eth = pull_eth(&b);
104     if (eth) {
105         if (ntohs(eth->eth_type) >= OFP_DL_TYPE_ETH2_CUTOFF) {
106             /* This is an Ethernet II frame */
107             flow->dl_type = eth->eth_type;
108         } else {
109             /* This is an 802.2 frame */
110             struct llc_header *llc = ofpbuf_at(&b, 0, sizeof *llc);
111             struct snap_header *snap = ofpbuf_at(&b, sizeof *llc,
112                                                  sizeof *snap);
113             if (llc == NULL) {
114                 return 0;
115             }
116             if (snap
117                 && llc->llc_dsap == LLC_DSAP_SNAP
118                 && llc->llc_ssap == LLC_SSAP_SNAP
119                 && llc->llc_cntl == LLC_CNTL_SNAP
120                 && !memcmp(snap->snap_org, SNAP_ORG_ETHERNET,
121                            sizeof snap->snap_org)) {
122                 flow->dl_type = snap->snap_type;
123                 ofpbuf_pull(&b, LLC_SNAP_HEADER_LEN);
124             } else {
125                 flow->dl_type = htons(OFP_DL_TYPE_NOT_ETH_TYPE);
126                 ofpbuf_pull(&b, sizeof(struct llc_header));
127             }
128         }
129
130         /* Check for a VLAN tag */
131         if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
132             struct vlan_header *vh = pull_vlan(&b);
133             if (vh) {
134                 flow->dl_type = vh->vlan_next_type;
135                 flow->dl_vlan = vh->vlan_tci & htons(VLAN_VID_MASK);
136             }
137         }
138         memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
139         memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
140
141         packet->l3 = b.data;
142         if (flow->dl_type == htons(ETH_TYPE_IP)) {
143             const struct ip_header *nh = pull_ip(&b);
144             if (nh) {
145                 flow->nw_src = nh->ip_src;
146                 flow->nw_dst = nh->ip_dst;
147                 flow->nw_proto = nh->ip_proto;
148                 packet->l4 = b.data;
149                 if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
150                     if (flow->nw_proto == IP_TYPE_TCP) {
151                         const struct tcp_header *tcp = pull_tcp(&b);
152                         if (tcp) {
153                             flow->tp_src = tcp->tcp_src;
154                             flow->tp_dst = tcp->tcp_dst;
155                             packet->l7 = b.data;
156                         } else {
157                             /* Avoid tricking other code into thinking that
158                              * this packet has an L4 header. */
159                             flow->nw_proto = 0;
160                         }
161                     } else if (flow->nw_proto == IP_TYPE_UDP) {
162                         const struct udp_header *udp = pull_udp(&b);
163                         if (udp) {
164                             flow->tp_src = udp->udp_src;
165                             flow->tp_dst = udp->udp_dst;
166                             packet->l7 = b.data;
167                         } else {
168                             /* Avoid tricking other code into thinking that
169                              * this packet has an L4 header. */
170                             flow->nw_proto = 0;
171                         }
172                     } else if (flow->nw_proto == IP_TYPE_ICMP) {
173                         const struct icmp_header *icmp = pull_icmp(&b);
174                         if (icmp) {
175                             flow->icmp_type = htons(icmp->icmp_type);
176                             flow->icmp_code = htons(icmp->icmp_code);
177                             packet->l7 = b.data;
178                         } else {
179                             /* Avoid tricking other code into thinking that
180                              * this packet has an L4 header. */
181                             flow->nw_proto = 0;
182                         }
183                     }
184                 } else {
185                     retval = 1;
186                 }
187             }
188         }
189     }
190     return retval;
191 }
192
193 /* Extracts the flow stats for a packet.  The 'flow' and 'packet'
194  * arguments must have been initialized through a call to flow_extract().
195  */
196 void
197 flow_extract_stats(const flow_t *flow, struct ofpbuf *packet, 
198         struct odp_flow_stats *stats)
199 {
200     memset(stats, '\0', sizeof(*stats));
201
202     if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
203         struct ip_header *ip = packet->l3;
204         stats->ip_tos = ip->ip_tos;
205         if ((flow->nw_proto == IP_TYPE_TCP) && packet->l7) {
206             struct tcp_header *tcp = packet->l4;
207             stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
208         }
209     }
210
211     stats->n_bytes = packet->size;
212     stats->n_packets = 1;
213 }
214
215 void
216 flow_to_match(const flow_t *flow, uint32_t wildcards, struct ofp_match *match)
217 {
218     match->wildcards = htonl(wildcards);
219     match->in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
220                            : flow->in_port);
221     match->dl_vlan = flow->dl_vlan;
222     memcpy(match->dl_src, flow->dl_src, ETH_ADDR_LEN);
223     memcpy(match->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
224     match->dl_type = flow->dl_type;
225     match->nw_src = flow->nw_src;
226     match->nw_dst = flow->nw_dst;
227     match->nw_proto = flow->nw_proto;
228     match->tp_src = flow->tp_src;
229     match->tp_dst = flow->tp_dst;
230     match->pad = 0;
231 }
232
233 void
234 flow_from_match(flow_t *flow, uint32_t *wildcards,
235                 const struct ofp_match *match)
236 {
237     if (wildcards) {
238         *wildcards = ntohl(match->wildcards);
239     }
240     flow->nw_src = match->nw_src;
241     flow->nw_dst = match->nw_dst;
242     flow->in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
243                      : ntohs(match->in_port));
244     flow->dl_vlan = match->dl_vlan;
245     flow->dl_type = match->dl_type;
246     flow->tp_src = match->tp_src;
247     flow->tp_dst = match->tp_dst;
248     memcpy(flow->dl_src, match->dl_src, ETH_ADDR_LEN);
249     memcpy(flow->dl_dst, match->dl_dst, ETH_ADDR_LEN);
250     flow->nw_proto = match->nw_proto;
251     flow->reserved = 0;
252 }
253
254 char *
255 flow_to_string(const flow_t *flow)
256 {
257     struct ds ds = DS_EMPTY_INITIALIZER;
258     flow_format(&ds, flow);
259     return ds_cstr(&ds);
260 }
261
262 void
263 flow_format(struct ds *ds, const flow_t *flow)
264 {
265     ds_put_format(ds, "port%04x:vlan%d mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" "
266                   "type%04x proto%"PRId8" ip"IP_FMT"->"IP_FMT" port%d->%d",
267                   flow->in_port, ntohs(flow->dl_vlan),
268                   ETH_ADDR_ARGS(flow->dl_src), ETH_ADDR_ARGS(flow->dl_dst),
269                   ntohs(flow->dl_type), flow->nw_proto,
270                   IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst),
271                   ntohs(flow->tp_src), ntohs(flow->tp_dst));
272 }
273
274 void
275 flow_print(FILE *stream, const flow_t *flow) 
276 {
277     char *s = flow_to_string(flow);
278     fputs(s, stream);
279     free(s);
280 }