xref: /linux/net/openvswitch/vport.c (revision 26ba30221c03364d6ed9910be8da4c1fd871b07b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2014 Nicira, Inc.
4  */
5 
6 #include <linux/etherdevice.h>
7 #include <linux/if.h>
8 #include <linux/if_vlan.h>
9 #include <linux/jhash.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/percpu.h>
14 #include <linux/rcupdate.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/compat.h>
17 #include <net/net_namespace.h>
18 #include <linux/module.h>
19 
20 #include "datapath.h"
21 #include "vport.h"
22 #include "vport-internal_dev.h"
23 
24 static LIST_HEAD(vport_ops_list);
25 
26 /* Protected by RCU read lock for reading, ovs_mutex for writing. */
27 static struct hlist_head *dev_table;
28 #define VPORT_HASH_BUCKETS 1024
29 
30 /**
31  *	ovs_vport_init - initialize vport subsystem
32  *
33  * Called at module load time to initialize the vport subsystem.
34  */
35 int ovs_vport_init(void)
36 {
37 	dev_table = kzalloc_objs(struct hlist_head, VPORT_HASH_BUCKETS);
38 	if (!dev_table)
39 		return -ENOMEM;
40 
41 	return 0;
42 }
43 
44 /**
45  *	ovs_vport_exit - shutdown vport subsystem
46  *
47  * Called at module exit time to shutdown the vport subsystem.
48  */
49 void ovs_vport_exit(void)
50 {
51 	kfree(dev_table);
52 }
53 
54 static struct hlist_head *hash_bucket(const struct net *net, const char *name)
55 {
56 	unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
57 	return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
58 }
59 
60 int ovs_vport_ops_register(struct vport_ops *ops)
61 {
62 	int err = -EEXIST;
63 	struct vport_ops *o;
64 
65 	ovs_lock();
66 	list_for_each_entry(o, &vport_ops_list, list)
67 		if (ops->type == o->type)
68 			goto errout;
69 
70 	list_add_tail(&ops->list, &vport_ops_list);
71 	err = 0;
72 errout:
73 	ovs_unlock();
74 	return err;
75 }
76 
77 void ovs_vport_ops_unregister(struct vport_ops *ops)
78 {
79 	ovs_lock();
80 	list_del(&ops->list);
81 	ovs_unlock();
82 }
83 
84 /**
85  *	ovs_vport_locate - find a port that has already been created
86  *
87  * @net: network namespace
88  * @name: name of port to find
89  *
90  * Must be called with ovs or RCU read lock.
91  */
92 struct vport *ovs_vport_locate(const struct net *net, const char *name)
93 {
94 	struct hlist_head *bucket = hash_bucket(net, name);
95 	struct vport *vport;
96 
97 	hlist_for_each_entry_rcu(vport, bucket, hash_node,
98 				 lockdep_ovsl_is_held())
99 		if (!strcmp(name, ovs_vport_name(vport)) &&
100 		    net_eq(ovs_dp_get_net(vport->dp), net))
101 			return vport;
102 
103 	return NULL;
104 }
105 
106 /**
107  *	ovs_vport_alloc - allocate and initialize new vport
108  *
109  * @priv_size: Size of private data area to allocate.
110  * @ops: vport device ops
111  * @parms: information about new vport.
112  *
113  * Allocate and initialize a new vport defined by @ops.  The vport will contain
114  * a private data area of size @priv_size that can be accessed using
115  * vport_priv().  Some parameters of the vport will be initialized from @parms.
116  * @vports that are no longer needed should be released with
117  * vport_free().
118  */
119 struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
120 			      const struct vport_parms *parms)
121 {
122 	struct vport *vport;
123 	size_t alloc_size;
124 	int err;
125 
126 	alloc_size = sizeof(struct vport);
127 	if (priv_size) {
128 		alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
129 		alloc_size += priv_size;
130 	}
131 
132 	vport = kzalloc(alloc_size, GFP_KERNEL);
133 	if (!vport)
134 		return ERR_PTR(-ENOMEM);
135 
136 	vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu);
137 	if (!vport->upcall_stats) {
138 		err = -ENOMEM;
139 		goto err_kfree_vport;
140 	}
141 
142 	vport->dp = parms->dp;
143 	vport->port_no = parms->port_no;
144 	vport->ops = ops;
145 	INIT_HLIST_NODE(&vport->dp_hash_node);
146 
147 	if (ovs_vport_set_upcall_portids(vport, parms->upcall_portids)) {
148 		err = -EINVAL;
149 		goto err_free_percpu;
150 	}
151 
152 	return vport;
153 
154 err_free_percpu:
155 	free_percpu(vport->upcall_stats);
156 err_kfree_vport:
157 	kfree(vport);
158 	return ERR_PTR(err);
159 }
160 EXPORT_SYMBOL_GPL(ovs_vport_alloc);
161 
162 /**
163  *	ovs_vport_free - uninitialize and free vport
164  *
165  * @vport: vport to free
166  *
167  * Frees a vport allocated with vport_alloc() when it is no longer needed.
168  *
169  * The caller must ensure that an RCU grace period has passed since the last
170  * time @vport was in a datapath.
171  */
172 void ovs_vport_free(struct vport *vport)
173 {
174 	/* vport is freed from RCU callback or error path, Therefore
175 	 * it is safe to use raw dereference.
176 	 */
177 	kfree(rcu_dereference_raw(vport->upcall_portids));
178 	free_percpu(vport->upcall_stats);
179 	kfree(vport);
180 }
181 EXPORT_SYMBOL_GPL(ovs_vport_free);
182 
183 static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms)
184 {
185 	struct vport_ops *ops;
186 
187 	list_for_each_entry(ops, &vport_ops_list, list)
188 		if (ops->type == parms->type)
189 			return ops;
190 
191 	return NULL;
192 }
193 
194 /**
195  *	ovs_vport_add - add vport device (for kernel callers)
196  *
197  * @parms: Information about new vport.
198  *
199  * Creates a new vport with the specified configuration (which is dependent on
200  * device type).  ovs_mutex must be held.
201  */
202 struct vport *ovs_vport_add(const struct vport_parms *parms)
203 {
204 	struct vport_ops *ops;
205 	struct vport *vport;
206 
207 	ops = ovs_vport_lookup(parms);
208 	if (ops) {
209 		struct hlist_head *bucket;
210 
211 		vport = ops->create(parms);
212 		if (IS_ERR(vport))
213 			return vport;
214 
215 		bucket = hash_bucket(ovs_dp_get_net(vport->dp),
216 				     ovs_vport_name(vport));
217 		hlist_add_head_rcu(&vport->hash_node, bucket);
218 		return vport;
219 	}
220 
221 	return ERR_PTR(-EAFNOSUPPORT);
222 }
223 
224 /**
225  *	ovs_vport_del - delete existing vport device
226  *
227  * @vport: vport to delete.
228  *
229  * Detaches @vport from its datapath and destroys it.  ovs_mutex must
230  * be held.
231  */
232 void ovs_vport_del(struct vport *vport)
233 {
234 	hlist_del_rcu(&vport->hash_node);
235 	vport->ops->destroy(vport);
236 }
237 
238 /**
239  *	ovs_vport_get_stats - retrieve device stats
240  *
241  * @vport: vport from which to retrieve the stats
242  * @stats: location to store stats
243  *
244  * Retrieves transmit, receive, and error stats for the given device.
245  *
246  * Must be called with ovs_mutex or rcu_read_lock.
247  */
248 void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
249 {
250 	const struct rtnl_link_stats64 *dev_stats;
251 	struct rtnl_link_stats64 temp;
252 
253 	dev_stats = dev_get_stats(vport->dev, &temp);
254 	stats->rx_errors  = dev_stats->rx_errors;
255 	stats->tx_errors  = dev_stats->tx_errors;
256 	stats->tx_dropped = dev_stats->tx_dropped;
257 	stats->rx_dropped = dev_stats->rx_dropped;
258 
259 	stats->rx_bytes	  = dev_stats->rx_bytes;
260 	stats->rx_packets = dev_stats->rx_packets;
261 	stats->tx_bytes	  = dev_stats->tx_bytes;
262 	stats->tx_packets = dev_stats->tx_packets;
263 }
264 
265 /**
266  *	ovs_vport_get_upcall_stats - retrieve upcall stats
267  *
268  * @vport: vport from which to retrieve the stats.
269  * @skb: sk_buff where upcall stats should be appended.
270  *
271  * Retrieves upcall stats for the given device.
272  *
273  * Must be called with ovs_mutex or rcu_read_lock.
274  */
275 int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb)
276 {
277 	u64 tx_success = 0, tx_fail = 0;
278 	struct nlattr *nla;
279 	int i;
280 
281 	for_each_possible_cpu(i) {
282 		const struct vport_upcall_stats_percpu *stats;
283 		u64 n_success, n_fail;
284 		unsigned int start;
285 
286 		stats = per_cpu_ptr(vport->upcall_stats, i);
287 		do {
288 			start = u64_stats_fetch_begin(&stats->syncp);
289 			n_success = u64_stats_read(&stats->n_success);
290 			n_fail = u64_stats_read(&stats->n_fail);
291 		} while (u64_stats_fetch_retry(&stats->syncp, start));
292 		tx_success += n_success;
293 		tx_fail += n_fail;
294 	}
295 
296 	nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS);
297 	if (!nla)
298 		return -EMSGSIZE;
299 
300 	if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_SUCCESS, tx_success,
301 			      OVS_VPORT_ATTR_PAD)) {
302 		nla_nest_cancel(skb, nla);
303 		return -EMSGSIZE;
304 	}
305 
306 	if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_FAIL, tx_fail,
307 			      OVS_VPORT_ATTR_PAD)) {
308 		nla_nest_cancel(skb, nla);
309 		return -EMSGSIZE;
310 	}
311 	nla_nest_end(skb, nla);
312 
313 	return 0;
314 }
315 
316 /**
317  *	ovs_vport_set_upcall_portids - set upcall portids of @vport.
318  *
319  * @vport: vport to modify.
320  * @ids: new configuration, an array of port ids.
321  *
322  * Sets the vport's upcall_portids to @ids.
323  *
324  * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed
325  * as an array of U32.
326  *
327  * Must be called with ovs_mutex.
328  */
329 int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
330 {
331 	struct vport_portids *old, *vport_portids;
332 
333 	if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
334 		return -EINVAL;
335 
336 	if (nla_len(ids) / sizeof(u32) > nr_cpu_ids)
337 		return -EINVAL;
338 
339 	old = ovsl_dereference(vport->upcall_portids);
340 
341 	vport_portids = kmalloc(sizeof(*vport_portids) + nla_len(ids),
342 				GFP_KERNEL);
343 	if (!vport_portids)
344 		return -ENOMEM;
345 
346 	vport_portids->n_ids = nla_len(ids) / sizeof(u32);
347 	vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids);
348 	nla_memcpy(vport_portids->ids, ids, nla_len(ids));
349 
350 	rcu_assign_pointer(vport->upcall_portids, vport_portids);
351 
352 	if (old)
353 		kfree_rcu(old, rcu);
354 	return 0;
355 }
356 
357 /**
358  *	ovs_vport_get_upcall_portids - get the upcall_portids of @vport.
359  *
360  * @vport: vport from which to retrieve the portids.
361  * @skb: sk_buff where portids should be appended.
362  *
363  * Retrieves the configuration of the given vport, appending the
364  * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall
365  * portids to @skb.
366  *
367  * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room.
368  * If an error occurs, @skb is left unmodified.  Must be called with
369  * ovs_mutex or rcu_read_lock.
370  */
371 int ovs_vport_get_upcall_portids(const struct vport *vport,
372 				 struct sk_buff *skb)
373 {
374 	struct vport_portids *ids;
375 
376 	ids = rcu_dereference_ovsl(vport->upcall_portids);
377 
378 	if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS)
379 		return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID,
380 			       ids->n_ids * sizeof(u32), (void *)ids->ids);
381 	else
382 		return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]);
383 }
384 
385 /**
386  *	ovs_vport_find_upcall_portid - find the upcall portid to send upcall.
387  *
388  * @vport: vport from which the missed packet is received.
389  * @skb: skb that the missed packet was received.
390  *
391  * Uses the skb_get_hash() to select the upcall portid to send the
392  * upcall.
393  *
394  * Returns the portid of the target socket.  Must be called with rcu_read_lock.
395  */
396 u32 ovs_vport_find_upcall_portid(const struct vport *vport,
397 				 struct sk_buff *skb)
398 {
399 	struct vport_portids *ids;
400 	u32 ids_index;
401 	u32 hash;
402 
403 	ids = rcu_dereference(vport->upcall_portids);
404 
405 	/* If there is only one portid, select it in the fast-path. */
406 	if (ids->n_ids == 1)
407 		return ids->ids[0];
408 
409 	hash = skb_get_hash(skb);
410 	ids_index = hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids);
411 	return ids->ids[ids_index];
412 }
413 
414 /**
415  *	ovs_vport_receive - pass up received packet to the datapath for processing
416  *
417  * @vport: vport that received the packet
418  * @skb: skb that was received
419  * @tun_info: tunnel (if any) that carried packet
420  *
421  * Must be called with rcu_read_lock.  The packet cannot be shared and
422  * skb->data should point to the Ethernet header.
423  */
424 int ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
425 		      const struct ip_tunnel_info *tun_info)
426 {
427 	struct sw_flow_key key;
428 	int error;
429 
430 	OVS_CB(skb)->input_vport = vport;
431 	OVS_CB(skb)->mru = 0;
432 	OVS_CB(skb)->cutlen = U32_MAX;
433 	OVS_CB(skb)->probability = 0;
434 	OVS_CB(skb)->upcall_pid = 0;
435 	if (unlikely(dev_net(skb->dev) != ovs_dp_get_net(vport->dp))) {
436 		u32 mark;
437 
438 		mark = skb->mark;
439 		skb_scrub_packet(skb, true);
440 		skb->mark = mark;
441 		tun_info = NULL;
442 	}
443 
444 	/* Extract flow from 'skb' into 'key'. */
445 	error = ovs_flow_key_extract(tun_info, skb, &key);
446 	if (unlikely(error)) {
447 		kfree_skb(skb);
448 		return error;
449 	}
450 	ovs_dp_process_packet(skb, &key);
451 	return 0;
452 }
453 
454 static int packet_length(const struct sk_buff *skb,
455 			 struct net_device *dev)
456 {
457 	int length = skb->len - dev->hard_header_len;
458 
459 	if (!skb_vlan_tag_present(skb) &&
460 	    eth_type_vlan(skb->protocol))
461 		length -= VLAN_HLEN;
462 
463 	/* Don't subtract for multiple VLAN tags. Most (all?) drivers allow
464 	 * (ETH_LEN + VLAN_HLEN) in addition to the mtu value, but almost none
465 	 * account for 802.1ad. e.g. is_skb_forwardable().
466 	 */
467 
468 	return length > 0 ? length : 0;
469 }
470 
471 void ovs_vport_send(struct vport *vport, struct sk_buff *skb, u8 mac_proto)
472 {
473 	int mtu = vport->dev->mtu;
474 
475 	switch (vport->dev->type) {
476 	case ARPHRD_NONE:
477 		if (mac_proto == MAC_PROTO_ETHERNET) {
478 			skb_reset_network_header(skb);
479 			skb_reset_mac_len(skb);
480 			skb->protocol = htons(ETH_P_TEB);
481 		} else if (mac_proto != MAC_PROTO_NONE) {
482 			WARN_ON_ONCE(1);
483 			goto drop;
484 		}
485 		break;
486 	case ARPHRD_ETHER:
487 		if (mac_proto != MAC_PROTO_ETHERNET)
488 			goto drop;
489 		break;
490 	default:
491 		goto drop;
492 	}
493 
494 	if (unlikely(packet_length(skb, vport->dev) > mtu &&
495 		     !skb_is_gso(skb))) {
496 		vport->dev->stats.tx_errors++;
497 		if (vport->dev->flags & IFF_UP)
498 			net_warn_ratelimited("%s: dropped over-mtu packet: "
499 					     "%d > %d\n", vport->dev->name,
500 					     packet_length(skb, vport->dev),
501 					     mtu);
502 		goto drop;
503 	}
504 
505 	skb->dev = vport->dev;
506 	skb_clear_tstamp(skb);
507 	vport->ops->send(skb);
508 	return;
509 
510 drop:
511 	kfree_skb(skb);
512 }
513