lguest_net.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* A simple network driver for lguest.
  2. *
  3. * Copyright 2006 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/module.h>
  23. #include <linux/mm_types.h>
  24. #include <linux/io.h>
  25. #include <linux/lguest_bus.h>
  26. #define SHARED_SIZE PAGE_SIZE
  27. #define MAX_LANS 4
  28. #define NUM_SKBS 8
  29. struct lguestnet_info
  30. {
  31. /* The shared page(s). */
  32. struct lguest_net *peer;
  33. unsigned long peer_phys;
  34. unsigned long mapsize;
  35. /* The lguest_device I come from */
  36. struct lguest_device *lgdev;
  37. /* My peerid. */
  38. unsigned int me;
  39. /* Receive queue. */
  40. struct sk_buff *skb[NUM_SKBS];
  41. struct lguest_dma dma[NUM_SKBS];
  42. };
  43. /* How many bytes left in this page. */
  44. static unsigned int rest_of_page(void *data)
  45. {
  46. return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
  47. }
  48. /* Simple convention: offset 4 * peernum. */
  49. static unsigned long peer_key(struct lguestnet_info *info, unsigned peernum)
  50. {
  51. return info->peer_phys + 4 * peernum;
  52. }
  53. static void skb_to_dma(const struct sk_buff *skb, unsigned int headlen,
  54. struct lguest_dma *dma)
  55. {
  56. unsigned int i, seg;
  57. for (i = seg = 0; i < headlen; seg++, i += rest_of_page(skb->data+i)) {
  58. dma->addr[seg] = virt_to_phys(skb->data + i);
  59. dma->len[seg] = min((unsigned)(headlen - i),
  60. rest_of_page(skb->data + i));
  61. }
  62. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, seg++) {
  63. const skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  64. /* Should not happen with MTU less than 64k - 2 * PAGE_SIZE. */
  65. if (seg == LGUEST_MAX_DMA_SECTIONS) {
  66. printk("Woah dude! Megapacket!\n");
  67. break;
  68. }
  69. dma->addr[seg] = page_to_phys(f->page) + f->page_offset;
  70. dma->len[seg] = f->size;
  71. }
  72. if (seg < LGUEST_MAX_DMA_SECTIONS)
  73. dma->len[seg] = 0;
  74. }
  75. /* We overload multicast bit to show promiscuous mode. */
  76. #define PROMISC_BIT 0x01
  77. static void lguestnet_set_multicast(struct net_device *dev)
  78. {
  79. struct lguestnet_info *info = netdev_priv(dev);
  80. if ((dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) || dev->mc_count)
  81. info->peer[info->me].mac[0] |= PROMISC_BIT;
  82. else
  83. info->peer[info->me].mac[0] &= ~PROMISC_BIT;
  84. }
  85. static int promisc(struct lguestnet_info *info, unsigned int peer)
  86. {
  87. return info->peer[peer].mac[0] & PROMISC_BIT;
  88. }
  89. static int mac_eq(const unsigned char mac[ETH_ALEN],
  90. struct lguestnet_info *info, unsigned int peer)
  91. {
  92. /* Ignore multicast bit, which peer turns on to mean promisc. */
  93. if ((info->peer[peer].mac[0] & (~PROMISC_BIT)) != mac[0])
  94. return 0;
  95. return memcmp(mac+1, info->peer[peer].mac+1, ETH_ALEN-1) == 0;
  96. }
  97. static void transfer_packet(struct net_device *dev,
  98. struct sk_buff *skb,
  99. unsigned int peernum)
  100. {
  101. struct lguestnet_info *info = netdev_priv(dev);
  102. struct lguest_dma dma;
  103. skb_to_dma(skb, skb_headlen(skb), &dma);
  104. pr_debug("xfer length %04x (%u)\n", htons(skb->len), skb->len);
  105. lguest_send_dma(peer_key(info, peernum), &dma);
  106. if (dma.used_len != skb->len) {
  107. dev->stats.tx_carrier_errors++;
  108. pr_debug("Bad xfer to peer %i: %i of %i (dma %p/%i)\n",
  109. peernum, dma.used_len, skb->len,
  110. (void *)dma.addr[0], dma.len[0]);
  111. } else {
  112. dev->stats.tx_bytes += skb->len;
  113. dev->stats.tx_packets++;
  114. }
  115. }
  116. static int unused_peer(const struct lguest_net peer[], unsigned int num)
  117. {
  118. return peer[num].mac[0] == 0;
  119. }
  120. static int lguestnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  121. {
  122. unsigned int i;
  123. int broadcast;
  124. struct lguestnet_info *info = netdev_priv(dev);
  125. const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
  126. pr_debug("%s: xmit %02x:%02x:%02x:%02x:%02x:%02x\n",
  127. dev->name, dest[0],dest[1],dest[2],dest[3],dest[4],dest[5]);
  128. broadcast = is_multicast_ether_addr(dest);
  129. for (i = 0; i < info->mapsize/sizeof(struct lguest_net); i++) {
  130. if (i == info->me || unused_peer(info->peer, i))
  131. continue;
  132. if (!broadcast && !promisc(info, i) && !mac_eq(dest, info, i))
  133. continue;
  134. pr_debug("lguestnet %s: sending from %i to %i\n",
  135. dev->name, info->me, i);
  136. transfer_packet(dev, skb, i);
  137. }
  138. dev_kfree_skb(skb);
  139. return 0;
  140. }
  141. /* Find a new skb to put in this slot in shared mem. */
  142. static int fill_slot(struct net_device *dev, unsigned int slot)
  143. {
  144. struct lguestnet_info *info = netdev_priv(dev);
  145. /* Try to create and register a new one. */
  146. info->skb[slot] = netdev_alloc_skb(dev, ETH_HLEN + ETH_DATA_LEN);
  147. if (!info->skb[slot]) {
  148. printk("%s: could not fill slot %i\n", dev->name, slot);
  149. return -ENOMEM;
  150. }
  151. skb_to_dma(info->skb[slot], ETH_HLEN + ETH_DATA_LEN, &info->dma[slot]);
  152. wmb();
  153. /* Now we tell hypervisor it can use the slot. */
  154. info->dma[slot].used_len = 0;
  155. return 0;
  156. }
  157. static irqreturn_t lguestnet_rcv(int irq, void *dev_id)
  158. {
  159. struct net_device *dev = dev_id;
  160. struct lguestnet_info *info = netdev_priv(dev);
  161. unsigned int i, done = 0;
  162. for (i = 0; i < ARRAY_SIZE(info->dma); i++) {
  163. unsigned int length;
  164. struct sk_buff *skb;
  165. length = info->dma[i].used_len;
  166. if (length == 0)
  167. continue;
  168. done++;
  169. skb = info->skb[i];
  170. fill_slot(dev, i);
  171. if (length < ETH_HLEN || length > ETH_HLEN + ETH_DATA_LEN) {
  172. pr_debug(KERN_WARNING "%s: unbelievable skb len: %i\n",
  173. dev->name, length);
  174. dev_kfree_skb(skb);
  175. continue;
  176. }
  177. skb_put(skb, length);
  178. skb->protocol = eth_type_trans(skb, dev);
  179. /* This is a reliable transport. */
  180. if (dev->features & NETIF_F_NO_CSUM)
  181. skb->ip_summed = CHECKSUM_UNNECESSARY;
  182. pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
  183. ntohs(skb->protocol), skb->len, skb->pkt_type);
  184. dev->stats.rx_bytes += skb->len;
  185. dev->stats.rx_packets++;
  186. netif_rx(skb);
  187. }
  188. return done ? IRQ_HANDLED : IRQ_NONE;
  189. }
  190. static int lguestnet_open(struct net_device *dev)
  191. {
  192. int i;
  193. struct lguestnet_info *info = netdev_priv(dev);
  194. /* Set up our MAC address */
  195. memcpy(info->peer[info->me].mac, dev->dev_addr, ETH_ALEN);
  196. /* Turn on promisc mode if needed */
  197. lguestnet_set_multicast(dev);
  198. for (i = 0; i < ARRAY_SIZE(info->dma); i++) {
  199. if (fill_slot(dev, i) != 0)
  200. goto cleanup;
  201. }
  202. if (lguest_bind_dma(peer_key(info,info->me), info->dma,
  203. NUM_SKBS, lgdev_irq(info->lgdev)) != 0)
  204. goto cleanup;
  205. return 0;
  206. cleanup:
  207. while (--i >= 0)
  208. dev_kfree_skb(info->skb[i]);
  209. return -ENOMEM;
  210. }
  211. static int lguestnet_close(struct net_device *dev)
  212. {
  213. unsigned int i;
  214. struct lguestnet_info *info = netdev_priv(dev);
  215. /* Clear all trace: others might deliver packets, we'll ignore it. */
  216. memset(&info->peer[info->me], 0, sizeof(info->peer[info->me]));
  217. /* Deregister sg lists. */
  218. lguest_unbind_dma(peer_key(info, info->me), info->dma);
  219. for (i = 0; i < ARRAY_SIZE(info->dma); i++)
  220. dev_kfree_skb(info->skb[i]);
  221. return 0;
  222. }
  223. static int lguestnet_probe(struct lguest_device *lgdev)
  224. {
  225. int err, irqf = IRQF_SHARED;
  226. struct net_device *dev;
  227. struct lguestnet_info *info;
  228. struct lguest_device_desc *desc = &lguest_devices[lgdev->index];
  229. pr_debug("lguest_net: probing for device %i\n", lgdev->index);
  230. dev = alloc_etherdev(sizeof(struct lguestnet_info));
  231. if (!dev)
  232. return -ENOMEM;
  233. SET_MODULE_OWNER(dev);
  234. /* Ethernet defaults with some changes */
  235. ether_setup(dev);
  236. dev->set_mac_address = NULL;
  237. dev->dev_addr[0] = 0x02; /* set local assignment bit (IEEE802) */
  238. dev->dev_addr[1] = 0x00;
  239. memcpy(&dev->dev_addr[2], &lguest_data.guestid, 2);
  240. dev->dev_addr[4] = 0x00;
  241. dev->dev_addr[5] = 0x00;
  242. dev->open = lguestnet_open;
  243. dev->stop = lguestnet_close;
  244. dev->hard_start_xmit = lguestnet_start_xmit;
  245. /* Turning on/off promisc will call dev->set_multicast_list.
  246. * We don't actually support multicast yet */
  247. dev->set_multicast_list = lguestnet_set_multicast;
  248. SET_NETDEV_DEV(dev, &lgdev->dev);
  249. if (desc->features & LGUEST_NET_F_NOCSUM)
  250. dev->features = NETIF_F_SG|NETIF_F_NO_CSUM;
  251. info = netdev_priv(dev);
  252. info->mapsize = PAGE_SIZE * desc->num_pages;
  253. info->peer_phys = ((unsigned long)desc->pfn << PAGE_SHIFT);
  254. info->lgdev = lgdev;
  255. info->peer = lguest_map(info->peer_phys, desc->num_pages);
  256. if (!info->peer) {
  257. err = -ENOMEM;
  258. goto free;
  259. }
  260. /* This stores our peerid (upper bits reserved for future). */
  261. info->me = (desc->features & (info->mapsize-1));
  262. err = register_netdev(dev);
  263. if (err) {
  264. pr_debug("lguestnet: registering device failed\n");
  265. goto unmap;
  266. }
  267. if (lguest_devices[lgdev->index].features & LGUEST_DEVICE_F_RANDOMNESS)
  268. irqf |= IRQF_SAMPLE_RANDOM;
  269. if (request_irq(lgdev_irq(lgdev), lguestnet_rcv, irqf, "lguestnet",
  270. dev) != 0) {
  271. pr_debug("lguestnet: cannot get irq %i\n", lgdev_irq(lgdev));
  272. goto unregister;
  273. }
  274. pr_debug("lguestnet: registered device %s\n", dev->name);
  275. lgdev->private = dev;
  276. return 0;
  277. unregister:
  278. unregister_netdev(dev);
  279. unmap:
  280. lguest_unmap(info->peer);
  281. free:
  282. free_netdev(dev);
  283. return err;
  284. }
  285. static struct lguest_driver lguestnet_drv = {
  286. .name = "lguestnet",
  287. .owner = THIS_MODULE,
  288. .device_type = LGUEST_DEVICE_T_NET,
  289. .probe = lguestnet_probe,
  290. };
  291. static __init int lguestnet_init(void)
  292. {
  293. return register_lguest_driver(&lguestnet_drv);
  294. }
  295. module_init(lguestnet_init);
  296. MODULE_DESCRIPTION("Lguest network driver");
  297. MODULE_LICENSE("GPL");