datapath: Per cpu flow stats.
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include "flow.h"
20 #include "datapath.h"
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/smp.h>
39 #include <linux/tcp.h>
40 #include <linux/udp.h>
41 #include <linux/icmp.h>
42 #include <linux/icmpv6.h>
43 #include <linux/rculist.h>
44 #include <net/ip.h>
45 #include <net/ipv6.h>
46 #include <net/ndisc.h>
47
48 #include "vlan.h"
49
50 u64 ovs_flow_used_time(unsigned long flow_jiffies)
51 {
52         struct timespec cur_ts;
53         u64 cur_ms, idle_ms;
54
55         ktime_get_ts(&cur_ts);
56         idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
57         cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
58                  cur_ts.tv_nsec / NSEC_PER_MSEC;
59
60         return cur_ms - idle_ms;
61 }
62
63 #define TCP_FLAGS_OFFSET 13
64 #define TCP_FLAG_MASK 0x3f
65
66 void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb)
67 {
68         struct sw_flow_stats *stats = &flow->stats[smp_processor_id()];
69         u8 tcp_flags = 0;
70
71         if ((flow->key.eth.type == htons(ETH_P_IP) ||
72              flow->key.eth.type == htons(ETH_P_IPV6)) &&
73             flow->key.ip.proto == IPPROTO_TCP &&
74             likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
75                 u8 *tcp = (u8 *)tcp_hdr(skb);
76                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
77         }
78
79         spin_lock(&stats->lock);
80         stats->used = jiffies;
81         stats->packet_count++;
82         stats->byte_count += skb->len;
83         stats->tcp_flags |= tcp_flags;
84         spin_unlock(&stats->lock);
85 }
86
87 void ovs_flow_stats_get(struct sw_flow *flow, struct sw_flow_stats *res)
88 {
89         int cpu, cur_cpu;
90
91         memset(res, 0, sizeof(*res));
92
93         cur_cpu = get_cpu();
94         for_each_possible_cpu(cpu) {
95                 struct sw_flow_stats *stats = &flow->stats[cpu];
96
97                 if (cpu == cur_cpu)
98                         local_bh_disable();
99
100                 spin_lock(&stats->lock);
101                 if (time_after(stats->used, res->used))
102                         res->used = stats->used;
103                 res->packet_count += stats->packet_count;
104                 res->byte_count += stats->byte_count;
105                 res->tcp_flags |= stats->tcp_flags;
106                 spin_unlock(&stats->lock);
107
108                 if (cpu == cur_cpu)
109                         local_bh_enable();
110
111         }
112         put_cpu();
113 }
114
115 void ovs_flow_stats_clear(struct sw_flow *flow)
116 {
117         int cpu, cur_cpu;
118
119         cur_cpu = get_cpu();
120         for_each_possible_cpu(cpu) {
121                 struct sw_flow_stats *stats = &flow->stats[cpu];
122
123                 if (cpu == cur_cpu)
124                         local_bh_disable();
125
126                 spin_lock(&stats->lock);
127                 stats->used = 0;
128                 stats->packet_count = 0;
129                 stats->byte_count = 0;
130                 stats->tcp_flags = 0;
131                 spin_unlock(&stats->lock);
132
133                 if (cpu == cur_cpu)
134                         local_bh_enable();
135         }
136         put_cpu();
137 }
138
139 static int check_header(struct sk_buff *skb, int len)
140 {
141         if (unlikely(skb->len < len))
142                 return -EINVAL;
143         if (unlikely(!pskb_may_pull(skb, len)))
144                 return -ENOMEM;
145         return 0;
146 }
147
148 static bool arphdr_ok(struct sk_buff *skb)
149 {
150         return pskb_may_pull(skb, skb_network_offset(skb) +
151                                   sizeof(struct arp_eth_header));
152 }
153
154 static int check_iphdr(struct sk_buff *skb)
155 {
156         unsigned int nh_ofs = skb_network_offset(skb);
157         unsigned int ip_len;
158         int err;
159
160         err = check_header(skb, nh_ofs + sizeof(struct iphdr));
161         if (unlikely(err))
162                 return err;
163
164         ip_len = ip_hdrlen(skb);
165         if (unlikely(ip_len < sizeof(struct iphdr) ||
166                      skb->len < nh_ofs + ip_len))
167                 return -EINVAL;
168
169         skb_set_transport_header(skb, nh_ofs + ip_len);
170         return 0;
171 }
172
173 static bool tcphdr_ok(struct sk_buff *skb)
174 {
175         int th_ofs = skb_transport_offset(skb);
176         int tcp_len;
177
178         if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
179                 return false;
180
181         tcp_len = tcp_hdrlen(skb);
182         if (unlikely(tcp_len < sizeof(struct tcphdr) ||
183                      skb->len < th_ofs + tcp_len))
184                 return false;
185
186         return true;
187 }
188
189 static bool udphdr_ok(struct sk_buff *skb)
190 {
191         return pskb_may_pull(skb, skb_transport_offset(skb) +
192                                   sizeof(struct udphdr));
193 }
194
195 static bool sctphdr_ok(struct sk_buff *skb)
196 {
197         return pskb_may_pull(skb, skb_transport_offset(skb) +
198                                   sizeof(struct sctphdr));
199 }
200
201 static bool icmphdr_ok(struct sk_buff *skb)
202 {
203         return pskb_may_pull(skb, skb_transport_offset(skb) +
204                                   sizeof(struct icmphdr));
205 }
206
207 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
208 {
209         unsigned int nh_ofs = skb_network_offset(skb);
210         unsigned int nh_len;
211         int payload_ofs;
212         struct ipv6hdr *nh;
213         uint8_t nexthdr;
214         __be16 frag_off;
215         int err;
216
217         err = check_header(skb, nh_ofs + sizeof(*nh));
218         if (unlikely(err))
219                 return err;
220
221         nh = ipv6_hdr(skb);
222         nexthdr = nh->nexthdr;
223         payload_ofs = (u8 *)(nh + 1) - skb->data;
224
225         key->ip.proto = NEXTHDR_NONE;
226         key->ip.tos = ipv6_get_dsfield(nh);
227         key->ip.ttl = nh->hop_limit;
228         key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
229         key->ipv6.addr.src = nh->saddr;
230         key->ipv6.addr.dst = nh->daddr;
231
232         payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
233         if (unlikely(payload_ofs < 0))
234                 return -EINVAL;
235
236         if (frag_off) {
237                 if (frag_off & htons(~0x7))
238                         key->ip.frag = OVS_FRAG_TYPE_LATER;
239                 else
240                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
241         }
242
243         nh_len = payload_ofs - nh_ofs;
244         skb_set_transport_header(skb, nh_ofs + nh_len);
245         key->ip.proto = nexthdr;
246         return nh_len;
247 }
248
249 static bool icmp6hdr_ok(struct sk_buff *skb)
250 {
251         return pskb_may_pull(skb, skb_transport_offset(skb) +
252                                   sizeof(struct icmp6hdr));
253 }
254
255 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
256 {
257         struct qtag_prefix {
258                 __be16 eth_type; /* ETH_P_8021Q */
259                 __be16 tci;
260         };
261         struct qtag_prefix *qp;
262
263         if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
264                 return 0;
265
266         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
267                                          sizeof(__be16))))
268                 return -ENOMEM;
269
270         qp = (struct qtag_prefix *) skb->data;
271         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
272         __skb_pull(skb, sizeof(struct qtag_prefix));
273
274         return 0;
275 }
276
277 static __be16 parse_ethertype(struct sk_buff *skb)
278 {
279         struct llc_snap_hdr {
280                 u8  dsap;  /* Always 0xAA */
281                 u8  ssap;  /* Always 0xAA */
282                 u8  ctrl;
283                 u8  oui[3];
284                 __be16 ethertype;
285         };
286         struct llc_snap_hdr *llc;
287         __be16 proto;
288
289         proto = *(__be16 *) skb->data;
290         __skb_pull(skb, sizeof(__be16));
291
292         if (ntohs(proto) >= ETH_P_802_3_MIN)
293                 return proto;
294
295         if (skb->len < sizeof(struct llc_snap_hdr))
296                 return htons(ETH_P_802_2);
297
298         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
299                 return htons(0);
300
301         llc = (struct llc_snap_hdr *) skb->data;
302         if (llc->dsap != LLC_SAP_SNAP ||
303             llc->ssap != LLC_SAP_SNAP ||
304             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
305                 return htons(ETH_P_802_2);
306
307         __skb_pull(skb, sizeof(struct llc_snap_hdr));
308
309         if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
310                 return llc->ethertype;
311
312         return htons(ETH_P_802_2);
313 }
314
315 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
316                         int nh_len)
317 {
318         struct icmp6hdr *icmp = icmp6_hdr(skb);
319
320         /* The ICMPv6 type and code fields use the 16-bit transport port
321          * fields, so we need to store them in 16-bit network byte order.
322          */
323         key->ipv6.tp.src = htons(icmp->icmp6_type);
324         key->ipv6.tp.dst = htons(icmp->icmp6_code);
325
326         if (icmp->icmp6_code == 0 &&
327             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
328              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
329                 int icmp_len = skb->len - skb_transport_offset(skb);
330                 struct nd_msg *nd;
331                 int offset;
332
333                 /* In order to process neighbor discovery options, we need the
334                  * entire packet.
335                  */
336                 if (unlikely(icmp_len < sizeof(*nd)))
337                         return 0;
338
339                 if (unlikely(skb_linearize(skb)))
340                         return -ENOMEM;
341
342                 nd = (struct nd_msg *)skb_transport_header(skb);
343                 key->ipv6.nd.target = nd->target;
344
345                 icmp_len -= sizeof(*nd);
346                 offset = 0;
347                 while (icmp_len >= 8) {
348                         struct nd_opt_hdr *nd_opt =
349                                  (struct nd_opt_hdr *)(nd->opt + offset);
350                         int opt_len = nd_opt->nd_opt_len * 8;
351
352                         if (unlikely(!opt_len || opt_len > icmp_len))
353                                 return 0;
354
355                         /* Store the link layer address if the appropriate
356                          * option is provided.  It is considered an error if
357                          * the same link layer option is specified twice.
358                          */
359                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
360                             && opt_len == 8) {
361                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
362                                         goto invalid;
363                                 memcpy(key->ipv6.nd.sll,
364                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
365                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
366                                    && opt_len == 8) {
367                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
368                                         goto invalid;
369                                 memcpy(key->ipv6.nd.tll,
370                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
371                         }
372
373                         icmp_len -= opt_len;
374                         offset += opt_len;
375                 }
376         }
377
378         return 0;
379
380 invalid:
381         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
382         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
383         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
384
385         return 0;
386 }
387
388 /**
389  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
390  * @skb: sk_buff that contains the frame, with skb->data pointing to the
391  * Ethernet header
392  * @in_port: port number on which @skb was received.
393  * @key: output flow key
394  *
395  * The caller must ensure that skb->len >= ETH_HLEN.
396  *
397  * Returns 0 if successful, otherwise a negative errno value.
398  *
399  * Initializes @skb header pointers as follows:
400  *
401  *    - skb->mac_header: the Ethernet header.
402  *
403  *    - skb->network_header: just past the Ethernet header, or just past the
404  *      VLAN header, to the first byte of the Ethernet payload.
405  *
406  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
407  *      on output, then just past the IP header, if one is present and
408  *      of a correct length, otherwise the same as skb->network_header.
409  *      For other key->eth.type values it is left untouched.
410  */
411 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
412 {
413         int error;
414         struct ethhdr *eth;
415
416         memset(key, 0, sizeof(*key));
417
418         key->phy.priority = skb->priority;
419         if (OVS_CB(skb)->tun_key)
420                 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
421         key->phy.in_port = in_port;
422         key->phy.skb_mark = skb->mark;
423
424         skb_reset_mac_header(skb);
425
426         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
427          * header in the linear data area.
428          */
429         eth = eth_hdr(skb);
430         memcpy(key->eth.src, eth->h_source, ETH_ALEN);
431         memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
432
433         __skb_pull(skb, 2 * ETH_ALEN);
434         /* We are going to push all headers that we pull, so no need to
435          * update skb->csum here. */
436
437         if (vlan_tx_tag_present(skb))
438                 key->eth.tci = htons(vlan_get_tci(skb));
439         else if (eth->h_proto == htons(ETH_P_8021Q))
440                 if (unlikely(parse_vlan(skb, key)))
441                         return -ENOMEM;
442
443         key->eth.type = parse_ethertype(skb);
444         if (unlikely(key->eth.type == htons(0)))
445                 return -ENOMEM;
446
447         skb_reset_network_header(skb);
448         __skb_push(skb, skb->data - skb_mac_header(skb));
449
450         /* Network layer. */
451         if (key->eth.type == htons(ETH_P_IP)) {
452                 struct iphdr *nh;
453                 __be16 offset;
454
455                 error = check_iphdr(skb);
456                 if (unlikely(error)) {
457                         if (error == -EINVAL) {
458                                 skb->transport_header = skb->network_header;
459                                 error = 0;
460                         }
461                         return error;
462                 }
463
464                 nh = ip_hdr(skb);
465                 key->ipv4.addr.src = nh->saddr;
466                 key->ipv4.addr.dst = nh->daddr;
467
468                 key->ip.proto = nh->protocol;
469                 key->ip.tos = nh->tos;
470                 key->ip.ttl = nh->ttl;
471
472                 offset = nh->frag_off & htons(IP_OFFSET);
473                 if (offset) {
474                         key->ip.frag = OVS_FRAG_TYPE_LATER;
475                         return 0;
476                 }
477                 if (nh->frag_off & htons(IP_MF) ||
478                          skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
479                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
480
481                 /* Transport layer. */
482                 if (key->ip.proto == IPPROTO_TCP) {
483                         if (tcphdr_ok(skb)) {
484                                 struct tcphdr *tcp = tcp_hdr(skb);
485                                 key->ipv4.tp.src = tcp->source;
486                                 key->ipv4.tp.dst = tcp->dest;
487                         }
488                 } else if (key->ip.proto == IPPROTO_UDP) {
489                         if (udphdr_ok(skb)) {
490                                 struct udphdr *udp = udp_hdr(skb);
491                                 key->ipv4.tp.src = udp->source;
492                                 key->ipv4.tp.dst = udp->dest;
493                         }
494                 } else if (key->ip.proto == IPPROTO_SCTP) {
495                         if (sctphdr_ok(skb)) {
496                                 struct sctphdr *sctp = sctp_hdr(skb);
497                                 key->ipv4.tp.src = sctp->source;
498                                 key->ipv4.tp.dst = sctp->dest;
499                         }
500                 } else if (key->ip.proto == IPPROTO_ICMP) {
501                         if (icmphdr_ok(skb)) {
502                                 struct icmphdr *icmp = icmp_hdr(skb);
503                                 /* The ICMP type and code fields use the 16-bit
504                                  * transport port fields, so we need to store
505                                  * them in 16-bit network byte order. */
506                                 key->ipv4.tp.src = htons(icmp->type);
507                                 key->ipv4.tp.dst = htons(icmp->code);
508                         }
509                 }
510
511         } else if ((key->eth.type == htons(ETH_P_ARP) ||
512                    key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
513                 struct arp_eth_header *arp;
514
515                 arp = (struct arp_eth_header *)skb_network_header(skb);
516
517                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
518                                 && arp->ar_pro == htons(ETH_P_IP)
519                                 && arp->ar_hln == ETH_ALEN
520                                 && arp->ar_pln == 4) {
521
522                         /* We only match on the lower 8 bits of the opcode. */
523                         if (ntohs(arp->ar_op) <= 0xff)
524                                 key->ip.proto = ntohs(arp->ar_op);
525                         memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
526                         memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
527                         memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
528                         memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
529                 }
530         } else if (key->eth.type == htons(ETH_P_IPV6)) {
531                 int nh_len;             /* IPv6 Header + Extensions */
532
533                 nh_len = parse_ipv6hdr(skb, key);
534                 if (unlikely(nh_len < 0)) {
535                         if (nh_len == -EINVAL) {
536                                 skb->transport_header = skb->network_header;
537                                 error = 0;
538                         } else {
539                                 error = nh_len;
540                         }
541                         return error;
542                 }
543
544                 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
545                         return 0;
546                 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
547                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
548
549                 /* Transport layer. */
550                 if (key->ip.proto == NEXTHDR_TCP) {
551                         if (tcphdr_ok(skb)) {
552                                 struct tcphdr *tcp = tcp_hdr(skb);
553                                 key->ipv6.tp.src = tcp->source;
554                                 key->ipv6.tp.dst = tcp->dest;
555                         }
556                 } else if (key->ip.proto == NEXTHDR_UDP) {
557                         if (udphdr_ok(skb)) {
558                                 struct udphdr *udp = udp_hdr(skb);
559                                 key->ipv6.tp.src = udp->source;
560                                 key->ipv6.tp.dst = udp->dest;
561                         }
562                 } else if (key->ip.proto == NEXTHDR_SCTP) {
563                         if (sctphdr_ok(skb)) {
564                                 struct sctphdr *sctp = sctp_hdr(skb);
565                                 key->ipv6.tp.src = sctp->source;
566                                 key->ipv6.tp.dst = sctp->dest;
567                         }
568                 } else if (key->ip.proto == NEXTHDR_ICMP) {
569                         if (icmp6hdr_ok(skb)) {
570                                 error = parse_icmpv6(skb, key, nh_len);
571                                 if (error)
572                                         return error;
573                         }
574                 }
575         }
576
577         return 0;
578 }