virtio_net.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /* A simple network driver using virtio.
  2. *
  3. * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. //#define DEBUG
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/ethtool.h>
  23. #include <linux/module.h>
  24. #include <linux/virtio.h>
  25. #include <linux/virtio_net.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/if_vlan.h>
  28. static int napi_weight = 128;
  29. module_param(napi_weight, int, 0444);
  30. static int csum = 1, gso = 1;
  31. module_param(csum, bool, 0444);
  32. module_param(gso, bool, 0444);
  33. /* FIXME: MTU in config. */
  34. #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
  35. #define GOOD_COPY_LEN 128
  36. #define VIRTNET_SEND_COMMAND_SG_MAX 2
  37. struct virtnet_info
  38. {
  39. struct virtio_device *vdev;
  40. struct virtqueue *rvq, *svq, *cvq;
  41. struct net_device *dev;
  42. struct napi_struct napi;
  43. unsigned int status;
  44. /* The skb we couldn't send because buffers were full. */
  45. struct sk_buff *last_xmit_skb;
  46. /* If we need to free in a timer, this is it. */
  47. struct timer_list xmit_free_timer;
  48. /* Number of input buffers, and max we've ever had. */
  49. unsigned int num, max;
  50. /* For cleaning up after transmission. */
  51. struct tasklet_struct tasklet;
  52. bool free_in_tasklet;
  53. /* I like... big packets and I cannot lie! */
  54. bool big_packets;
  55. /* Host will merge rx buffers for big packets (shake it! shake it!) */
  56. bool mergeable_rx_bufs;
  57. /* Receive & send queues. */
  58. struct sk_buff_head recv;
  59. struct sk_buff_head send;
  60. /* Chain pages by the private ptr. */
  61. struct page *pages;
  62. };
  63. static inline void *skb_vnet_hdr(struct sk_buff *skb)
  64. {
  65. return (struct virtio_net_hdr *)skb->cb;
  66. }
  67. static void give_a_page(struct virtnet_info *vi, struct page *page)
  68. {
  69. page->private = (unsigned long)vi->pages;
  70. vi->pages = page;
  71. }
  72. static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
  73. {
  74. unsigned int i;
  75. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  76. give_a_page(vi, skb_shinfo(skb)->frags[i].page);
  77. skb_shinfo(skb)->nr_frags = 0;
  78. skb->data_len = 0;
  79. }
  80. static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
  81. {
  82. struct page *p = vi->pages;
  83. if (p)
  84. vi->pages = (struct page *)p->private;
  85. else
  86. p = alloc_page(gfp_mask);
  87. return p;
  88. }
  89. static void skb_xmit_done(struct virtqueue *svq)
  90. {
  91. struct virtnet_info *vi = svq->vdev->priv;
  92. /* Suppress further interrupts. */
  93. svq->vq_ops->disable_cb(svq);
  94. /* We were probably waiting for more output buffers. */
  95. netif_wake_queue(vi->dev);
  96. /* Make sure we re-xmit last_xmit_skb: if there are no more packets
  97. * queued, start_xmit won't be called. */
  98. tasklet_schedule(&vi->tasklet);
  99. }
  100. static void receive_skb(struct net_device *dev, struct sk_buff *skb,
  101. unsigned len)
  102. {
  103. struct virtnet_info *vi = netdev_priv(dev);
  104. struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
  105. int err;
  106. int i;
  107. if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
  108. pr_debug("%s: short packet %i\n", dev->name, len);
  109. dev->stats.rx_length_errors++;
  110. goto drop;
  111. }
  112. if (vi->mergeable_rx_bufs) {
  113. struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
  114. unsigned int copy;
  115. char *p = page_address(skb_shinfo(skb)->frags[0].page);
  116. if (len > PAGE_SIZE)
  117. len = PAGE_SIZE;
  118. len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
  119. memcpy(hdr, p, sizeof(*mhdr));
  120. p += sizeof(*mhdr);
  121. copy = len;
  122. if (copy > skb_tailroom(skb))
  123. copy = skb_tailroom(skb);
  124. memcpy(skb_put(skb, copy), p, copy);
  125. len -= copy;
  126. if (!len) {
  127. give_a_page(vi, skb_shinfo(skb)->frags[0].page);
  128. skb_shinfo(skb)->nr_frags--;
  129. } else {
  130. skb_shinfo(skb)->frags[0].page_offset +=
  131. sizeof(*mhdr) + copy;
  132. skb_shinfo(skb)->frags[0].size = len;
  133. skb->data_len += len;
  134. skb->len += len;
  135. }
  136. while (--mhdr->num_buffers) {
  137. struct sk_buff *nskb;
  138. i = skb_shinfo(skb)->nr_frags;
  139. if (i >= MAX_SKB_FRAGS) {
  140. pr_debug("%s: packet too long %d\n", dev->name,
  141. len);
  142. dev->stats.rx_length_errors++;
  143. goto drop;
  144. }
  145. nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
  146. if (!nskb) {
  147. pr_debug("%s: rx error: %d buffers missing\n",
  148. dev->name, mhdr->num_buffers);
  149. dev->stats.rx_length_errors++;
  150. goto drop;
  151. }
  152. __skb_unlink(nskb, &vi->recv);
  153. vi->num--;
  154. skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
  155. skb_shinfo(nskb)->nr_frags = 0;
  156. kfree_skb(nskb);
  157. if (len > PAGE_SIZE)
  158. len = PAGE_SIZE;
  159. skb_shinfo(skb)->frags[i].size = len;
  160. skb_shinfo(skb)->nr_frags++;
  161. skb->data_len += len;
  162. skb->len += len;
  163. }
  164. } else {
  165. len -= sizeof(struct virtio_net_hdr);
  166. if (len <= MAX_PACKET_LEN)
  167. trim_pages(vi, skb);
  168. err = pskb_trim(skb, len);
  169. if (err) {
  170. pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
  171. len, err);
  172. dev->stats.rx_dropped++;
  173. goto drop;
  174. }
  175. }
  176. skb->truesize += skb->data_len;
  177. dev->stats.rx_bytes += skb->len;
  178. dev->stats.rx_packets++;
  179. if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  180. pr_debug("Needs csum!\n");
  181. if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
  182. goto frame_err;
  183. }
  184. skb->protocol = eth_type_trans(skb, dev);
  185. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  186. ntohs(skb->protocol), skb->len, skb->pkt_type);
  187. if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  188. pr_debug("GSO!\n");
  189. switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  190. case VIRTIO_NET_HDR_GSO_TCPV4:
  191. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  192. break;
  193. case VIRTIO_NET_HDR_GSO_UDP:
  194. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  195. break;
  196. case VIRTIO_NET_HDR_GSO_TCPV6:
  197. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  198. break;
  199. default:
  200. if (net_ratelimit())
  201. printk(KERN_WARNING "%s: bad gso type %u.\n",
  202. dev->name, hdr->gso_type);
  203. goto frame_err;
  204. }
  205. if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  206. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  207. skb_shinfo(skb)->gso_size = hdr->gso_size;
  208. if (skb_shinfo(skb)->gso_size == 0) {
  209. if (net_ratelimit())
  210. printk(KERN_WARNING "%s: zero gso size.\n",
  211. dev->name);
  212. goto frame_err;
  213. }
  214. /* Header must be checked, and gso_segs computed. */
  215. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  216. skb_shinfo(skb)->gso_segs = 0;
  217. }
  218. netif_receive_skb(skb);
  219. return;
  220. frame_err:
  221. dev->stats.rx_frame_errors++;
  222. drop:
  223. dev_kfree_skb(skb);
  224. }
  225. static void try_fill_recv_maxbufs(struct virtnet_info *vi)
  226. {
  227. struct sk_buff *skb;
  228. struct scatterlist sg[2+MAX_SKB_FRAGS];
  229. int num, err, i;
  230. sg_init_table(sg, 2+MAX_SKB_FRAGS);
  231. for (;;) {
  232. struct virtio_net_hdr *hdr;
  233. skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
  234. if (unlikely(!skb))
  235. break;
  236. skb_put(skb, MAX_PACKET_LEN);
  237. hdr = skb_vnet_hdr(skb);
  238. sg_set_buf(sg, hdr, sizeof(*hdr));
  239. if (vi->big_packets) {
  240. for (i = 0; i < MAX_SKB_FRAGS; i++) {
  241. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  242. f->page = get_a_page(vi, GFP_ATOMIC);
  243. if (!f->page)
  244. break;
  245. f->page_offset = 0;
  246. f->size = PAGE_SIZE;
  247. skb->data_len += PAGE_SIZE;
  248. skb->len += PAGE_SIZE;
  249. skb_shinfo(skb)->nr_frags++;
  250. }
  251. }
  252. num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
  253. skb_queue_head(&vi->recv, skb);
  254. err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
  255. if (err) {
  256. skb_unlink(skb, &vi->recv);
  257. trim_pages(vi, skb);
  258. kfree_skb(skb);
  259. break;
  260. }
  261. vi->num++;
  262. }
  263. if (unlikely(vi->num > vi->max))
  264. vi->max = vi->num;
  265. vi->rvq->vq_ops->kick(vi->rvq);
  266. }
  267. static void try_fill_recv(struct virtnet_info *vi)
  268. {
  269. struct sk_buff *skb;
  270. struct scatterlist sg[1];
  271. int err;
  272. if (!vi->mergeable_rx_bufs) {
  273. try_fill_recv_maxbufs(vi);
  274. return;
  275. }
  276. for (;;) {
  277. skb_frag_t *f;
  278. skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
  279. if (unlikely(!skb))
  280. break;
  281. skb_reserve(skb, NET_IP_ALIGN);
  282. f = &skb_shinfo(skb)->frags[0];
  283. f->page = get_a_page(vi, GFP_ATOMIC);
  284. if (!f->page) {
  285. kfree_skb(skb);
  286. break;
  287. }
  288. f->page_offset = 0;
  289. f->size = PAGE_SIZE;
  290. skb_shinfo(skb)->nr_frags++;
  291. sg_init_one(sg, page_address(f->page), PAGE_SIZE);
  292. skb_queue_head(&vi->recv, skb);
  293. err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
  294. if (err) {
  295. skb_unlink(skb, &vi->recv);
  296. kfree_skb(skb);
  297. break;
  298. }
  299. vi->num++;
  300. }
  301. if (unlikely(vi->num > vi->max))
  302. vi->max = vi->num;
  303. vi->rvq->vq_ops->kick(vi->rvq);
  304. }
  305. static void skb_recv_done(struct virtqueue *rvq)
  306. {
  307. struct virtnet_info *vi = rvq->vdev->priv;
  308. /* Schedule NAPI, Suppress further interrupts if successful. */
  309. if (napi_schedule_prep(&vi->napi)) {
  310. rvq->vq_ops->disable_cb(rvq);
  311. __napi_schedule(&vi->napi);
  312. }
  313. }
  314. static int virtnet_poll(struct napi_struct *napi, int budget)
  315. {
  316. struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
  317. struct sk_buff *skb = NULL;
  318. unsigned int len, received = 0;
  319. again:
  320. while (received < budget &&
  321. (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
  322. __skb_unlink(skb, &vi->recv);
  323. receive_skb(vi->dev, skb, len);
  324. vi->num--;
  325. received++;
  326. }
  327. /* FIXME: If we oom and completely run out of inbufs, we need
  328. * to start a timer trying to fill more. */
  329. if (vi->num < vi->max / 2)
  330. try_fill_recv(vi);
  331. /* Out of packets? */
  332. if (received < budget) {
  333. napi_complete(napi);
  334. if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
  335. && napi_schedule_prep(napi)) {
  336. vi->rvq->vq_ops->disable_cb(vi->rvq);
  337. __napi_schedule(napi);
  338. goto again;
  339. }
  340. }
  341. return received;
  342. }
  343. static void free_old_xmit_skbs(struct virtnet_info *vi)
  344. {
  345. struct sk_buff *skb;
  346. unsigned int len;
  347. while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
  348. pr_debug("Sent skb %p\n", skb);
  349. __skb_unlink(skb, &vi->send);
  350. vi->dev->stats.tx_bytes += skb->len;
  351. vi->dev->stats.tx_packets++;
  352. kfree_skb(skb);
  353. }
  354. }
  355. /* If the virtio transport doesn't always notify us when all in-flight packets
  356. * are consumed, we fall back to using this function on a timer to free them. */
  357. static void xmit_free(unsigned long data)
  358. {
  359. struct virtnet_info *vi = (void *)data;
  360. netif_tx_lock(vi->dev);
  361. free_old_xmit_skbs(vi);
  362. if (!skb_queue_empty(&vi->send))
  363. mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
  364. netif_tx_unlock(vi->dev);
  365. }
  366. static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
  367. {
  368. int num, err;
  369. struct scatterlist sg[2+MAX_SKB_FRAGS];
  370. struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb);
  371. struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
  372. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  373. sg_init_table(sg, 2+MAX_SKB_FRAGS);
  374. pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
  375. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  376. hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  377. hdr->csum_start = skb->csum_start - skb_headroom(skb);
  378. hdr->csum_offset = skb->csum_offset;
  379. } else {
  380. hdr->flags = 0;
  381. hdr->csum_offset = hdr->csum_start = 0;
  382. }
  383. if (skb_is_gso(skb)) {
  384. hdr->hdr_len = skb_transport_header(skb) - skb->data;
  385. hdr->gso_size = skb_shinfo(skb)->gso_size;
  386. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  387. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  388. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  389. hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  390. else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  391. hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  392. else
  393. BUG();
  394. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
  395. hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  396. } else {
  397. hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  398. hdr->gso_size = hdr->hdr_len = 0;
  399. }
  400. mhdr->num_buffers = 0;
  401. /* Encode metadata header at front. */
  402. if (vi->mergeable_rx_bufs)
  403. sg_set_buf(sg, mhdr, sizeof(*mhdr));
  404. else
  405. sg_set_buf(sg, hdr, sizeof(*hdr));
  406. num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
  407. err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
  408. if (!err && !vi->free_in_tasklet)
  409. mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
  410. return err;
  411. }
  412. static void xmit_tasklet(unsigned long data)
  413. {
  414. struct virtnet_info *vi = (void *)data;
  415. netif_tx_lock_bh(vi->dev);
  416. if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
  417. vi->svq->vq_ops->kick(vi->svq);
  418. vi->last_xmit_skb = NULL;
  419. }
  420. if (vi->free_in_tasklet)
  421. free_old_xmit_skbs(vi);
  422. netif_tx_unlock_bh(vi->dev);
  423. }
  424. static int start_xmit(struct sk_buff *skb, struct net_device *dev)
  425. {
  426. struct virtnet_info *vi = netdev_priv(dev);
  427. again:
  428. /* Free up any pending old buffers before queueing new ones. */
  429. free_old_xmit_skbs(vi);
  430. /* If we has a buffer left over from last time, send it now. */
  431. if (unlikely(vi->last_xmit_skb) &&
  432. xmit_skb(vi, vi->last_xmit_skb) != 0)
  433. goto stop_queue;
  434. vi->last_xmit_skb = NULL;
  435. /* Put new one in send queue and do transmit */
  436. if (likely(skb)) {
  437. __skb_queue_head(&vi->send, skb);
  438. if (xmit_skb(vi, skb) != 0) {
  439. vi->last_xmit_skb = skb;
  440. skb = NULL;
  441. goto stop_queue;
  442. }
  443. }
  444. done:
  445. vi->svq->vq_ops->kick(vi->svq);
  446. return NETDEV_TX_OK;
  447. stop_queue:
  448. pr_debug("%s: virtio not prepared to send\n", dev->name);
  449. netif_stop_queue(dev);
  450. /* Activate callback for using skbs: if this returns false it
  451. * means some were used in the meantime. */
  452. if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
  453. vi->svq->vq_ops->disable_cb(vi->svq);
  454. netif_start_queue(dev);
  455. goto again;
  456. }
  457. if (skb) {
  458. /* Drop this skb: we only queue one. */
  459. vi->dev->stats.tx_dropped++;
  460. kfree_skb(skb);
  461. }
  462. goto done;
  463. }
  464. static int virtnet_set_mac_address(struct net_device *dev, void *p)
  465. {
  466. struct virtnet_info *vi = netdev_priv(dev);
  467. struct virtio_device *vdev = vi->vdev;
  468. int ret;
  469. ret = eth_mac_addr(dev, p);
  470. if (ret)
  471. return ret;
  472. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
  473. vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
  474. dev->dev_addr, dev->addr_len);
  475. return 0;
  476. }
  477. #ifdef CONFIG_NET_POLL_CONTROLLER
  478. static void virtnet_netpoll(struct net_device *dev)
  479. {
  480. struct virtnet_info *vi = netdev_priv(dev);
  481. napi_schedule(&vi->napi);
  482. }
  483. #endif
  484. static int virtnet_open(struct net_device *dev)
  485. {
  486. struct virtnet_info *vi = netdev_priv(dev);
  487. napi_enable(&vi->napi);
  488. /* If all buffers were filled by other side before we napi_enabled, we
  489. * won't get another interrupt, so process any outstanding packets
  490. * now. virtnet_poll wants re-enable the queue, so we disable here.
  491. * We synchronize against interrupts via NAPI_STATE_SCHED */
  492. if (napi_schedule_prep(&vi->napi)) {
  493. vi->rvq->vq_ops->disable_cb(vi->rvq);
  494. __napi_schedule(&vi->napi);
  495. }
  496. return 0;
  497. }
  498. /*
  499. * Send command via the control virtqueue and check status. Commands
  500. * supported by the hypervisor, as indicated by feature bits, should
  501. * never fail unless improperly formated.
  502. */
  503. static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
  504. struct scatterlist *data, int out, int in)
  505. {
  506. struct scatterlist sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
  507. struct virtio_net_ctrl_hdr ctrl;
  508. virtio_net_ctrl_ack status = ~0;
  509. unsigned int tmp;
  510. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
  511. BUG(); /* Caller should know better */
  512. return false;
  513. }
  514. BUG_ON(out + in > VIRTNET_SEND_COMMAND_SG_MAX);
  515. out++; /* Add header */
  516. in++; /* Add return status */
  517. ctrl.class = class;
  518. ctrl.cmd = cmd;
  519. sg_init_table(sg, out + in);
  520. sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
  521. memcpy(&sg[1], data, sizeof(struct scatterlist) * (out + in - 2));
  522. sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
  523. if (vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) != 0)
  524. BUG();
  525. vi->cvq->vq_ops->kick(vi->cvq);
  526. /*
  527. * Spin for a response, the kick causes an ioport write, trapping
  528. * into the hypervisor, so the request should be handled immediately.
  529. */
  530. while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
  531. cpu_relax();
  532. return status == VIRTIO_NET_OK;
  533. }
  534. static int virtnet_close(struct net_device *dev)
  535. {
  536. struct virtnet_info *vi = netdev_priv(dev);
  537. napi_disable(&vi->napi);
  538. return 0;
  539. }
  540. static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
  541. {
  542. struct virtnet_info *vi = netdev_priv(dev);
  543. struct virtio_device *vdev = vi->vdev;
  544. if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
  545. return -ENOSYS;
  546. return ethtool_op_set_tx_hw_csum(dev, data);
  547. }
  548. static void virtnet_set_rx_mode(struct net_device *dev)
  549. {
  550. struct virtnet_info *vi = netdev_priv(dev);
  551. struct scatterlist sg[2];
  552. u8 promisc, allmulti;
  553. struct virtio_net_ctrl_mac *mac_data;
  554. struct dev_addr_list *addr;
  555. void *buf;
  556. int i;
  557. /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
  558. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
  559. return;
  560. promisc = ((dev->flags & IFF_PROMISC) != 0);
  561. allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
  562. sg_set_buf(sg, &promisc, sizeof(promisc));
  563. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  564. VIRTIO_NET_CTRL_RX_PROMISC,
  565. sg, 1, 0))
  566. dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
  567. promisc ? "en" : "dis");
  568. sg_set_buf(sg, &allmulti, sizeof(allmulti));
  569. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
  570. VIRTIO_NET_CTRL_RX_ALLMULTI,
  571. sg, 1, 0))
  572. dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
  573. allmulti ? "en" : "dis");
  574. /* MAC filter - use one buffer for both lists */
  575. mac_data = buf = kzalloc(((dev->uc_count + dev->mc_count) * ETH_ALEN) +
  576. (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
  577. if (!buf) {
  578. dev_warn(&dev->dev, "No memory for MAC address buffer\n");
  579. return;
  580. }
  581. /* Store the unicast list and count in the front of the buffer */
  582. mac_data->entries = dev->uc_count;
  583. addr = dev->uc_list;
  584. for (i = 0; i < dev->uc_count; i++, addr = addr->next)
  585. memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
  586. sg_set_buf(&sg[0], mac_data,
  587. sizeof(mac_data->entries) + (dev->uc_count * ETH_ALEN));
  588. /* multicast list and count fill the end */
  589. mac_data = (void *)&mac_data->macs[dev->uc_count][0];
  590. mac_data->entries = dev->mc_count;
  591. addr = dev->mc_list;
  592. for (i = 0; i < dev->mc_count; i++, addr = addr->next)
  593. memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
  594. sg_set_buf(&sg[1], mac_data,
  595. sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
  596. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
  597. VIRTIO_NET_CTRL_MAC_TABLE_SET,
  598. sg, 2, 0))
  599. dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
  600. kfree(buf);
  601. }
  602. static void virnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
  603. {
  604. struct virtnet_info *vi = netdev_priv(dev);
  605. struct scatterlist sg;
  606. sg_set_buf(&sg, &vid, sizeof(vid));
  607. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  608. VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
  609. dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
  610. }
  611. static void virnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
  612. {
  613. struct virtnet_info *vi = netdev_priv(dev);
  614. struct scatterlist sg;
  615. sg_set_buf(&sg, &vid, sizeof(vid));
  616. if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
  617. VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
  618. dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
  619. }
  620. static struct ethtool_ops virtnet_ethtool_ops = {
  621. .set_tx_csum = virtnet_set_tx_csum,
  622. .set_sg = ethtool_op_set_sg,
  623. .set_tso = ethtool_op_set_tso,
  624. .get_link = ethtool_op_get_link,
  625. };
  626. #define MIN_MTU 68
  627. #define MAX_MTU 65535
  628. static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
  629. {
  630. if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
  631. return -EINVAL;
  632. dev->mtu = new_mtu;
  633. return 0;
  634. }
  635. static const struct net_device_ops virtnet_netdev = {
  636. .ndo_open = virtnet_open,
  637. .ndo_stop = virtnet_close,
  638. .ndo_start_xmit = start_xmit,
  639. .ndo_validate_addr = eth_validate_addr,
  640. .ndo_set_mac_address = virtnet_set_mac_address,
  641. .ndo_set_rx_mode = virtnet_set_rx_mode,
  642. .ndo_change_mtu = virtnet_change_mtu,
  643. .ndo_vlan_rx_add_vid = virnet_vlan_rx_add_vid,
  644. .ndo_vlan_rx_kill_vid = virnet_vlan_rx_kill_vid,
  645. #ifdef CONFIG_NET_POLL_CONTROLLER
  646. .ndo_poll_controller = virtnet_netpoll,
  647. #endif
  648. };
  649. static void virtnet_update_status(struct virtnet_info *vi)
  650. {
  651. u16 v;
  652. if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
  653. return;
  654. vi->vdev->config->get(vi->vdev,
  655. offsetof(struct virtio_net_config, status),
  656. &v, sizeof(v));
  657. /* Ignore unknown (future) status bits */
  658. v &= VIRTIO_NET_S_LINK_UP;
  659. if (vi->status == v)
  660. return;
  661. vi->status = v;
  662. if (vi->status & VIRTIO_NET_S_LINK_UP) {
  663. netif_carrier_on(vi->dev);
  664. netif_wake_queue(vi->dev);
  665. } else {
  666. netif_carrier_off(vi->dev);
  667. netif_stop_queue(vi->dev);
  668. }
  669. }
  670. static void virtnet_config_changed(struct virtio_device *vdev)
  671. {
  672. struct virtnet_info *vi = vdev->priv;
  673. virtnet_update_status(vi);
  674. }
  675. static int virtnet_probe(struct virtio_device *vdev)
  676. {
  677. int err;
  678. struct net_device *dev;
  679. struct virtnet_info *vi;
  680. /* Allocate ourselves a network device with room for our info */
  681. dev = alloc_etherdev(sizeof(struct virtnet_info));
  682. if (!dev)
  683. return -ENOMEM;
  684. /* Set up network device as normal. */
  685. dev->netdev_ops = &virtnet_netdev;
  686. dev->features = NETIF_F_HIGHDMA;
  687. SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
  688. SET_NETDEV_DEV(dev, &vdev->dev);
  689. /* Do we support "hardware" checksums? */
  690. if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  691. /* This opens up the world of extra features. */
  692. dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
  693. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
  694. dev->features |= NETIF_F_TSO | NETIF_F_UFO
  695. | NETIF_F_TSO_ECN | NETIF_F_TSO6;
  696. }
  697. /* Individual feature bits: what can host handle? */
  698. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
  699. dev->features |= NETIF_F_TSO;
  700. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
  701. dev->features |= NETIF_F_TSO6;
  702. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
  703. dev->features |= NETIF_F_TSO_ECN;
  704. if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
  705. dev->features |= NETIF_F_UFO;
  706. }
  707. /* Configuration may specify what MAC to use. Otherwise random. */
  708. if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
  709. vdev->config->get(vdev,
  710. offsetof(struct virtio_net_config, mac),
  711. dev->dev_addr, dev->addr_len);
  712. } else
  713. random_ether_addr(dev->dev_addr);
  714. /* Set up our device-specific information */
  715. vi = netdev_priv(dev);
  716. netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
  717. vi->dev = dev;
  718. vi->vdev = vdev;
  719. vdev->priv = vi;
  720. vi->pages = NULL;
  721. /* If they give us a callback when all buffers are done, we don't need
  722. * the timer. */
  723. vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
  724. /* If we can receive ANY GSO packets, we must allocate large ones. */
  725. if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
  726. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
  727. || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
  728. vi->big_packets = true;
  729. if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
  730. vi->mergeable_rx_bufs = true;
  731. /* We expect two virtqueues, receive then send. */
  732. vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
  733. if (IS_ERR(vi->rvq)) {
  734. err = PTR_ERR(vi->rvq);
  735. goto free;
  736. }
  737. vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
  738. if (IS_ERR(vi->svq)) {
  739. err = PTR_ERR(vi->svq);
  740. goto free_recv;
  741. }
  742. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
  743. vi->cvq = vdev->config->find_vq(vdev, 2, NULL);
  744. if (IS_ERR(vi->cvq)) {
  745. err = PTR_ERR(vi->svq);
  746. goto free_send;
  747. }
  748. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
  749. dev->features |= NETIF_F_HW_VLAN_FILTER;
  750. }
  751. /* Initialize our empty receive and send queues. */
  752. skb_queue_head_init(&vi->recv);
  753. skb_queue_head_init(&vi->send);
  754. tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
  755. if (!vi->free_in_tasklet)
  756. setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
  757. err = register_netdev(dev);
  758. if (err) {
  759. pr_debug("virtio_net: registering device failed\n");
  760. goto free_ctrl;
  761. }
  762. /* Last of all, set up some receive buffers. */
  763. try_fill_recv(vi);
  764. /* If we didn't even get one input buffer, we're useless. */
  765. if (vi->num == 0) {
  766. err = -ENOMEM;
  767. goto unregister;
  768. }
  769. vi->status = VIRTIO_NET_S_LINK_UP;
  770. virtnet_update_status(vi);
  771. netif_carrier_on(dev);
  772. pr_debug("virtnet: registered device %s\n", dev->name);
  773. return 0;
  774. unregister:
  775. unregister_netdev(dev);
  776. free_ctrl:
  777. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
  778. vdev->config->del_vq(vi->cvq);
  779. free_send:
  780. vdev->config->del_vq(vi->svq);
  781. free_recv:
  782. vdev->config->del_vq(vi->rvq);
  783. free:
  784. free_netdev(dev);
  785. return err;
  786. }
  787. static void virtnet_remove(struct virtio_device *vdev)
  788. {
  789. struct virtnet_info *vi = vdev->priv;
  790. struct sk_buff *skb;
  791. /* Stop all the virtqueues. */
  792. vdev->config->reset(vdev);
  793. if (!vi->free_in_tasklet)
  794. del_timer_sync(&vi->xmit_free_timer);
  795. /* Free our skbs in send and recv queues, if any. */
  796. while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
  797. kfree_skb(skb);
  798. vi->num--;
  799. }
  800. __skb_queue_purge(&vi->send);
  801. BUG_ON(vi->num != 0);
  802. vdev->config->del_vq(vi->svq);
  803. vdev->config->del_vq(vi->rvq);
  804. if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ))
  805. vdev->config->del_vq(vi->cvq);
  806. unregister_netdev(vi->dev);
  807. while (vi->pages)
  808. __free_pages(get_a_page(vi, GFP_KERNEL), 0);
  809. free_netdev(vi->dev);
  810. }
  811. static struct virtio_device_id id_table[] = {
  812. { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
  813. { 0 },
  814. };
  815. static unsigned int features[] = {
  816. VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
  817. VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
  818. VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
  819. VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
  820. VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
  821. VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
  822. VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
  823. VIRTIO_F_NOTIFY_ON_EMPTY,
  824. };
  825. static struct virtio_driver virtio_net = {
  826. .feature_table = features,
  827. .feature_table_size = ARRAY_SIZE(features),
  828. .driver.name = KBUILD_MODNAME,
  829. .driver.owner = THIS_MODULE,
  830. .id_table = id_table,
  831. .probe = virtnet_probe,
  832. .remove = __devexit_p(virtnet_remove),
  833. .config_changed = virtnet_config_changed,
  834. };
  835. static int __init init(void)
  836. {
  837. return register_virtio_driver(&virtio_net);
  838. }
  839. static void __exit fini(void)
  840. {
  841. unregister_virtio_driver(&virtio_net);
  842. }
  843. module_init(init);
  844. module_exit(fini);
  845. MODULE_DEVICE_TABLE(virtio, id_table);
  846. MODULE_DESCRIPTION("Virtio network driver");
  847. MODULE_LICENSE("GPL");