ofpbuf: New function ofpbuf_push_zeros().
[openvswitch] / 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 "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 #include "xtoxll.h"
31
32 #include "vlog.h"
33 #define THIS_MODULE VLM_flow
34
35 static struct arp_eth_header *
36 pull_arp(struct ofpbuf *packet)
37 {
38     return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
39 }
40
41 static struct ip_header *
42 pull_ip(struct ofpbuf *packet)
43 {
44     if (packet->size >= IP_HEADER_LEN) {
45         struct ip_header *ip = packet->data;
46         int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
47         if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
48             return ofpbuf_pull(packet, ip_len);
49         }
50     }
51     return NULL;
52 }
53
54 static struct tcp_header *
55 pull_tcp(struct ofpbuf *packet) 
56 {
57     if (packet->size >= TCP_HEADER_LEN) {
58         struct tcp_header *tcp = packet->data;
59         int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
60         if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
61             return ofpbuf_pull(packet, tcp_len);
62         }
63     }
64     return NULL;
65 }
66
67 static struct udp_header *
68 pull_udp(struct ofpbuf *packet) 
69 {
70     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
71 }
72
73 static struct icmp_header *
74 pull_icmp(struct ofpbuf *packet) 
75 {
76     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
77 }
78
79 static struct eth_header *
80 pull_eth(struct ofpbuf *packet) 
81 {
82     return ofpbuf_try_pull(packet, ETH_HEADER_LEN);
83 }
84
85 static struct vlan_header *
86 pull_vlan(struct ofpbuf *packet)
87 {
88     return ofpbuf_try_pull(packet, VLAN_HEADER_LEN);
89 }
90
91 /* Returns 1 if 'packet' is an IP fragment, 0 otherwise.
92  * 'tun_id' is in network byte order, while 'in_port' is in host byte order.
93  * These byte orders are the same as they are in struct odp_flow_key. */
94 int
95 flow_extract(struct ofpbuf *packet, uint32_t tun_id, uint16_t in_port,
96              flow_t *flow)
97 {
98     struct ofpbuf b = *packet;
99     struct eth_header *eth;
100     int retval = 0;
101
102     COVERAGE_INC(flow_extract);
103
104     memset(flow, 0, sizeof *flow);
105     flow->tun_id = tun_id;
106     flow->in_port = in_port;
107     flow->dl_vlan = htons(OFP_VLAN_NONE);
108
109     packet->l2 = b.data;
110     packet->l3 = NULL;
111     packet->l4 = NULL;
112     packet->l7 = NULL;
113
114     eth = pull_eth(&b);
115     if (eth) {
116         if (ntohs(eth->eth_type) >= OFP_DL_TYPE_ETH2_CUTOFF) {
117             /* This is an Ethernet II frame */
118             flow->dl_type = eth->eth_type;
119         } else {
120             /* This is an 802.2 frame */
121             struct llc_header *llc = ofpbuf_at(&b, 0, sizeof *llc);
122             struct snap_header *snap = ofpbuf_at(&b, sizeof *llc,
123                                                  sizeof *snap);
124             if (llc == NULL) {
125                 return 0;
126             }
127             if (snap
128                 && llc->llc_dsap == LLC_DSAP_SNAP
129                 && llc->llc_ssap == LLC_SSAP_SNAP
130                 && llc->llc_cntl == LLC_CNTL_SNAP
131                 && !memcmp(snap->snap_org, SNAP_ORG_ETHERNET,
132                            sizeof snap->snap_org)) {
133                 flow->dl_type = snap->snap_type;
134                 ofpbuf_pull(&b, LLC_SNAP_HEADER_LEN);
135             } else {
136                 flow->dl_type = htons(OFP_DL_TYPE_NOT_ETH_TYPE);
137                 ofpbuf_pull(&b, sizeof(struct llc_header));
138             }
139         }
140
141         /* Check for a VLAN tag */
142         if (flow->dl_type == htons(ETH_TYPE_VLAN)) {
143             struct vlan_header *vh = pull_vlan(&b);
144             if (vh) {
145                 flow->dl_type = vh->vlan_next_type;
146                 flow->dl_vlan = vh->vlan_tci & htons(VLAN_VID_MASK);
147                 flow->dl_vlan_pcp = (ntohs(vh->vlan_tci) & 0xe000) >> 13;
148             }
149         }
150         memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
151         memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
152
153         packet->l3 = b.data;
154         if (flow->dl_type == htons(ETH_TYPE_IP)) {
155             const struct ip_header *nh = pull_ip(&b);
156             if (nh) {
157                 flow->nw_src = nh->ip_src;
158                 flow->nw_dst = nh->ip_dst;
159                 flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
160                 flow->nw_proto = nh->ip_proto;
161                 packet->l4 = b.data;
162                 if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
163                     if (flow->nw_proto == IP_TYPE_TCP) {
164                         const struct tcp_header *tcp = pull_tcp(&b);
165                         if (tcp) {
166                             flow->tp_src = tcp->tcp_src;
167                             flow->tp_dst = tcp->tcp_dst;
168                             packet->l7 = b.data;
169                         } else {
170                             /* Avoid tricking other code into thinking that
171                              * this packet has an L4 header. */
172                             flow->nw_proto = 0;
173                         }
174                     } else if (flow->nw_proto == IP_TYPE_UDP) {
175                         const struct udp_header *udp = pull_udp(&b);
176                         if (udp) {
177                             flow->tp_src = udp->udp_src;
178                             flow->tp_dst = udp->udp_dst;
179                             packet->l7 = b.data;
180                         } else {
181                             /* Avoid tricking other code into thinking that
182                              * this packet has an L4 header. */
183                             flow->nw_proto = 0;
184                         }
185                     } else if (flow->nw_proto == IP_TYPE_ICMP) {
186                         const struct icmp_header *icmp = pull_icmp(&b);
187                         if (icmp) {
188                             flow->icmp_type = htons(icmp->icmp_type);
189                             flow->icmp_code = htons(icmp->icmp_code);
190                             packet->l7 = b.data;
191                         } else {
192                             /* Avoid tricking other code into thinking that
193                              * this packet has an L4 header. */
194                             flow->nw_proto = 0;
195                         }
196                     }
197                 } else {
198                     retval = 1;
199                 }
200             }
201         } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
202             const struct arp_eth_header *arp = pull_arp(&b);
203             if (arp && arp->ar_hrd == htons(1)
204                     && arp->ar_pro == htons(ETH_TYPE_IP) 
205                     && arp->ar_hln == ETH_ADDR_LEN
206                     && arp->ar_pln == 4) {
207                 /* We only match on the lower 8 bits of the opcode. */
208                 if (ntohs(arp->ar_op) <= 0xff) {
209                     flow->nw_proto = ntohs(arp->ar_op);
210                 }
211
212                 if ((flow->nw_proto == ARP_OP_REQUEST) 
213                         || (flow->nw_proto == ARP_OP_REPLY)) {
214                     flow->nw_src = arp->ar_spa;
215                     flow->nw_dst = arp->ar_tpa;
216                 }
217             }
218         }
219     }
220     return retval;
221 }
222
223 /* Extracts the flow stats for a packet.  The 'flow' and 'packet'
224  * arguments must have been initialized through a call to flow_extract().
225  */
226 void
227 flow_extract_stats(const flow_t *flow, struct ofpbuf *packet, 
228         struct odp_flow_stats *stats)
229 {
230     memset(stats, '\0', sizeof(*stats));
231
232     if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
233         struct ip_header *ip = packet->l3;
234         stats->ip_tos = ip->ip_tos;
235         if ((flow->nw_proto == IP_TYPE_TCP) && packet->l7) {
236             struct tcp_header *tcp = packet->l4;
237             stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
238         }
239     }
240
241     stats->n_bytes = packet->size;
242     stats->n_packets = 1;
243 }
244
245 /* Extract 'flow' with 'wildcards' into the OpenFlow match structure
246  * 'match'. */
247 void
248 flow_to_match(const flow_t *flow, uint32_t wildcards, bool tun_id_from_cookie,
249               struct ofp_match *match)
250 {
251     if (!tun_id_from_cookie) {
252         wildcards &= OFPFW_ALL;
253     }
254     match->wildcards = htonl(wildcards);
255
256     match->in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
257                            : flow->in_port);
258     match->dl_vlan = flow->dl_vlan;
259     match->dl_vlan_pcp = flow->dl_vlan_pcp;
260     memcpy(match->dl_src, flow->dl_src, ETH_ADDR_LEN);
261     memcpy(match->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
262     match->dl_type = flow->dl_type;
263     match->nw_src = flow->nw_src;
264     match->nw_dst = flow->nw_dst;
265     match->nw_tos = flow->nw_tos;
266     match->nw_proto = flow->nw_proto;
267     match->tp_src = flow->tp_src;
268     match->tp_dst = flow->tp_dst;
269     memset(match->pad1, '\0', sizeof match->pad1);
270     memset(match->pad2, '\0', sizeof match->pad2);
271 }
272
273 void
274 flow_from_match(const struct ofp_match *match, bool tun_id_from_cookie,
275                 uint64_t cookie, flow_t *flow, uint32_t *wildcards)
276 {
277     if (wildcards) {
278         *wildcards = ntohl(match->wildcards);
279
280         if (!tun_id_from_cookie) {
281             *wildcards |= NXFW_TUN_ID;
282         }
283     }
284     flow->nw_src = match->nw_src;
285     flow->nw_dst = match->nw_dst;
286     if (tun_id_from_cookie) {
287         flow->tun_id = htonl(ntohll(cookie) >> 32);
288     }
289     flow->in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
290                      : ntohs(match->in_port));
291     flow->dl_vlan = match->dl_vlan;
292     flow->dl_vlan_pcp = match->dl_vlan_pcp;
293     flow->dl_type = match->dl_type;
294     flow->tp_src = match->tp_src;
295     flow->tp_dst = match->tp_dst;
296     memcpy(flow->dl_src, match->dl_src, ETH_ADDR_LEN);
297     memcpy(flow->dl_dst, match->dl_dst, ETH_ADDR_LEN);
298     flow->nw_tos = match->nw_tos;
299     flow->nw_proto = match->nw_proto;
300     memset(flow->reserved, 0, sizeof flow->reserved);
301 }
302
303 char *
304 flow_to_string(const flow_t *flow)
305 {
306     struct ds ds = DS_EMPTY_INITIALIZER;
307     flow_format(&ds, flow);
308     return ds_cstr(&ds);
309 }
310
311 void
312 flow_format(struct ds *ds, const flow_t *flow)
313 {
314     ds_put_format(ds, "tunnel%08"PRIx32":in_port%04"PRIx16
315                       ":vlan%"PRIu16":pcp%"PRIu8
316                       " mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
317                       " type%04"PRIx16
318                       " proto%"PRIu8
319                       " tos%"PRIu8
320                       " ip"IP_FMT"->"IP_FMT
321                       " port%"PRIu16"->%"PRIu16,
322                   ntohl(flow->tun_id),
323                   flow->in_port,
324                   ntohs(flow->dl_vlan),
325                   flow->dl_vlan_pcp,
326                   ETH_ADDR_ARGS(flow->dl_src),
327                   ETH_ADDR_ARGS(flow->dl_dst),
328                   ntohs(flow->dl_type),
329                   flow->nw_proto,
330                   flow->nw_tos,
331                   IP_ARGS(&flow->nw_src),
332                   IP_ARGS(&flow->nw_dst),
333                   ntohs(flow->tp_src),
334                   ntohs(flow->tp_dst));
335 }
336
337 void
338 flow_print(FILE *stream, const flow_t *flow) 
339 {
340     char *s = flow_to_string(flow);
341     fputs(s, stream);
342     free(s);
343 }