2 * Distributed under the terms of the GNU GPL version 2.
3 * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_vlan.h>
15 #include <net/llc_pdu.h>
16 #include <linux/kernel.h>
17 #include <linux/jhash.h>
18 #include <linux/jiffies.h>
19 #include <linux/llc.h>
20 #include <linux/module.h>
22 #include <linux/rcupdate.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/icmp.h>
29 #include <net/inet_ecn.h>
34 struct kmem_cache *flow_cache;
35 static unsigned int hash_seed;
39 __be16 ar_hrd; /* format of hardware address */
40 __be16 ar_pro; /* format of protocol address */
41 unsigned char ar_hln; /* length of hardware address */
42 unsigned char ar_pln; /* length of protocol address */
43 __be16 ar_op; /* ARP opcode (command) */
45 /* Ethernet+IPv4 specific members. */
46 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
47 unsigned char ar_sip[4]; /* sender IP address */
48 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
49 unsigned char ar_tip[4]; /* target IP address */
50 } __attribute__((packed));
52 static inline int arphdr_ok(struct sk_buff *skb)
54 int nh_ofs = skb_network_offset(skb);
55 return pskb_may_pull(skb, nh_ofs + sizeof(struct arp_eth_header));
58 static inline int iphdr_ok(struct sk_buff *skb)
60 int nh_ofs = skb_network_offset(skb);
61 if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
62 int ip_len = ip_hdrlen(skb);
63 return (ip_len >= sizeof(struct iphdr)
64 && pskb_may_pull(skb, nh_ofs + ip_len));
69 static inline int tcphdr_ok(struct sk_buff *skb)
71 int th_ofs = skb_transport_offset(skb);
72 if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
73 int tcp_len = tcp_hdrlen(skb);
74 return (tcp_len >= sizeof(struct tcphdr)
75 && skb->len >= th_ofs + tcp_len);
80 static inline int udphdr_ok(struct sk_buff *skb)
82 int th_ofs = skb_transport_offset(skb);
83 return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
86 static inline int icmphdr_ok(struct sk_buff *skb)
88 int th_ofs = skb_transport_offset(skb);
89 return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
92 #define TCP_FLAGS_OFFSET 13
93 #define TCP_FLAG_MASK 0x3f
95 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
99 if (flow->key.dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
100 struct iphdr *nh = ip_hdr(skb);
101 flow->ip_tos = nh->tos;
102 if (flow->key.nw_proto == IPPROTO_TCP && tcphdr_ok(skb)) {
103 u8 *tcp = (u8 *)tcp_hdr(skb);
104 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
108 spin_lock_bh(&flow->lock);
109 flow->used = jiffies;
110 flow->packet_count++;
111 flow->byte_count += skb->len;
112 flow->tcp_flags |= tcp_flags;
113 spin_unlock_bh(&flow->lock);
116 struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
118 struct sw_flow_actions *sfa;
120 if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
121 return ERR_PTR(-EINVAL);
123 sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
126 return ERR_PTR(-ENOMEM);
128 sfa->n_actions = n_actions;
133 /* Frees 'flow' immediately. */
134 static void flow_free(struct sw_flow *flow)
138 kfree(flow->sf_acts);
139 kmem_cache_free(flow_cache, flow);
142 void flow_free_tbl(struct tbl_node *node)
144 struct sw_flow *flow = flow_cast(node);
148 /* RCU callback used by flow_deferred_free. */
149 static void rcu_free_flow_callback(struct rcu_head *rcu)
151 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
155 /* Schedules 'flow' to be freed after the next RCU grace period.
156 * The caller must hold rcu_read_lock for this to be sensible. */
157 void flow_deferred_free(struct sw_flow *flow)
159 call_rcu(&flow->rcu, rcu_free_flow_callback);
162 /* RCU callback used by flow_deferred_free_acts. */
163 static void rcu_free_acts_callback(struct rcu_head *rcu)
165 struct sw_flow_actions *sf_acts = container_of(rcu,
166 struct sw_flow_actions, rcu);
170 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
171 * The caller must hold rcu_read_lock for this to be sensible. */
172 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
174 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
177 #define SNAP_OUI_LEN 3
182 u8 dsap; /* Always 0xAA */
183 u8 ssap; /* Always 0xAA */
185 u8 oui[SNAP_OUI_LEN];
187 } __attribute__ ((packed));
189 static int is_snap(const struct eth_snap_hdr *esh)
191 return (esh->dsap == LLC_SAP_SNAP
192 && esh->ssap == LLC_SAP_SNAP
193 && !memcmp(esh->oui, "\0\0\0", 3));
196 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
197 * and initializes 'key' to match. Returns 1 if 'skb' contains an IP
198 * fragment, 0 otherwise. */
199 int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
202 struct eth_snap_hdr *esh;
206 memset(key, 0, sizeof *key);
207 key->tun_id = OVS_CB(skb)->tun_id;
208 key->in_port = in_port;
209 key->dl_vlan = htons(ODP_VLAN_NONE);
211 if (skb->len < sizeof *eth)
213 if (!pskb_may_pull(skb, skb->len >= 64 ? 64 : skb->len)) {
217 skb_reset_mac_header(skb);
219 esh = (struct eth_snap_hdr *) eth;
220 nh_ofs = sizeof *eth;
221 if (likely(ntohs(eth->h_proto) >= ODP_DL_TYPE_ETH2_CUTOFF))
222 key->dl_type = eth->h_proto;
223 else if (skb->len >= sizeof *esh && is_snap(esh)) {
224 key->dl_type = esh->ethertype;
225 nh_ofs = sizeof *esh;
227 key->dl_type = htons(ODP_DL_TYPE_NOT_ETH_TYPE);
228 if (skb->len >= nh_ofs + sizeof(struct llc_pdu_un)) {
229 nh_ofs += sizeof(struct llc_pdu_un);
233 /* Check for a VLAN tag */
234 if (key->dl_type == htons(ETH_P_8021Q) &&
235 skb->len >= nh_ofs + sizeof(struct vlan_hdr)) {
236 struct vlan_hdr *vh = (struct vlan_hdr*)(skb->data + nh_ofs);
237 key->dl_type = vh->h_vlan_encapsulated_proto;
238 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
239 key->dl_vlan_pcp = (ntohs(vh->h_vlan_TCI) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
240 nh_ofs += sizeof(struct vlan_hdr);
242 memcpy(key->dl_src, eth->h_source, ETH_ALEN);
243 memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
244 skb_set_network_header(skb, nh_ofs);
247 if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
248 struct iphdr *nh = ip_hdr(skb);
249 int th_ofs = nh_ofs + nh->ihl * 4;
250 key->nw_src = nh->saddr;
251 key->nw_dst = nh->daddr;
252 key->nw_tos = nh->tos & ~INET_ECN_MASK;
253 key->nw_proto = nh->protocol;
254 skb_set_transport_header(skb, th_ofs);
256 /* Transport layer. */
257 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
258 if (key->nw_proto == IPPROTO_TCP) {
259 if (tcphdr_ok(skb)) {
260 struct tcphdr *tcp = tcp_hdr(skb);
261 key->tp_src = tcp->source;
262 key->tp_dst = tcp->dest;
264 /* Avoid tricking other code into
265 * thinking that this packet has an L4
269 } else if (key->nw_proto == IPPROTO_UDP) {
270 if (udphdr_ok(skb)) {
271 struct udphdr *udp = udp_hdr(skb);
272 key->tp_src = udp->source;
273 key->tp_dst = udp->dest;
275 /* Avoid tricking other code into
276 * thinking that this packet has an L4
280 } else if (key->nw_proto == IPPROTO_ICMP) {
281 if (icmphdr_ok(skb)) {
282 struct icmphdr *icmp = icmp_hdr(skb);
283 /* The ICMP type and code fields use the 16-bit
284 * transport port fields, so we need to store them
285 * in 16-bit network byte order. */
286 key->tp_src = htons(icmp->type);
287 key->tp_dst = htons(icmp->code);
289 /* Avoid tricking other code into
290 * thinking that this packet has an L4
298 } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
299 struct arp_eth_header *arp;
301 arp = (struct arp_eth_header *)skb_network_header(skb);
303 if (arp->ar_hrd == htons(ARPHRD_ETHER)
304 && arp->ar_pro == htons(ETH_P_IP)
305 && arp->ar_hln == ETH_ALEN
306 && arp->ar_pln == 4) {
308 /* We only match on the lower 8 bits of the opcode. */
309 if (ntohs(arp->ar_op) <= 0xff) {
310 key->nw_proto = ntohs(arp->ar_op);
313 if (key->nw_proto == ARPOP_REQUEST
314 || key->nw_proto == ARPOP_REPLY) {
315 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
316 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
320 skb_reset_transport_header(skb);
325 struct sw_flow *flow_cast(const struct tbl_node *node)
327 return container_of(node, struct sw_flow, tbl_node);
330 u32 flow_hash(const struct odp_flow_key *key)
332 return jhash2((u32*)key, sizeof *key / sizeof(u32), hash_seed);
335 int flow_cmp(const struct tbl_node *node, void *key2_)
337 const struct odp_flow_key *key1 = &flow_cast(node)->key;
338 const struct odp_flow_key *key2 = key2_;
340 return !memcmp(key1, key2, sizeof(struct odp_flow_key));
343 /* Initializes the flow module.
344 * Returns zero if successful or a negative error code. */
347 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
349 if (flow_cache == NULL)
352 get_random_bytes(&hash_seed, sizeof hash_seed);
357 /* Uninitializes the flow module. */
360 kmem_cache_destroy(flow_cache);