]> git.proxmox.com Git - mirror_ovs.git/blob - datapath/vport.c
Merge remote-tracking branch 'origin/master' into ovn
[mirror_ovs.git] / datapath / vport.c
1 /*
2 * Copyright (c) 2007-2012 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 <linux/etherdevice.h>
20 #include <linux/if.h>
21 #include <linux/if_vlan.h>
22 #include <linux/jhash.h>
23 #include <linux/kconfig.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/percpu.h>
29 #include <linux/rcupdate.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/compat.h>
32 #include <linux/version.h>
33 #include <net/net_namespace.h>
34
35 #include "datapath.h"
36 #include "gso.h"
37 #include "vport.h"
38 #include "vport-internal_dev.h"
39
40 static void ovs_vport_record_error(struct vport *,
41 enum vport_err_type err_type);
42
43 static LIST_HEAD(vport_ops_list);
44
45 /* Protected by RCU read lock for reading, ovs_mutex for writing. */
46 static struct hlist_head *dev_table;
47 #define VPORT_HASH_BUCKETS 1024
48
49 /**
50 * ovs_vport_init - initialize vport subsystem
51 *
52 * Called at module load time to initialize the vport subsystem.
53 */
54 int ovs_vport_init(void)
55 {
56 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
57 GFP_KERNEL);
58 if (!dev_table)
59 return -ENOMEM;
60
61 return 0;
62 }
63
64 /**
65 * ovs_vport_exit - shutdown vport subsystem
66 *
67 * Called at module exit time to shutdown the vport subsystem.
68 */
69 void ovs_vport_exit(void)
70 {
71 kfree(dev_table);
72 }
73
74 static struct hlist_head *hash_bucket(const struct net *net, const char *name)
75 {
76 unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
77 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
78 }
79
80 int ovs_vport_ops_register(struct vport_ops *ops)
81 {
82 int err = -EEXIST;
83 struct vport_ops *o;
84
85 ovs_lock();
86 list_for_each_entry(o, &vport_ops_list, list)
87 if (ops->type == o->type)
88 goto errout;
89
90 list_add_tail(&ops->list, &vport_ops_list);
91 err = 0;
92 errout:
93 ovs_unlock();
94 return err;
95 }
96 EXPORT_SYMBOL_GPL(ovs_vport_ops_register);
97
98 void ovs_vport_ops_unregister(struct vport_ops *ops)
99 {
100 ovs_lock();
101 list_del(&ops->list);
102 ovs_unlock();
103 }
104 EXPORT_SYMBOL_GPL(ovs_vport_ops_unregister);
105
106 /**
107 * ovs_vport_locate - find a port that has already been created
108 *
109 * @name: name of port to find
110 *
111 * Must be called with ovs or RCU read lock.
112 */
113 struct vport *ovs_vport_locate(const struct net *net, const char *name)
114 {
115 struct hlist_head *bucket = hash_bucket(net, name);
116 struct vport *vport;
117
118 hlist_for_each_entry_rcu(vport, bucket, hash_node)
119 if (!strcmp(name, vport->ops->get_name(vport)) &&
120 net_eq(ovs_dp_get_net(vport->dp), net))
121 return vport;
122
123 return NULL;
124 }
125
126 /**
127 * ovs_vport_alloc - allocate and initialize new vport
128 *
129 * @priv_size: Size of private data area to allocate.
130 * @ops: vport device ops
131 *
132 * Allocate and initialize a new vport defined by @ops. The vport will contain
133 * a private data area of size @priv_size that can be accessed using
134 * vport_priv(). vports that are no longer needed should be released with
135 * ovs_vport_free().
136 */
137 struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
138 const struct vport_parms *parms)
139 {
140 struct vport *vport;
141 size_t alloc_size;
142
143 alloc_size = sizeof(struct vport);
144 if (priv_size) {
145 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
146 alloc_size += priv_size;
147 }
148
149 vport = kzalloc(alloc_size, GFP_KERNEL);
150 if (!vport)
151 return ERR_PTR(-ENOMEM);
152
153 vport->dp = parms->dp;
154 vport->port_no = parms->port_no;
155 vport->ops = ops;
156 INIT_HLIST_NODE(&vport->dp_hash_node);
157
158 if (ovs_vport_set_upcall_portids(vport, parms->upcall_portids)) {
159 kfree(vport);
160 return ERR_PTR(-EINVAL);
161 }
162
163 vport->percpu_stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
164 if (!vport->percpu_stats) {
165 kfree(vport);
166 return ERR_PTR(-ENOMEM);
167 }
168
169 return vport;
170 }
171 EXPORT_SYMBOL_GPL(ovs_vport_alloc);
172
173 static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms)
174 {
175 struct vport_ops *ops;
176
177 list_for_each_entry(ops, &vport_ops_list, list)
178 if (ops->type == parms->type)
179 return ops;
180
181 return NULL;
182 }
183
184 /**
185 * ovs_vport_free - uninitialize and free vport
186 *
187 * @vport: vport to free
188 *
189 * Frees a vport allocated with ovs_vport_alloc() when it is no longer needed.
190 *
191 * The caller must ensure that an RCU grace period has passed since the last
192 * time @vport was in a datapath.
193 */
194 void ovs_vport_free(struct vport *vport)
195 {
196 kfree(rcu_dereference_raw(vport->upcall_portids));
197 free_percpu(vport->percpu_stats);
198 kfree(vport);
199 }
200 EXPORT_SYMBOL_GPL(ovs_vport_free);
201
202 /**
203 * ovs_vport_add - add vport device (for kernel callers)
204 *
205 * @parms: Information about new vport.
206 *
207 * Creates a new vport with the specified configuration (which is dependent on
208 * device type). ovs_mutex must be held.
209 */
210 struct vport *ovs_vport_add(const struct vport_parms *parms)
211 {
212 struct vport_ops *ops;
213 struct vport *vport;
214
215 ops = ovs_vport_lookup(parms);
216 if (ops) {
217 struct hlist_head *bucket;
218
219 if (!try_module_get(ops->owner))
220 return ERR_PTR(-EAFNOSUPPORT);
221
222 vport = ops->create(parms);
223 if (IS_ERR(vport)) {
224 module_put(ops->owner);
225 return vport;
226 }
227
228 bucket = hash_bucket(ovs_dp_get_net(vport->dp),
229 vport->ops->get_name(vport));
230 hlist_add_head_rcu(&vport->hash_node, bucket);
231 return vport;
232 }
233
234 /* Unlock to attempt module load and return -EAGAIN if load
235 * was successful as we need to restart the port addition
236 * workflow.
237 */
238 ovs_unlock();
239 request_module("vport-type-%d", parms->type);
240 ovs_lock();
241
242 if (!ovs_vport_lookup(parms))
243 return ERR_PTR(-EAFNOSUPPORT);
244 else
245 return ERR_PTR(-EAGAIN);
246 }
247
248 /**
249 * ovs_vport_set_options - modify existing vport device (for kernel callers)
250 *
251 * @vport: vport to modify.
252 * @options: New configuration.
253 *
254 * Modifies an existing device with the specified configuration (which is
255 * dependent on device type). ovs_mutex must be held.
256 */
257 int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
258 {
259 if (!vport->ops->set_options)
260 return -EOPNOTSUPP;
261 return vport->ops->set_options(vport, options);
262 }
263
264 /**
265 * ovs_vport_del - delete existing vport device
266 *
267 * @vport: vport to delete.
268 *
269 * Detaches @vport from its datapath and destroys it. It is possible to fail
270 * for reasons such as lack of memory. ovs_mutex must be held.
271 */
272 void ovs_vport_del(struct vport *vport)
273 {
274 ASSERT_OVSL();
275
276 hlist_del_rcu(&vport->hash_node);
277 module_put(vport->ops->owner);
278 vport->ops->destroy(vport);
279 }
280
281 /**
282 * ovs_vport_get_stats - retrieve device stats
283 *
284 * @vport: vport from which to retrieve the stats
285 * @stats: location to store stats
286 *
287 * Retrieves transmit, receive, and error stats for the given device.
288 *
289 * Must be called with ovs_mutex or rcu_read_lock.
290 */
291 void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
292 {
293 int i;
294
295 /* We potentially have two surces of stats that need to be
296 * combined: those we have collected (split into err_stats and
297 * percpu_stats), and device error stats from netdev->get_stats()
298 * (for errors that happen downstream and therefore aren't
299 * reported through our vport_record_error() function).
300 * Stats from first source are reported by ovs over
301 * OVS_VPORT_ATTR_STATS.
302 * netdev-stats can be directly read over netlink-ioctl.
303 */
304
305 stats->rx_errors = atomic_long_read(&vport->err_stats.rx_errors);
306 stats->tx_errors = atomic_long_read(&vport->err_stats.tx_errors);
307 stats->tx_dropped = atomic_long_read(&vport->err_stats.tx_dropped);
308 stats->rx_dropped = atomic_long_read(&vport->err_stats.rx_dropped);
309
310 stats->rx_bytes = 0;
311 stats->rx_packets = 0;
312 stats->tx_bytes = 0;
313 stats->tx_packets = 0;
314
315 for_each_possible_cpu(i) {
316 const struct pcpu_sw_netstats *percpu_stats;
317 struct pcpu_sw_netstats local_stats;
318 unsigned int start;
319
320 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
321
322 do {
323 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
324 local_stats = *percpu_stats;
325 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
326
327 stats->rx_bytes += local_stats.rx_bytes;
328 stats->rx_packets += local_stats.rx_packets;
329 stats->tx_bytes += local_stats.tx_bytes;
330 stats->tx_packets += local_stats.tx_packets;
331 }
332 }
333
334 /**
335 * ovs_vport_get_options - retrieve device options
336 *
337 * @vport: vport from which to retrieve the options.
338 * @skb: sk_buff where options should be appended.
339 *
340 * Retrieves the configuration of the given device, appending an
341 * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
342 * vport-specific attributes to @skb.
343 *
344 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
345 * negative error code if a real error occurred. If an error occurs, @skb is
346 * left unmodified.
347 *
348 * Must be called with ovs_mutex or rcu_read_lock.
349 */
350 int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
351 {
352 struct nlattr *nla;
353 int err;
354
355 if (!vport->ops->get_options)
356 return 0;
357
358 nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
359 if (!nla)
360 return -EMSGSIZE;
361
362 err = vport->ops->get_options(vport, skb);
363 if (err) {
364 nla_nest_cancel(skb, nla);
365 return err;
366 }
367
368 nla_nest_end(skb, nla);
369 return 0;
370 }
371
372 static void vport_portids_destroy_rcu_cb(struct rcu_head *rcu)
373 {
374 struct vport_portids *ids = container_of(rcu, struct vport_portids,
375 rcu);
376
377 kfree(ids);
378 }
379
380 /**
381 * ovs_vport_set_upcall_portids - set upcall portids of @vport.
382 *
383 * @vport: vport to modify.
384 * @ids: new configuration, an array of port ids.
385 *
386 * Sets the vport's upcall_portids to @ids.
387 *
388 * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed
389 * as an array of U32.
390 *
391 * Must be called with ovs_mutex.
392 */
393 int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
394 {
395 struct vport_portids *old, *vport_portids;
396
397 if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
398 return -EINVAL;
399
400 old = ovsl_dereference(vport->upcall_portids);
401
402 vport_portids = kmalloc(sizeof *vport_portids + nla_len(ids),
403 GFP_KERNEL);
404 if (!vport_portids)
405 return -ENOMEM;
406
407 vport_portids->n_ids = nla_len(ids) / sizeof(u32);
408 vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids);
409 nla_memcpy(vport_portids->ids, ids, nla_len(ids));
410
411 rcu_assign_pointer(vport->upcall_portids, vport_portids);
412
413 if (old)
414 call_rcu(&old->rcu, vport_portids_destroy_rcu_cb);
415
416 return 0;
417 }
418
419 /**
420 * ovs_vport_get_upcall_portids - get the upcall_portids of @vport.
421 *
422 * @vport: vport from which to retrieve the portids.
423 * @skb: sk_buff where portids should be appended.
424 *
425 * Retrieves the configuration of the given vport, appending the
426 * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall
427 * portids to @skb.
428 *
429 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room.
430 * If an error occurs, @skb is left unmodified. Must be called with
431 * ovs_mutex or rcu_read_lock.
432 */
433 int ovs_vport_get_upcall_portids(const struct vport *vport,
434 struct sk_buff *skb)
435 {
436 struct vport_portids *ids;
437
438 ids = rcu_dereference_ovsl(vport->upcall_portids);
439
440 if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS)
441 return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID,
442 ids->n_ids * sizeof(u32), (void *) ids->ids);
443 else
444 return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]);
445 }
446
447 /**
448 * ovs_vport_find_upcall_portid - find the upcall portid to send upcall.
449 *
450 * @vport: vport from which the missed packet is received.
451 * @skb: skb that the missed packet was received.
452 *
453 * Uses the skb_get_hash() to select the upcall portid to send the
454 * upcall.
455 *
456 * Returns the portid of the target socket. Must be called with rcu_read_lock.
457 */
458 u32 ovs_vport_find_upcall_portid(const struct vport *vport, struct sk_buff *skb)
459 {
460 struct vport_portids *ids;
461 u32 hash;
462
463 ids = rcu_dereference(vport->upcall_portids);
464
465 if (ids->n_ids == 1 && ids->ids[0] == 0)
466 return 0;
467
468 hash = skb_get_hash(skb);
469 return ids->ids[hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids)];
470 }
471
472 /**
473 * ovs_vport_receive - pass up received packet to the datapath for processing
474 *
475 * @vport: vport that received the packet
476 * @skb: skb that was received
477 * @tun_info: tunnel (if any) that carried packet
478 *
479 * Must be called with rcu_read_lock. The packet cannot be shared and
480 * skb->data should point to the Ethernet header. The caller must have already
481 * called compute_ip_summed() to initialize the checksumming fields.
482 */
483 void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
484 const struct ovs_tunnel_info *tun_info)
485 {
486 struct pcpu_sw_netstats *stats;
487 struct sw_flow_key key;
488 int error;
489
490 stats = this_cpu_ptr(vport->percpu_stats);
491 u64_stats_update_begin(&stats->syncp);
492 stats->rx_packets++;
493 stats->rx_bytes += skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
494 u64_stats_update_end(&stats->syncp);
495
496 ovs_skb_init_inner_protocol(skb);
497 OVS_CB(skb)->input_vport = vport;
498 OVS_CB(skb)->egress_tun_info = NULL;
499 error = ovs_flow_key_extract(tun_info, skb, &key);
500 if (unlikely(error)) {
501 kfree_skb(skb);
502 return;
503 }
504
505 ovs_dp_process_packet(skb, &key);
506 }
507 EXPORT_SYMBOL_GPL(ovs_vport_receive);
508
509 /**
510 * ovs_vport_send - send a packet on a device
511 *
512 * @vport: vport on which to send the packet
513 * @skb: skb to send
514 *
515 * Sends the given packet and returns the length of data sent. Either ovs
516 * lock or rcu_read_lock must be held.
517 */
518 int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
519 {
520 int sent = vport->ops->send(vport, skb);
521
522 if (likely(sent > 0)) {
523 struct pcpu_sw_netstats *stats;
524
525 stats = this_cpu_ptr(vport->percpu_stats);
526
527 u64_stats_update_begin(&stats->syncp);
528 stats->tx_packets++;
529 stats->tx_bytes += sent;
530 u64_stats_update_end(&stats->syncp);
531 } else if (sent < 0) {
532 ovs_vport_record_error(vport, VPORT_E_TX_ERROR);
533 } else {
534 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
535 }
536
537 return sent;
538 }
539
540 /**
541 * ovs_vport_record_error - indicate device error to generic stats layer
542 *
543 * @vport: vport that encountered the error
544 * @err_type: one of enum vport_err_type types to indicate the error type
545 *
546 * If using the vport generic stats layer indicate that an error of the given
547 * type has occurred.
548 */
549 static void ovs_vport_record_error(struct vport *vport,
550 enum vport_err_type err_type)
551 {
552 switch (err_type) {
553 case VPORT_E_RX_DROPPED:
554 atomic_long_inc(&vport->err_stats.rx_dropped);
555 break;
556
557 case VPORT_E_RX_ERROR:
558 atomic_long_inc(&vport->err_stats.rx_errors);
559 break;
560
561 case VPORT_E_TX_DROPPED:
562 atomic_long_inc(&vport->err_stats.tx_dropped);
563 break;
564
565 case VPORT_E_TX_ERROR:
566 atomic_long_inc(&vport->err_stats.tx_errors);
567 break;
568 }
569 }
570
571 static void free_vport_rcu(struct rcu_head *rcu)
572 {
573 struct vport *vport = container_of(rcu, struct vport, rcu);
574
575 ovs_vport_free(vport);
576 }
577
578 void ovs_vport_deferred_free(struct vport *vport)
579 {
580 if (!vport)
581 return;
582
583 call_rcu(&vport->rcu, free_vport_rcu);
584 }
585 EXPORT_SYMBOL_GPL(ovs_vport_deferred_free);
586
587 int ovs_tunnel_get_egress_info(struct ovs_tunnel_info *egress_tun_info,
588 struct net *net,
589 const struct ovs_tunnel_info *tun_info,
590 u8 ipproto,
591 u32 skb_mark,
592 __be16 tp_src,
593 __be16 tp_dst)
594 {
595 const struct ovs_key_ipv4_tunnel *tun_key;
596 struct rtable *rt;
597 __be32 saddr;
598
599 if (unlikely(!tun_info))
600 return -EINVAL;
601
602 tun_key = &tun_info->tunnel;
603 saddr = tun_key->ipv4_src;
604 /* Route lookup to get srouce IP address: saddr.
605 * The process may need to be changed if the corresponding process
606 * in vports ops changed.
607 */
608 rt = find_route(net,
609 &saddr,
610 tun_key->ipv4_dst,
611 ipproto,
612 tun_key->ipv4_tos,
613 skb_mark);
614 if (IS_ERR(rt))
615 return PTR_ERR(rt);
616
617 ip_rt_put(rt);
618
619 /* Generate egress_tun_info based on tun_info,
620 * saddr, tp_src and tp_dst
621 */
622 __ovs_flow_tun_info_init(egress_tun_info,
623 saddr, tun_key->ipv4_dst,
624 tun_key->ipv4_tos,
625 tun_key->ipv4_ttl,
626 tp_src, tp_dst,
627 tun_key->tun_id,
628 tun_key->tun_flags,
629 tun_info->options,
630 tun_info->options_len);
631
632 return 0;
633 }
634 EXPORT_SYMBOL_GPL(ovs_tunnel_get_egress_info);
635
636 int ovs_vport_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
637 struct ovs_tunnel_info *info)
638 {
639 /* get_egress_tun_info() is only implemented on tunnel ports. */
640 if (unlikely(!vport->ops->get_egress_tun_info))
641 return -EINVAL;
642
643 return vport->ops->get_egress_tun_info(vport, skb, info);
644 }