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 */
ovs_vport_init(void)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 */
ovs_vport_exit(void)49 void ovs_vport_exit(void)
50 {
51 kfree(dev_table);
52 }
53
hash_bucket(const struct net * net,const char * name)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
ovs_vport_ops_register(struct vport_ops * ops)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
ovs_vport_ops_unregister(struct vport_ops * ops)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 */
ovs_vport_locate(const struct net * net,const char * name)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 */
ovs_vport_alloc(int priv_size,const struct vport_ops * ops,const struct vport_parms * parms)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
161 /**
162 * ovs_vport_free - uninitialize and free vport
163 *
164 * @vport: vport to free
165 *
166 * Frees a vport allocated with vport_alloc() when it is no longer needed.
167 *
168 * The caller must ensure that an RCU grace period has passed since the last
169 * time @vport was in a datapath.
170 */
ovs_vport_free(struct vport * vport)171 void ovs_vport_free(struct vport *vport)
172 {
173 /* vport is freed from RCU callback or error path, Therefore
174 * it is safe to use raw dereference.
175 */
176 kfree(rcu_dereference_raw(vport->upcall_portids));
177 free_percpu(vport->upcall_stats);
178 kfree(vport);
179 }
180
ovs_vport_lookup(const struct vport_parms * parms)181 static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms)
182 {
183 struct vport_ops *ops;
184
185 list_for_each_entry(ops, &vport_ops_list, list)
186 if (ops->type == parms->type)
187 return ops;
188
189 return NULL;
190 }
191
192 /**
193 * ovs_vport_add - add vport device (for kernel callers)
194 *
195 * @parms: Information about new vport.
196 *
197 * Creates a new vport with the specified configuration (which is dependent on
198 * device type). ovs_mutex must be held.
199 */
ovs_vport_add(const struct vport_parms * parms)200 struct vport *ovs_vport_add(const struct vport_parms *parms)
201 {
202 struct vport_ops *ops;
203 struct vport *vport;
204
205 ops = ovs_vport_lookup(parms);
206 if (ops) {
207 struct hlist_head *bucket;
208
209 vport = ops->create(parms);
210 if (IS_ERR(vport))
211 return vport;
212
213 bucket = hash_bucket(ovs_dp_get_net(vport->dp),
214 ovs_vport_name(vport));
215 hlist_add_head_rcu(&vport->hash_node, bucket);
216 return vport;
217 }
218
219 return ERR_PTR(-EAFNOSUPPORT);
220 }
221
222 /**
223 * ovs_vport_del - delete existing vport device
224 *
225 * @vport: vport to delete.
226 *
227 * Detaches @vport from its datapath and destroys it. ovs_mutex must
228 * be held.
229 */
ovs_vport_del(struct vport * vport)230 void ovs_vport_del(struct vport *vport)
231 {
232 hlist_del_rcu(&vport->hash_node);
233 vport->ops->destroy(vport);
234 }
235
236 /**
237 * ovs_vport_get_stats - retrieve device stats
238 *
239 * @vport: vport from which to retrieve the stats
240 * @stats: location to store stats
241 *
242 * Retrieves transmit, receive, and error stats for the given device.
243 *
244 * Must be called with ovs_mutex or rcu_read_lock.
245 */
ovs_vport_get_stats(struct vport * vport,struct ovs_vport_stats * stats)246 void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
247 {
248 const struct rtnl_link_stats64 *dev_stats;
249 struct rtnl_link_stats64 temp;
250
251 dev_stats = dev_get_stats(vport->dev, &temp);
252 stats->rx_errors = dev_stats->rx_errors;
253 stats->tx_errors = dev_stats->tx_errors;
254 stats->tx_dropped = dev_stats->tx_dropped;
255 stats->rx_dropped = dev_stats->rx_dropped;
256
257 stats->rx_bytes = dev_stats->rx_bytes;
258 stats->rx_packets = dev_stats->rx_packets;
259 stats->tx_bytes = dev_stats->tx_bytes;
260 stats->tx_packets = dev_stats->tx_packets;
261 }
262
263 /**
264 * ovs_vport_get_upcall_stats - retrieve upcall stats
265 *
266 * @vport: vport from which to retrieve the stats.
267 * @skb: sk_buff where upcall stats should be appended.
268 *
269 * Retrieves upcall stats for the given device.
270 *
271 * Must be called with ovs_mutex or rcu_read_lock.
272 */
ovs_vport_get_upcall_stats(struct vport * vport,struct sk_buff * skb)273 int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb)
274 {
275 u64 tx_success = 0, tx_fail = 0;
276 struct nlattr *nla;
277 int i;
278
279 for_each_possible_cpu(i) {
280 const struct vport_upcall_stats_percpu *stats;
281 u64 n_success, n_fail;
282 unsigned int start;
283
284 stats = per_cpu_ptr(vport->upcall_stats, i);
285 do {
286 start = u64_stats_fetch_begin(&stats->syncp);
287 n_success = u64_stats_read(&stats->n_success);
288 n_fail = u64_stats_read(&stats->n_fail);
289 } while (u64_stats_fetch_retry(&stats->syncp, start));
290 tx_success += n_success;
291 tx_fail += n_fail;
292 }
293
294 nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS);
295 if (!nla)
296 return -EMSGSIZE;
297
298 if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_SUCCESS, tx_success,
299 OVS_VPORT_ATTR_PAD)) {
300 nla_nest_cancel(skb, nla);
301 return -EMSGSIZE;
302 }
303
304 if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_FAIL, tx_fail,
305 OVS_VPORT_ATTR_PAD)) {
306 nla_nest_cancel(skb, nla);
307 return -EMSGSIZE;
308 }
309 nla_nest_end(skb, nla);
310
311 return 0;
312 }
313
314 /**
315 * ovs_vport_set_upcall_portids - set upcall portids of @vport.
316 *
317 * @vport: vport to modify.
318 * @ids: new configuration, an array of port ids.
319 *
320 * Sets the vport's upcall_portids to @ids.
321 *
322 * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed
323 * as an array of U32.
324 *
325 * Must be called with ovs_mutex.
326 */
ovs_vport_set_upcall_portids(struct vport * vport,const struct nlattr * ids)327 int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
328 {
329 struct vport_portids *old, *vport_portids;
330
331 if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
332 return -EINVAL;
333
334 if (nla_len(ids) / sizeof(u32) > nr_cpu_ids)
335 return -EINVAL;
336
337 old = ovsl_dereference(vport->upcall_portids);
338
339 vport_portids = kmalloc(sizeof(*vport_portids) + nla_len(ids),
340 GFP_KERNEL);
341 if (!vport_portids)
342 return -ENOMEM;
343
344 vport_portids->n_ids = nla_len(ids) / sizeof(u32);
345 vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids);
346 nla_memcpy(vport_portids->ids, ids, nla_len(ids));
347
348 rcu_assign_pointer(vport->upcall_portids, vport_portids);
349
350 if (old)
351 kfree_rcu(old, rcu);
352 return 0;
353 }
354
355 /**
356 * ovs_vport_get_upcall_portids - get the upcall_portids of @vport.
357 *
358 * @vport: vport from which to retrieve the portids.
359 * @skb: sk_buff where portids should be appended.
360 *
361 * Retrieves the configuration of the given vport, appending the
362 * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall
363 * portids to @skb.
364 *
365 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room.
366 * If an error occurs, @skb is left unmodified. Must be called with
367 * ovs_mutex or rcu_read_lock.
368 */
ovs_vport_get_upcall_portids(const struct vport * vport,struct sk_buff * skb)369 int ovs_vport_get_upcall_portids(const struct vport *vport,
370 struct sk_buff *skb)
371 {
372 struct vport_portids *ids;
373
374 ids = rcu_dereference_ovsl(vport->upcall_portids);
375
376 if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS)
377 return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID,
378 ids->n_ids * sizeof(u32), (void *)ids->ids);
379 else
380 return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]);
381 }
382
383 /**
384 * ovs_vport_find_upcall_portid - find the upcall portid to send upcall.
385 *
386 * @vport: vport from which the missed packet is received.
387 * @skb: skb that the missed packet was received.
388 *
389 * Uses the skb_get_hash() to select the upcall portid to send the
390 * upcall.
391 *
392 * Returns the portid of the target socket. Must be called with rcu_read_lock.
393 */
ovs_vport_find_upcall_portid(const struct vport * vport,struct sk_buff * skb)394 u32 ovs_vport_find_upcall_portid(const struct vport *vport,
395 struct sk_buff *skb)
396 {
397 struct vport_portids *ids;
398 u32 ids_index;
399 u32 hash;
400
401 ids = rcu_dereference(vport->upcall_portids);
402
403 /* If there is only one portid, select it in the fast-path. */
404 if (ids->n_ids == 1)
405 return ids->ids[0];
406
407 hash = skb_get_hash(skb);
408 ids_index = hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids);
409 return ids->ids[ids_index];
410 }
411
412 /**
413 * ovs_vport_receive - pass up received packet to the datapath for processing
414 *
415 * @vport: vport that received the packet
416 * @skb: skb that was received
417 * @tun_info: tunnel (if any) that carried packet
418 *
419 * Must be called with rcu_read_lock. The packet cannot be shared and
420 * skb->data should point to the Ethernet header.
421 */
ovs_vport_receive(struct vport * vport,struct sk_buff * skb,const struct ip_tunnel_info * tun_info)422 int ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
423 const struct ip_tunnel_info *tun_info)
424 {
425 struct sw_flow_key key;
426 int error;
427
428 OVS_CB(skb)->input_vport = vport;
429 OVS_CB(skb)->mru = 0;
430 OVS_CB(skb)->cutlen = U32_MAX;
431 OVS_CB(skb)->probability = 0;
432 OVS_CB(skb)->upcall_pid = 0;
433 if (unlikely(dev_net(skb->dev) != ovs_dp_get_net(vport->dp))) {
434 u32 mark;
435
436 mark = skb->mark;
437 skb_scrub_packet(skb, true);
438 skb->mark = mark;
439 tun_info = NULL;
440 }
441
442 /* Extract flow from 'skb' into 'key'. */
443 error = ovs_flow_key_extract(tun_info, skb, &key);
444 if (unlikely(error)) {
445 kfree_skb(skb);
446 return error;
447 }
448 ovs_dp_process_packet(skb, &key);
449 return 0;
450 }
451
packet_length(const struct sk_buff * skb,struct net_device * dev)452 static int packet_length(const struct sk_buff *skb,
453 struct net_device *dev)
454 {
455 int length = skb->len - dev->hard_header_len;
456
457 if (!skb_vlan_tag_present(skb) &&
458 eth_type_vlan(skb->protocol))
459 length -= VLAN_HLEN;
460
461 /* Don't subtract for multiple VLAN tags. Most (all?) drivers allow
462 * (ETH_LEN + VLAN_HLEN) in addition to the mtu value, but almost none
463 * account for 802.1ad. e.g. is_skb_forwardable().
464 */
465
466 return length > 0 ? length : 0;
467 }
468
ovs_vport_send(struct vport * vport,struct sk_buff * skb,u8 mac_proto)469 void ovs_vport_send(struct vport *vport, struct sk_buff *skb, u8 mac_proto)
470 {
471 int mtu = vport->dev->mtu;
472
473 switch (vport->dev->type) {
474 case ARPHRD_NONE:
475 if (mac_proto == MAC_PROTO_ETHERNET) {
476 skb_reset_network_header(skb);
477 skb_reset_mac_len(skb);
478 skb->protocol = htons(ETH_P_TEB);
479 } else if (mac_proto != MAC_PROTO_NONE) {
480 WARN_ON_ONCE(1);
481 goto drop;
482 }
483 break;
484 case ARPHRD_ETHER:
485 if (mac_proto != MAC_PROTO_ETHERNET)
486 goto drop;
487 break;
488 default:
489 goto drop;
490 }
491
492 if (unlikely(packet_length(skb, vport->dev) > mtu &&
493 !skb_is_gso(skb))) {
494 vport->dev->stats.tx_errors++;
495 if (vport->dev->flags & IFF_UP)
496 net_warn_ratelimited("%s: dropped over-mtu packet: "
497 "%d > %d\n", vport->dev->name,
498 packet_length(skb, vport->dev),
499 mtu);
500 goto drop;
501 }
502
503 skb->dev = vport->dev;
504 skb_clear_tstamp(skb);
505 vport->ops->send(skb);
506 return;
507
508 drop:
509 kfree_skb(skb);
510 }
511