virtio_net.c 26 KB

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