2 * Copyright (c) 2007-2014 Nicira, Inc.
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.
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.
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
19 #include <linux/etherdevice.h>
21 #include <linux/if_vlan.h>
22 #include <linux/jhash.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/percpu.h>
27 #include <linux/rcupdate.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/compat.h>
30 #include <net/net_namespace.h>
31 #include <linux/module.h>
35 #include "vport-internal_dev.h"
37 static void ovs_vport_record_error(struct vport *,
38 enum vport_err_type err_type);
40 static LIST_HEAD(vport_ops_list);
42 /* Protected by RCU read lock for reading, ovs_mutex for writing. */
43 static struct hlist_head *dev_table;
44 #define VPORT_HASH_BUCKETS 1024
47 * ovs_vport_init - initialize vport subsystem
49 * Called at module load time to initialize the vport subsystem.
51 int ovs_vport_init(void)
53 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
62 * ovs_vport_exit - shutdown vport subsystem
64 * Called at module exit time to shutdown the vport subsystem.
66 void ovs_vport_exit(void)
71 static struct hlist_head *hash_bucket(const struct net *net, const char *name)
73 unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
74 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
77 int ovs_vport_ops_register(struct vport_ops *ops)
83 list_for_each_entry(o, &vport_ops_list, list)
84 if (ops->type == o->type)
87 list_add_tail(&ops->list, &vport_ops_list);
93 EXPORT_SYMBOL_GPL(ovs_vport_ops_register);
95 void ovs_vport_ops_unregister(struct vport_ops *ops)
101 EXPORT_SYMBOL_GPL(ovs_vport_ops_unregister);
104 * ovs_vport_locate - find a port that has already been created
106 * @name: name of port to find
108 * Must be called with ovs or RCU read lock.
110 struct vport *ovs_vport_locate(const struct net *net, const char *name)
112 struct hlist_head *bucket = hash_bucket(net, name);
115 hlist_for_each_entry_rcu(vport, bucket, hash_node)
116 if (!strcmp(name, vport->ops->get_name(vport)) &&
117 net_eq(ovs_dp_get_net(vport->dp), net))
124 * ovs_vport_alloc - allocate and initialize new vport
126 * @priv_size: Size of private data area to allocate.
127 * @ops: vport device ops
129 * Allocate and initialize a new vport defined by @ops. The vport will contain
130 * a private data area of size @priv_size that can be accessed using
131 * vport_priv(). vports that are no longer needed should be released with
134 struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
135 const struct vport_parms *parms)
140 alloc_size = sizeof(struct vport);
142 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
143 alloc_size += priv_size;
146 vport = kzalloc(alloc_size, GFP_KERNEL);
148 return ERR_PTR(-ENOMEM);
150 vport->dp = parms->dp;
151 vport->port_no = parms->port_no;
153 INIT_HLIST_NODE(&vport->dp_hash_node);
155 if (ovs_vport_set_upcall_portids(vport, parms->upcall_portids)) {
157 return ERR_PTR(-EINVAL);
160 vport->percpu_stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
161 if (!vport->percpu_stats) {
163 return ERR_PTR(-ENOMEM);
168 EXPORT_SYMBOL_GPL(ovs_vport_alloc);
171 * ovs_vport_free - uninitialize and free vport
173 * @vport: vport to free
175 * Frees a vport allocated with vport_alloc() when it is no longer needed.
177 * The caller must ensure that an RCU grace period has passed since the last
178 * time @vport was in a datapath.
180 void ovs_vport_free(struct vport *vport)
182 /* vport is freed from RCU callback or error path, Therefore
183 * it is safe to use raw dereference.
185 kfree(rcu_dereference_raw(vport->upcall_portids));
186 free_percpu(vport->percpu_stats);
189 EXPORT_SYMBOL_GPL(ovs_vport_free);
191 static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms)
193 struct vport_ops *ops;
195 list_for_each_entry(ops, &vport_ops_list, list)
196 if (ops->type == parms->type)
203 * ovs_vport_add - add vport device (for kernel callers)
205 * @parms: Information about new vport.
207 * Creates a new vport with the specified configuration (which is dependent on
208 * device type). ovs_mutex must be held.
210 struct vport *ovs_vport_add(const struct vport_parms *parms)
212 struct vport_ops *ops;
215 ops = ovs_vport_lookup(parms);
217 struct hlist_head *bucket;
219 if (!try_module_get(ops->owner))
220 return ERR_PTR(-EAFNOSUPPORT);
222 vport = ops->create(parms);
224 module_put(ops->owner);
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);
234 /* Unlock to attempt module load and return -EAGAIN if load
235 * was successful as we need to restart the port addition
239 request_module("vport-type-%d", parms->type);
242 if (!ovs_vport_lookup(parms))
243 return ERR_PTR(-EAFNOSUPPORT);
245 return ERR_PTR(-EAGAIN);
249 * ovs_vport_set_options - modify existing vport device (for kernel callers)
251 * @vport: vport to modify.
252 * @options: New configuration.
254 * Modifies an existing device with the specified configuration (which is
255 * dependent on device type). ovs_mutex must be held.
257 int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
259 if (!vport->ops->set_options)
261 return vport->ops->set_options(vport, options);
265 * ovs_vport_del - delete existing vport device
267 * @vport: vport to delete.
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.
272 void ovs_vport_del(struct vport *vport)
276 hlist_del_rcu(&vport->hash_node);
278 vport->ops->destroy(vport);
280 module_put(vport->ops->owner);
284 * ovs_vport_get_stats - retrieve device stats
286 * @vport: vport from which to retrieve the stats
287 * @stats: location to store stats
289 * Retrieves transmit, receive, and error stats for the given device.
291 * Must be called with ovs_mutex or rcu_read_lock.
293 void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
297 memset(stats, 0, sizeof(*stats));
299 /* We potentially have 2 sources of stats that need to be combined:
300 * those we have collected (split into err_stats and percpu_stats) from
301 * set_stats() and device error stats from netdev->get_stats() (for
302 * errors that happen downstream and therefore aren't reported through
303 * our vport_record_error() function).
304 * Stats from first source are reported by ovs (OVS_VPORT_ATTR_STATS).
305 * netdev-stats can be directly read over netlink-ioctl.
308 stats->rx_errors = atomic_long_read(&vport->err_stats.rx_errors);
309 stats->tx_errors = atomic_long_read(&vport->err_stats.tx_errors);
310 stats->tx_dropped = atomic_long_read(&vport->err_stats.tx_dropped);
311 stats->rx_dropped = atomic_long_read(&vport->err_stats.rx_dropped);
313 for_each_possible_cpu(i) {
314 const struct pcpu_sw_netstats *percpu_stats;
315 struct pcpu_sw_netstats local_stats;
318 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
321 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
322 local_stats = *percpu_stats;
323 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
325 stats->rx_bytes += local_stats.rx_bytes;
326 stats->rx_packets += local_stats.rx_packets;
327 stats->tx_bytes += local_stats.tx_bytes;
328 stats->tx_packets += local_stats.tx_packets;
333 * ovs_vport_get_options - retrieve device options
335 * @vport: vport from which to retrieve the options.
336 * @skb: sk_buff where options should be appended.
338 * Retrieves the configuration of the given device, appending an
339 * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
340 * vport-specific attributes to @skb.
342 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
343 * negative error code if a real error occurred. If an error occurs, @skb is
346 * Must be called with ovs_mutex or rcu_read_lock.
348 int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
353 if (!vport->ops->get_options)
356 nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
360 err = vport->ops->get_options(vport, skb);
362 nla_nest_cancel(skb, nla);
366 nla_nest_end(skb, nla);
371 * ovs_vport_set_upcall_portids - set upcall portids of @vport.
373 * @vport: vport to modify.
374 * @ids: new configuration, an array of port ids.
376 * Sets the vport's upcall_portids to @ids.
378 * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed
379 * as an array of U32.
381 * Must be called with ovs_mutex.
383 int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
385 struct vport_portids *old, *vport_portids;
387 if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
390 old = ovsl_dereference(vport->upcall_portids);
392 vport_portids = kmalloc(sizeof(*vport_portids) + nla_len(ids),
397 vport_portids->n_ids = nla_len(ids) / sizeof(u32);
398 vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids);
399 nla_memcpy(vport_portids->ids, ids, nla_len(ids));
401 rcu_assign_pointer(vport->upcall_portids, vport_portids);
409 * ovs_vport_get_upcall_portids - get the upcall_portids of @vport.
411 * @vport: vport from which to retrieve the portids.
412 * @skb: sk_buff where portids should be appended.
414 * Retrieves the configuration of the given vport, appending the
415 * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall
418 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room.
419 * If an error occurs, @skb is left unmodified. Must be called with
420 * ovs_mutex or rcu_read_lock.
422 int ovs_vport_get_upcall_portids(const struct vport *vport,
425 struct vport_portids *ids;
427 ids = rcu_dereference_ovsl(vport->upcall_portids);
429 if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS)
430 return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID,
431 ids->n_ids * sizeof(u32), (void *)ids->ids);
433 return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]);
437 * ovs_vport_find_upcall_portid - find the upcall portid to send upcall.
439 * @vport: vport from which the missed packet is received.
440 * @skb: skb that the missed packet was received.
442 * Uses the skb_get_hash() to select the upcall portid to send the
445 * Returns the portid of the target socket. Must be called with rcu_read_lock.
447 u32 ovs_vport_find_upcall_portid(const struct vport *vport, struct sk_buff *skb)
449 struct vport_portids *ids;
453 ids = rcu_dereference(vport->upcall_portids);
455 if (ids->n_ids == 1 && ids->ids[0] == 0)
458 hash = skb_get_hash(skb);
459 ids_index = hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids);
460 return ids->ids[ids_index];
464 * ovs_vport_receive - pass up received packet to the datapath for processing
466 * @vport: vport that received the packet
467 * @skb: skb that was received
468 * @tun_key: tunnel (if any) that carried packet
470 * Must be called with rcu_read_lock. The packet cannot be shared and
471 * skb->data should point to the Ethernet header.
473 void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
474 const struct ovs_tunnel_info *tun_info)
476 struct pcpu_sw_netstats *stats;
477 struct sw_flow_key key;
480 stats = this_cpu_ptr(vport->percpu_stats);
481 u64_stats_update_begin(&stats->syncp);
483 stats->rx_bytes += skb->len;
484 u64_stats_update_end(&stats->syncp);
486 OVS_CB(skb)->input_vport = vport;
487 OVS_CB(skb)->egress_tun_info = NULL;
488 /* Extract flow from 'skb' into 'key'. */
489 error = ovs_flow_key_extract(tun_info, skb, &key);
490 if (unlikely(error)) {
494 ovs_dp_process_packet(skb, &key);
496 EXPORT_SYMBOL_GPL(ovs_vport_receive);
499 * ovs_vport_send - send a packet on a device
501 * @vport: vport on which to send the packet
504 * Sends the given packet and returns the length of data sent. Either ovs
505 * lock or rcu_read_lock must be held.
507 int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
509 int sent = vport->ops->send(vport, skb);
511 if (likely(sent > 0)) {
512 struct pcpu_sw_netstats *stats;
514 stats = this_cpu_ptr(vport->percpu_stats);
516 u64_stats_update_begin(&stats->syncp);
518 stats->tx_bytes += sent;
519 u64_stats_update_end(&stats->syncp);
520 } else if (sent < 0) {
521 ovs_vport_record_error(vport, VPORT_E_TX_ERROR);
524 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
530 * ovs_vport_record_error - indicate device error to generic stats layer
532 * @vport: vport that encountered the error
533 * @err_type: one of enum vport_err_type types to indicate the error type
535 * If using the vport generic stats layer indicate that an error of the given
538 static void ovs_vport_record_error(struct vport *vport,
539 enum vport_err_type err_type)
542 case VPORT_E_RX_DROPPED:
543 atomic_long_inc(&vport->err_stats.rx_dropped);
546 case VPORT_E_RX_ERROR:
547 atomic_long_inc(&vport->err_stats.rx_errors);
550 case VPORT_E_TX_DROPPED:
551 atomic_long_inc(&vport->err_stats.tx_dropped);
554 case VPORT_E_TX_ERROR:
555 atomic_long_inc(&vport->err_stats.tx_errors);
561 static void free_vport_rcu(struct rcu_head *rcu)
563 struct vport *vport = container_of(rcu, struct vport, rcu);
565 ovs_vport_free(vport);
568 void ovs_vport_deferred_free(struct vport *vport)
573 call_rcu(&vport->rcu, free_vport_rcu);
575 EXPORT_SYMBOL_GPL(ovs_vport_deferred_free);
577 int ovs_tunnel_get_egress_info(struct ovs_tunnel_info *egress_tun_info,
579 const struct ovs_tunnel_info *tun_info,
585 const struct ovs_key_ipv4_tunnel *tun_key;
589 if (unlikely(!tun_info))
592 tun_key = &tun_info->tunnel;
594 /* Route lookup to get srouce IP address.
595 * The process may need to be changed if the corresponding process
596 * in vports ops changed.
598 memset(&fl, 0, sizeof(fl));
599 fl.daddr = tun_key->ipv4_dst;
600 fl.saddr = tun_key->ipv4_src;
601 fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
602 fl.flowi4_mark = skb_mark;
603 fl.flowi4_proto = IPPROTO_GRE;
605 rt = ip_route_output_key(net, &fl);
611 /* Generate egress_tun_info based on tun_info,
612 * saddr, tp_src and tp_dst
614 __ovs_flow_tun_info_init(egress_tun_info,
615 fl.saddr, tun_key->ipv4_dst,
622 tun_info->options_len);
626 EXPORT_SYMBOL_GPL(ovs_tunnel_get_egress_info);
628 int ovs_vport_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
629 struct ovs_tunnel_info *info)
631 /* get_egress_tun_info() is only implemented on tunnel ports. */
632 if (unlikely(!vport->ops->get_egress_tun_info))
635 return vport->ops->get_egress_tun_info(vport, skb, info);