xref: /linux/arch/um/drivers/vector_kern.c (revision 2dca89df0d1116f722b4be100e4bfcff858058e8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2017 - 2019 Cambridge Greys Limited
4  * Copyright (C) 2011 - 2014 Cisco Systems Inc
5  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6  * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
7  * James Leu (jleu@mindspring.net).
8  * Copyright (C) 2001 by various other people who didn't put their name here.
9  */
10 
11 #define pr_fmt(fmt) "uml-vector: " fmt
12 
13 #include <linux/memblock.h>
14 #include <linux/etherdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/inetdevice.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/netdevice.h>
20 #include <linux/platform_device.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/skbuff.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/firmware.h>
26 #include <linux/fs.h>
27 #include <asm/atomic.h>
28 #include <uapi/linux/filter.h>
29 #include <init.h>
30 #include <irq_kern.h>
31 #include <irq_user.h>
32 #include <os.h>
33 #include "mconsole_kern.h"
34 #include "vector_user.h"
35 #include "vector_kern.h"
36 
37 /*
38  * Adapted from network devices with the following major changes:
39  * All transports are static - simplifies the code significantly
40  * Multiple FDs/IRQs per device
41  * Vector IO optionally used for read/write, falling back to legacy
42  * based on configuration and/or availability
43  * Configuration is no longer positional - L2TPv3 and GRE require up to
44  * 10 parameters, passing this as positional is not fit for purpose.
45  * Only socket transports are supported
46  */
47 
48 
49 #define DRIVER_NAME "uml-vector"
50 struct vector_cmd_line_arg {
51 	struct list_head list;
52 	int unit;
53 	char *arguments;
54 };
55 
56 struct vector_device {
57 	struct list_head list;
58 	struct net_device *dev;
59 	struct platform_device pdev;
60 	int unit;
61 	int opened;
62 };
63 
64 static LIST_HEAD(vec_cmd_line);
65 
66 static DEFINE_SPINLOCK(vector_devices_lock);
67 static LIST_HEAD(vector_devices);
68 
69 static int driver_registered;
70 
71 static void vector_eth_configure(int n, struct arglist *def);
72 static int vector_mmsg_rx(struct vector_private *vp, int budget);
73 
74 /* Argument accessors to set variables (and/or set default values)
75  * mtu, buffer sizing, default headroom, etc
76  */
77 
78 #define DEFAULT_HEADROOM 2
79 #define SAFETY_MARGIN 32
80 #define DEFAULT_VECTOR_SIZE 64
81 #define TX_SMALL_PACKET 128
82 #define MAX_IOV_SIZE (MAX_SKB_FRAGS + 1)
83 
84 static const struct {
85 	const char string[ETH_GSTRING_LEN];
86 } ethtool_stats_keys[] = {
87 	{ "rx_queue_max" },
88 	{ "rx_queue_running_average" },
89 	{ "tx_queue_max" },
90 	{ "tx_queue_running_average" },
91 	{ "rx_encaps_errors" },
92 	{ "tx_timeout_count" },
93 	{ "tx_restart_queue" },
94 	{ "tx_kicks" },
95 	{ "tx_flow_control_xon" },
96 	{ "tx_flow_control_xoff" },
97 	{ "rx_csum_offload_good" },
98 	{ "rx_csum_offload_errors"},
99 	{ "sg_ok"},
100 	{ "sg_linearized"},
101 };
102 
103 #define VECTOR_NUM_STATS	ARRAY_SIZE(ethtool_stats_keys)
104 
vector_reset_stats(struct vector_private * vp)105 static void vector_reset_stats(struct vector_private *vp)
106 {
107 	/* We reuse the existing queue locks for stats */
108 
109 	/* RX stats are modified with RX head_lock held
110 	 * in vector_poll.
111 	 */
112 
113 	spin_lock(&vp->rx_queue->head_lock);
114 	vp->estats.rx_queue_max = 0;
115 	vp->estats.rx_queue_running_average = 0;
116 	vp->estats.rx_encaps_errors = 0;
117 	vp->estats.sg_ok = 0;
118 	vp->estats.sg_linearized = 0;
119 	spin_unlock(&vp->rx_queue->head_lock);
120 
121 	/* TX stats are modified with TX head_lock held
122 	 * in vector_send.
123 	 */
124 
125 	spin_lock(&vp->tx_queue->head_lock);
126 	vp->estats.tx_timeout_count = 0;
127 	vp->estats.tx_restart_queue = 0;
128 	vp->estats.tx_kicks = 0;
129 	vp->estats.tx_flow_control_xon = 0;
130 	vp->estats.tx_flow_control_xoff = 0;
131 	vp->estats.tx_queue_max = 0;
132 	vp->estats.tx_queue_running_average = 0;
133 	spin_unlock(&vp->tx_queue->head_lock);
134 }
135 
get_mtu(struct arglist * def)136 static int get_mtu(struct arglist *def)
137 {
138 	char *mtu = uml_vector_fetch_arg(def, "mtu");
139 	long result;
140 
141 	if (mtu != NULL) {
142 		if (kstrtoul(mtu, 10, &result) == 0)
143 			if ((result < (1 << 16) - 1) && (result >= 576))
144 				return result;
145 	}
146 	return ETH_MAX_PACKET;
147 }
148 
get_bpf_file(struct arglist * def)149 static char *get_bpf_file(struct arglist *def)
150 {
151 	return uml_vector_fetch_arg(def, "bpffile");
152 }
153 
get_bpf_flash(struct arglist * def)154 static bool get_bpf_flash(struct arglist *def)
155 {
156 	char *allow = uml_vector_fetch_arg(def, "bpfflash");
157 	long result;
158 
159 	if (allow != NULL) {
160 		if (kstrtoul(allow, 10, &result) == 0)
161 			return result > 0;
162 	}
163 	return false;
164 }
165 
get_depth(struct arglist * def)166 static int get_depth(struct arglist *def)
167 {
168 	char *mtu = uml_vector_fetch_arg(def, "depth");
169 	long result;
170 
171 	if (mtu != NULL) {
172 		if (kstrtoul(mtu, 10, &result) == 0)
173 			return result;
174 	}
175 	return DEFAULT_VECTOR_SIZE;
176 }
177 
get_headroom(struct arglist * def)178 static int get_headroom(struct arglist *def)
179 {
180 	char *mtu = uml_vector_fetch_arg(def, "headroom");
181 	long result;
182 
183 	if (mtu != NULL) {
184 		if (kstrtoul(mtu, 10, &result) == 0)
185 			return result;
186 	}
187 	return DEFAULT_HEADROOM;
188 }
189 
get_req_size(struct arglist * def)190 static int get_req_size(struct arglist *def)
191 {
192 	char *gro = uml_vector_fetch_arg(def, "gro");
193 	long result;
194 
195 	if (gro != NULL) {
196 		if (kstrtoul(gro, 10, &result) == 0) {
197 			if (result > 0)
198 				return 65536;
199 		}
200 	}
201 	return get_mtu(def) + ETH_HEADER_OTHER +
202 		get_headroom(def) + SAFETY_MARGIN;
203 }
204 
205 
get_transport_options(struct arglist * def)206 static int get_transport_options(struct arglist *def)
207 {
208 	char *transport = uml_vector_fetch_arg(def, "transport");
209 	char *vector = uml_vector_fetch_arg(def, "vec");
210 
211 	int vec_rx = VECTOR_RX;
212 	int vec_tx = VECTOR_TX;
213 	long parsed;
214 	int result = 0;
215 
216 	if (transport == NULL)
217 		return -EINVAL;
218 
219 	if (vector != NULL) {
220 		if (kstrtoul(vector, 10, &parsed) == 0) {
221 			if (parsed == 0) {
222 				vec_rx = 0;
223 				vec_tx = 0;
224 			}
225 		}
226 	}
227 
228 	if (get_bpf_flash(def))
229 		result = VECTOR_BPF_FLASH;
230 
231 	if (strncmp(transport, TRANS_TAP, TRANS_TAP_LEN) == 0)
232 		return result;
233 	if (strncmp(transport, TRANS_HYBRID, TRANS_HYBRID_LEN) == 0)
234 		return (result | vec_rx | VECTOR_BPF);
235 	if (strncmp(transport, TRANS_RAW, TRANS_RAW_LEN) == 0)
236 		return (result | vec_rx | vec_tx | VECTOR_QDISC_BYPASS);
237 	return (result | vec_rx | vec_tx);
238 }
239 
240 
241 /* A mini-buffer for packet drop read
242  * All of our supported transports are datagram oriented and we always
243  * read using recvmsg or recvmmsg. If we pass a buffer which is smaller
244  * than the packet size it still counts as full packet read and will
245  * clean the incoming stream to keep sigio/epoll happy
246  */
247 
248 #define DROP_BUFFER_SIZE 32
249 
250 static char *drop_buffer;
251 
252 
253 /*
254  * Advance the mmsg queue head by n = advance. Resets the queue to
255  * maximum enqueue/dequeue-at-once capacity if possible. Called by
256  * dequeuers. Caller must hold the head_lock!
257  */
258 
vector_advancehead(struct vector_queue * qi,int advance)259 static int vector_advancehead(struct vector_queue *qi, int advance)
260 {
261 	qi->head =
262 		(qi->head + advance)
263 			% qi->max_depth;
264 
265 
266 	atomic_sub(advance, &qi->queue_depth);
267 	return atomic_read(&qi->queue_depth);
268 }
269 
270 /*	Advance the queue tail by n = advance.
271  *	This is called by enqueuers which should hold the
272  *	head lock already
273  */
274 
vector_advancetail(struct vector_queue * qi,int advance)275 static int vector_advancetail(struct vector_queue *qi, int advance)
276 {
277 	qi->tail =
278 		(qi->tail + advance)
279 			% qi->max_depth;
280 	atomic_add(advance, &qi->queue_depth);
281 	return atomic_read(&qi->queue_depth);
282 }
283 
prep_msg(struct vector_private * vp,struct sk_buff * skb,struct iovec * iov)284 static int prep_msg(struct vector_private *vp,
285 	struct sk_buff *skb,
286 	struct iovec *iov)
287 {
288 	int iov_index = 0;
289 	int nr_frags, frag;
290 	skb_frag_t *skb_frag;
291 
292 	nr_frags = skb_shinfo(skb)->nr_frags;
293 	if (nr_frags > MAX_IOV_SIZE) {
294 		if (skb_linearize(skb) != 0)
295 			goto drop;
296 	}
297 	if (vp->header_size > 0) {
298 		iov[iov_index].iov_len = vp->header_size;
299 		vp->form_header(iov[iov_index].iov_base, skb, vp);
300 		iov_index++;
301 	}
302 	iov[iov_index].iov_base = skb->data;
303 	if (nr_frags > 0) {
304 		iov[iov_index].iov_len = skb->len - skb->data_len;
305 		vp->estats.sg_ok++;
306 	} else
307 		iov[iov_index].iov_len = skb->len;
308 	iov_index++;
309 	for (frag = 0; frag < nr_frags; frag++) {
310 		skb_frag = &skb_shinfo(skb)->frags[frag];
311 		iov[iov_index].iov_base = skb_frag_address_safe(skb_frag);
312 		iov[iov_index].iov_len = skb_frag_size(skb_frag);
313 		iov_index++;
314 	}
315 	return iov_index;
316 drop:
317 	return -1;
318 }
319 /*
320  * Generic vector enqueue with support for forming headers using transport
321  * specific callback. Allows GRE, L2TPv3, RAW and other transports
322  * to use a common enqueue procedure in vector mode
323  */
324 
vector_enqueue(struct vector_queue * qi,struct sk_buff * skb)325 static int vector_enqueue(struct vector_queue *qi, struct sk_buff *skb)
326 {
327 	struct vector_private *vp = netdev_priv(qi->dev);
328 	int queue_depth;
329 	int packet_len;
330 	struct mmsghdr *mmsg_vector = qi->mmsg_vector;
331 	int iov_count;
332 
333 	spin_lock(&qi->tail_lock);
334 	queue_depth = atomic_read(&qi->queue_depth);
335 
336 	if (skb)
337 		packet_len = skb->len;
338 
339 	if (queue_depth < qi->max_depth) {
340 
341 		*(qi->skbuff_vector + qi->tail) = skb;
342 		mmsg_vector += qi->tail;
343 		iov_count = prep_msg(
344 			vp,
345 			skb,
346 			mmsg_vector->msg_hdr.msg_iov
347 		);
348 		if (iov_count < 1)
349 			goto drop;
350 		mmsg_vector->msg_hdr.msg_iovlen = iov_count;
351 		mmsg_vector->msg_hdr.msg_name = vp->fds->remote_addr;
352 		mmsg_vector->msg_hdr.msg_namelen = vp->fds->remote_addr_size;
353 		wmb(); /* Make the packet visible to the NAPI poll thread */
354 		queue_depth = vector_advancetail(qi, 1);
355 	} else
356 		goto drop;
357 	spin_unlock(&qi->tail_lock);
358 	return queue_depth;
359 drop:
360 	qi->dev->stats.tx_dropped++;
361 	if (skb != NULL) {
362 		packet_len = skb->len;
363 		dev_consume_skb_any(skb);
364 		netdev_completed_queue(qi->dev, 1, packet_len);
365 	}
366 	spin_unlock(&qi->tail_lock);
367 	return queue_depth;
368 }
369 
consume_vector_skbs(struct vector_queue * qi,int count)370 static int consume_vector_skbs(struct vector_queue *qi, int count)
371 {
372 	struct sk_buff *skb;
373 	int skb_index;
374 	int bytes_compl = 0;
375 
376 	for (skb_index = qi->head; skb_index < qi->head + count; skb_index++) {
377 		skb = *(qi->skbuff_vector + skb_index);
378 		/* mark as empty to ensure correct destruction if
379 		 * needed
380 		 */
381 		bytes_compl += skb->len;
382 		*(qi->skbuff_vector + skb_index) = NULL;
383 		dev_consume_skb_any(skb);
384 	}
385 	qi->dev->stats.tx_bytes += bytes_compl;
386 	qi->dev->stats.tx_packets += count;
387 	netdev_completed_queue(qi->dev, count, bytes_compl);
388 	return vector_advancehead(qi, count);
389 }
390 
391 /*
392  * Generic vector dequeue via sendmmsg with support for forming headers
393  * using transport specific callback. Allows GRE, L2TPv3, RAW and
394  * other transports to use a common dequeue procedure in vector mode
395  */
396 
397 
vector_send(struct vector_queue * qi)398 static int vector_send(struct vector_queue *qi)
399 {
400 	struct vector_private *vp = netdev_priv(qi->dev);
401 	struct mmsghdr *send_from;
402 	int result = 0, send_len;
403 
404 	if (spin_trylock(&qi->head_lock)) {
405 		/* update queue_depth to current value */
406 		while (atomic_read(&qi->queue_depth) > 0) {
407 			/* Calculate the start of the vector */
408 			send_len = atomic_read(&qi->queue_depth);
409 			send_from = qi->mmsg_vector;
410 			send_from += qi->head;
411 			/* Adjust vector size if wraparound */
412 			if (send_len + qi->head > qi->max_depth)
413 				send_len = qi->max_depth - qi->head;
414 			/* Try to TX as many packets as possible */
415 			if (send_len > 0) {
416 				result = uml_vector_sendmmsg(
417 					 vp->fds->tx_fd,
418 					 send_from,
419 					 send_len,
420 					 0
421 				);
422 				vp->in_write_poll =
423 					(result != send_len);
424 			}
425 			/* For some of the sendmmsg error scenarios
426 			 * we may end being unsure in the TX success
427 			 * for all packets. It is safer to declare
428 			 * them all TX-ed and blame the network.
429 			 */
430 			if (result < 0) {
431 				if (net_ratelimit())
432 					netdev_err(vp->dev, "sendmmsg err=%i\n",
433 						result);
434 				vp->in_error = true;
435 				result = send_len;
436 			}
437 			if (result > 0) {
438 				consume_vector_skbs(qi, result);
439 				/* This is equivalent to an TX IRQ.
440 				 * Restart the upper layers to feed us
441 				 * more packets.
442 				 */
443 				if (result > vp->estats.tx_queue_max)
444 					vp->estats.tx_queue_max = result;
445 				vp->estats.tx_queue_running_average =
446 					(vp->estats.tx_queue_running_average + result) >> 1;
447 			}
448 			netif_wake_queue(qi->dev);
449 			/* if TX is busy, break out of the send loop,
450 			 *  poll write IRQ will reschedule xmit for us.
451 			 */
452 			if (result != send_len) {
453 				vp->estats.tx_restart_queue++;
454 				break;
455 			}
456 		}
457 		spin_unlock(&qi->head_lock);
458 	}
459 	return atomic_read(&qi->queue_depth);
460 }
461 
462 /* Queue destructor. Deliberately stateless so we can use
463  * it in queue cleanup if initialization fails.
464  */
465 
destroy_queue(struct vector_queue * qi)466 static void destroy_queue(struct vector_queue *qi)
467 {
468 	int i;
469 	struct iovec *iov;
470 	struct vector_private *vp = netdev_priv(qi->dev);
471 	struct mmsghdr *mmsg_vector;
472 
473 	if (qi == NULL)
474 		return;
475 	/* deallocate any skbuffs - we rely on any unused to be
476 	 * set to NULL.
477 	 */
478 	if (qi->skbuff_vector != NULL) {
479 		for (i = 0; i < qi->max_depth; i++) {
480 			if (*(qi->skbuff_vector + i) != NULL)
481 				dev_kfree_skb_any(*(qi->skbuff_vector + i));
482 		}
483 		kfree(qi->skbuff_vector);
484 	}
485 	/* deallocate matching IOV structures including header buffs */
486 	if (qi->mmsg_vector != NULL) {
487 		mmsg_vector = qi->mmsg_vector;
488 		for (i = 0; i < qi->max_depth; i++) {
489 			iov = mmsg_vector->msg_hdr.msg_iov;
490 			if (iov != NULL) {
491 				if ((vp->header_size > 0) &&
492 					(iov->iov_base != NULL))
493 					kfree(iov->iov_base);
494 				kfree(iov);
495 			}
496 			mmsg_vector++;
497 		}
498 		kfree(qi->mmsg_vector);
499 	}
500 	kfree(qi);
501 }
502 
503 /*
504  * Queue constructor. Create a queue with a given side.
505  */
create_queue(struct vector_private * vp,int max_size,int header_size,int num_extra_frags)506 static struct vector_queue *create_queue(
507 	struct vector_private *vp,
508 	int max_size,
509 	int header_size,
510 	int num_extra_frags)
511 {
512 	struct vector_queue *result;
513 	int i;
514 	struct iovec *iov;
515 	struct mmsghdr *mmsg_vector;
516 
517 	result = kmalloc(sizeof(struct vector_queue), GFP_KERNEL);
518 	if (result == NULL)
519 		return NULL;
520 	result->max_depth = max_size;
521 	result->dev = vp->dev;
522 	result->mmsg_vector = kmalloc(
523 		(sizeof(struct mmsghdr) * max_size), GFP_KERNEL);
524 	if (result->mmsg_vector == NULL)
525 		goto out_mmsg_fail;
526 	result->skbuff_vector = kmalloc(
527 		(sizeof(void *) * max_size), GFP_KERNEL);
528 	if (result->skbuff_vector == NULL)
529 		goto out_skb_fail;
530 
531 	/* further failures can be handled safely by destroy_queue*/
532 
533 	mmsg_vector = result->mmsg_vector;
534 	for (i = 0; i < max_size; i++) {
535 		/* Clear all pointers - we use non-NULL as marking on
536 		 * what to free on destruction
537 		 */
538 		*(result->skbuff_vector + i) = NULL;
539 		mmsg_vector->msg_hdr.msg_iov = NULL;
540 		mmsg_vector++;
541 	}
542 	mmsg_vector = result->mmsg_vector;
543 	result->max_iov_frags = num_extra_frags;
544 	for (i = 0; i < max_size; i++) {
545 		if (vp->header_size > 0)
546 			iov = kmalloc_array(3 + num_extra_frags,
547 					    sizeof(struct iovec),
548 					    GFP_KERNEL
549 			);
550 		else
551 			iov = kmalloc_array(2 + num_extra_frags,
552 					    sizeof(struct iovec),
553 					    GFP_KERNEL
554 			);
555 		if (iov == NULL)
556 			goto out_fail;
557 		mmsg_vector->msg_hdr.msg_iov = iov;
558 		mmsg_vector->msg_hdr.msg_iovlen = 1;
559 		mmsg_vector->msg_hdr.msg_control = NULL;
560 		mmsg_vector->msg_hdr.msg_controllen = 0;
561 		mmsg_vector->msg_hdr.msg_flags = MSG_DONTWAIT;
562 		mmsg_vector->msg_hdr.msg_name = NULL;
563 		mmsg_vector->msg_hdr.msg_namelen = 0;
564 		if (vp->header_size > 0) {
565 			iov->iov_base = kmalloc(header_size, GFP_KERNEL);
566 			if (iov->iov_base == NULL)
567 				goto out_fail;
568 			iov->iov_len = header_size;
569 			mmsg_vector->msg_hdr.msg_iovlen = 2;
570 			iov++;
571 		}
572 		iov->iov_base = NULL;
573 		iov->iov_len = 0;
574 		mmsg_vector++;
575 	}
576 	spin_lock_init(&result->head_lock);
577 	spin_lock_init(&result->tail_lock);
578 	atomic_set(&result->queue_depth, 0);
579 	result->head = 0;
580 	result->tail = 0;
581 	return result;
582 out_skb_fail:
583 	kfree(result->mmsg_vector);
584 out_mmsg_fail:
585 	kfree(result);
586 	return NULL;
587 out_fail:
588 	destroy_queue(result);
589 	return NULL;
590 }
591 
592 /*
593  * We do not use the RX queue as a proper wraparound queue for now
594  * This is not necessary because the consumption via napi_gro_receive()
595  * happens in-line. While we can try using the return code of
596  * netif_rx() for flow control there are no drivers doing this today.
597  * For this RX specific use we ignore the tail/head locks and
598  * just read into a prepared queue filled with skbuffs.
599  */
600 
prep_skb(struct vector_private * vp,struct user_msghdr * msg)601 static struct sk_buff *prep_skb(
602 	struct vector_private *vp,
603 	struct user_msghdr *msg)
604 {
605 	int linear = vp->max_packet + vp->headroom + SAFETY_MARGIN;
606 	struct sk_buff *result;
607 	int iov_index = 0, len;
608 	struct iovec *iov = msg->msg_iov;
609 	int err, nr_frags, frag;
610 	skb_frag_t *skb_frag;
611 
612 	if (vp->req_size <= linear)
613 		len = linear;
614 	else
615 		len = vp->req_size;
616 	result = alloc_skb_with_frags(
617 		linear,
618 		len - vp->max_packet,
619 		3,
620 		&err,
621 		GFP_ATOMIC
622 	);
623 	if (vp->header_size > 0)
624 		iov_index++;
625 	if (result == NULL) {
626 		iov[iov_index].iov_base = NULL;
627 		iov[iov_index].iov_len = 0;
628 		goto done;
629 	}
630 	skb_reserve(result, vp->headroom);
631 	result->dev = vp->dev;
632 	skb_put(result, vp->max_packet);
633 	result->data_len = len - vp->max_packet;
634 	result->len += len - vp->max_packet;
635 	skb_reset_mac_header(result);
636 	result->ip_summed = CHECKSUM_NONE;
637 	iov[iov_index].iov_base = result->data;
638 	iov[iov_index].iov_len = vp->max_packet;
639 	iov_index++;
640 
641 	nr_frags = skb_shinfo(result)->nr_frags;
642 	for (frag = 0; frag < nr_frags; frag++) {
643 		skb_frag = &skb_shinfo(result)->frags[frag];
644 		iov[iov_index].iov_base = skb_frag_address_safe(skb_frag);
645 		if (iov[iov_index].iov_base != NULL)
646 			iov[iov_index].iov_len = skb_frag_size(skb_frag);
647 		else
648 			iov[iov_index].iov_len = 0;
649 		iov_index++;
650 	}
651 done:
652 	msg->msg_iovlen = iov_index;
653 	return result;
654 }
655 
656 
657 /* Prepare queue for recvmmsg one-shot rx - fill with fresh sk_buffs */
658 
prep_queue_for_rx(struct vector_queue * qi)659 static void prep_queue_for_rx(struct vector_queue *qi)
660 {
661 	struct vector_private *vp = netdev_priv(qi->dev);
662 	struct mmsghdr *mmsg_vector = qi->mmsg_vector;
663 	void **skbuff_vector = qi->skbuff_vector;
664 	int i, queue_depth;
665 
666 	queue_depth = atomic_read(&qi->queue_depth);
667 
668 	if (queue_depth == 0)
669 		return;
670 
671 	/* RX is always emptied 100% during each cycle, so we do not
672 	 * have to do the tail wraparound math for it.
673 	 */
674 
675 	qi->head = qi->tail = 0;
676 
677 	for (i = 0; i < queue_depth; i++) {
678 		/* it is OK if allocation fails - recvmmsg with NULL data in
679 		 * iov argument still performs an RX, just drops the packet
680 		 * This allows us stop faffing around with a "drop buffer"
681 		 */
682 
683 		*skbuff_vector = prep_skb(vp, &mmsg_vector->msg_hdr);
684 		skbuff_vector++;
685 		mmsg_vector++;
686 	}
687 	atomic_set(&qi->queue_depth, 0);
688 }
689 
find_device(int n)690 static struct vector_device *find_device(int n)
691 {
692 	struct vector_device *device;
693 	struct list_head *ele;
694 
695 	spin_lock(&vector_devices_lock);
696 	list_for_each(ele, &vector_devices) {
697 		device = list_entry(ele, struct vector_device, list);
698 		if (device->unit == n)
699 			goto out;
700 	}
701 	device = NULL;
702  out:
703 	spin_unlock(&vector_devices_lock);
704 	return device;
705 }
706 
vector_parse(char * str,int * index_out,char ** str_out,char ** error_out)707 static int vector_parse(char *str, int *index_out, char **str_out,
708 			char **error_out)
709 {
710 	int n, err;
711 	char *start = str;
712 
713 	while ((*str != ':') && (strlen(str) > 1))
714 		str++;
715 	if (*str != ':') {
716 		*error_out = "Expected ':' after device number";
717 		return -EINVAL;
718 	}
719 	*str = '\0';
720 
721 	err = kstrtouint(start, 0, &n);
722 	if (err < 0) {
723 		*error_out = "Bad device number";
724 		return err;
725 	}
726 
727 	str++;
728 	if (find_device(n)) {
729 		*error_out = "Device already configured";
730 		return -EINVAL;
731 	}
732 
733 	*index_out = n;
734 	*str_out = str;
735 	return 0;
736 }
737 
vector_config(char * str,char ** error_out)738 static int vector_config(char *str, char **error_out)
739 {
740 	int err, n;
741 	char *params;
742 	struct arglist *parsed;
743 
744 	err = vector_parse(str, &n, &params, error_out);
745 	if (err != 0)
746 		return err;
747 
748 	/* This string is broken up and the pieces used by the underlying
749 	 * driver. We should copy it to make sure things do not go wrong
750 	 * later.
751 	 */
752 
753 	params = kstrdup(params, GFP_KERNEL);
754 	if (params == NULL) {
755 		*error_out = "vector_config failed to strdup string";
756 		return -ENOMEM;
757 	}
758 
759 	parsed = uml_parse_vector_ifspec(params);
760 
761 	if (parsed == NULL) {
762 		*error_out = "vector_config failed to parse parameters";
763 		kfree(params);
764 		return -EINVAL;
765 	}
766 
767 	vector_eth_configure(n, parsed);
768 	return 0;
769 }
770 
vector_id(char ** str,int * start_out,int * end_out)771 static int vector_id(char **str, int *start_out, int *end_out)
772 {
773 	char *end;
774 	int n;
775 
776 	n = simple_strtoul(*str, &end, 0);
777 	if ((*end != '\0') || (end == *str))
778 		return -1;
779 
780 	*start_out = n;
781 	*end_out = n;
782 	*str = end;
783 	return n;
784 }
785 
vector_remove(int n,char ** error_out)786 static int vector_remove(int n, char **error_out)
787 {
788 	struct vector_device *vec_d;
789 	struct net_device *dev;
790 	struct vector_private *vp;
791 
792 	vec_d = find_device(n);
793 	if (vec_d == NULL)
794 		return -ENODEV;
795 	dev = vec_d->dev;
796 	vp = netdev_priv(dev);
797 	if (vp->fds != NULL)
798 		return -EBUSY;
799 	unregister_netdev(dev);
800 	platform_device_unregister(&vec_d->pdev);
801 	return 0;
802 }
803 
804 /*
805  * There is no shared per-transport initialization code, so
806  * we will just initialize each interface one by one and
807  * add them to a list
808  */
809 
810 static struct platform_driver uml_net_driver = {
811 	.driver = {
812 		.name = DRIVER_NAME,
813 	},
814 };
815 
816 
vector_device_release(struct device * dev)817 static void vector_device_release(struct device *dev)
818 {
819 	struct vector_device *device =
820 		container_of(dev, struct vector_device, pdev.dev);
821 	struct net_device *netdev = device->dev;
822 
823 	list_del(&device->list);
824 	kfree(device);
825 	free_netdev(netdev);
826 }
827 
828 /* Bog standard recv using recvmsg - not used normally unless the user
829  * explicitly specifies not to use recvmmsg vector RX.
830  */
831 
vector_legacy_rx(struct vector_private * vp)832 static int vector_legacy_rx(struct vector_private *vp)
833 {
834 	int pkt_len;
835 	struct user_msghdr hdr;
836 	struct iovec iov[2 + MAX_IOV_SIZE]; /* header + data use case only */
837 	int iovpos = 0;
838 	struct sk_buff *skb;
839 	int header_check;
840 
841 	hdr.msg_name = NULL;
842 	hdr.msg_namelen = 0;
843 	hdr.msg_iov = (struct iovec *) &iov;
844 	hdr.msg_control = NULL;
845 	hdr.msg_controllen = 0;
846 	hdr.msg_flags = 0;
847 
848 	if (vp->header_size > 0) {
849 		iov[0].iov_base = vp->header_rxbuffer;
850 		iov[0].iov_len = vp->header_size;
851 	}
852 
853 	skb = prep_skb(vp, &hdr);
854 
855 	if (skb == NULL) {
856 		/* Read a packet into drop_buffer and don't do
857 		 * anything with it.
858 		 */
859 		iov[iovpos].iov_base = drop_buffer;
860 		iov[iovpos].iov_len = DROP_BUFFER_SIZE;
861 		hdr.msg_iovlen = 1;
862 		vp->dev->stats.rx_dropped++;
863 	}
864 
865 	pkt_len = uml_vector_recvmsg(vp->fds->rx_fd, &hdr, 0);
866 	if (pkt_len < 0) {
867 		vp->in_error = true;
868 		return pkt_len;
869 	}
870 
871 	if (skb != NULL) {
872 		if (pkt_len > vp->header_size) {
873 			if (vp->header_size > 0) {
874 				header_check = vp->verify_header(
875 					vp->header_rxbuffer, skb, vp);
876 				if (header_check < 0) {
877 					dev_kfree_skb_irq(skb);
878 					vp->dev->stats.rx_dropped++;
879 					vp->estats.rx_encaps_errors++;
880 					return 0;
881 				}
882 				if (header_check > 0) {
883 					vp->estats.rx_csum_offload_good++;
884 					skb->ip_summed = CHECKSUM_UNNECESSARY;
885 				}
886 			}
887 			pskb_trim(skb, pkt_len - vp->rx_header_size);
888 			skb->protocol = eth_type_trans(skb, skb->dev);
889 			vp->dev->stats.rx_bytes += skb->len;
890 			vp->dev->stats.rx_packets++;
891 			napi_gro_receive(&vp->napi, skb);
892 		} else {
893 			dev_kfree_skb_irq(skb);
894 		}
895 	}
896 	return pkt_len;
897 }
898 
899 /*
900  * Packet at a time TX which falls back to vector TX if the
901  * underlying transport is busy.
902  */
903 
904 
905 
writev_tx(struct vector_private * vp,struct sk_buff * skb)906 static int writev_tx(struct vector_private *vp, struct sk_buff *skb)
907 {
908 	struct iovec iov[3 + MAX_IOV_SIZE];
909 	int iov_count, pkt_len = 0;
910 
911 	iov[0].iov_base = vp->header_txbuffer;
912 	iov_count = prep_msg(vp, skb, (struct iovec *) &iov);
913 
914 	if (iov_count < 1)
915 		goto drop;
916 
917 	pkt_len = uml_vector_writev(
918 		vp->fds->tx_fd,
919 		(struct iovec *) &iov,
920 		iov_count
921 	);
922 
923 	if (pkt_len < 0)
924 		goto drop;
925 
926 	netif_trans_update(vp->dev);
927 	netif_wake_queue(vp->dev);
928 
929 	if (pkt_len > 0) {
930 		vp->dev->stats.tx_bytes += skb->len;
931 		vp->dev->stats.tx_packets++;
932 	} else {
933 		vp->dev->stats.tx_dropped++;
934 	}
935 	consume_skb(skb);
936 	return pkt_len;
937 drop:
938 	vp->dev->stats.tx_dropped++;
939 	consume_skb(skb);
940 	if (pkt_len < 0)
941 		vp->in_error = true;
942 	return pkt_len;
943 }
944 
945 /*
946  * Receive as many messages as we can in one call using the special
947  * mmsg vector matched to an skb vector which we prepared earlier.
948  */
949 
vector_mmsg_rx(struct vector_private * vp,int budget)950 static int vector_mmsg_rx(struct vector_private *vp, int budget)
951 {
952 	int packet_count, i;
953 	struct vector_queue *qi = vp->rx_queue;
954 	struct sk_buff *skb;
955 	struct mmsghdr *mmsg_vector = qi->mmsg_vector;
956 	void **skbuff_vector = qi->skbuff_vector;
957 	int header_check;
958 
959 	/* Refresh the vector and make sure it is with new skbs and the
960 	 * iovs are updated to point to them.
961 	 */
962 
963 	prep_queue_for_rx(qi);
964 
965 	/* Fire the Lazy Gun - get as many packets as we can in one go. */
966 
967 	if (budget > qi->max_depth)
968 		budget = qi->max_depth;
969 
970 	packet_count = uml_vector_recvmmsg(
971 		vp->fds->rx_fd, qi->mmsg_vector, budget, 0);
972 
973 	if (packet_count < 0)
974 		vp->in_error = true;
975 
976 	if (packet_count <= 0)
977 		return packet_count;
978 
979 	/* We treat packet processing as enqueue, buffer refresh as dequeue
980 	 * The queue_depth tells us how many buffers have been used and how
981 	 * many do we need to prep the next time prep_queue_for_rx() is called.
982 	 */
983 
984 	atomic_add(packet_count, &qi->queue_depth);
985 
986 	for (i = 0; i < packet_count; i++) {
987 		skb = (*skbuff_vector);
988 		if (mmsg_vector->msg_len > vp->header_size) {
989 			if (vp->header_size > 0) {
990 				header_check = vp->verify_header(
991 					mmsg_vector->msg_hdr.msg_iov->iov_base,
992 					skb,
993 					vp
994 				);
995 				if (header_check < 0) {
996 				/* Overlay header failed to verify - discard.
997 				 * We can actually keep this skb and reuse it,
998 				 * but that will make the prep logic too
999 				 * complex.
1000 				 */
1001 					dev_kfree_skb_irq(skb);
1002 					vp->estats.rx_encaps_errors++;
1003 					continue;
1004 				}
1005 				if (header_check > 0) {
1006 					vp->estats.rx_csum_offload_good++;
1007 					skb->ip_summed = CHECKSUM_UNNECESSARY;
1008 				}
1009 			}
1010 			pskb_trim(skb,
1011 				mmsg_vector->msg_len - vp->rx_header_size);
1012 			skb->protocol = eth_type_trans(skb, skb->dev);
1013 			/*
1014 			 * We do not need to lock on updating stats here
1015 			 * The interrupt loop is non-reentrant.
1016 			 */
1017 			vp->dev->stats.rx_bytes += skb->len;
1018 			vp->dev->stats.rx_packets++;
1019 			napi_gro_receive(&vp->napi, skb);
1020 		} else {
1021 			/* Overlay header too short to do anything - discard.
1022 			 * We can actually keep this skb and reuse it,
1023 			 * but that will make the prep logic too complex.
1024 			 */
1025 			if (skb != NULL)
1026 				dev_kfree_skb_irq(skb);
1027 		}
1028 		(*skbuff_vector) = NULL;
1029 		/* Move to the next buffer element */
1030 		mmsg_vector++;
1031 		skbuff_vector++;
1032 	}
1033 	if (packet_count > 0) {
1034 		if (vp->estats.rx_queue_max < packet_count)
1035 			vp->estats.rx_queue_max = packet_count;
1036 		vp->estats.rx_queue_running_average =
1037 			(vp->estats.rx_queue_running_average + packet_count) >> 1;
1038 	}
1039 	return packet_count;
1040 }
1041 
vector_net_start_xmit(struct sk_buff * skb,struct net_device * dev)1042 static int vector_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
1043 {
1044 	struct vector_private *vp = netdev_priv(dev);
1045 	int queue_depth = 0;
1046 
1047 	if (vp->in_error) {
1048 		deactivate_fd(vp->fds->rx_fd, vp->rx_irq);
1049 		if ((vp->fds->rx_fd != vp->fds->tx_fd) && (vp->tx_irq != 0))
1050 			deactivate_fd(vp->fds->tx_fd, vp->tx_irq);
1051 		return NETDEV_TX_BUSY;
1052 	}
1053 
1054 	if ((vp->options & VECTOR_TX) == 0) {
1055 		writev_tx(vp, skb);
1056 		return NETDEV_TX_OK;
1057 	}
1058 
1059 	/* We do BQL only in the vector path, no point doing it in
1060 	 * packet at a time mode as there is no device queue
1061 	 */
1062 
1063 	netdev_sent_queue(vp->dev, skb->len);
1064 	queue_depth = vector_enqueue(vp->tx_queue, skb);
1065 
1066 	if (queue_depth < vp->tx_queue->max_depth && netdev_xmit_more()) {
1067 		mod_timer(&vp->tl, vp->coalesce);
1068 		return NETDEV_TX_OK;
1069 	} else {
1070 		queue_depth = vector_send(vp->tx_queue);
1071 		if (queue_depth > 0)
1072 			napi_schedule(&vp->napi);
1073 	}
1074 
1075 	return NETDEV_TX_OK;
1076 }
1077 
vector_rx_interrupt(int irq,void * dev_id)1078 static irqreturn_t vector_rx_interrupt(int irq, void *dev_id)
1079 {
1080 	struct net_device *dev = dev_id;
1081 	struct vector_private *vp = netdev_priv(dev);
1082 
1083 	if (!netif_running(dev))
1084 		return IRQ_NONE;
1085 	napi_schedule(&vp->napi);
1086 	return IRQ_HANDLED;
1087 
1088 }
1089 
vector_tx_interrupt(int irq,void * dev_id)1090 static irqreturn_t vector_tx_interrupt(int irq, void *dev_id)
1091 {
1092 	struct net_device *dev = dev_id;
1093 	struct vector_private *vp = netdev_priv(dev);
1094 
1095 	if (!netif_running(dev))
1096 		return IRQ_NONE;
1097 	/* We need to pay attention to it only if we got
1098 	 * -EAGAIN or -ENOBUFFS from sendmmsg. Otherwise
1099 	 * we ignore it. In the future, it may be worth
1100 	 * it to improve the IRQ controller a bit to make
1101 	 * tweaking the IRQ mask less costly
1102 	 */
1103 
1104 	napi_schedule(&vp->napi);
1105 	return IRQ_HANDLED;
1106 
1107 }
1108 
1109 static int irq_rr;
1110 
vector_net_close(struct net_device * dev)1111 static int vector_net_close(struct net_device *dev)
1112 {
1113 	struct vector_private *vp = netdev_priv(dev);
1114 
1115 	netif_stop_queue(dev);
1116 	timer_delete(&vp->tl);
1117 
1118 	vp->opened = false;
1119 
1120 	if (vp->fds == NULL)
1121 		return 0;
1122 
1123 	/* Disable and free all IRQS */
1124 	if (vp->rx_irq > 0) {
1125 		um_free_irq(vp->rx_irq, dev);
1126 		vp->rx_irq = 0;
1127 	}
1128 	if (vp->tx_irq > 0) {
1129 		um_free_irq(vp->tx_irq, dev);
1130 		vp->tx_irq = 0;
1131 	}
1132 	napi_disable(&vp->napi);
1133 	netif_napi_del(&vp->napi);
1134 	if (vp->fds->rx_fd > 0) {
1135 		if (vp->bpf)
1136 			uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
1137 		os_close_file(vp->fds->rx_fd);
1138 		vp->fds->rx_fd = -1;
1139 	}
1140 	if (vp->fds->tx_fd > 0) {
1141 		os_close_file(vp->fds->tx_fd);
1142 		vp->fds->tx_fd = -1;
1143 	}
1144 	if (vp->bpf != NULL)
1145 		kfree(vp->bpf->filter);
1146 	kfree(vp->bpf);
1147 	vp->bpf = NULL;
1148 	kfree(vp->fds->remote_addr);
1149 	kfree(vp->transport_data);
1150 	kfree(vp->header_rxbuffer);
1151 	kfree(vp->header_txbuffer);
1152 	if (vp->rx_queue != NULL)
1153 		destroy_queue(vp->rx_queue);
1154 	if (vp->tx_queue != NULL)
1155 		destroy_queue(vp->tx_queue);
1156 	kfree(vp->fds);
1157 	vp->fds = NULL;
1158 	vp->in_error = false;
1159 	return 0;
1160 }
1161 
vector_poll(struct napi_struct * napi,int budget)1162 static int vector_poll(struct napi_struct *napi, int budget)
1163 {
1164 	struct vector_private *vp = container_of(napi, struct vector_private, napi);
1165 	int work_done = 0;
1166 	int err;
1167 	bool tx_enqueued = false;
1168 
1169 	if ((vp->options & VECTOR_TX) != 0)
1170 		tx_enqueued = (vector_send(vp->tx_queue) > 0);
1171 	spin_lock(&vp->rx_queue->head_lock);
1172 	if ((vp->options & VECTOR_RX) > 0)
1173 		err = vector_mmsg_rx(vp, budget);
1174 	else {
1175 		err = vector_legacy_rx(vp);
1176 		if (err > 0)
1177 			err = 1;
1178 	}
1179 	spin_unlock(&vp->rx_queue->head_lock);
1180 	if (err > 0)
1181 		work_done += err;
1182 
1183 	if (tx_enqueued || err > 0)
1184 		napi_schedule(napi);
1185 	if (work_done <= budget)
1186 		napi_complete_done(napi, work_done);
1187 	return work_done;
1188 }
1189 
vector_reset_tx(struct work_struct * work)1190 static void vector_reset_tx(struct work_struct *work)
1191 {
1192 	struct vector_private *vp =
1193 		container_of(work, struct vector_private, reset_tx);
1194 	netdev_reset_queue(vp->dev);
1195 	netif_start_queue(vp->dev);
1196 	netif_wake_queue(vp->dev);
1197 }
1198 
vector_net_open(struct net_device * dev)1199 static int vector_net_open(struct net_device *dev)
1200 {
1201 	struct vector_private *vp = netdev_priv(dev);
1202 	int err = -EINVAL;
1203 	struct vector_device *vdevice;
1204 
1205 	if (vp->opened)
1206 		return -ENXIO;
1207 	vp->opened = true;
1208 
1209 	vp->bpf = uml_vector_user_bpf(get_bpf_file(vp->parsed));
1210 
1211 	vp->fds = uml_vector_user_open(vp->unit, vp->parsed);
1212 
1213 	if (vp->fds == NULL)
1214 		goto out_close;
1215 
1216 	if (build_transport_data(vp) < 0)
1217 		goto out_close;
1218 
1219 	if ((vp->options & VECTOR_RX) > 0) {
1220 		vp->rx_queue = create_queue(
1221 			vp,
1222 			get_depth(vp->parsed),
1223 			vp->rx_header_size,
1224 			MAX_IOV_SIZE
1225 		);
1226 		atomic_set(&vp->rx_queue->queue_depth, get_depth(vp->parsed));
1227 	} else {
1228 		vp->header_rxbuffer = kmalloc(
1229 			vp->rx_header_size,
1230 			GFP_KERNEL
1231 		);
1232 		if (vp->header_rxbuffer == NULL)
1233 			goto out_close;
1234 	}
1235 	if ((vp->options & VECTOR_TX) > 0) {
1236 		vp->tx_queue = create_queue(
1237 			vp,
1238 			get_depth(vp->parsed),
1239 			vp->header_size,
1240 			MAX_IOV_SIZE
1241 		);
1242 	} else {
1243 		vp->header_txbuffer = kmalloc(vp->header_size, GFP_KERNEL);
1244 		if (vp->header_txbuffer == NULL)
1245 			goto out_close;
1246 	}
1247 
1248 	netif_napi_add_weight(vp->dev, &vp->napi, vector_poll,
1249 			      get_depth(vp->parsed));
1250 	napi_enable(&vp->napi);
1251 
1252 	/* READ IRQ */
1253 	err = um_request_irq(
1254 		irq_rr + VECTOR_BASE_IRQ, vp->fds->rx_fd,
1255 			IRQ_READ, vector_rx_interrupt,
1256 			IRQF_SHARED, dev->name, dev);
1257 	if (err < 0) {
1258 		netdev_err(dev, "vector_open: failed to get rx irq(%d)\n", err);
1259 		err = -ENETUNREACH;
1260 		goto out_close;
1261 	}
1262 	vp->rx_irq = irq_rr + VECTOR_BASE_IRQ;
1263 	dev->irq = irq_rr + VECTOR_BASE_IRQ;
1264 	irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
1265 
1266 	/* WRITE IRQ - we need it only if we have vector TX */
1267 	if ((vp->options & VECTOR_TX) > 0) {
1268 		err = um_request_irq(
1269 			irq_rr + VECTOR_BASE_IRQ, vp->fds->tx_fd,
1270 				IRQ_WRITE, vector_tx_interrupt,
1271 				IRQF_SHARED, dev->name, dev);
1272 		if (err < 0) {
1273 			netdev_err(dev,
1274 				"vector_open: failed to get tx irq(%d)\n", err);
1275 			err = -ENETUNREACH;
1276 			goto out_close;
1277 		}
1278 		vp->tx_irq = irq_rr + VECTOR_BASE_IRQ;
1279 		irq_rr = (irq_rr + 1) % VECTOR_IRQ_SPACE;
1280 	}
1281 
1282 	if ((vp->options & VECTOR_QDISC_BYPASS) != 0) {
1283 		if (!uml_raw_enable_qdisc_bypass(vp->fds->rx_fd))
1284 			vp->options |= VECTOR_BPF;
1285 	}
1286 	if (((vp->options & VECTOR_BPF) != 0) && (vp->bpf == NULL))
1287 		vp->bpf = uml_vector_default_bpf(dev->dev_addr);
1288 
1289 	if (vp->bpf != NULL)
1290 		uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
1291 
1292 	netif_start_queue(dev);
1293 	vector_reset_stats(vp);
1294 
1295 	/* clear buffer - it can happen that the host side of the interface
1296 	 * is full when we get here. In this case, new data is never queued,
1297 	 * SIGIOs never arrive, and the net never works.
1298 	 */
1299 
1300 	napi_schedule(&vp->napi);
1301 
1302 	vdevice = find_device(vp->unit);
1303 	vdevice->opened = 1;
1304 
1305 	if ((vp->options & VECTOR_TX) != 0)
1306 		add_timer(&vp->tl);
1307 	return 0;
1308 out_close:
1309 	vector_net_close(dev);
1310 	return err;
1311 }
1312 
1313 
vector_net_set_multicast_list(struct net_device * dev)1314 static void vector_net_set_multicast_list(struct net_device *dev)
1315 {
1316 	/* TODO: - we can do some BPF games here */
1317 	return;
1318 }
1319 
vector_net_tx_timeout(struct net_device * dev,unsigned int txqueue)1320 static void vector_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
1321 {
1322 	struct vector_private *vp = netdev_priv(dev);
1323 
1324 	vp->estats.tx_timeout_count++;
1325 	netif_trans_update(dev);
1326 	schedule_work(&vp->reset_tx);
1327 }
1328 
vector_fix_features(struct net_device * dev,netdev_features_t features)1329 static netdev_features_t vector_fix_features(struct net_device *dev,
1330 	netdev_features_t features)
1331 {
1332 	features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
1333 	return features;
1334 }
1335 
vector_set_features(struct net_device * dev,netdev_features_t features)1336 static int vector_set_features(struct net_device *dev,
1337 	netdev_features_t features)
1338 {
1339 	struct vector_private *vp = netdev_priv(dev);
1340 	/* Adjust buffer sizes for GSO/GRO. Unfortunately, there is
1341 	 * no way to negotiate it on raw sockets, so we can change
1342 	 * only our side.
1343 	 */
1344 	if (features & NETIF_F_GRO)
1345 		/* All new frame buffers will be GRO-sized */
1346 		vp->req_size = 65536;
1347 	else
1348 		/* All new frame buffers will be normal sized */
1349 		vp->req_size = vp->max_packet + vp->headroom + SAFETY_MARGIN;
1350 	return 0;
1351 }
1352 
1353 #ifdef CONFIG_NET_POLL_CONTROLLER
vector_net_poll_controller(struct net_device * dev)1354 static void vector_net_poll_controller(struct net_device *dev)
1355 {
1356 	disable_irq(dev->irq);
1357 	vector_rx_interrupt(dev->irq, dev);
1358 	enable_irq(dev->irq);
1359 }
1360 #endif
1361 
vector_net_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)1362 static void vector_net_get_drvinfo(struct net_device *dev,
1363 				struct ethtool_drvinfo *info)
1364 {
1365 	strscpy(info->driver, DRIVER_NAME);
1366 }
1367 
vector_net_load_bpf_flash(struct net_device * dev,struct ethtool_flash * efl)1368 static int vector_net_load_bpf_flash(struct net_device *dev,
1369 				struct ethtool_flash *efl)
1370 {
1371 	struct vector_private *vp = netdev_priv(dev);
1372 	struct vector_device *vdevice;
1373 	const struct firmware *fw;
1374 	int result = 0;
1375 
1376 	if (!(vp->options & VECTOR_BPF_FLASH)) {
1377 		netdev_err(dev, "loading firmware not permitted: %s\n", efl->data);
1378 		return -1;
1379 	}
1380 
1381 	if (vp->bpf != NULL) {
1382 		if (vp->opened)
1383 			uml_vector_detach_bpf(vp->fds->rx_fd, vp->bpf);
1384 		kfree(vp->bpf->filter);
1385 		vp->bpf->filter = NULL;
1386 	} else {
1387 		vp->bpf = kmalloc(sizeof(struct sock_fprog), GFP_ATOMIC);
1388 		if (vp->bpf == NULL) {
1389 			netdev_err(dev, "failed to allocate memory for firmware\n");
1390 			goto flash_fail;
1391 		}
1392 	}
1393 
1394 	vdevice = find_device(vp->unit);
1395 
1396 	if (request_firmware(&fw, efl->data, &vdevice->pdev.dev))
1397 		goto flash_fail;
1398 
1399 	vp->bpf->filter = kmemdup(fw->data, fw->size, GFP_ATOMIC);
1400 	if (!vp->bpf->filter)
1401 		goto free_buffer;
1402 
1403 	vp->bpf->len = fw->size / sizeof(struct sock_filter);
1404 	release_firmware(fw);
1405 
1406 	if (vp->opened)
1407 		result = uml_vector_attach_bpf(vp->fds->rx_fd, vp->bpf);
1408 
1409 	return result;
1410 
1411 free_buffer:
1412 	release_firmware(fw);
1413 
1414 flash_fail:
1415 	if (vp->bpf != NULL)
1416 		kfree(vp->bpf->filter);
1417 	kfree(vp->bpf);
1418 	vp->bpf = NULL;
1419 	return -1;
1420 }
1421 
vector_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)1422 static void vector_get_ringparam(struct net_device *netdev,
1423 				 struct ethtool_ringparam *ring,
1424 				 struct kernel_ethtool_ringparam *kernel_ring,
1425 				 struct netlink_ext_ack *extack)
1426 {
1427 	struct vector_private *vp = netdev_priv(netdev);
1428 
1429 	ring->rx_max_pending = vp->rx_queue->max_depth;
1430 	ring->tx_max_pending = vp->tx_queue->max_depth;
1431 	ring->rx_pending = vp->rx_queue->max_depth;
1432 	ring->tx_pending = vp->tx_queue->max_depth;
1433 }
1434 
vector_get_strings(struct net_device * dev,u32 stringset,u8 * buf)1435 static void vector_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
1436 {
1437 	switch (stringset) {
1438 	case ETH_SS_TEST:
1439 		*buf = '\0';
1440 		break;
1441 	case ETH_SS_STATS:
1442 		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
1443 		break;
1444 	default:
1445 		WARN_ON(1);
1446 		break;
1447 	}
1448 }
1449 
vector_get_sset_count(struct net_device * dev,int sset)1450 static int vector_get_sset_count(struct net_device *dev, int sset)
1451 {
1452 	switch (sset) {
1453 	case ETH_SS_TEST:
1454 		return 0;
1455 	case ETH_SS_STATS:
1456 		return VECTOR_NUM_STATS;
1457 	default:
1458 		return -EOPNOTSUPP;
1459 	}
1460 }
1461 
vector_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * estats,u64 * tmp_stats)1462 static void vector_get_ethtool_stats(struct net_device *dev,
1463 	struct ethtool_stats *estats,
1464 	u64 *tmp_stats)
1465 {
1466 	struct vector_private *vp = netdev_priv(dev);
1467 
1468 	/* Stats are modified in the dequeue portions of
1469 	 * rx/tx which are protected by the head locks
1470 	 * grabbing these locks here ensures they are up
1471 	 * to date.
1472 	 */
1473 
1474 	spin_lock(&vp->tx_queue->head_lock);
1475 	spin_lock(&vp->rx_queue->head_lock);
1476 	memcpy(tmp_stats, &vp->estats, sizeof(struct vector_estats));
1477 	spin_unlock(&vp->rx_queue->head_lock);
1478 	spin_unlock(&vp->tx_queue->head_lock);
1479 }
1480 
vector_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1481 static int vector_get_coalesce(struct net_device *netdev,
1482 			       struct ethtool_coalesce *ec,
1483 			       struct kernel_ethtool_coalesce *kernel_coal,
1484 			       struct netlink_ext_ack *extack)
1485 {
1486 	struct vector_private *vp = netdev_priv(netdev);
1487 
1488 	ec->tx_coalesce_usecs = (vp->coalesce * 1000000) / HZ;
1489 	return 0;
1490 }
1491 
vector_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1492 static int vector_set_coalesce(struct net_device *netdev,
1493 			       struct ethtool_coalesce *ec,
1494 			       struct kernel_ethtool_coalesce *kernel_coal,
1495 			       struct netlink_ext_ack *extack)
1496 {
1497 	struct vector_private *vp = netdev_priv(netdev);
1498 
1499 	vp->coalesce = (ec->tx_coalesce_usecs * HZ) / 1000000;
1500 	if (vp->coalesce == 0)
1501 		vp->coalesce = 1;
1502 	return 0;
1503 }
1504 
1505 static const struct ethtool_ops vector_net_ethtool_ops = {
1506 	.supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS,
1507 	.get_drvinfo	= vector_net_get_drvinfo,
1508 	.get_link	= ethtool_op_get_link,
1509 	.get_ts_info	= ethtool_op_get_ts_info,
1510 	.get_ringparam	= vector_get_ringparam,
1511 	.get_strings	= vector_get_strings,
1512 	.get_sset_count	= vector_get_sset_count,
1513 	.get_ethtool_stats = vector_get_ethtool_stats,
1514 	.get_coalesce	= vector_get_coalesce,
1515 	.set_coalesce	= vector_set_coalesce,
1516 	.flash_device	= vector_net_load_bpf_flash,
1517 };
1518 
1519 
1520 static const struct net_device_ops vector_netdev_ops = {
1521 	.ndo_open		= vector_net_open,
1522 	.ndo_stop		= vector_net_close,
1523 	.ndo_start_xmit		= vector_net_start_xmit,
1524 	.ndo_set_rx_mode	= vector_net_set_multicast_list,
1525 	.ndo_tx_timeout		= vector_net_tx_timeout,
1526 	.ndo_set_mac_address	= eth_mac_addr,
1527 	.ndo_validate_addr	= eth_validate_addr,
1528 	.ndo_fix_features	= vector_fix_features,
1529 	.ndo_set_features	= vector_set_features,
1530 #ifdef CONFIG_NET_POLL_CONTROLLER
1531 	.ndo_poll_controller = vector_net_poll_controller,
1532 #endif
1533 };
1534 
vector_timer_expire(struct timer_list * t)1535 static void vector_timer_expire(struct timer_list *t)
1536 {
1537 	struct vector_private *vp = timer_container_of(vp, t, tl);
1538 
1539 	vp->estats.tx_kicks++;
1540 	napi_schedule(&vp->napi);
1541 }
1542 
vector_setup_etheraddr(struct net_device * dev,char * str)1543 static void vector_setup_etheraddr(struct net_device *dev, char *str)
1544 {
1545 	u8 addr[ETH_ALEN];
1546 
1547 	if (str == NULL)
1548 		goto random;
1549 
1550 	if (!mac_pton(str, addr)) {
1551 		netdev_err(dev,
1552 			"Failed to parse '%s' as an ethernet address\n", str);
1553 		goto random;
1554 	}
1555 	if (is_multicast_ether_addr(addr)) {
1556 		netdev_err(dev,
1557 			"Attempt to assign a multicast ethernet address to a device disallowed\n");
1558 		goto random;
1559 	}
1560 	if (!is_valid_ether_addr(addr)) {
1561 		netdev_err(dev,
1562 			"Attempt to assign an invalid ethernet address to a device disallowed\n");
1563 		goto random;
1564 	}
1565 	if (!is_local_ether_addr(addr)) {
1566 		netdev_warn(dev, "Warning: Assigning a globally valid ethernet address to a device\n");
1567 		netdev_warn(dev, "You should set the 2nd rightmost bit in the first byte of the MAC,\n");
1568 		netdev_warn(dev, "i.e. %02x:%02x:%02x:%02x:%02x:%02x\n",
1569 			addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4], addr[5]);
1570 	}
1571 	eth_hw_addr_set(dev, addr);
1572 	return;
1573 
1574 random:
1575 	netdev_info(dev, "Choosing a random ethernet address\n");
1576 	eth_hw_addr_random(dev);
1577 }
1578 
vector_eth_configure(int n,struct arglist * def)1579 static void vector_eth_configure(
1580 		int n,
1581 		struct arglist *def
1582 	)
1583 {
1584 	struct vector_device *device;
1585 	struct net_device *dev;
1586 	struct vector_private *vp;
1587 	int err;
1588 
1589 	device = kzalloc(sizeof(*device), GFP_KERNEL);
1590 	if (device == NULL) {
1591 		pr_err("Failed to allocate struct vector_device for vec%d\n", n);
1592 		return;
1593 	}
1594 	dev = alloc_etherdev(sizeof(struct vector_private));
1595 	if (dev == NULL) {
1596 		pr_err("Failed to allocate struct net_device for vec%d\n", n);
1597 		goto out_free_device;
1598 	}
1599 
1600 	dev->mtu = get_mtu(def);
1601 
1602 	INIT_LIST_HEAD(&device->list);
1603 	device->unit = n;
1604 
1605 	/* If this name ends up conflicting with an existing registered
1606 	 * netdevice, that is OK, register_netdev{,ice}() will notice this
1607 	 * and fail.
1608 	 */
1609 	snprintf(dev->name, sizeof(dev->name), "vec%d", n);
1610 	vector_setup_etheraddr(dev, uml_vector_fetch_arg(def, "mac"));
1611 	vp = netdev_priv(dev);
1612 
1613 	/* sysfs register */
1614 	if (!driver_registered) {
1615 		platform_driver_register(&uml_net_driver);
1616 		driver_registered = 1;
1617 	}
1618 	device->pdev.id = n;
1619 	device->pdev.name = DRIVER_NAME;
1620 	device->pdev.dev.release = vector_device_release;
1621 	dev_set_drvdata(&device->pdev.dev, device);
1622 	if (platform_device_register(&device->pdev))
1623 		goto out_free_netdev;
1624 	SET_NETDEV_DEV(dev, &device->pdev.dev);
1625 
1626 	device->dev = dev;
1627 
1628 	INIT_LIST_HEAD(&vp->list);
1629 	vp->dev		= dev;
1630 	vp->unit	= n;
1631 	vp->options	= get_transport_options(def);
1632 	vp->parsed	= def;
1633 	vp->max_packet	= get_mtu(def) + ETH_HEADER_OTHER;
1634 	/*
1635 	 * TODO - we need to calculate headroom so that ip header
1636 	 * is 16 byte aligned all the time
1637 	 */
1638 	vp->headroom	= get_headroom(def);
1639 	vp->coalesce	= 2;
1640 	vp->req_size	= get_req_size(def);
1641 
1642 	dev->features = dev->hw_features = (NETIF_F_SG | NETIF_F_FRAGLIST);
1643 	INIT_WORK(&vp->reset_tx, vector_reset_tx);
1644 
1645 	timer_setup(&vp->tl, vector_timer_expire, 0);
1646 
1647 	/* FIXME */
1648 	dev->netdev_ops = &vector_netdev_ops;
1649 	dev->ethtool_ops = &vector_net_ethtool_ops;
1650 	dev->watchdog_timeo = (HZ >> 1);
1651 	/* primary IRQ - fixme */
1652 	dev->irq = 0; /* we will adjust this once opened */
1653 
1654 	rtnl_lock();
1655 	err = register_netdevice(dev);
1656 	rtnl_unlock();
1657 	if (err)
1658 		goto out_undo_user_init;
1659 
1660 	spin_lock(&vector_devices_lock);
1661 	list_add(&device->list, &vector_devices);
1662 	spin_unlock(&vector_devices_lock);
1663 
1664 	return;
1665 
1666 out_undo_user_init:
1667 	return;
1668 out_free_netdev:
1669 	free_netdev(dev);
1670 out_free_device:
1671 	kfree(device);
1672 }
1673 
1674 
1675 
1676 
1677 /*
1678  * Invoked late in the init
1679  */
1680 
vector_init(void)1681 static int __init vector_init(void)
1682 {
1683 	struct list_head *ele;
1684 	struct vector_cmd_line_arg *def;
1685 	struct arglist *parsed;
1686 
1687 	list_for_each(ele, &vec_cmd_line) {
1688 		def = list_entry(ele, struct vector_cmd_line_arg, list);
1689 		parsed = uml_parse_vector_ifspec(def->arguments);
1690 		if (parsed != NULL)
1691 			vector_eth_configure(def->unit, parsed);
1692 	}
1693 	return 0;
1694 }
1695 
1696 
1697 /* Invoked at initial argument parsing, only stores
1698  * arguments until a proper vector_init is called
1699  * later
1700  */
1701 
vector_setup(char * str)1702 static int __init vector_setup(char *str)
1703 {
1704 	char *error;
1705 	int n, err;
1706 	struct vector_cmd_line_arg *new;
1707 
1708 	err = vector_parse(str, &n, &str, &error);
1709 	if (err) {
1710 		pr_err("Couldn't parse '%s': %s\n", str, error);
1711 		return 1;
1712 	}
1713 	new = memblock_alloc_or_panic(sizeof(*new), SMP_CACHE_BYTES);
1714 	INIT_LIST_HEAD(&new->list);
1715 	new->unit = n;
1716 	new->arguments = str;
1717 	list_add_tail(&new->list, &vec_cmd_line);
1718 	return 1;
1719 }
1720 
1721 __setup("vec", vector_setup);
1722 __uml_help(vector_setup,
1723 "vec[0-9]+:<option>=<value>,<option>=<value>\n"
1724 "	 Configure a vector io network device.\n\n"
1725 );
1726 
1727 late_initcall(vector_init);
1728 
1729 static struct mc_device vector_mc = {
1730 	.list		= LIST_HEAD_INIT(vector_mc.list),
1731 	.name		= "vec",
1732 	.config		= vector_config,
1733 	.get_config	= NULL,
1734 	.id		= vector_id,
1735 	.remove		= vector_remove,
1736 };
1737 
1738 #ifdef CONFIG_INET
vector_inetaddr_event(struct notifier_block * this,unsigned long event,void * ptr)1739 static int vector_inetaddr_event(
1740 	struct notifier_block *this,
1741 	unsigned long event,
1742 	void *ptr)
1743 {
1744 	return NOTIFY_DONE;
1745 }
1746 
1747 static struct notifier_block vector_inetaddr_notifier = {
1748 	.notifier_call		= vector_inetaddr_event,
1749 };
1750 
inet_register(void)1751 static void inet_register(void)
1752 {
1753 	register_inetaddr_notifier(&vector_inetaddr_notifier);
1754 }
1755 #else
inet_register(void)1756 static inline void inet_register(void)
1757 {
1758 }
1759 #endif
1760 
vector_net_init(void)1761 static int vector_net_init(void)
1762 {
1763 	mconsole_register_dev(&vector_mc);
1764 	inet_register();
1765 	return 0;
1766 }
1767 
1768 __initcall(vector_net_init);
1769 
1770 
1771 
1772