2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009 Nicira Networks.
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/if_ether.h>
10 #include <linux/if_vlan.h>
11 #include <net/llc_pdu.h>
12 #include <linux/kernel.h>
13 #include <linux/jiffies.h>
14 #include <linux/llc.h>
15 #include <linux/module.h>
17 #include <linux/rcupdate.h>
18 #include <linux/if_ether.h>
20 #include <linux/tcp.h>
21 #include <linux/udp.h>
22 #include <linux/icmp.h>
27 struct kmem_cache *flow_cache;
29 static inline int iphdr_ok(struct sk_buff *skb)
31 int nh_ofs = skb_network_offset(skb);
32 if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
33 int ip_len = ip_hdrlen(skb);
34 return (ip_len >= sizeof(struct iphdr)
35 && pskb_may_pull(skb, nh_ofs + ip_len));
40 static inline int tcphdr_ok(struct sk_buff *skb)
42 int th_ofs = skb_transport_offset(skb);
43 if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
44 int tcp_len = tcp_hdrlen(skb);
45 return (tcp_len >= sizeof(struct tcphdr)
46 && skb->len >= th_ofs + tcp_len);
51 static inline int udphdr_ok(struct sk_buff *skb)
53 int th_ofs = skb_transport_offset(skb);
54 return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
57 static inline int icmphdr_ok(struct sk_buff *skb)
59 int th_ofs = skb_transport_offset(skb);
60 return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
63 #define TCP_FLAGS_OFFSET 13
64 #define TCP_FLAG_MASK 0x3f
66 static inline struct ovs_tcphdr *ovs_tcp_hdr(const struct sk_buff *skb)
68 return (struct ovs_tcphdr *)skb_transport_header(skb);
71 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
76 if (flow->key.dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
77 struct iphdr *nh = ip_hdr(skb);
78 flow->ip_tos = nh->tos;
79 if (flow->key.nw_proto == IPPROTO_TCP && tcphdr_ok(skb)) {
80 u8 *tcp = (u8 *)tcp_hdr(skb);
81 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
85 spin_lock_irqsave(&flow->lock, flags);
86 getnstimeofday(&flow->used);
88 flow->byte_count += skb->len;
89 flow->tcp_flags |= tcp_flags;
90 spin_unlock_irqrestore(&flow->lock, flags);
93 struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
95 struct sw_flow_actions *sfa;
97 if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
98 return ERR_PTR(-EINVAL);
100 sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
103 return ERR_PTR(-ENOMEM);
105 sfa->n_actions = n_actions;
110 /* Frees 'flow' immediately. */
111 void flow_free(struct sw_flow *flow)
115 kfree(flow->sf_acts);
116 kmem_cache_free(flow_cache, flow);
119 /* RCU callback used by flow_deferred_free. */
120 static void rcu_free_flow_callback(struct rcu_head *rcu)
122 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
126 /* Schedules 'flow' to be freed after the next RCU grace period.
127 * The caller must hold rcu_read_lock for this to be sensible. */
128 void flow_deferred_free(struct sw_flow *flow)
130 call_rcu(&flow->rcu, rcu_free_flow_callback);
133 /* RCU callback used by flow_deferred_free_acts. */
134 static void rcu_free_acts_callback(struct rcu_head *rcu)
136 struct sw_flow_actions *sf_acts = container_of(rcu,
137 struct sw_flow_actions, rcu);
141 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
142 * The caller must hold rcu_read_lock for this to be sensible. */
143 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
145 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
148 #define SNAP_OUI_LEN 3
153 u8 dsap; /* Always 0xAA */
154 u8 ssap; /* Always 0xAA */
156 u8 oui[SNAP_OUI_LEN];
158 } __attribute__ ((packed));
160 static int is_snap(const struct eth_snap_hdr *esh)
162 return (esh->dsap == LLC_SAP_SNAP
163 && esh->ssap == LLC_SAP_SNAP
164 && !memcmp(esh->oui, "\0\0\0", 3));
167 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
168 * and initializes 'key' to match. Returns 1 if 'skb' contains an IP
169 * fragment, 0 otherwise. */
170 int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
173 struct eth_snap_hdr *esh;
177 memset(key, 0, sizeof *key);
178 key->dl_vlan = htons(ODP_VLAN_NONE);
179 key->in_port = in_port;
181 if (skb->len < sizeof *eth)
183 if (!pskb_may_pull(skb, skb->len >= 64 ? 64 : skb->len)) {
187 skb_reset_mac_header(skb);
189 esh = (struct eth_snap_hdr *) eth;
190 nh_ofs = sizeof *eth;
191 if (likely(ntohs(eth->h_proto) >= ODP_DL_TYPE_ETH2_CUTOFF))
192 key->dl_type = eth->h_proto;
193 else if (skb->len >= sizeof *esh && is_snap(esh)) {
194 key->dl_type = esh->ethertype;
195 nh_ofs = sizeof *esh;
197 key->dl_type = htons(ODP_DL_TYPE_NOT_ETH_TYPE);
198 if (skb->len >= nh_ofs + sizeof(struct llc_pdu_un)) {
199 nh_ofs += sizeof(struct llc_pdu_un);
203 /* Check for a VLAN tag */
204 if (key->dl_type == htons(ETH_P_8021Q) &&
205 skb->len >= nh_ofs + sizeof(struct vlan_hdr)) {
206 struct vlan_hdr *vh = (struct vlan_hdr*)(skb->data + nh_ofs);
207 key->dl_type = vh->h_vlan_encapsulated_proto;
208 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
209 nh_ofs += sizeof(struct vlan_hdr);
211 memcpy(key->dl_src, eth->h_source, ETH_ALEN);
212 memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
213 skb_set_network_header(skb, nh_ofs);
216 if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
217 struct iphdr *nh = ip_hdr(skb);
218 int th_ofs = nh_ofs + nh->ihl * 4;
219 key->nw_src = nh->saddr;
220 key->nw_dst = nh->daddr;
221 key->nw_proto = nh->protocol;
222 skb_set_transport_header(skb, th_ofs);
224 /* Transport layer. */
225 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
226 if (key->nw_proto == IPPROTO_TCP) {
227 if (tcphdr_ok(skb)) {
228 struct tcphdr *tcp = tcp_hdr(skb);
229 key->tp_src = tcp->source;
230 key->tp_dst = tcp->dest;
232 /* Avoid tricking other code into
233 * thinking that this packet has an L4
237 } else if (key->nw_proto == IPPROTO_UDP) {
238 if (udphdr_ok(skb)) {
239 struct udphdr *udp = udp_hdr(skb);
240 key->tp_src = udp->source;
241 key->tp_dst = udp->dest;
243 /* Avoid tricking other code into
244 * thinking that this packet has an L4
248 } else if (key->nw_proto == IPPROTO_ICMP) {
249 if (icmphdr_ok(skb)) {
250 struct icmphdr *icmp = icmp_hdr(skb);
251 /* The ICMP type and code fields use the 16-bit
252 * transport port fields, so we need to store them
253 * in 16-bit network byte order. */
254 key->tp_src = htons(icmp->type);
255 key->tp_dst = htons(icmp->code);
257 /* Avoid tricking other code into
258 * thinking that this packet has an L4
267 skb_reset_transport_header(skb);
272 /* Initializes the flow module.
273 * Returns zero if successful or a negative error code. */
276 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
278 if (flow_cache == NULL)
284 /* Uninitializes the flow module. */
287 kmem_cache_destroy(flow_cache);
290 void print_flow(const struct odp_flow_key *key)
292 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
293 #define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
294 printk("port%04x:vlan%d mac"MAC_FMT"->"MAC_FMT" "
295 "type%04x proto%d ip%x->%x port%d->%d\n",
296 key->in_port, ntohs(key->dl_vlan),
297 MAC_ARG(key->dl_src), MAC_ARG(key->dl_dst),
298 ntohs(key->dl_type), key->nw_proto,
299 key->nw_src, key->nw_dst,
300 ntohs(key->tp_src), ntohs(key->tp_dst));