]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/flow.c
Update primary code license to Apache 2.0.
[mirror_ovs.git] / datapath / flow.c
CommitLineData
064af421
BP
1/*
2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009 Nicira Networks.
a14bc59f
BP
4 *
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
064af421
BP
7 */
8
9#include "flow.h"
10#include <linux/netdevice.h>
11#include <linux/etherdevice.h>
12#include <linux/if_ether.h>
13#include <linux/if_vlan.h>
14#include <net/llc_pdu.h>
15#include <linux/kernel.h>
16#include <linux/jiffies.h>
17#include <linux/llc.h>
18#include <linux/module.h>
19#include <linux/in.h>
20#include <linux/rcupdate.h>
21#include <linux/if_ether.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/udp.h>
25#include <linux/icmp.h>
26#include <net/ip.h>
27
28#include "compat.h"
29
30struct kmem_cache *flow_cache;
31
32static inline int iphdr_ok(struct sk_buff *skb)
33{
34 int nh_ofs = skb_network_offset(skb);
35 if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
36 int ip_len = ip_hdrlen(skb);
37 return (ip_len >= sizeof(struct iphdr)
38 && pskb_may_pull(skb, nh_ofs + ip_len));
39 }
40 return 0;
41}
42
43static inline int tcphdr_ok(struct sk_buff *skb)
44{
45 int th_ofs = skb_transport_offset(skb);
46 if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
47 int tcp_len = tcp_hdrlen(skb);
48 return (tcp_len >= sizeof(struct tcphdr)
49 && skb->len >= th_ofs + tcp_len);
50 }
51 return 0;
52}
53
54static inline int udphdr_ok(struct sk_buff *skb)
55{
56 int th_ofs = skb_transport_offset(skb);
57 return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
58}
59
60static inline int icmphdr_ok(struct sk_buff *skb)
61{
62 int th_ofs = skb_transport_offset(skb);
63 return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
64}
65
66#define TCP_FLAGS_OFFSET 13
67#define TCP_FLAG_MASK 0x3f
68
69static inline struct ovs_tcphdr *ovs_tcp_hdr(const struct sk_buff *skb)
70{
71 return (struct ovs_tcphdr *)skb_transport_header(skb);
72}
73
74void flow_used(struct sw_flow *flow, struct sk_buff *skb)
75{
76 unsigned long flags;
77 u8 tcp_flags = 0;
78
79 if (flow->key.dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
80 struct iphdr *nh = ip_hdr(skb);
81 flow->ip_tos = nh->tos;
82 if (flow->key.nw_proto == IPPROTO_TCP && tcphdr_ok(skb)) {
83 u8 *tcp = (u8 *)tcp_hdr(skb);
84 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
85 }
86 }
87
88 spin_lock_irqsave(&flow->lock, flags);
89 getnstimeofday(&flow->used);
90 flow->packet_count++;
91 flow->byte_count += skb->len;
92 flow->tcp_flags |= tcp_flags;
93 spin_unlock_irqrestore(&flow->lock, flags);
94}
95
96struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
97{
98 struct sw_flow_actions *sfa;
99
100 if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
101 return ERR_PTR(-EINVAL);
102
103 sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
104 GFP_KERNEL);
105 if (!sfa)
106 return ERR_PTR(-ENOMEM);
107
108 sfa->n_actions = n_actions;
109 return sfa;
110}
111
112
113/* Frees 'flow' immediately. */
114void flow_free(struct sw_flow *flow)
115{
116 if (unlikely(!flow))
117 return;
118 kfree(flow->sf_acts);
119 kmem_cache_free(flow_cache, flow);
120}
121
122/* RCU callback used by flow_deferred_free. */
123static void rcu_free_flow_callback(struct rcu_head *rcu)
124{
125 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
126 flow_free(flow);
127}
128
129/* Schedules 'flow' to be freed after the next RCU grace period.
130 * The caller must hold rcu_read_lock for this to be sensible. */
131void flow_deferred_free(struct sw_flow *flow)
132{
133 call_rcu(&flow->rcu, rcu_free_flow_callback);
134}
135
136/* RCU callback used by flow_deferred_free_acts. */
137static void rcu_free_acts_callback(struct rcu_head *rcu)
138{
139 struct sw_flow_actions *sf_acts = container_of(rcu,
140 struct sw_flow_actions, rcu);
141 kfree(sf_acts);
142}
143
144/* Schedules 'sf_acts' to be freed after the next RCU grace period.
145 * The caller must hold rcu_read_lock for this to be sensible. */
146void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
147{
148 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
149}
150
151#define SNAP_OUI_LEN 3
152
153struct eth_snap_hdr
154{
155 struct ethhdr eth;
156 u8 dsap; /* Always 0xAA */
157 u8 ssap; /* Always 0xAA */
158 u8 ctrl;
159 u8 oui[SNAP_OUI_LEN];
160 u16 ethertype;
161} __attribute__ ((packed));
162
163static int is_snap(const struct eth_snap_hdr *esh)
164{
165 return (esh->dsap == LLC_SAP_SNAP
166 && esh->ssap == LLC_SAP_SNAP
167 && !memcmp(esh->oui, "\0\0\0", 3));
168}
169
170/* Parses the Ethernet frame in 'skb', which was received on 'in_port',
171 * and initializes 'key' to match. Returns 1 if 'skb' contains an IP
172 * fragment, 0 otherwise. */
173int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
174{
175 struct ethhdr *eth;
176 struct eth_snap_hdr *esh;
177 int retval = 0;
178 int nh_ofs;
179
180 memset(key, 0, sizeof *key);
181 key->dl_vlan = htons(ODP_VLAN_NONE);
182 key->in_port = in_port;
183
184 if (skb->len < sizeof *eth)
185 return 0;
186 if (!pskb_may_pull(skb, skb->len >= 64 ? 64 : skb->len)) {
187 return 0;
188 }
189
190 skb_reset_mac_header(skb);
191 eth = eth_hdr(skb);
192 esh = (struct eth_snap_hdr *) eth;
193 nh_ofs = sizeof *eth;
194 if (likely(ntohs(eth->h_proto) >= ODP_DL_TYPE_ETH2_CUTOFF))
195 key->dl_type = eth->h_proto;
196 else if (skb->len >= sizeof *esh && is_snap(esh)) {
197 key->dl_type = esh->ethertype;
198 nh_ofs = sizeof *esh;
199 } else {
200 key->dl_type = htons(ODP_DL_TYPE_NOT_ETH_TYPE);
201 if (skb->len >= nh_ofs + sizeof(struct llc_pdu_un)) {
202 nh_ofs += sizeof(struct llc_pdu_un);
203 }
204 }
205
206 /* Check for a VLAN tag */
207 if (key->dl_type == htons(ETH_P_8021Q) &&
208 skb->len >= nh_ofs + sizeof(struct vlan_hdr)) {
209 struct vlan_hdr *vh = (struct vlan_hdr*)(skb->data + nh_ofs);
210 key->dl_type = vh->h_vlan_encapsulated_proto;
211 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
212 nh_ofs += sizeof(struct vlan_hdr);
213 }
214 memcpy(key->dl_src, eth->h_source, ETH_ALEN);
215 memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
216 skb_set_network_header(skb, nh_ofs);
217
218 /* Network layer. */
219 if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
220 struct iphdr *nh = ip_hdr(skb);
221 int th_ofs = nh_ofs + nh->ihl * 4;
222 key->nw_src = nh->saddr;
223 key->nw_dst = nh->daddr;
224 key->nw_proto = nh->protocol;
225 skb_set_transport_header(skb, th_ofs);
226
227 /* Transport layer. */
228 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
229 if (key->nw_proto == IPPROTO_TCP) {
230 if (tcphdr_ok(skb)) {
231 struct tcphdr *tcp = tcp_hdr(skb);
232 key->tp_src = tcp->source;
233 key->tp_dst = tcp->dest;
234 } else {
235 /* Avoid tricking other code into
236 * thinking that this packet has an L4
237 * header. */
238 key->nw_proto = 0;
239 }
240 } else if (key->nw_proto == IPPROTO_UDP) {
241 if (udphdr_ok(skb)) {
242 struct udphdr *udp = udp_hdr(skb);
243 key->tp_src = udp->source;
244 key->tp_dst = udp->dest;
245 } else {
246 /* Avoid tricking other code into
247 * thinking that this packet has an L4
248 * header. */
249 key->nw_proto = 0;
250 }
251 } else if (key->nw_proto == IPPROTO_ICMP) {
252 if (icmphdr_ok(skb)) {
253 struct icmphdr *icmp = icmp_hdr(skb);
254 /* The ICMP type and code fields use the 16-bit
255 * transport port fields, so we need to store them
256 * in 16-bit network byte order. */
257 key->tp_src = htons(icmp->type);
258 key->tp_dst = htons(icmp->code);
259 } else {
260 /* Avoid tricking other code into
261 * thinking that this packet has an L4
262 * header. */
263 key->nw_proto = 0;
264 }
265 }
266 } else {
267 retval = 1;
268 }
269 } else {
270 skb_reset_transport_header(skb);
271 }
272 return retval;
273}
274
275/* Initializes the flow module.
276 * Returns zero if successful or a negative error code. */
277int flow_init(void)
278{
279 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
280 0, NULL);
281 if (flow_cache == NULL)
282 return -ENOMEM;
283
284 return 0;
285}
286
287/* Uninitializes the flow module. */
288void flow_exit(void)
289{
290 kmem_cache_destroy(flow_cache);
291}
292
293void print_flow(const struct odp_flow_key *key)
294{
295#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
296#define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
297 printk("port%04x:vlan%d mac"MAC_FMT"->"MAC_FMT" "
298 "type%04x proto%d ip%x->%x port%d->%d\n",
299 key->in_port, ntohs(key->dl_vlan),
300 MAC_ARG(key->dl_src), MAC_ARG(key->dl_dst),
301 ntohs(key->dl_type), key->nw_proto,
302 key->nw_src, key->nw_dst,
303 ntohs(key->tp_src), ntohs(key->tp_dst));
304}