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