1 /* dummy.c: a dummy net driver
3 The purpose of this driver is to provide a device to point a
4 route through, but not to actually transmit packets.
6 Why? If you have a machine whose only connection is an occasional
7 PPP/SLIP/PLIP link, you can only connect to your own hostname
8 when the link is up. Otherwise you have to use localhost.
9 This isn't very consistent.
11 One solution is to set up a dummy link using PPP/SLIP/PLIP,
12 but this seems (to me) too much overhead for too little gain.
13 This driver provides a small alternative. Thus you can do
15 [when not running slip]
16 ifconfig dummy slip.addr.ess.here up
21 This was written by looking at Donald Becker's skeleton driver
22 and the loopback driver. I then threw away anything that didn't
23 apply! Thanks to Alan Cox for the key clue on what to do with
26 Nick Holloway, 27th May 1994
27 [I tweaked this explanation a little but that's all]
28 Alan Cox, 30th May 1994
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/init.h>
36 #include <linux/moduleparam.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/net_tstamp.h>
39 #include <net/rtnetlink.h>
40 #include <linux/u64_stats_sync.h>
42 #define DRV_NAME "dummy"
43 #define DRV_VERSION "1.0"
46 #define pr_fmt(fmt) DRV_NAME ": " fmt
48 static int numdummies = 1;
51 struct vf_data_storage {
53 u16 pf_vlan; /* When set, guest VLAN config not allowed. */
59 bool rss_query_enabled;
65 struct vf_data_storage *vfinfo;
68 static int dummy_num_vf(struct device *dev)
73 static struct bus_type dummy_bus = {
75 .num_vf = dummy_num_vf,
78 static void release_dummy_parent(struct device *dev)
82 static struct device dummy_parent = {
85 .release = release_dummy_parent,
88 /* fake multicast ability */
89 static void set_multicast_list(struct net_device *dev)
96 struct u64_stats_sync syncp;
99 static void dummy_get_stats64(struct net_device *dev,
100 struct rtnl_link_stats64 *stats)
104 for_each_possible_cpu(i) {
105 const struct pcpu_dstats *dstats;
106 u64 tbytes, tpackets;
109 dstats = per_cpu_ptr(dev->dstats, i);
111 start = u64_stats_fetch_begin_irq(&dstats->syncp);
112 tbytes = dstats->tx_bytes;
113 tpackets = dstats->tx_packets;
114 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
115 stats->tx_bytes += tbytes;
116 stats->tx_packets += tpackets;
120 static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev)
122 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
124 u64_stats_update_begin(&dstats->syncp);
125 dstats->tx_packets++;
126 dstats->tx_bytes += skb->len;
127 u64_stats_update_end(&dstats->syncp);
129 skb_tx_timestamp(skb);
134 static int dummy_dev_init(struct net_device *dev)
136 struct dummy_priv *priv = netdev_priv(dev);
138 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
147 dev->dev.parent = &dummy_parent;
148 priv->vfinfo = kcalloc(num_vfs, sizeof(struct vf_data_storage),
151 free_percpu(dev->dstats);
158 static void dummy_dev_uninit(struct net_device *dev)
160 free_percpu(dev->dstats);
163 static int dummy_change_carrier(struct net_device *dev, bool new_carrier)
166 netif_carrier_on(dev);
168 netif_carrier_off(dev);
172 static int dummy_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
174 struct dummy_priv *priv = netdev_priv(dev);
176 if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
179 memcpy(priv->vfinfo[vf].vf_mac, mac, ETH_ALEN);
184 static int dummy_set_vf_vlan(struct net_device *dev, int vf,
185 u16 vlan, u8 qos, __be16 vlan_proto)
187 struct dummy_priv *priv = netdev_priv(dev);
189 if ((vf >= num_vfs) || (vlan > 4095) || (qos > 7))
192 priv->vfinfo[vf].pf_vlan = vlan;
193 priv->vfinfo[vf].pf_qos = qos;
194 priv->vfinfo[vf].vlan_proto = vlan_proto;
199 static int dummy_set_vf_rate(struct net_device *dev, int vf, int min, int max)
201 struct dummy_priv *priv = netdev_priv(dev);
206 priv->vfinfo[vf].min_tx_rate = min;
207 priv->vfinfo[vf].max_tx_rate = max;
212 static int dummy_set_vf_spoofchk(struct net_device *dev, int vf, bool val)
214 struct dummy_priv *priv = netdev_priv(dev);
219 priv->vfinfo[vf].spoofchk_enabled = val;
224 static int dummy_set_vf_rss_query_en(struct net_device *dev, int vf, bool val)
226 struct dummy_priv *priv = netdev_priv(dev);
231 priv->vfinfo[vf].rss_query_enabled = val;
236 static int dummy_set_vf_trust(struct net_device *dev, int vf, bool val)
238 struct dummy_priv *priv = netdev_priv(dev);
243 priv->vfinfo[vf].trusted = val;
248 static int dummy_get_vf_config(struct net_device *dev,
249 int vf, struct ifla_vf_info *ivi)
251 struct dummy_priv *priv = netdev_priv(dev);
257 memcpy(&ivi->mac, priv->vfinfo[vf].vf_mac, ETH_ALEN);
258 ivi->vlan = priv->vfinfo[vf].pf_vlan;
259 ivi->qos = priv->vfinfo[vf].pf_qos;
260 ivi->spoofchk = priv->vfinfo[vf].spoofchk_enabled;
261 ivi->linkstate = priv->vfinfo[vf].link_state;
262 ivi->min_tx_rate = priv->vfinfo[vf].min_tx_rate;
263 ivi->max_tx_rate = priv->vfinfo[vf].max_tx_rate;
264 ivi->rss_query_en = priv->vfinfo[vf].rss_query_enabled;
265 ivi->trusted = priv->vfinfo[vf].trusted;
266 ivi->vlan_proto = priv->vfinfo[vf].vlan_proto;
271 static int dummy_set_vf_link_state(struct net_device *dev, int vf, int state)
273 struct dummy_priv *priv = netdev_priv(dev);
278 priv->vfinfo[vf].link_state = state;
283 static const struct net_device_ops dummy_netdev_ops = {
284 .ndo_init = dummy_dev_init,
285 .ndo_uninit = dummy_dev_uninit,
286 .ndo_start_xmit = dummy_xmit,
287 .ndo_validate_addr = eth_validate_addr,
288 .ndo_set_rx_mode = set_multicast_list,
289 .ndo_set_mac_address = eth_mac_addr,
290 .ndo_get_stats64 = dummy_get_stats64,
291 .ndo_change_carrier = dummy_change_carrier,
292 .ndo_set_vf_mac = dummy_set_vf_mac,
293 .ndo_set_vf_vlan = dummy_set_vf_vlan,
294 .ndo_set_vf_rate = dummy_set_vf_rate,
295 .ndo_set_vf_spoofchk = dummy_set_vf_spoofchk,
296 .ndo_set_vf_trust = dummy_set_vf_trust,
297 .ndo_get_vf_config = dummy_get_vf_config,
298 .ndo_set_vf_link_state = dummy_set_vf_link_state,
299 .ndo_set_vf_rss_query_en = dummy_set_vf_rss_query_en,
302 static void dummy_get_drvinfo(struct net_device *dev,
303 struct ethtool_drvinfo *info)
305 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
306 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
309 static int dummy_get_ts_info(struct net_device *dev,
310 struct ethtool_ts_info *ts_info)
312 ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
313 SOF_TIMESTAMPING_RX_SOFTWARE |
314 SOF_TIMESTAMPING_SOFTWARE;
316 ts_info->phc_index = -1;
321 static const struct ethtool_ops dummy_ethtool_ops = {
322 .get_drvinfo = dummy_get_drvinfo,
323 .get_ts_info = dummy_get_ts_info,
326 static void dummy_free_netdev(struct net_device *dev)
328 struct dummy_priv *priv = netdev_priv(dev);
333 static void dummy_setup(struct net_device *dev)
337 /* Initialize the device structure. */
338 dev->netdev_ops = &dummy_netdev_ops;
339 dev->ethtool_ops = &dummy_ethtool_ops;
340 dev->needs_free_netdev = true;
341 dev->priv_destructor = dummy_free_netdev;
343 /* Fill in device structure with ethernet-generic values. */
344 dev->flags |= IFF_NOARP;
345 dev->flags &= ~IFF_MULTICAST;
346 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
347 dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST;
348 dev->features |= NETIF_F_ALL_TSO;
349 dev->features |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX;
350 dev->features |= NETIF_F_GSO_ENCAP_ALL;
351 dev->hw_features |= dev->features;
352 dev->hw_enc_features |= dev->features;
353 eth_hw_addr_random(dev);
356 dev->max_mtu = ETH_MAX_MTU;
359 static int dummy_validate(struct nlattr *tb[], struct nlattr *data[],
360 struct netlink_ext_ack *extack)
362 if (tb[IFLA_ADDRESS]) {
363 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
365 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
366 return -EADDRNOTAVAIL;
371 static struct rtnl_link_ops dummy_link_ops __read_mostly = {
373 .priv_size = sizeof(struct dummy_priv),
374 .setup = dummy_setup,
375 .validate = dummy_validate,
378 /* Number of dummy devices to be set up by this module. */
379 module_param(numdummies, int, 0);
380 MODULE_PARM_DESC(numdummies, "Number of dummy pseudo devices");
382 module_param(num_vfs, int, 0);
383 MODULE_PARM_DESC(num_vfs, "Number of dummy VFs per dummy device");
385 static int __init dummy_init_one(void)
387 struct net_device *dev_dummy;
390 dev_dummy = alloc_netdev(sizeof(struct dummy_priv),
391 "dummy%d", NET_NAME_UNKNOWN, dummy_setup);
395 dev_dummy->rtnl_link_ops = &dummy_link_ops;
396 err = register_netdevice(dev_dummy);
402 free_netdev(dev_dummy);
406 static int __init dummy_init_module(void)
411 err = bus_register(&dummy_bus);
413 pr_err("registering dummy bus failed\n");
417 err = device_register(&dummy_parent);
419 pr_err("registering dummy parent device failed\n");
420 bus_unregister(&dummy_bus);
426 err = __rtnl_link_register(&dummy_link_ops);
430 for (i = 0; i < numdummies && !err; i++) {
431 err = dummy_init_one();
435 __rtnl_link_unregister(&dummy_link_ops);
440 if (err && num_vfs) {
441 device_unregister(&dummy_parent);
442 bus_unregister(&dummy_bus);
448 static void __exit dummy_cleanup_module(void)
450 rtnl_link_unregister(&dummy_link_ops);
453 device_unregister(&dummy_parent);
454 bus_unregister(&dummy_bus);
458 module_init(dummy_init_module);
459 module_exit(dummy_cleanup_module);
460 MODULE_LICENSE("GPL");
461 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
462 MODULE_VERSION(DRV_VERSION);