datapath: Use TCP flags in the flow key for stats.
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Copyright (c) 2007-2014 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_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
64
65 void ovs_flow_stats_update(struct sw_flow *flow, struct sk_buff *skb)
66 {
67         struct flow_stats *stats;
68         __be16 tcp_flags = 0;
69         int node = numa_node_id();
70
71         stats = rcu_dereference(flow->stats[node]);
72
73         if (likely(flow->key.ip.proto == IPPROTO_TCP)) {
74                 if (likely(flow->key.eth.type == htons(ETH_P_IP)))
75                         tcp_flags = flow->key.ipv4.tp.flags;
76                 else if (likely(flow->key.eth.type == htons(ETH_P_IPV6)))
77                         tcp_flags = flow->key.ipv6.tp.flags;
78         }
79         /* Check if already have node-specific stats. */
80         if (likely(stats)) {
81                 spin_lock(&stats->lock);
82                 /* Mark if we write on the pre-allocated stats. */
83                 if (node == 0 && unlikely(flow->stats_last_writer != node))
84                         flow->stats_last_writer = node;
85         } else {
86                 stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */
87                 spin_lock(&stats->lock);
88
89                 /* If the current NUMA-node is the only writer on the
90                  * pre-allocated stats keep using them.
91                  */
92                 if (unlikely(flow->stats_last_writer != node)) {
93                         /* A previous locker may have already allocated the
94                          * stats, so we need to check again.  If node-specific
95                          * stats were already allocated, we update the pre-
96                          * allocated stats as we have already locked them.
97                          */
98                         if (likely(flow->stats_last_writer != NUMA_NO_NODE)
99                             && likely(!rcu_dereference(flow->stats[node]))) {
100                                 /* Try to allocate node-specific stats. */
101                                 struct flow_stats *new_stats;
102
103                                 new_stats =
104                                         kmem_cache_alloc_node(flow_stats_cache,
105                                                               GFP_THISNODE |
106                                                               __GFP_NOMEMALLOC,
107                                                               node);
108                                 if (likely(new_stats)) {
109                                         new_stats->used = jiffies;
110                                         new_stats->packet_count = 1;
111                                         new_stats->byte_count = skb->len;
112                                         new_stats->tcp_flags = tcp_flags;
113                                         spin_lock_init(&new_stats->lock);
114
115                                         rcu_assign_pointer(flow->stats[node],
116                                                            new_stats);
117                                         goto unlock;
118                                 }
119                         }
120                         flow->stats_last_writer = node;
121                 }
122         }
123
124         stats->used = jiffies;
125         stats->packet_count++;
126         stats->byte_count += skb->len;
127         stats->tcp_flags |= tcp_flags;
128 unlock:
129         spin_unlock(&stats->lock);
130 }
131
132 void ovs_flow_stats_get(struct sw_flow *flow, struct ovs_flow_stats *ovs_stats,
133                         unsigned long *used, __be16 *tcp_flags)
134 {
135         int node;
136
137         *used = 0;
138         *tcp_flags = 0;
139         memset(ovs_stats, 0, sizeof(*ovs_stats));
140
141         for_each_node(node) {
142                 struct flow_stats *stats = rcu_dereference(flow->stats[node]);
143
144                 if (stats) {
145                         /* Local CPU may write on non-local stats, so we must
146                          * block bottom-halves here.
147                          */
148                         spin_lock_bh(&stats->lock);
149                         if (!*used || time_after(stats->used, *used))
150                                 *used = stats->used;
151                         *tcp_flags |= stats->tcp_flags;
152                         ovs_stats->n_packets += stats->packet_count;
153                         ovs_stats->n_bytes += stats->byte_count;
154                         spin_unlock_bh(&stats->lock);
155                 }
156         }
157 }
158
159 void ovs_flow_stats_clear(struct sw_flow *flow)
160 {
161         int node;
162
163         for_each_node(node) {
164                 struct flow_stats *stats = rcu_dereference(flow->stats[node]);
165
166                 if (stats) {
167                         spin_lock_bh(&stats->lock);
168                         stats->used = 0;
169                         stats->packet_count = 0;
170                         stats->byte_count = 0;
171                         stats->tcp_flags = 0;
172                         spin_unlock_bh(&stats->lock);
173                 }
174         }
175 }
176
177 static int check_header(struct sk_buff *skb, int len)
178 {
179         if (unlikely(skb->len < len))
180                 return -EINVAL;
181         if (unlikely(!pskb_may_pull(skb, len)))
182                 return -ENOMEM;
183         return 0;
184 }
185
186 static bool arphdr_ok(struct sk_buff *skb)
187 {
188         return pskb_may_pull(skb, skb_network_offset(skb) +
189                                   sizeof(struct arp_eth_header));
190 }
191
192 static int check_iphdr(struct sk_buff *skb)
193 {
194         unsigned int nh_ofs = skb_network_offset(skb);
195         unsigned int ip_len;
196         int err;
197
198         err = check_header(skb, nh_ofs + sizeof(struct iphdr));
199         if (unlikely(err))
200                 return err;
201
202         ip_len = ip_hdrlen(skb);
203         if (unlikely(ip_len < sizeof(struct iphdr) ||
204                      skb->len < nh_ofs + ip_len))
205                 return -EINVAL;
206
207         skb_set_transport_header(skb, nh_ofs + ip_len);
208         return 0;
209 }
210
211 static bool tcphdr_ok(struct sk_buff *skb)
212 {
213         int th_ofs = skb_transport_offset(skb);
214         int tcp_len;
215
216         if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
217                 return false;
218
219         tcp_len = tcp_hdrlen(skb);
220         if (unlikely(tcp_len < sizeof(struct tcphdr) ||
221                      skb->len < th_ofs + tcp_len))
222                 return false;
223
224         return true;
225 }
226
227 static bool udphdr_ok(struct sk_buff *skb)
228 {
229         return pskb_may_pull(skb, skb_transport_offset(skb) +
230                                   sizeof(struct udphdr));
231 }
232
233 static bool sctphdr_ok(struct sk_buff *skb)
234 {
235         return pskb_may_pull(skb, skb_transport_offset(skb) +
236                                   sizeof(struct sctphdr));
237 }
238
239 static bool icmphdr_ok(struct sk_buff *skb)
240 {
241         return pskb_may_pull(skb, skb_transport_offset(skb) +
242                                   sizeof(struct icmphdr));
243 }
244
245 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
246 {
247         unsigned int nh_ofs = skb_network_offset(skb);
248         unsigned int nh_len;
249         int payload_ofs;
250         struct ipv6hdr *nh;
251         uint8_t nexthdr;
252         __be16 frag_off;
253         int err;
254
255         err = check_header(skb, nh_ofs + sizeof(*nh));
256         if (unlikely(err))
257                 return err;
258
259         nh = ipv6_hdr(skb);
260         nexthdr = nh->nexthdr;
261         payload_ofs = (u8 *)(nh + 1) - skb->data;
262
263         key->ip.proto = NEXTHDR_NONE;
264         key->ip.tos = ipv6_get_dsfield(nh);
265         key->ip.ttl = nh->hop_limit;
266         key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
267         key->ipv6.addr.src = nh->saddr;
268         key->ipv6.addr.dst = nh->daddr;
269
270         payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
271         if (unlikely(payload_ofs < 0))
272                 return -EINVAL;
273
274         if (frag_off) {
275                 if (frag_off & htons(~0x7))
276                         key->ip.frag = OVS_FRAG_TYPE_LATER;
277                 else
278                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
279         }
280
281         nh_len = payload_ofs - nh_ofs;
282         skb_set_transport_header(skb, nh_ofs + nh_len);
283         key->ip.proto = nexthdr;
284         return nh_len;
285 }
286
287 static bool icmp6hdr_ok(struct sk_buff *skb)
288 {
289         return pskb_may_pull(skb, skb_transport_offset(skb) +
290                                   sizeof(struct icmp6hdr));
291 }
292
293 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
294 {
295         struct qtag_prefix {
296                 __be16 eth_type; /* ETH_P_8021Q */
297                 __be16 tci;
298         };
299         struct qtag_prefix *qp;
300
301         if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
302                 return 0;
303
304         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
305                                          sizeof(__be16))))
306                 return -ENOMEM;
307
308         qp = (struct qtag_prefix *) skb->data;
309         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
310         __skb_pull(skb, sizeof(struct qtag_prefix));
311
312         return 0;
313 }
314
315 static __be16 parse_ethertype(struct sk_buff *skb)
316 {
317         struct llc_snap_hdr {
318                 u8  dsap;  /* Always 0xAA */
319                 u8  ssap;  /* Always 0xAA */
320                 u8  ctrl;
321                 u8  oui[3];
322                 __be16 ethertype;
323         };
324         struct llc_snap_hdr *llc;
325         __be16 proto;
326
327         proto = *(__be16 *) skb->data;
328         __skb_pull(skb, sizeof(__be16));
329
330         if (ntohs(proto) >= ETH_P_802_3_MIN)
331                 return proto;
332
333         if (skb->len < sizeof(struct llc_snap_hdr))
334                 return htons(ETH_P_802_2);
335
336         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
337                 return htons(0);
338
339         llc = (struct llc_snap_hdr *) skb->data;
340         if (llc->dsap != LLC_SAP_SNAP ||
341             llc->ssap != LLC_SAP_SNAP ||
342             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
343                 return htons(ETH_P_802_2);
344
345         __skb_pull(skb, sizeof(struct llc_snap_hdr));
346
347         if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
348                 return llc->ethertype;
349
350         return htons(ETH_P_802_2);
351 }
352
353 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
354                         int nh_len)
355 {
356         struct icmp6hdr *icmp = icmp6_hdr(skb);
357
358         /* The ICMPv6 type and code fields use the 16-bit transport port
359          * fields, so we need to store them in 16-bit network byte order.
360          */
361         key->ipv6.tp.src = htons(icmp->icmp6_type);
362         key->ipv6.tp.dst = htons(icmp->icmp6_code);
363
364         if (icmp->icmp6_code == 0 &&
365             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
366              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
367                 int icmp_len = skb->len - skb_transport_offset(skb);
368                 struct nd_msg *nd;
369                 int offset;
370
371                 /* In order to process neighbor discovery options, we need the
372                  * entire packet.
373                  */
374                 if (unlikely(icmp_len < sizeof(*nd)))
375                         return 0;
376
377                 if (unlikely(skb_linearize(skb)))
378                         return -ENOMEM;
379
380                 nd = (struct nd_msg *)skb_transport_header(skb);
381                 key->ipv6.nd.target = nd->target;
382
383                 icmp_len -= sizeof(*nd);
384                 offset = 0;
385                 while (icmp_len >= 8) {
386                         struct nd_opt_hdr *nd_opt =
387                                  (struct nd_opt_hdr *)(nd->opt + offset);
388                         int opt_len = nd_opt->nd_opt_len * 8;
389
390                         if (unlikely(!opt_len || opt_len > icmp_len))
391                                 return 0;
392
393                         /* Store the link layer address if the appropriate
394                          * option is provided.  It is considered an error if
395                          * the same link layer option is specified twice.
396                          */
397                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
398                             && opt_len == 8) {
399                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
400                                         goto invalid;
401                                 ether_addr_copy(key->ipv6.nd.sll,
402                                     &nd->opt[offset+sizeof(*nd_opt)]);
403                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
404                                    && opt_len == 8) {
405                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
406                                         goto invalid;
407                                 ether_addr_copy(key->ipv6.nd.tll,
408                                     &nd->opt[offset+sizeof(*nd_opt)]);
409                         }
410
411                         icmp_len -= opt_len;
412                         offset += opt_len;
413                 }
414         }
415
416         return 0;
417
418 invalid:
419         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
420         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
421         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
422
423         return 0;
424 }
425
426 /**
427  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
428  * @skb: sk_buff that contains the frame, with skb->data pointing to the
429  * Ethernet header
430  * @in_port: port number on which @skb was received.
431  * @key: output flow key
432  *
433  * The caller must ensure that skb->len >= ETH_HLEN.
434  *
435  * Returns 0 if successful, otherwise a negative errno value.
436  *
437  * Initializes @skb header pointers as follows:
438  *
439  *    - skb->mac_header: the Ethernet header.
440  *
441  *    - skb->network_header: just past the Ethernet header, or just past the
442  *      VLAN header, to the first byte of the Ethernet payload.
443  *
444  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
445  *      on output, then just past the IP header, if one is present and
446  *      of a correct length, otherwise the same as skb->network_header.
447  *      For other key->eth.type values it is left untouched.
448  */
449 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
450 {
451         int error;
452         struct ethhdr *eth;
453
454         memset(key, 0, sizeof(*key));
455
456         key->phy.priority = skb->priority;
457         if (OVS_CB(skb)->tun_key)
458                 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
459         key->phy.in_port = in_port;
460         key->phy.skb_mark = skb->mark;
461
462         skb_reset_mac_header(skb);
463
464         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
465          * header in the linear data area.
466          */
467         eth = eth_hdr(skb);
468         ether_addr_copy(key->eth.src, eth->h_source);
469         ether_addr_copy(key->eth.dst, eth->h_dest);
470
471         __skb_pull(skb, 2 * ETH_ALEN);
472         /* We are going to push all headers that we pull, so no need to
473          * update skb->csum here. */
474
475         if (vlan_tx_tag_present(skb))
476                 key->eth.tci = htons(vlan_get_tci(skb));
477         else if (eth->h_proto == htons(ETH_P_8021Q))
478                 if (unlikely(parse_vlan(skb, key)))
479                         return -ENOMEM;
480
481         key->eth.type = parse_ethertype(skb);
482         if (unlikely(key->eth.type == htons(0)))
483                 return -ENOMEM;
484
485         skb_reset_network_header(skb);
486         __skb_push(skb, skb->data - skb_mac_header(skb));
487
488         /* Network layer. */
489         if (key->eth.type == htons(ETH_P_IP)) {
490                 struct iphdr *nh;
491                 __be16 offset;
492
493                 error = check_iphdr(skb);
494                 if (unlikely(error)) {
495                         if (error == -EINVAL) {
496                                 skb->transport_header = skb->network_header;
497                                 error = 0;
498                         }
499                         return error;
500                 }
501
502                 nh = ip_hdr(skb);
503                 key->ipv4.addr.src = nh->saddr;
504                 key->ipv4.addr.dst = nh->daddr;
505
506                 key->ip.proto = nh->protocol;
507                 key->ip.tos = nh->tos;
508                 key->ip.ttl = nh->ttl;
509
510                 offset = nh->frag_off & htons(IP_OFFSET);
511                 if (offset) {
512                         key->ip.frag = OVS_FRAG_TYPE_LATER;
513                         return 0;
514                 }
515                 if (nh->frag_off & htons(IP_MF) ||
516                          skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
517                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
518
519                 /* Transport layer. */
520                 if (key->ip.proto == IPPROTO_TCP) {
521                         if (tcphdr_ok(skb)) {
522                                 struct tcphdr *tcp = tcp_hdr(skb);
523                                 key->ipv4.tp.src = tcp->source;
524                                 key->ipv4.tp.dst = tcp->dest;
525                                 key->ipv4.tp.flags = TCP_FLAGS_BE16(tcp);
526                         }
527                 } else if (key->ip.proto == IPPROTO_UDP) {
528                         if (udphdr_ok(skb)) {
529                                 struct udphdr *udp = udp_hdr(skb);
530                                 key->ipv4.tp.src = udp->source;
531                                 key->ipv4.tp.dst = udp->dest;
532                         }
533                 } else if (key->ip.proto == IPPROTO_SCTP) {
534                         if (sctphdr_ok(skb)) {
535                                 struct sctphdr *sctp = sctp_hdr(skb);
536                                 key->ipv4.tp.src = sctp->source;
537                                 key->ipv4.tp.dst = sctp->dest;
538                         }
539                 } else if (key->ip.proto == IPPROTO_ICMP) {
540                         if (icmphdr_ok(skb)) {
541                                 struct icmphdr *icmp = icmp_hdr(skb);
542                                 /* The ICMP type and code fields use the 16-bit
543                                  * transport port fields, so we need to store
544                                  * them in 16-bit network byte order. */
545                                 key->ipv4.tp.src = htons(icmp->type);
546                                 key->ipv4.tp.dst = htons(icmp->code);
547                         }
548                 }
549
550         } else if ((key->eth.type == htons(ETH_P_ARP) ||
551                    key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
552                 struct arp_eth_header *arp;
553
554                 arp = (struct arp_eth_header *)skb_network_header(skb);
555
556                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
557                                 && arp->ar_pro == htons(ETH_P_IP)
558                                 && arp->ar_hln == ETH_ALEN
559                                 && arp->ar_pln == 4) {
560
561                         /* We only match on the lower 8 bits of the opcode. */
562                         if (ntohs(arp->ar_op) <= 0xff)
563                                 key->ip.proto = ntohs(arp->ar_op);
564                         memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
565                         memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
566                         ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha);
567                         ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha);
568                 }
569         } else if (key->eth.type == htons(ETH_P_IPV6)) {
570                 int nh_len;             /* IPv6 Header + Extensions */
571
572                 nh_len = parse_ipv6hdr(skb, key);
573                 if (unlikely(nh_len < 0)) {
574                         if (nh_len == -EINVAL) {
575                                 skb->transport_header = skb->network_header;
576                                 error = 0;
577                         } else {
578                                 error = nh_len;
579                         }
580                         return error;
581                 }
582
583                 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
584                         return 0;
585                 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
586                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
587
588                 /* Transport layer. */
589                 if (key->ip.proto == NEXTHDR_TCP) {
590                         if (tcphdr_ok(skb)) {
591                                 struct tcphdr *tcp = tcp_hdr(skb);
592                                 key->ipv6.tp.src = tcp->source;
593                                 key->ipv6.tp.dst = tcp->dest;
594                                 key->ipv6.tp.flags = TCP_FLAGS_BE16(tcp);
595                         }
596                 } else if (key->ip.proto == NEXTHDR_UDP) {
597                         if (udphdr_ok(skb)) {
598                                 struct udphdr *udp = udp_hdr(skb);
599                                 key->ipv6.tp.src = udp->source;
600                                 key->ipv6.tp.dst = udp->dest;
601                         }
602                 } else if (key->ip.proto == NEXTHDR_SCTP) {
603                         if (sctphdr_ok(skb)) {
604                                 struct sctphdr *sctp = sctp_hdr(skb);
605                                 key->ipv6.tp.src = sctp->source;
606                                 key->ipv6.tp.dst = sctp->dest;
607                         }
608                 } else if (key->ip.proto == NEXTHDR_ICMP) {
609                         if (icmp6hdr_ok(skb)) {
610                                 error = parse_icmpv6(skb, key, nh_len);
611                                 if (error)
612                                         return error;
613                         }
614                 }
615         }
616
617         return 0;
618 }