20 #include <linux/netdevice.h>
22 #include <linux/ethtool.h>
23 #include <linux/module.h>
24 #include <linux/virtio.h>
27 #include <linux/if_vlan.h>
28 #include <linux/slab.h>
30 static int napi_weight = 128;
33 static bool csum =
true, gso =
true;
38 #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39 #define GOOD_COPY_LEN 128
41 #define VIRTNET_SEND_COMMAND_SG_MAX 2
42 #define VIRTNET_DRIVER_VERSION "1.0.0"
125 for (end = page; end->
private; end = (
struct page *)end->
private);
132 struct page *
p = vi->
pages;
143 static void skb_xmit_done(
struct virtqueue *svq)
151 netif_wake_queue(vi->
dev);
154 static void set_skb_frag(
struct sk_buff *
skb,
struct page *page,
155 unsigned int offset,
unsigned int *len)
158 int i = skb_shinfo(skb)->nr_frags;
160 __skb_fill_page_desc(skb, i, page, offset, size);
165 skb_shinfo(skb)->nr_frags++;
171 struct page *page,
unsigned int len)
188 hdr_len =
sizeof hdr->
mhdr;
191 hdr_len =
sizeof hdr->
hdr;
201 if (copy > skb_tailroom(skb))
202 copy = skb_tailroom(skb);
222 set_skb_frag(skb, page, offset, &len);
223 page = (
struct page *)page->
private;
228 give_pages(vi, page);
239 num_buf = hdr->
mhdr.num_buffers;
241 i = skb_shinfo(skb)->nr_frags;
244 skb->
dev->stats.rx_length_errors++;
249 pr_debug(
"%s: rx error: %d buffers missing\n",
250 skb->
dev->name, hdr->
mhdr.num_buffers);
251 skb->
dev->stats.rx_length_errors++;
258 set_skb_frag(skb, page, 0, &len);
265 static void receive_buf(
struct net_device *
dev,
void *
buf,
unsigned int len)
275 dev->
stats.rx_length_errors++;
289 skb = page_to_skb(vi, page, len);
291 dev->
stats.rx_dropped++;
292 give_pages(vi, page);
296 if (receive_mergeable(vi, skb)) {
304 u64_stats_update_begin(&stats->
rx_syncp);
307 u64_stats_update_end(&stats->
rx_syncp);
313 hdr->
hdr.csum_offset))
320 pr_debug(
"Receiving skb proto 0x%04x len %i type %i\n",
345 skb_shinfo(skb)->gso_size = hdr->
hdr.gso_size;
346 if (skb_shinfo(skb)->
gso_size == 0) {
355 skb_shinfo(skb)->gso_segs = 0;
362 dev->
stats.rx_frame_errors++;
398 first = get_a_page(vi, gfp);
401 give_pages(vi, list);
411 first = get_a_page(vi, gfp);
413 give_pages(vi, list);
424 sg_set_buf(&vi->
rx_sg[1], p + offset, PAGE_SIZE - offset);
431 give_pages(vi, first);
441 page = get_a_page(vi, gfp);
449 give_pages(vi, page);
468 err = add_recvbuf_mergeable(vi, gfp);
470 err = add_recvbuf_big(vi, gfp);
472 err = add_recvbuf_small(vi, gfp);
485 static void skb_recv_done(
struct virtqueue *rvq)
489 if (napi_schedule_prep(&vi->
napi)) {
495 static void virtnet_napi_enable(
struct virtnet_info *vi)
497 napi_enable(&vi->
napi);
503 if (napi_schedule_prep(&vi->
napi)) {
517 napi_disable(&vi->
napi);
519 virtnet_napi_enable(vi);
531 unsigned int len, received = 0;
534 while (received < budget &&
536 receive_buf(vi->
dev, buf, len);
541 if (vi->
num < vi->
max / 2) {
547 if (received < budget) {
550 napi_schedule_prep(napi)) {
560 static unsigned int free_old_xmit_skbs(
struct virtnet_info *vi)
563 unsigned int len, tot_sgs = 0;
569 u64_stats_update_begin(&stats->
tx_syncp);
572 u64_stats_update_end(&stats->
tx_syncp);
585 pr_debug(
"%s: xmit %p %pM\n", vi->
dev->name, skb, dest);
589 hdr->
hdr.csum_start = skb_checksum_start_offset(skb);
593 hdr->
hdr.csum_offset = hdr->
hdr.csum_start = 0;
596 if (skb_is_gso(skb)) {
597 hdr->
hdr.hdr_len = skb_headlen(skb);
598 hdr->
hdr.gso_size = skb_shinfo(skb)->gso_size;
611 hdr->
hdr.gso_size = hdr->
hdr.hdr_len = 0;
614 hdr->
mhdr.num_buffers = 0;
633 free_old_xmit_skbs(vi);
636 capacity = xmit_skb(vi, skb);
643 "TX queue failure: out of memory\n");
645 dev->
stats.tx_fifo_errors++;
648 "Unexpected TX queue failure: %d\n",
651 dev->
stats.tx_dropped++;
664 netif_stop_queue(dev);
667 capacity += free_old_xmit_skbs(vi);
669 netif_start_queue(dev);
678 static int virtnet_set_mac_address(
struct net_device *dev,
void *p)
707 start = u64_stats_fetch_begin_bh(&stats->
tx_syncp);
710 }
while (u64_stats_fetch_retry_bh(&stats->
tx_syncp, start));
713 start = u64_stats_fetch_begin_bh(&stats->
rx_syncp);
716 }
while (u64_stats_fetch_retry_bh(&stats->
rx_syncp, start));
733 #ifdef CONFIG_NET_POLL_CONTROLLER
734 static void virtnet_netpoll(
struct net_device *dev)
738 napi_schedule(&vi->
napi);
742 static int virtnet_open(
struct net_device *dev)
750 virtnet_napi_enable(vi);
780 sg_set_buf(&sg[0], &
ctrl,
sizeof(
ctrl));
782 sg_set_buf(&sg[i + 1], sg_virt(s), s->
length);
783 sg_set_buf(&sg[out + in - 1], &status,
sizeof(status));
805 dev_warn(&vi->dev->dev,
"Failed to ack link announce.\n");
809 static int virtnet_close(
struct net_device *dev)
815 napi_disable(&vi->
napi);
820 static void virtnet_set_rx_mode(
struct net_device *dev)
824 u8 promisc, allmulti;
844 dev_warn(&dev->
dev,
"Failed to %sable promisc mode.\n",
845 promisc ?
"en" :
"dis");
852 dev_warn(&dev->
dev,
"Failed to %sable allmulti mode.\n",
853 allmulti ?
"en" :
"dis");
858 buf = kzalloc(((uc_count + mc_count) *
ETH_ALEN) +
859 (2 *
sizeof(mac_data->
entries)), GFP_ATOMIC);
862 dev_warn(&dev->
dev,
"No memory for MAC address buffer\n");
874 sg_set_buf(&sg[0], mac_data,
878 mac_data = (
void *)&mac_data->
macs[uc_count][0];
883 memcpy(&mac_data->
macs[i++][0], ha->addr, ETH_ALEN);
885 sg_set_buf(&sg[1], mac_data,
905 dev_warn(&dev->dev,
"Failed to add VLAN ID %d.\n", vid);
909 static int virtnet_vlan_rx_kill_vid(
struct net_device *dev,
u16 vid)
918 dev_warn(&dev->
dev,
"Failed to kill VLAN ID %d.\n", vid);
922 static void virtnet_get_ringparam(
struct net_device *dev,
935 static void virtnet_get_drvinfo(
struct net_device *dev,
947 static const struct ethtool_ops virtnet_ethtool_ops = {
948 .get_drvinfo = virtnet_get_drvinfo,
950 .get_ringparam = virtnet_get_ringparam,
954 #define MAX_MTU 65535
956 static int virtnet_change_mtu(
struct net_device *dev,
int new_mtu)
958 if (new_mtu < MIN_MTU || new_mtu >
MAX_MTU)
965 .ndo_open = virtnet_open,
966 .ndo_stop = virtnet_close,
967 .ndo_start_xmit = start_xmit,
969 .ndo_set_mac_address = virtnet_set_mac_address,
970 .ndo_set_rx_mode = virtnet_set_rx_mode,
971 .ndo_change_mtu = virtnet_change_mtu,
973 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
974 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
975 #ifdef CONFIG_NET_POLL_CONTROLLER
976 .ndo_poll_controller = virtnet_netpoll,
980 static void virtnet_config_changed_work(
struct work_struct *work)
997 virtnet_ack_link_announce(vi);
1010 netif_wake_queue(vi->
dev);
1013 netif_stop_queue(vi->
dev);
1019 static void virtnet_config_changed(
struct virtio_device *vdev)
1030 const char *names[] = {
"input",
"output",
"control" };
1037 err = vi->
vdev->config->find_vqs(vi->
vdev, nvqs, vqs, callbacks, names);
1102 eth_hw_addr_random(dev);
1105 vi = netdev_priv(dev);
1138 pr_debug(
"virtio_net: registering device failed\n");
1161 pr_debug(
"virtnet: registered device %s\n", dev->
name);
1167 vdev->
config->del_vqs(vdev);
1189 give_pages(vi, buf);
1202 free_unused_bufs(vi);
1204 vi->
vdev->config->del_vqs(vi->
vdev);
1221 remove_vq_common(vi);
1242 if (netif_running(vi->
dev))
1243 napi_disable(&vi->
napi);
1245 remove_vq_common(vi);
1261 if (netif_running(vi->
dev))
1262 virtnet_napi_enable(vi);
1296 .driver.name = KBUILD_MODNAME,
1299 .probe = virtnet_probe,
1301 .config_changed = virtnet_config_changed,
1303 .freeze = virtnet_freeze,
1304 .restore = virtnet_restore,
1313 static void __exit fini(
void)