Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* dummy.c: a dummy net driver |
| 2 | |
| 3 | The purpose of this driver is to provide a device to point a |
| 4 | route through, but not to actually transmit packets. |
| 5 | |
| 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. |
| 10 | |
| 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 |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | [when not running slip] |
| 16 | ifconfig dummy slip.addr.ess.here up |
| 17 | [to go to slip] |
| 18 | ifconfig dummy down |
| 19 | dip whatever |
| 20 | |
| 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 |
| 24 | misguided packets. |
| 25 | |
| 26 | Nick Holloway, 27th May 1994 |
| 27 | [I tweaked this explanation a little but that's all] |
| 28 | Alan Cox, 30th May 1994 |
| 29 | */ |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 37 | #include <linux/rtnetlink.h> |
Ezequiel Lara Gomez | 6df014c | 2017-03-11 20:06:54 +0000 | [diff] [blame] | 38 | #include <linux/net_tstamp.h> |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 39 | #include <net/rtnetlink.h> |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 40 | #include <linux/u64_stats_sync.h> |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 41 | |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 42 | #define DRV_NAME "dummy" |
| 43 | #define DRV_VERSION "1.0" |
| 44 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 45 | #undef pr_fmt |
| 46 | #define pr_fmt(fmt) DRV_NAME ": " fmt |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static int numdummies = 1; |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 49 | static int num_vfs; |
| 50 | |
| 51 | struct vf_data_storage { |
| 52 | u8 vf_mac[ETH_ALEN]; |
| 53 | u16 pf_vlan; /* When set, guest VLAN config not allowed. */ |
| 54 | u16 pf_qos; |
| 55 | __be16 vlan_proto; |
| 56 | u16 min_tx_rate; |
| 57 | u16 max_tx_rate; |
| 58 | u8 spoofchk_enabled; |
| 59 | bool rss_query_enabled; |
| 60 | u8 trusted; |
| 61 | int link_state; |
| 62 | }; |
| 63 | |
| 64 | struct dummy_priv { |
| 65 | struct vf_data_storage *vfinfo; |
| 66 | }; |
| 67 | |
| 68 | static int dummy_num_vf(struct device *dev) |
| 69 | { |
| 70 | return num_vfs; |
| 71 | } |
| 72 | |
| 73 | static struct bus_type dummy_bus = { |
| 74 | .name = "dummy", |
| 75 | .num_vf = dummy_num_vf, |
| 76 | }; |
| 77 | |
| 78 | static void release_dummy_parent(struct device *dev) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | static struct device dummy_parent = { |
| 83 | .init_name = "dummy", |
| 84 | .bus = &dummy_bus, |
| 85 | .release = release_dummy_parent, |
| 86 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* fake multicast ability */ |
| 89 | static void set_multicast_list(struct net_device *dev) |
| 90 | { |
| 91 | } |
| 92 | |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 93 | struct pcpu_dstats { |
| 94 | u64 tx_packets; |
| 95 | u64 tx_bytes; |
| 96 | struct u64_stats_sync syncp; |
| 97 | }; |
| 98 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 99 | static void dummy_get_stats64(struct net_device *dev, |
| 100 | struct rtnl_link_stats64 *stats) |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 101 | { |
| 102 | int i; |
| 103 | |
| 104 | for_each_possible_cpu(i) { |
| 105 | const struct pcpu_dstats *dstats; |
| 106 | u64 tbytes, tpackets; |
| 107 | unsigned int start; |
| 108 | |
| 109 | dstats = per_cpu_ptr(dev->dstats, i); |
| 110 | do { |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 111 | start = u64_stats_fetch_begin_irq(&dstats->syncp); |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 112 | tbytes = dstats->tx_bytes; |
| 113 | tpackets = dstats->tx_packets; |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 114 | } while (u64_stats_fetch_retry_irq(&dstats->syncp, start)); |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 115 | stats->tx_bytes += tbytes; |
| 116 | stats->tx_packets += tpackets; |
| 117 | } |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 118 | } |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 119 | |
| 120 | static netdev_tx_t dummy_xmit(struct sk_buff *skb, struct net_device *dev) |
| 121 | { |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 122 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); |
| 123 | |
| 124 | u64_stats_update_begin(&dstats->syncp); |
| 125 | dstats->tx_packets++; |
| 126 | dstats->tx_bytes += skb->len; |
| 127 | u64_stats_update_end(&dstats->syncp); |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 128 | |
Ezequiel Lara Gomez | 6df014c | 2017-03-11 20:06:54 +0000 | [diff] [blame] | 129 | skb_tx_timestamp(skb); |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 130 | dev_kfree_skb(skb); |
| 131 | return NETDEV_TX_OK; |
| 132 | } |
| 133 | |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 134 | static int dummy_dev_init(struct net_device *dev) |
| 135 | { |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 136 | struct dummy_priv *priv = netdev_priv(dev); |
| 137 | |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 138 | dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 139 | if (!dev->dstats) |
| 140 | return -ENOMEM; |
| 141 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 142 | priv->vfinfo = NULL; |
| 143 | |
| 144 | if (!num_vfs) |
| 145 | return 0; |
| 146 | |
| 147 | dev->dev.parent = &dummy_parent; |
| 148 | priv->vfinfo = kcalloc(num_vfs, sizeof(struct vf_data_storage), |
| 149 | GFP_KERNEL); |
| 150 | if (!priv->vfinfo) { |
| 151 | free_percpu(dev->dstats); |
| 152 | return -ENOMEM; |
| 153 | } |
| 154 | |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Hiroaki SHIMODA | 890fdf2 | 2012-04-15 13:26:01 +0000 | [diff] [blame] | 158 | static void dummy_dev_uninit(struct net_device *dev) |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 159 | { |
| 160 | free_percpu(dev->dstats); |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Jiri Pirko | 210ab66 | 2012-12-27 23:49:40 +0000 | [diff] [blame] | 163 | static int dummy_change_carrier(struct net_device *dev, bool new_carrier) |
| 164 | { |
| 165 | if (new_carrier) |
| 166 | netif_carrier_on(dev); |
| 167 | else |
| 168 | netif_carrier_off(dev); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 172 | static int dummy_set_vf_mac(struct net_device *dev, int vf, u8 *mac) |
| 173 | { |
| 174 | struct dummy_priv *priv = netdev_priv(dev); |
| 175 | |
| 176 | if (!is_valid_ether_addr(mac) || (vf >= num_vfs)) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | memcpy(priv->vfinfo[vf].vf_mac, mac, ETH_ALEN); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int dummy_set_vf_vlan(struct net_device *dev, int vf, |
| 185 | u16 vlan, u8 qos, __be16 vlan_proto) |
| 186 | { |
| 187 | struct dummy_priv *priv = netdev_priv(dev); |
| 188 | |
| 189 | if ((vf >= num_vfs) || (vlan > 4095) || (qos > 7)) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | priv->vfinfo[vf].pf_vlan = vlan; |
| 193 | priv->vfinfo[vf].pf_qos = qos; |
| 194 | priv->vfinfo[vf].vlan_proto = vlan_proto; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int dummy_set_vf_rate(struct net_device *dev, int vf, int min, int max) |
| 200 | { |
| 201 | struct dummy_priv *priv = netdev_priv(dev); |
| 202 | |
| 203 | if (vf >= num_vfs) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | priv->vfinfo[vf].min_tx_rate = min; |
| 207 | priv->vfinfo[vf].max_tx_rate = max; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int dummy_set_vf_spoofchk(struct net_device *dev, int vf, bool val) |
| 213 | { |
| 214 | struct dummy_priv *priv = netdev_priv(dev); |
| 215 | |
| 216 | if (vf >= num_vfs) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | priv->vfinfo[vf].spoofchk_enabled = val; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int dummy_set_vf_rss_query_en(struct net_device *dev, int vf, bool val) |
| 225 | { |
| 226 | struct dummy_priv *priv = netdev_priv(dev); |
| 227 | |
| 228 | if (vf >= num_vfs) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | priv->vfinfo[vf].rss_query_enabled = val; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int dummy_set_vf_trust(struct net_device *dev, int vf, bool val) |
| 237 | { |
| 238 | struct dummy_priv *priv = netdev_priv(dev); |
| 239 | |
| 240 | if (vf >= num_vfs) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | priv->vfinfo[vf].trusted = val; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int dummy_get_vf_config(struct net_device *dev, |
| 249 | int vf, struct ifla_vf_info *ivi) |
| 250 | { |
| 251 | struct dummy_priv *priv = netdev_priv(dev); |
| 252 | |
| 253 | if (vf >= num_vfs) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | ivi->vf = vf; |
| 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; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int dummy_set_vf_link_state(struct net_device *dev, int vf, int state) |
| 272 | { |
| 273 | struct dummy_priv *priv = netdev_priv(dev); |
| 274 | |
| 275 | if (vf >= num_vfs) |
| 276 | return -EINVAL; |
| 277 | |
| 278 | priv->vfinfo[vf].link_state = state; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 283 | static const struct net_device_ops dummy_netdev_ops = { |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 284 | .ndo_init = dummy_dev_init, |
Hiroaki SHIMODA | 890fdf2 | 2012-04-15 13:26:01 +0000 | [diff] [blame] | 285 | .ndo_uninit = dummy_dev_uninit, |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 286 | .ndo_start_xmit = dummy_xmit, |
| 287 | .ndo_validate_addr = eth_validate_addr, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 288 | .ndo_set_rx_mode = set_multicast_list, |
Jiri Pirko | 0d1632b | 2012-06-29 05:10:08 +0000 | [diff] [blame] | 289 | .ndo_set_mac_address = eth_mac_addr, |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 290 | .ndo_get_stats64 = dummy_get_stats64, |
Jiri Pirko | 210ab66 | 2012-12-27 23:49:40 +0000 | [diff] [blame] | 291 | .ndo_change_carrier = dummy_change_carrier, |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 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, |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 300 | }; |
| 301 | |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 302 | static void dummy_get_drvinfo(struct net_device *dev, |
| 303 | struct ethtool_drvinfo *info) |
| 304 | { |
| 305 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 306 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 307 | } |
| 308 | |
Ezequiel Lara Gomez | 6df014c | 2017-03-11 20:06:54 +0000 | [diff] [blame] | 309 | static int dummy_get_ts_info(struct net_device *dev, |
| 310 | struct ethtool_ts_info *ts_info) |
| 311 | { |
| 312 | ts_info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | |
| 313 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 314 | SOF_TIMESTAMPING_SOFTWARE; |
| 315 | |
| 316 | ts_info->phc_index = -1; |
| 317 | |
| 318 | return 0; |
| 319 | }; |
| 320 | |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 321 | static const struct ethtool_ops dummy_ethtool_ops = { |
| 322 | .get_drvinfo = dummy_get_drvinfo, |
Ezequiel Lara Gomez | 6df014c | 2017-03-11 20:06:54 +0000 | [diff] [blame] | 323 | .get_ts_info = dummy_get_ts_info, |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 324 | }; |
| 325 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 326 | static void dummy_free_netdev(struct net_device *dev) |
| 327 | { |
| 328 | struct dummy_priv *priv = netdev_priv(dev); |
| 329 | |
| 330 | kfree(priv->vfinfo); |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 333 | static void dummy_setup(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 335 | ether_setup(dev); |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | /* Initialize the device structure. */ |
Stephen Hemminger | aa18e9e | 2008-11-20 20:28:00 -0800 | [diff] [blame] | 338 | dev->netdev_ops = &dummy_netdev_ops; |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 339 | dev->ethtool_ops = &dummy_ethtool_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 340 | dev->needs_free_netdev = true; |
| 341 | dev->priv_destructor = dummy_free_netdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | /* Fill in device structure with ethernet-generic values. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | dev->flags |= IFF_NOARP; |
| 345 | dev->flags &= ~IFF_MULTICAST; |
Phil Sutter | ff42c02 | 2015-08-18 10:30:30 +0200 | [diff] [blame] | 346 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; |
Eric Dumazet | 8f3af27 | 2015-10-19 20:17:59 -0700 | [diff] [blame] | 347 | dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST; |
David S. Miller | 2082499 | 2017-07-03 06:36:07 -0700 | [diff] [blame] | 348 | dev->features |= NETIF_F_ALL_TSO; |
Michał Mirosław | 34324dc | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 349 | dev->features |= NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_LLTX; |
Eric Dumazet | 8f3af27 | 2015-10-19 20:17:59 -0700 | [diff] [blame] | 350 | dev->features |= NETIF_F_GSO_ENCAP_ALL; |
| 351 | dev->hw_features |= dev->features; |
| 352 | dev->hw_enc_features |= dev->features; |
Danny Kukawka | 7ce5d22 | 2012-02-15 06:45:40 +0000 | [diff] [blame] | 353 | eth_hw_addr_random(dev); |
Zhang Shengju | 25e3e84 | 2016-12-07 17:41:33 +0800 | [diff] [blame] | 354 | |
| 355 | dev->min_mtu = 0; |
| 356 | dev->max_mtu = ETH_MAX_MTU; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
Eric Dumazet | 6d81f41 | 2010-09-27 20:50:33 +0000 | [diff] [blame] | 358 | |
Matthias Schiffer | a8b8a889 | 2017-06-25 23:56:01 +0200 | [diff] [blame] | 359 | static int dummy_validate(struct nlattr *tb[], struct nlattr *data[], |
| 360 | struct netlink_ext_ack *extack) |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 361 | { |
| 362 | if (tb[IFLA_ADDRESS]) { |
| 363 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 364 | return -EINVAL; |
| 365 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 366 | return -EADDRNOTAVAIL; |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 371 | static struct rtnl_link_ops dummy_link_ops __read_mostly = { |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 372 | .kind = DRV_NAME, |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 373 | .priv_size = sizeof(struct dummy_priv), |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 374 | .setup = dummy_setup, |
Patrick McHardy | 0e06877 | 2007-07-11 19:42:31 -0700 | [diff] [blame] | 375 | .validate = dummy_validate, |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 376 | }; |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
| 381 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 382 | module_param(num_vfs, int, 0); |
| 383 | MODULE_PARM_DESC(num_vfs, "Number of dummy VFs per dummy device"); |
| 384 | |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 385 | static int __init dummy_init_one(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct net_device *dev_dummy; |
| 388 | int err; |
| 389 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 390 | dev_dummy = alloc_netdev(sizeof(struct dummy_priv), |
| 391 | "dummy%d", NET_NAME_UNKNOWN, dummy_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (!dev_dummy) |
| 393 | return -ENOMEM; |
| 394 | |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 395 | dev_dummy->rtnl_link_ops = &dummy_link_ops; |
| 396 | err = register_netdevice(dev_dummy); |
| 397 | if (err < 0) |
| 398 | goto err; |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 399 | return 0; |
| 400 | |
| 401 | err: |
| 402 | free_netdev(dev_dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return err; |
| 404 | } |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | static int __init dummy_init_module(void) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 407 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | int i, err = 0; |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 409 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 410 | if (num_vfs) { |
| 411 | err = bus_register(&dummy_bus); |
| 412 | if (err < 0) { |
| 413 | pr_err("registering dummy bus failed\n"); |
| 414 | return err; |
| 415 | } |
| 416 | |
| 417 | err = device_register(&dummy_parent); |
| 418 | if (err < 0) { |
| 419 | pr_err("registering dummy parent device failed\n"); |
| 420 | bus_unregister(&dummy_bus); |
| 421 | return err; |
| 422 | } |
| 423 | } |
| 424 | |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 425 | rtnl_lock(); |
| 426 | err = __rtnl_link_register(&dummy_link_ops); |
dingtianhong | 2c8a018 | 2013-07-11 19:04:02 +0800 | [diff] [blame] | 427 | if (err < 0) |
| 428 | goto out; |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 429 | |
Eric Dumazet | 16b0dc2 | 2012-06-10 21:11:57 +0000 | [diff] [blame] | 430 | for (i = 0; i < numdummies && !err; i++) { |
Patrick McHardy | 206c9fb | 2007-06-13 12:04:20 -0700 | [diff] [blame] | 431 | err = dummy_init_one(); |
Eric Dumazet | 16b0dc2 | 2012-06-10 21:11:57 +0000 | [diff] [blame] | 432 | cond_resched(); |
| 433 | } |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 434 | if (err < 0) |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 435 | __rtnl_link_unregister(&dummy_link_ops); |
dingtianhong | 2c8a018 | 2013-07-11 19:04:02 +0800 | [diff] [blame] | 436 | |
| 437 | out: |
Patrick McHardy | 5d5cb17 | 2007-06-13 12:04:34 -0700 | [diff] [blame] | 438 | rtnl_unlock(); |
| 439 | |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 440 | if (err && num_vfs) { |
| 441 | device_unregister(&dummy_parent); |
| 442 | bus_unregister(&dummy_bus); |
| 443 | } |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return err; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
| 448 | static void __exit dummy_cleanup_module(void) |
| 449 | { |
Patrick McHardy | 2d85cba | 2007-07-11 19:42:13 -0700 | [diff] [blame] | 450 | rtnl_link_unregister(&dummy_link_ops); |
Phil Sutter | e163683 | 2017-01-23 12:17:33 +0100 | [diff] [blame] | 451 | |
| 452 | if (num_vfs) { |
| 453 | device_unregister(&dummy_parent); |
| 454 | bus_unregister(&dummy_bus); |
| 455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | module_init(dummy_init_module); |
| 459 | module_exit(dummy_cleanup_module); |
| 460 | MODULE_LICENSE("GPL"); |
Flavio Leitner | c19be73 | 2014-12-05 22:13:24 -0200 | [diff] [blame] | 461 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |
Flavio Leitner | 6c702fa | 2014-12-09 22:41:48 -0200 | [diff] [blame] | 462 | MODULE_VERSION(DRV_VERSION); |