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