dummy.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* dummy.c: a dummy net driver
  2. The purpose of this driver is to provide a device to point a
  3. route through, but not to actually transmit packets.
  4. Why? If you have a machine whose only connection is an occasional
  5. PPP/SLIP/PLIP link, you can only connect to your own hostname
  6. when the link is up. Otherwise you have to use localhost.
  7. This isn't very consistent.
  8. One solution is to set up a dummy link using PPP/SLIP/PLIP,
  9. but this seems (to me) too much overhead for too little gain.
  10. This driver provides a small alternative. Thus you can do
  11. [when not running slip]
  12. ifconfig dummy slip.addr.ess.here up
  13. [to go to slip]
  14. ifconfig dummy down
  15. dip whatever
  16. This was written by looking at Donald Becker's skeleton driver
  17. and the loopback driver. I then threw away anything that didn't
  18. apply! Thanks to Alan Cox for the key clue on what to do with
  19. misguided packets.
  20. Nick Holloway, 27th May 1994
  21. [I tweaked this explanation a little but that's all]
  22. Alan Cox, 30th May 1994
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/init.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/net_tstamp.h>
  32. #include <net/rtnetlink.h>
  33. #include <linux/u64_stats_sync.h>
  34. #define DRV_NAME "dummy"
  35. #define DRV_VERSION "1.0"
  36. static int numdummies = 1;
  37. /* fake multicast ability */
  38. static void set_multicast_list(struct net_device *dev)
  39. {
  40. }
  41. struct pcpu_dstats {
  42. u64 tx_packets;
  43. u64 tx_bytes;
  44. struct u64_stats_sync syncp;
  45. };
  46. static void dummy_get_stats64(struct net_device *dev,
  47. struct rtnl_link_stats64 *stats)
  48. {
  49. int i;
  50. for_each_possible_cpu(i) {
  51. const struct pcpu_dstats *dstats;
  52. u64 tbytes, tpackets;
  53. unsigned int start;
  54. dstats = per_cpu_ptr(dev->dstats, i);
  55. do {
  56. start = u64_stats_fetch_begin_irq(&dstats->syncp);
  57. tbytes = dstats->tx_bytes;
  58. tpackets = dstats->tx_packets;
  59. } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
  60. stats->tx_bytes += tbytes;
  61. stats->tx_packets += tpackets;
  62. }
  63. }
  64. static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev)
  65. {
  66. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  67. u64_stats_update_begin(&dstats->syncp);
  68. dstats->tx_packets++;
  69. dstats->tx_bytes += skb->len;
  70. u64_stats_update_end(&dstats->syncp);
  71. skb_tx_timestamp(skb);
  72. dev_kfree_skb(skb);
  73. return NETDEV_TX_OK;
  74. }
  75. static int dummy_dev_init(struct net_device *dev)
  76. {
  77. dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
  78. if (!dev->dstats)
  79. return -ENOMEM;
  80. return 0;
  81. }
  82. static void dummy_dev_uninit(struct net_device *dev)
  83. {
  84. free_percpu(dev->dstats);
  85. }
  86. static int dummy_change_carrier(struct net_device *dev, bool new_carrier)
  87. {
  88. if (new_carrier)
  89. netif_carrier_on(dev);
  90. else
  91. netif_carrier_off(dev);
  92. return 0;
  93. }
  94. static const struct net_device_ops dummy_netdev_ops = {
  95. .ndo_init = dummy_dev_init,
  96. .ndo_uninit = dummy_dev_uninit,
  97. .ndo_start_xmit = dummy_xmit,
  98. .ndo_validate_addr = eth_validate_addr,
  99. .ndo_set_rx_mode = set_multicast_list,
  100. .ndo_set_mac_address = eth_mac_addr,
  101. .ndo_get_stats64 = dummy_get_stats64,
  102. .ndo_change_carrier = dummy_change_carrier,
  103. };
  104. static void dummy_get_drvinfo(struct net_device *dev,
  105. struct ethtool_drvinfo *info)
  106. {
  107. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  108. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  109. }
  110. static int dummy_get_ts_info(struct net_device *dev,
  111. struct ethtool_ts_info *ts_info)
  112. {
  113. ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  114. SOF_TIMESTAMPING_RX_SOFTWARE |
  115. SOF_TIMESTAMPING_SOFTWARE;
  116. ts_info->phc_index = -1;
  117. return 0;
  118. };
  119. static const struct ethtool_ops dummy_ethtool_ops = {
  120. .get_drvinfo = dummy_get_drvinfo,
  121. .get_ts_info = dummy_get_ts_info,
  122. };
  123. static void dummy_setup(struct net_device *dev)
  124. {
  125. ether_setup(dev);
  126. /* Initialize the device structure. */
  127. dev->netdev_ops = &dummy_netdev_ops;
  128. dev->ethtool_ops = &dummy_ethtool_ops;
  129. dev->needs_free_netdev = true;
  130. /* Fill in device structure with ethernet-generic values. */
  131. dev->flags |= IFF_NOARP;
  132. dev->flags &= ~IFF_MULTICAST;
  133. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
  134. dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST;
  135. dev->features |= NETIF_F_ALL_TSO;
  136. dev->features |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX;
  137. dev->features |= NETIF_F_GSO_ENCAP_ALL;
  138. dev->hw_features |= dev->features;
  139. dev->hw_enc_features |= dev->features;
  140. eth_hw_addr_random(dev);
  141. dev->min_mtu = 0;
  142. dev->max_mtu = 0;
  143. }
  144. static int dummy_validate(struct nlattr *tb[], struct nlattr *data[],
  145. struct netlink_ext_ack *extack)
  146. {
  147. if (tb[IFLA_ADDRESS]) {
  148. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  149. return -EINVAL;
  150. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  151. return -EADDRNOTAVAIL;
  152. }
  153. return 0;
  154. }
  155. static struct rtnl_link_ops dummy_link_ops __read_mostly = {
  156. .kind = DRV_NAME,
  157. .setup = dummy_setup,
  158. .validate = dummy_validate,
  159. };
  160. /* Number of dummy devices to be set up by this module. */
  161. module_param(numdummies, int, 0);
  162. MODULE_PARM_DESC(numdummies, "Number of dummy pseudo devices");
  163. static int __init dummy_init_one(void)
  164. {
  165. struct net_device *dev_dummy;
  166. int err;
  167. dev_dummy = alloc_netdev(0, "dummy%d", NET_NAME_ENUM, dummy_setup);
  168. if (!dev_dummy)
  169. return -ENOMEM;
  170. dev_dummy->rtnl_link_ops = &dummy_link_ops;
  171. err = register_netdevice(dev_dummy);
  172. if (err < 0)
  173. goto err;
  174. return 0;
  175. err:
  176. free_netdev(dev_dummy);
  177. return err;
  178. }
  179. static int __init dummy_init_module(void)
  180. {
  181. int i, err = 0;
  182. down_write(&pernet_ops_rwsem);
  183. rtnl_lock();
  184. err = __rtnl_link_register(&dummy_link_ops);
  185. if (err < 0)
  186. goto out;
  187. for (i = 0; i < numdummies && !err; i++) {
  188. err = dummy_init_one();
  189. cond_resched();
  190. }
  191. if (err < 0)
  192. __rtnl_link_unregister(&dummy_link_ops);
  193. out:
  194. rtnl_unlock();
  195. up_write(&pernet_ops_rwsem);
  196. return err;
  197. }
  198. static void __exit dummy_cleanup_module(void)
  199. {
  200. rtnl_link_unregister(&dummy_link_ops);
  201. }
  202. module_init(dummy_init_module);
  203. module_exit(dummy_cleanup_module);
  204. MODULE_LICENSE("GPL");
  205. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  206. MODULE_VERSION(DRV_VERSION);