Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
Jeff Kirsher | adf8d3f | 2013-12-06 06:28:47 -0800 | [diff] [blame] | 14 | * this program; if not, see <http://www.gnu.org/licenses/>. |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 15 | * |
| 16 | * Authors: |
Haiyang Zhang | d0e94d1 | 2009-11-23 17:00:22 +0000 | [diff] [blame] | 17 | * Haiyang Zhang <haiyangz@microsoft.com> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 18 | * Hank Janssen <hjanssen@microsoft.com> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 19 | */ |
Hank Janssen | eb335bc | 2011-03-29 13:58:48 -0700 | [diff] [blame] | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 21 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
K. Y. Srinivasan | 9079ce6 | 2011-06-16 13:16:37 -0700 | [diff] [blame] | 23 | #include <linux/atomic.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/device.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 27 | #include <linux/io.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 28 | #include <linux/delay.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/inetdevice.h> |
| 31 | #include <linux/etherdevice.h> |
| 32 | #include <linux/skbuff.h> |
Haiyang Zhang | c802db1 | 2013-05-28 06:15:56 +0000 | [diff] [blame] | 33 | #include <linux/if_vlan.h> |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 34 | #include <linux/in.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
stephen hemminger | 27f5aa9 | 2017-07-24 10:57:29 -0700 | [diff] [blame] | 36 | #include <linux/rtnetlink.h> |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 37 | #include <linux/netpoll.h> |
stephen hemminger | 27f5aa9 | 2017-07-24 10:57:29 -0700 | [diff] [blame] | 38 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 39 | #include <net/arp.h> |
| 40 | #include <net/route.h> |
| 41 | #include <net/sock.h> |
| 42 | #include <net/pkt_sched.h> |
Michael Kelley | 8eb1b3c | 2017-05-30 11:36:56 -0700 | [diff] [blame] | 43 | #include <net/checksum.h> |
| 44 | #include <net/ip6_checksum.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 45 | |
K. Y. Srinivasan | 5ca7252 | 2011-05-12 19:34:37 -0700 | [diff] [blame] | 46 | #include "hyperv_net.h" |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 47 | |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 48 | #define RING_SIZE_MIN 64 |
| 49 | #define NETVSC_MIN_TX_SECTIONS 10 |
| 50 | #define NETVSC_DEFAULT_TX 192 /* ~1M */ |
| 51 | #define NETVSC_MIN_RX_SECTIONS 10 /* ~64K */ |
Stephen Hemminger | 5023a6d | 2017-09-14 09:31:07 -0700 | [diff] [blame] | 52 | #define NETVSC_DEFAULT_RX 10485 /* Max ~16M */ |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 53 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 54 | #define LINKCHANGE_INT (2 * HZ) |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 55 | #define VF_TAKEOVER_INT (HZ / 10) |
stephen hemminger | a50af86 | 2016-12-06 13:43:54 -0800 | [diff] [blame] | 56 | |
Hank Janssen | 99c8da0 | 2010-10-12 10:45:23 -0700 | [diff] [blame] | 57 | static int ring_size = 128; |
Stephen Hemminger | 450d7a4 | 2010-05-04 09:58:53 -0700 | [diff] [blame] | 58 | module_param(ring_size, int, S_IRUGO); |
| 59 | MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)"); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 60 | |
Simon Xiao | 3f300ff | 2015-04-28 01:05:17 -0700 | [diff] [blame] | 61 | static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | |
| 62 | NETIF_MSG_LINK | NETIF_MSG_IFUP | |
| 63 | NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | |
| 64 | NETIF_MSG_TX_ERR; |
| 65 | |
| 66 | static int debug = -1; |
| 67 | module_param(debug, int, S_IRUGO); |
| 68 | MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); |
| 69 | |
Greg Kroah-Hartman | 4e9bfef | 2009-07-15 12:45:20 -0700 | [diff] [blame] | 70 | static void netvsc_set_multicast_list(struct net_device *net) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 71 | { |
Wenqi Ma | 792df87 | 2012-04-19 00:39:37 +0000 | [diff] [blame] | 72 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 4f19c0d | 2017-06-07 15:53:49 -0700 | [diff] [blame] | 73 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
Haiyang Zhang | d426b2e | 2011-11-30 07:19:08 -0800 | [diff] [blame] | 74 | |
stephen hemminger | 4f19c0d | 2017-06-07 15:53:49 -0700 | [diff] [blame] | 75 | rndis_filter_update(nvdev); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 78 | static int netvsc_open(struct net_device *net) |
| 79 | { |
Haiyang Zhang | 53fa1a6 | 2017-06-21 16:40:47 -0700 | [diff] [blame] | 80 | struct net_device_context *ndev_ctx = netdev_priv(net); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 81 | struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
stephen hemminger | 79e8cbe | 2017-07-19 11:53:13 -0700 | [diff] [blame] | 82 | struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev); |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 83 | struct rndis_device *rdev; |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 84 | int ret = 0; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 85 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 86 | netif_carrier_off(net); |
| 87 | |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 88 | /* Open up the device */ |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 89 | ret = rndis_filter_open(nvdev); |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 90 | if (ret != 0) { |
| 91 | netdev_err(net, "unable to open device (ret %d).\n", ret); |
| 92 | return ret; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 95 | netif_tx_wake_all_queues(net); |
Haiyang Zhang | d515d0f | 2011-09-28 13:24:15 -0700 | [diff] [blame] | 96 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 97 | rdev = nvdev->extension; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 98 | |
| 99 | if (!rdev->link_state) |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 100 | netif_carrier_on(net); |
| 101 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 102 | if (vf_netdev) { |
| 103 | /* Setting synthetic device up transparently sets |
| 104 | * slave as up. If open fails, then slave will be |
| 105 | * still be offline (and not used). |
| 106 | */ |
| 107 | ret = dev_open(vf_netdev); |
| 108 | if (ret) |
| 109 | netdev_warn(net, |
| 110 | "unable to open slave: %s: %d\n", |
| 111 | vf_netdev->name, ret); |
| 112 | } |
| 113 | return 0; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 116 | static int netvsc_close(struct net_device *net) |
| 117 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 118 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 119 | struct net_device *vf_netdev |
| 120 | = rtnl_dereference(net_device_ctx->vf_netdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 121 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
Haiyang Zhang | c6f71c4 | 2017-08-24 11:50:02 -0700 | [diff] [blame] | 122 | int ret = 0; |
stephen hemminger | 4097596 | 2017-06-08 16:21:19 -0700 | [diff] [blame] | 123 | u32 aread, i, msec = 10, retry = 0, retry_max = 20; |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 124 | struct vmbus_channel *chn; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 125 | |
Haiyang Zhang | 0a28253 | 2012-02-02 07:17:59 +0000 | [diff] [blame] | 126 | netif_tx_disable(net); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 127 | |
Haiyang Zhang | c6f71c4 | 2017-08-24 11:50:02 -0700 | [diff] [blame] | 128 | /* No need to close rndis filter if it is removed already */ |
| 129 | if (!nvdev) |
| 130 | goto out; |
| 131 | |
Vitaly Kuznetsov | 2f5fa6c | 2016-06-03 17:51:00 +0200 | [diff] [blame] | 132 | ret = rndis_filter_close(nvdev); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 133 | if (ret != 0) { |
Hank Janssen | eb335bc | 2011-03-29 13:58:48 -0700 | [diff] [blame] | 134 | netdev_err(net, "unable to close device (ret %d).\n", ret); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* Ensure pending bytes in ring are read */ |
| 139 | while (true) { |
| 140 | aread = 0; |
| 141 | for (i = 0; i < nvdev->num_chn; i++) { |
stephen hemminger | b8b835a | 2017-01-24 13:06:07 -0800 | [diff] [blame] | 142 | chn = nvdev->chan_table[i].channel; |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 143 | if (!chn) |
| 144 | continue; |
| 145 | |
stephen hemminger | 4097596 | 2017-06-08 16:21:19 -0700 | [diff] [blame] | 146 | aread = hv_get_bytes_to_read(&chn->inbound); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 147 | if (aread) |
| 148 | break; |
| 149 | |
stephen hemminger | 4097596 | 2017-06-08 16:21:19 -0700 | [diff] [blame] | 150 | aread = hv_get_bytes_to_read(&chn->outbound); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 151 | if (aread) |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | retry++; |
| 156 | if (retry > retry_max || aread == 0) |
| 157 | break; |
| 158 | |
| 159 | msleep(msec); |
| 160 | |
| 161 | if (msec < 1000) |
| 162 | msec *= 2; |
| 163 | } |
| 164 | |
| 165 | if (aread) { |
| 166 | netdev_err(net, "Ring buffer not empty after closing rndis\n"); |
| 167 | ret = -ETIMEDOUT; |
| 168 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 169 | |
Haiyang Zhang | c6f71c4 | 2017-08-24 11:50:02 -0700 | [diff] [blame] | 170 | out: |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 171 | if (vf_netdev) |
| 172 | dev_close(vf_netdev); |
| 173 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 174 | return ret; |
| 175 | } |
| 176 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 177 | static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size, |
stephen hemminger | 89bb42b | 2017-08-09 17:46:08 -0700 | [diff] [blame] | 178 | int pkt_type) |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 179 | { |
| 180 | struct rndis_packet *rndis_pkt; |
| 181 | struct rndis_per_packet_info *ppi; |
| 182 | |
| 183 | rndis_pkt = &msg->msg.pkt; |
| 184 | rndis_pkt->data_offset += ppi_size; |
| 185 | |
| 186 | ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt + |
| 187 | rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len); |
| 188 | |
| 189 | ppi->size = ppi_size; |
| 190 | ppi->type = pkt_type; |
| 191 | ppi->ppi_offset = sizeof(struct rndis_per_packet_info); |
| 192 | |
| 193 | rndis_pkt->per_pkt_info_len += ppi_size; |
| 194 | |
| 195 | return ppi; |
| 196 | } |
| 197 | |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 198 | /* Azure hosts don't support non-TCP port numbers in hashing for fragmented |
| 199 | * packets. We can use ethtool to change UDP hash level when necessary. |
Haiyang Zhang | f72860a | 2017-04-12 11:45:18 -0700 | [diff] [blame] | 200 | */ |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 201 | static inline u32 netvsc_get_hash( |
| 202 | struct sk_buff *skb, |
| 203 | const struct net_device_context *ndc) |
Haiyang Zhang | f72860a | 2017-04-12 11:45:18 -0700 | [diff] [blame] | 204 | { |
| 205 | struct flow_keys flow; |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 206 | u32 hash, pkt_proto = 0; |
Haiyang Zhang | f72860a | 2017-04-12 11:45:18 -0700 | [diff] [blame] | 207 | static u32 hashrnd __read_mostly; |
| 208 | |
| 209 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 210 | |
| 211 | if (!skb_flow_dissect_flow_keys(skb, &flow, 0)) |
| 212 | return 0; |
| 213 | |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 214 | switch (flow.basic.ip_proto) { |
| 215 | case IPPROTO_TCP: |
| 216 | if (flow.basic.n_proto == htons(ETH_P_IP)) |
| 217 | pkt_proto = HV_TCP4_L4HASH; |
| 218 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) |
| 219 | pkt_proto = HV_TCP6_L4HASH; |
| 220 | |
| 221 | break; |
| 222 | |
| 223 | case IPPROTO_UDP: |
| 224 | if (flow.basic.n_proto == htons(ETH_P_IP)) |
| 225 | pkt_proto = HV_UDP4_L4HASH; |
| 226 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) |
| 227 | pkt_proto = HV_UDP6_L4HASH; |
| 228 | |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | if (pkt_proto & ndc->l4_hash) { |
Haiyang Zhang | f72860a | 2017-04-12 11:45:18 -0700 | [diff] [blame] | 233 | return skb_get_hash(skb); |
| 234 | } else { |
| 235 | if (flow.basic.n_proto == htons(ETH_P_IP)) |
| 236 | hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd); |
| 237 | else if (flow.basic.n_proto == htons(ETH_P_IPV6)) |
| 238 | hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd); |
| 239 | else |
| 240 | hash = 0; |
| 241 | |
| 242 | skb_set_hash(skb, hash, PKT_HASH_TYPE_L3); |
| 243 | } |
| 244 | |
| 245 | return hash; |
| 246 | } |
| 247 | |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 248 | static inline int netvsc_get_tx_queue(struct net_device *ndev, |
| 249 | struct sk_buff *skb, int old_idx) |
| 250 | { |
| 251 | const struct net_device_context *ndc = netdev_priv(ndev); |
| 252 | struct sock *sk = skb->sk; |
| 253 | int q_idx; |
| 254 | |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 255 | q_idx = ndc->tx_send_table[netvsc_get_hash(skb, ndc) & |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 256 | (VRSS_SEND_TAB_SIZE - 1)]; |
| 257 | |
| 258 | /* If queue index changed record the new value */ |
| 259 | if (q_idx != old_idx && |
| 260 | sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache)) |
| 261 | sk_tx_queue_set(sk, q_idx); |
| 262 | |
| 263 | return q_idx; |
| 264 | } |
| 265 | |
stephen hemminger | d8e18ee | 2017-01-24 13:06:05 -0800 | [diff] [blame] | 266 | /* |
| 267 | * Select queue for transmit. |
| 268 | * |
| 269 | * If a valid queue has already been assigned, then use that. |
| 270 | * Otherwise compute tx queue based on hash and the send table. |
| 271 | * |
| 272 | * This is basically similar to default (__netdev_pick_tx) with the added step |
| 273 | * of using the host send_table when no other queue has been assigned. |
| 274 | * |
| 275 | * TODO support XPS - but get_xps_queue not exported |
| 276 | */ |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 277 | static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb) |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 278 | { |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 279 | int q_idx = sk_tx_queue_get(skb->sk); |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 280 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 281 | if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) { |
Haiyang Zhang | 8db91f6 | 2017-04-12 11:35:05 -0700 | [diff] [blame] | 282 | /* If forwarding a packet, we use the recorded queue when |
| 283 | * available for better cache locality. |
| 284 | */ |
| 285 | if (skb_rx_queue_recorded(skb)) |
| 286 | q_idx = skb_get_rx_queue(skb); |
| 287 | else |
| 288 | q_idx = netvsc_get_tx_queue(ndev, skb, q_idx); |
stephen hemminger | d8e18ee | 2017-01-24 13:06:05 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 291 | return q_idx; |
| 292 | } |
| 293 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 294 | static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb, |
| 295 | void *accel_priv, |
| 296 | select_queue_fallback_t fallback) |
| 297 | { |
| 298 | struct net_device_context *ndc = netdev_priv(ndev); |
| 299 | struct net_device *vf_netdev; |
| 300 | u16 txq; |
| 301 | |
| 302 | rcu_read_lock(); |
| 303 | vf_netdev = rcu_dereference(ndc->vf_netdev); |
| 304 | if (vf_netdev) { |
| 305 | txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0; |
| 306 | qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping; |
| 307 | } else { |
| 308 | txq = netvsc_pick_tx(ndev, skb); |
| 309 | } |
| 310 | rcu_read_unlock(); |
| 311 | |
| 312 | while (unlikely(txq >= ndev->real_num_tx_queues)) |
| 313 | txq -= ndev->real_num_tx_queues; |
| 314 | |
| 315 | return txq; |
| 316 | } |
| 317 | |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 318 | static u32 fill_pg_buf(struct page *page, u32 offset, u32 len, |
stephen hemminger | 89bb42b | 2017-08-09 17:46:08 -0700 | [diff] [blame] | 319 | struct hv_page_buffer *pb) |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 320 | { |
| 321 | int j = 0; |
| 322 | |
| 323 | /* Deal with compund pages by ignoring unused part |
| 324 | * of the page. |
| 325 | */ |
| 326 | page += (offset >> PAGE_SHIFT); |
| 327 | offset &= ~PAGE_MASK; |
| 328 | |
| 329 | while (len > 0) { |
| 330 | unsigned long bytes; |
| 331 | |
| 332 | bytes = PAGE_SIZE - offset; |
| 333 | if (bytes > len) |
| 334 | bytes = len; |
| 335 | pb[j].pfn = page_to_pfn(page); |
| 336 | pb[j].offset = offset; |
| 337 | pb[j].len = bytes; |
| 338 | |
| 339 | offset += bytes; |
| 340 | len -= bytes; |
| 341 | |
| 342 | if (offset == PAGE_SIZE && len) { |
| 343 | page++; |
| 344 | offset = 0; |
| 345 | j++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return j + 1; |
| 350 | } |
| 351 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 352 | static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb, |
KY Srinivasan | a9f2e2d | 2015-12-01 16:43:13 -0800 | [diff] [blame] | 353 | struct hv_netvsc_packet *packet, |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 354 | struct hv_page_buffer *pb) |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 355 | { |
| 356 | u32 slots_used = 0; |
| 357 | char *data = skb->data; |
| 358 | int frags = skb_shinfo(skb)->nr_frags; |
| 359 | int i; |
| 360 | |
| 361 | /* The packet is laid out thus: |
Haiyang Zhang | aa0a34b | 2015-04-13 16:34:35 -0700 | [diff] [blame] | 362 | * 1. hdr: RNDIS header and PPI |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 363 | * 2. skb linear data |
| 364 | * 3. skb fragment data |
| 365 | */ |
stephen hemminger | ea5a32c | 2017-08-09 17:46:10 -0700 | [diff] [blame] | 366 | slots_used += fill_pg_buf(virt_to_page(hdr), |
| 367 | offset_in_page(hdr), |
| 368 | len, &pb[slots_used]); |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 369 | |
Haiyang Zhang | aa0a34b | 2015-04-13 16:34:35 -0700 | [diff] [blame] | 370 | packet->rmsg_size = len; |
| 371 | packet->rmsg_pgcnt = slots_used; |
| 372 | |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 373 | slots_used += fill_pg_buf(virt_to_page(data), |
| 374 | offset_in_page(data), |
| 375 | skb_headlen(skb), &pb[slots_used]); |
| 376 | |
| 377 | for (i = 0; i < frags; i++) { |
| 378 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 379 | |
| 380 | slots_used += fill_pg_buf(skb_frag_page(frag), |
| 381 | frag->page_offset, |
| 382 | skb_frag_size(frag), &pb[slots_used]); |
| 383 | } |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 384 | return slots_used; |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 385 | } |
| 386 | |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 387 | static int count_skb_frag_slots(struct sk_buff *skb) |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 388 | { |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 389 | int i, frags = skb_shinfo(skb)->nr_frags; |
| 390 | int pages = 0; |
| 391 | |
| 392 | for (i = 0; i < frags; i++) { |
| 393 | skb_frag_t *frag = skb_shinfo(skb)->frags + i; |
| 394 | unsigned long size = skb_frag_size(frag); |
| 395 | unsigned long offset = frag->page_offset; |
| 396 | |
| 397 | /* Skip unused frames from start of page */ |
| 398 | offset &= ~PAGE_MASK; |
| 399 | pages += PFN_UP(offset + size); |
| 400 | } |
| 401 | return pages; |
| 402 | } |
| 403 | |
| 404 | static int netvsc_get_slots(struct sk_buff *skb) |
| 405 | { |
| 406 | char *data = skb->data; |
| 407 | unsigned int offset = offset_in_page(data); |
| 408 | unsigned int len = skb_headlen(skb); |
| 409 | int slots; |
| 410 | int frag_slots; |
| 411 | |
| 412 | slots = DIV_ROUND_UP(offset + len, PAGE_SIZE); |
| 413 | frag_slots = count_skb_frag_slots(skb); |
| 414 | return slots + frag_slots; |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 415 | } |
| 416 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 417 | static u32 net_checksum_info(struct sk_buff *skb) |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 418 | { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 419 | if (skb->protocol == htons(ETH_P_IP)) { |
| 420 | struct iphdr *ip = ip_hdr(skb); |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 421 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 422 | if (ip->protocol == IPPROTO_TCP) |
| 423 | return TRANSPORT_INFO_IPV4_TCP; |
| 424 | else if (ip->protocol == IPPROTO_UDP) |
| 425 | return TRANSPORT_INFO_IPV4_UDP; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 426 | } else { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 427 | struct ipv6hdr *ip6 = ipv6_hdr(skb); |
| 428 | |
| 429 | if (ip6->nexthdr == IPPROTO_TCP) |
| 430 | return TRANSPORT_INFO_IPV6_TCP; |
Mohammed Gamal | 37b9dfa | 2017-07-24 10:57:26 -0700 | [diff] [blame] | 431 | else if (ip6->nexthdr == IPPROTO_UDP) |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 432 | return TRANSPORT_INFO_IPV6_UDP; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 433 | } |
| 434 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 435 | return TRANSPORT_INFO_NOT_IP; |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 436 | } |
| 437 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 438 | /* Send skb on the slave VF device. */ |
| 439 | static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev, |
| 440 | struct sk_buff *skb) |
| 441 | { |
| 442 | struct net_device_context *ndev_ctx = netdev_priv(net); |
| 443 | unsigned int len = skb->len; |
| 444 | int rc; |
| 445 | |
| 446 | skb->dev = vf_netdev; |
| 447 | skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping; |
| 448 | |
| 449 | rc = dev_queue_xmit(skb); |
| 450 | if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) { |
| 451 | struct netvsc_vf_pcpu_stats *pcpu_stats |
| 452 | = this_cpu_ptr(ndev_ctx->vf_stats); |
| 453 | |
| 454 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 455 | pcpu_stats->tx_packets++; |
| 456 | pcpu_stats->tx_bytes += len; |
| 457 | u64_stats_update_end(&pcpu_stats->syncp); |
| 458 | } else { |
| 459 | this_cpu_inc(ndev_ctx->vf_stats->tx_dropped); |
| 460 | } |
| 461 | |
| 462 | return rc; |
| 463 | } |
| 464 | |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 465 | static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 466 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 467 | struct net_device_context *net_device_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 981a1bd | 2015-04-08 17:54:05 +0200 | [diff] [blame] | 468 | struct hv_netvsc_packet *packet = NULL; |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 469 | int ret; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 470 | unsigned int num_data_pgs; |
| 471 | struct rndis_message *rndis_msg; |
| 472 | struct rndis_packet *rndis_pkt; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 473 | struct net_device *vf_netdev; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 474 | u32 rndis_msg_size; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 475 | struct rndis_per_packet_info *ppi; |
Haiyang Zhang | 307f099 | 2014-05-21 12:55:39 -0700 | [diff] [blame] | 476 | u32 hash; |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 477 | struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT]; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 478 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 479 | /* if VF is present and up then redirect packets |
| 480 | * already called with rcu_read_lock_bh |
| 481 | */ |
| 482 | vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev); |
| 483 | if (vf_netdev && netif_running(vf_netdev) && |
| 484 | !netpoll_tx_running(net)) |
| 485 | return netvsc_vf_xmit(net, vf_netdev, skb); |
| 486 | |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 487 | /* We will atmost need two pages to describe the rndis |
| 488 | * header. We can only transmit MAX_PAGE_BUFFER_COUNT number |
Vitaly Kuznetsov | e88f7e0 | 2015-04-08 17:54:06 +0200 | [diff] [blame] | 489 | * of pages in a single packet. If skb is scattered around |
| 490 | * more pages we try linearizing it. |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 491 | */ |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 492 | |
| 493 | num_data_pgs = netvsc_get_slots(skb) + 2; |
| 494 | |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 495 | if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) { |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 496 | ++net_device_ctx->eth_stats.tx_scattered; |
| 497 | |
| 498 | if (skb_linearize(skb)) |
| 499 | goto no_memory; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 500 | |
stephen hemminger | 80d887d | 2017-07-24 21:03:19 -0700 | [diff] [blame] | 501 | num_data_pgs = netvsc_get_slots(skb) + 2; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 502 | if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) { |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 503 | ++net_device_ctx->eth_stats.tx_too_big; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 504 | goto drop; |
| 505 | } |
KY Srinivasan | 54a7357 | 2014-03-08 19:23:13 -0800 | [diff] [blame] | 506 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 507 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 508 | /* |
| 509 | * Place the rndis header in the skb head room and |
| 510 | * the skb->cb will be used for hv_netvsc_packet |
| 511 | * structure. |
| 512 | */ |
| 513 | ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE); |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 514 | if (ret) |
| 515 | goto no_memory; |
| 516 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 517 | /* Use the skb control buffer for building up the packet */ |
| 518 | BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) > |
| 519 | FIELD_SIZEOF(struct sk_buff, cb)); |
| 520 | packet = (struct hv_netvsc_packet *)skb->cb; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 521 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 522 | packet->q_idx = skb_get_queue_mapping(skb); |
| 523 | |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 524 | packet->total_data_buflen = skb->len; |
stephen hemminger | 793e395 | 2017-01-24 13:06:12 -0800 | [diff] [blame] | 525 | packet->total_bytes = skb->len; |
| 526 | packet->total_packets = 1; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 527 | |
KY Srinivasan | c0eb454 | 2015-12-01 16:43:10 -0800 | [diff] [blame] | 528 | rndis_msg = (struct rndis_message *)skb->head; |
KY Srinivasan | b08cc79 | 2015-03-29 21:08:42 -0700 | [diff] [blame] | 529 | |
KY Srinivasan | 2447676 | 2015-12-01 16:43:06 -0800 | [diff] [blame] | 530 | memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 531 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 532 | /* Add the rndis header */ |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 533 | rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET; |
| 534 | rndis_msg->msg_len = packet->total_data_buflen; |
| 535 | rndis_pkt = &rndis_msg->msg.pkt; |
| 536 | rndis_pkt->data_offset = sizeof(struct rndis_packet); |
| 537 | rndis_pkt->data_len = packet->total_data_buflen; |
| 538 | rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet); |
| 539 | |
| 540 | rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet); |
| 541 | |
Haiyang Zhang | 307f099 | 2014-05-21 12:55:39 -0700 | [diff] [blame] | 542 | hash = skb_get_hash_raw(skb); |
| 543 | if (hash != 0 && net->real_num_tx_queues > 1) { |
| 544 | rndis_msg_size += NDIS_HASH_PPI_SIZE; |
| 545 | ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE, |
| 546 | NBL_HASH_VALUE); |
| 547 | *(u32 *)((void *)ppi + ppi->ppi_offset) = hash; |
| 548 | } |
| 549 | |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 550 | if (skb_vlan_tag_present(skb)) { |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 551 | struct ndis_pkt_8021q_info *vlan; |
| 552 | |
| 553 | rndis_msg_size += NDIS_VLAN_PPI_SIZE; |
| 554 | ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE, |
stephen hemminger | 00f5024 | 2017-08-09 17:46:09 -0700 | [diff] [blame] | 555 | IEEE_8021Q_INFO); |
| 556 | |
| 557 | vlan = (void *)ppi + ppi->ppi_offset; |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 558 | vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK; |
| 559 | vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >> |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 560 | VLAN_PRIO_SHIFT; |
| 561 | } |
| 562 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 563 | if (skb_is_gso(skb)) { |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 564 | struct ndis_tcp_lso_info *lso_info; |
| 565 | |
| 566 | rndis_msg_size += NDIS_LSO_PPI_SIZE; |
| 567 | ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE, |
| 568 | TCP_LARGESEND_PKTINFO); |
| 569 | |
stephen hemminger | 00f5024 | 2017-08-09 17:46:09 -0700 | [diff] [blame] | 570 | lso_info = (void *)ppi + ppi->ppi_offset; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 571 | |
| 572 | lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE; |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 573 | if (skb->protocol == htons(ETH_P_IP)) { |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 574 | lso_info->lso_v2_transmit.ip_version = |
| 575 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4; |
| 576 | ip_hdr(skb)->tot_len = 0; |
| 577 | ip_hdr(skb)->check = 0; |
| 578 | tcp_hdr(skb)->check = |
| 579 | ~csum_tcpudp_magic(ip_hdr(skb)->saddr, |
| 580 | ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); |
| 581 | } else { |
| 582 | lso_info->lso_v2_transmit.ip_version = |
| 583 | NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6; |
| 584 | ipv6_hdr(skb)->payload_len = 0; |
| 585 | tcp_hdr(skb)->check = |
| 586 | ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
| 587 | &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); |
| 588 | } |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 589 | lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb); |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 590 | lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size; |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 591 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 592 | if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) { |
| 593 | struct ndis_tcp_ip_checksum_info *csum_info; |
| 594 | |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 595 | rndis_msg_size += NDIS_CSUM_PPI_SIZE; |
| 596 | ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE, |
| 597 | TCPIP_CHKSUM_PKTINFO); |
| 598 | |
| 599 | csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi + |
| 600 | ppi->ppi_offset); |
| 601 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 602 | csum_info->transmit.tcp_header_offset = skb_transport_offset(skb); |
| 603 | |
| 604 | if (skb->protocol == htons(ETH_P_IP)) { |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 605 | csum_info->transmit.is_ipv4 = 1; |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 606 | |
| 607 | if (ip_hdr(skb)->protocol == IPPROTO_TCP) |
| 608 | csum_info->transmit.tcp_checksum = 1; |
| 609 | else |
| 610 | csum_info->transmit.udp_checksum = 1; |
| 611 | } else { |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 612 | csum_info->transmit.is_ipv6 = 1; |
| 613 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 614 | if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP) |
| 615 | csum_info->transmit.tcp_checksum = 1; |
| 616 | else |
| 617 | csum_info->transmit.udp_checksum = 1; |
| 618 | } |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 619 | } else { |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 620 | /* Can't do offload of this type of checksum */ |
stephen hemminger | ad19bc8 | 2016-10-11 14:03:07 -0700 | [diff] [blame] | 621 | if (skb_checksum_help(skb)) |
| 622 | goto drop; |
| 623 | } |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 624 | } |
KY Srinivasan | 08cd04b | 2014-03-08 19:23:17 -0800 | [diff] [blame] | 625 | |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 626 | /* Start filling in the page buffers with the rndis hdr */ |
| 627 | rndis_msg->msg_len += rndis_msg_size; |
Haiyang Zhang | 942396b | 2014-10-22 13:47:18 -0700 | [diff] [blame] | 628 | packet->total_data_buflen = rndis_msg->msg_len; |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 629 | packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size, |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 630 | skb, packet, pb); |
KY Srinivasan | 8a00251 | 2014-03-08 19:23:14 -0800 | [diff] [blame] | 631 | |
sixiao@microsoft.com | 76d13b5 | 2016-02-17 16:43:59 -0800 | [diff] [blame] | 632 | /* timestamp packet in software */ |
| 633 | skb_tx_timestamp(skb); |
stephen hemminger | 2a926f7 | 2017-07-19 11:53:17 -0700 | [diff] [blame] | 634 | |
stephen hemminger | 02b6de0 | 2017-07-28 08:59:44 -0700 | [diff] [blame] | 635 | ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb); |
stephen hemminger | 793e395 | 2017-01-24 13:06:12 -0800 | [diff] [blame] | 636 | if (likely(ret == 0)) |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 637 | return NETDEV_TX_OK; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 638 | |
| 639 | if (ret == -EAGAIN) { |
| 640 | ++net_device_ctx->eth_stats.tx_busy; |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 641 | return NETDEV_TX_BUSY; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | if (ret == -ENOSPC) |
| 645 | ++net_device_ctx->eth_stats.tx_no_space; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 646 | |
Stephen Hemminger | 0ab0514 | 2016-08-23 12:17:52 -0700 | [diff] [blame] | 647 | drop: |
| 648 | dev_kfree_skb_any(skb); |
| 649 | net->stats.tx_dropped++; |
| 650 | |
| 651 | return NETDEV_TX_OK; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 652 | |
| 653 | no_memory: |
| 654 | ++net_device_ctx->eth_stats.tx_no_memory; |
| 655 | goto drop; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 656 | } |
stephen hemminger | 89bb42b | 2017-08-09 17:46:08 -0700 | [diff] [blame] | 657 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 658 | /* |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 659 | * netvsc_linkstatus_callback - Link up/down notification |
| 660 | */ |
K. Y. Srinivasan | 90ef117 | 2011-05-12 19:34:50 -0700 | [diff] [blame] | 661 | void netvsc_linkstatus_callback(struct hv_device *device_obj, |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 662 | struct rndis_message *resp) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 663 | { |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 664 | struct rndis_indicate_status *indicate = &resp->msg.indicate_status; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 665 | struct net_device *net; |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 666 | struct net_device_context *ndev_ctx; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 667 | struct netvsc_reconfig *event; |
| 668 | unsigned long flags; |
| 669 | |
Haiyang Zhang | 7f5d5af | 2016-08-04 10:42:15 -0700 | [diff] [blame] | 670 | net = hv_get_drvdata(device_obj); |
| 671 | |
| 672 | if (!net) |
| 673 | return; |
| 674 | |
| 675 | ndev_ctx = netdev_priv(net); |
| 676 | |
| 677 | /* Update the physical link speed when changing to another vSwitch */ |
| 678 | if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) { |
| 679 | u32 speed; |
| 680 | |
stephen hemminger | 89bb42b | 2017-08-09 17:46:08 -0700 | [diff] [blame] | 681 | speed = *(u32 *)((void *)indicate |
| 682 | + indicate->status_buf_offset) / 10000; |
Haiyang Zhang | 7f5d5af | 2016-08-04 10:42:15 -0700 | [diff] [blame] | 683 | ndev_ctx->speed = speed; |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | /* Handle these link change statuses below */ |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 688 | if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE && |
| 689 | indicate->status != RNDIS_STATUS_MEDIA_CONNECT && |
| 690 | indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT) |
| 691 | return; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 692 | |
Haiyang Zhang | 7f5d5af | 2016-08-04 10:42:15 -0700 | [diff] [blame] | 693 | if (net->reg_state != NETREG_REGISTERED) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 694 | return; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 695 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 696 | event = kzalloc(sizeof(*event), GFP_ATOMIC); |
| 697 | if (!event) |
| 698 | return; |
| 699 | event->event = indicate->status; |
| 700 | |
| 701 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
| 702 | list_add_tail(&event->list, &ndev_ctx->reconfig_events); |
| 703 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 704 | |
| 705 | schedule_delayed_work(&ndev_ctx->dwork, 0); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 706 | } |
| 707 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 708 | static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net, |
stephen hemminger | e91e7dd | 2017-02-27 10:26:51 -0800 | [diff] [blame] | 709 | struct napi_struct *napi, |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 710 | const struct ndis_tcp_ip_checksum_info *csum_info, |
| 711 | const struct ndis_pkt_8021q_info *vlan, |
| 712 | void *data, u32 buflen) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 713 | { |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 714 | struct sk_buff *skb; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 715 | |
stephen hemminger | e91e7dd | 2017-02-27 10:26:51 -0800 | [diff] [blame] | 716 | skb = napi_alloc_skb(napi, buflen); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 717 | if (!skb) |
| 718 | return skb; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 719 | |
Greg Kroah-Hartman | 02fafbc | 2009-08-31 21:09:45 -0700 | [diff] [blame] | 720 | /* |
| 721 | * Copy to skb. This copy is needed here since the memory pointed by |
| 722 | * hv_netvsc_packet cannot be deallocated |
| 723 | */ |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 724 | skb_put_data(skb, data, buflen); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 725 | |
| 726 | skb->protocol = eth_type_trans(skb, net); |
Stephen Hemminger | e52fed7 | 2016-10-23 21:32:47 -0700 | [diff] [blame] | 727 | |
| 728 | /* skb is already created with CHECKSUM_NONE */ |
| 729 | skb_checksum_none_assert(skb); |
| 730 | |
| 731 | /* |
| 732 | * In Linux, the IP checksum is always checked. |
| 733 | * Do L4 checksum offload if enabled and present. |
| 734 | */ |
| 735 | if (csum_info && (net->features & NETIF_F_RXCSUM)) { |
| 736 | if (csum_info->receive.tcp_checksum_succeeded || |
| 737 | csum_info->receive.udp_checksum_succeeded) |
KY Srinivasan | e3d605e | 2014-03-08 19:23:16 -0800 | [diff] [blame] | 738 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
KY Srinivasan | e3d605e | 2014-03-08 19:23:16 -0800 | [diff] [blame] | 739 | } |
| 740 | |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 741 | if (vlan) { |
| 742 | u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT); |
| 743 | |
Haiyang Zhang | 93725cb | 2013-06-17 15:36:49 -0700 | [diff] [blame] | 744 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
KY Srinivasan | 760d1e3 | 2015-12-01 16:43:19 -0800 | [diff] [blame] | 745 | vlan_tci); |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 746 | } |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 747 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 748 | return skb; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * netvsc_recv_callback - Callback when we receive a packet from the |
| 753 | * "wire" on the specified device. |
| 754 | */ |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 755 | int netvsc_recv_callback(struct net_device *net, |
| 756 | struct vmbus_channel *channel, |
| 757 | void *data, u32 len, |
| 758 | const struct ndis_tcp_ip_checksum_info *csum_info, |
| 759 | const struct ndis_pkt_8021q_info *vlan) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 760 | { |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 761 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 762 | struct netvsc_device *net_device; |
stephen hemminger | 742fe54 | 2017-02-27 10:26:50 -0800 | [diff] [blame] | 763 | u16 q_idx = channel->offermsg.offer.sub_channel_index; |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 764 | struct netvsc_channel *nvchan; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 765 | struct sk_buff *skb; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 766 | struct netvsc_stats *rx_stats; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 767 | |
Stephen Hemminger | 9cbcc42 | 2016-09-22 16:56:34 -0700 | [diff] [blame] | 768 | if (net->reg_state != NETREG_REGISTERED) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 769 | return NVSP_STAT_FAIL; |
| 770 | |
stephen hemminger | 0719e72 | 2017-01-11 09:16:32 -0800 | [diff] [blame] | 771 | rcu_read_lock(); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 772 | net_device = rcu_dereference(net_device_ctx->nvdev); |
| 773 | if (unlikely(!net_device)) |
| 774 | goto drop; |
| 775 | |
| 776 | nvchan = &net_device->chan_table[q_idx]; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 777 | |
| 778 | /* Allocate a skb - TODO direct I/O to pages? */ |
stephen hemminger | e91e7dd | 2017-02-27 10:26:51 -0800 | [diff] [blame] | 779 | skb = netvsc_alloc_recv_skb(net, &nvchan->napi, |
| 780 | csum_info, vlan, data, len); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 781 | if (unlikely(!skb)) { |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 782 | drop: |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 783 | ++net->stats.rx_dropped; |
stephen hemminger | 0719e72 | 2017-01-11 09:16:32 -0800 | [diff] [blame] | 784 | rcu_read_unlock(); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 785 | return NVSP_STAT_FAIL; |
| 786 | } |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 787 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 788 | skb_record_rx_queue(skb, q_idx); |
Stephen Hemminger | 9cbcc42 | 2016-09-22 16:56:34 -0700 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * Even if injecting the packet, record the statistics |
| 792 | * on the synthetic device because modifying the VF device |
| 793 | * statistics will not work correctly. |
| 794 | */ |
stephen hemminger | 742fe54 | 2017-02-27 10:26:50 -0800 | [diff] [blame] | 795 | rx_stats = &nvchan->rx_stats; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 796 | u64_stats_update_begin(&rx_stats->syncp); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 797 | rx_stats->packets++; |
stephen hemminger | dc54a08 | 2017-01-24 13:06:08 -0800 | [diff] [blame] | 798 | rx_stats->bytes += len; |
Stephen Hemminger | f7ad75b | 2016-09-22 16:56:35 -0700 | [diff] [blame] | 799 | |
| 800 | if (skb->pkt_type == PACKET_BROADCAST) |
| 801 | ++rx_stats->broadcast; |
| 802 | else if (skb->pkt_type == PACKET_MULTICAST) |
| 803 | ++rx_stats->multicast; |
sixiao@microsoft.com | 4b02b58 | 2015-05-15 02:33:03 -0700 | [diff] [blame] | 804 | u64_stats_update_end(&rx_stats->syncp); |
Stephen Hemminger | 9495c28 | 2010-03-09 17:42:17 -0800 | [diff] [blame] | 805 | |
stephen hemminger | 742fe54 | 2017-02-27 10:26:50 -0800 | [diff] [blame] | 806 | napi_gro_receive(&nvchan->napi, skb); |
stephen hemminger | 0719e72 | 2017-01-11 09:16:32 -0800 | [diff] [blame] | 807 | rcu_read_unlock(); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 808 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 812 | static void netvsc_get_drvinfo(struct net_device *net, |
| 813 | struct ethtool_drvinfo *info) |
| 814 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 815 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 816 | strlcpy(info->fw_version, "N/A", sizeof(info->fw_version)); |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 819 | static void netvsc_get_channels(struct net_device *net, |
| 820 | struct ethtool_channels *channel) |
| 821 | { |
| 822 | struct net_device_context *net_device_ctx = netdev_priv(net); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 823 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 824 | |
| 825 | if (nvdev) { |
| 826 | channel->max_combined = nvdev->max_chn; |
| 827 | channel->combined_count = nvdev->num_chn; |
| 828 | } |
| 829 | } |
| 830 | |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 831 | static int netvsc_set_channels(struct net_device *net, |
| 832 | struct ethtool_channels *channels) |
| 833 | { |
| 834 | struct net_device_context *net_device_ctx = netdev_priv(net); |
| 835 | struct hv_device *dev = net_device_ctx->device_ctx; |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 836 | struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev); |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 837 | unsigned int orig, count = channels->combined_count; |
| 838 | struct netvsc_device_info device_info; |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 839 | bool was_opened; |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 840 | int ret = 0; |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 841 | |
| 842 | /* We do not support separate count for rx, tx, or other */ |
| 843 | if (count == 0 || |
| 844 | channels->rx_count || channels->tx_count || channels->other_count) |
| 845 | return -EINVAL; |
| 846 | |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 847 | if (!nvdev || nvdev->destroy) |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 848 | return -ENODEV; |
| 849 | |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 850 | if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5) |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 851 | return -EINVAL; |
| 852 | |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 853 | if (count > nvdev->max_chn) |
| 854 | return -EINVAL; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 855 | |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 856 | orig = nvdev->num_chn; |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 857 | was_opened = rndis_filter_opened(nvdev); |
| 858 | if (was_opened) |
| 859 | rndis_filter_close(nvdev); |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 860 | |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 861 | memset(&device_info, 0, sizeof(device_info)); |
| 862 | device_info.num_chn = count; |
| 863 | device_info.ring_size = ring_size; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 864 | device_info.send_sections = nvdev->send_section_cnt; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 865 | device_info.send_section_size = nvdev->send_section_size; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 866 | device_info.recv_sections = nvdev->recv_section_cnt; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 867 | device_info.recv_section_size = nvdev->recv_section_size; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 868 | |
| 869 | rndis_filter_device_remove(dev, nvdev); |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 870 | |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 871 | nvdev = rndis_filter_device_add(dev, &device_info); |
Stephen Hemminger | 8195b13 | 2017-09-06 13:53:05 -0700 | [diff] [blame] | 872 | if (IS_ERR(nvdev)) { |
stephen hemminger | d6aac1f | 2017-07-28 08:59:41 -0700 | [diff] [blame] | 873 | ret = PTR_ERR(nvdev); |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 874 | device_info.num_chn = orig; |
stephen hemminger | 68d715f | 2017-08-09 17:46:06 -0700 | [diff] [blame] | 875 | nvdev = rndis_filter_device_add(dev, &device_info); |
| 876 | |
| 877 | if (IS_ERR(nvdev)) { |
| 878 | netdev_err(net, "restoring channel setting failed: %ld\n", |
| 879 | PTR_ERR(nvdev)); |
| 880 | return ret; |
| 881 | } |
stephen hemminger | 7ca4593 | 2017-07-24 10:57:28 -0700 | [diff] [blame] | 882 | } |
| 883 | |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 884 | if (was_opened) |
| 885 | rndis_filter_open(nvdev); |
stephen hemminger | 163891d | 2017-03-22 14:50:58 -0700 | [diff] [blame] | 886 | |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 887 | /* We may have missed link change notifications */ |
stephen hemminger | 1b01994 | 2017-07-19 11:53:12 -0700 | [diff] [blame] | 888 | net_device_ctx->last_reconfig = 0; |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 889 | schedule_delayed_work(&net_device_ctx->dwork, 0); |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 890 | |
| 891 | return ret; |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 894 | static bool |
| 895 | netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd) |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 896 | { |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 897 | struct ethtool_link_ksettings diff1 = *cmd; |
| 898 | struct ethtool_link_ksettings diff2 = {}; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 899 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 900 | diff1.base.speed = 0; |
| 901 | diff1.base.duplex = 0; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 902 | /* advertising and cmd are usually set */ |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 903 | ethtool_link_ksettings_zero_link_mode(&diff1, advertising); |
| 904 | diff1.base.cmd = 0; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 905 | /* We set port to PORT_OTHER */ |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 906 | diff2.base.port = PORT_OTHER; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 907 | |
| 908 | return !memcmp(&diff1, &diff2, sizeof(diff1)); |
| 909 | } |
| 910 | |
| 911 | static void netvsc_init_settings(struct net_device *dev) |
| 912 | { |
| 913 | struct net_device_context *ndc = netdev_priv(dev); |
| 914 | |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 915 | ndc->l4_hash = HV_DEFAULT_L4HASH; |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 916 | |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 917 | ndc->speed = SPEED_UNKNOWN; |
Simon Xiao | f3c9d40e | 2017-04-14 14:42:58 -0700 | [diff] [blame] | 918 | ndc->duplex = DUPLEX_FULL; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 919 | } |
| 920 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 921 | static int netvsc_get_link_ksettings(struct net_device *dev, |
| 922 | struct ethtool_link_ksettings *cmd) |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 923 | { |
| 924 | struct net_device_context *ndc = netdev_priv(dev); |
| 925 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 926 | cmd->base.speed = ndc->speed; |
| 927 | cmd->base.duplex = ndc->duplex; |
| 928 | cmd->base.port = PORT_OTHER; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 933 | static int netvsc_set_link_ksettings(struct net_device *dev, |
| 934 | const struct ethtool_link_ksettings *cmd) |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 935 | { |
| 936 | struct net_device_context *ndc = netdev_priv(dev); |
| 937 | u32 speed; |
| 938 | |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 939 | speed = cmd->base.speed; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 940 | if (!ethtool_validate_speed(speed) || |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 941 | !ethtool_validate_duplex(cmd->base.duplex) || |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 942 | !netvsc_validate_ethtool_ss_cmd(cmd)) |
| 943 | return -EINVAL; |
| 944 | |
| 945 | ndc->speed = speed; |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 946 | ndc->duplex = cmd->base.duplex; |
sixiao@microsoft.com | 49eb938 | 2016-02-25 15:24:08 -0800 | [diff] [blame] | 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 951 | static int netvsc_change_mtu(struct net_device *ndev, int mtu) |
| 952 | { |
| 953 | struct net_device_context *ndevctx = netdev_priv(ndev); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 954 | struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 955 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 956 | struct hv_device *hdev = ndevctx->device_ctx; |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 957 | int orig_mtu = ndev->mtu; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 958 | struct netvsc_device_info device_info; |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 959 | bool was_opened; |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 960 | int ret = 0; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 961 | |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 962 | if (!nvdev || nvdev->destroy) |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 963 | return -ENODEV; |
| 964 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 965 | /* Change MTU of underlying VF netdev first. */ |
| 966 | if (vf_netdev) { |
| 967 | ret = dev_set_mtu(vf_netdev, mtu); |
| 968 | if (ret) |
| 969 | return ret; |
| 970 | } |
| 971 | |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 972 | netif_device_detach(ndev); |
| 973 | was_opened = rndis_filter_opened(nvdev); |
| 974 | if (was_opened) |
| 975 | rndis_filter_close(nvdev); |
Haiyang Zhang | 2de8530 | 2015-07-13 13:09:16 -0700 | [diff] [blame] | 976 | |
Andrew Schwartzmeyer | 8ebdcc5 | 2015-08-11 17:14:31 -0700 | [diff] [blame] | 977 | memset(&device_info, 0, sizeof(device_info)); |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 978 | device_info.ring_size = ring_size; |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 979 | device_info.num_chn = nvdev->num_chn; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 980 | device_info.send_sections = nvdev->send_section_cnt; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 981 | device_info.send_section_size = nvdev->send_section_size; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 982 | device_info.recv_sections = nvdev->recv_section_cnt; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 983 | device_info.recv_section_size = nvdev->recv_section_size; |
Dexuan Cui | 152669b | 2017-03-02 13:00:53 +0000 | [diff] [blame] | 984 | |
Dexuan Cui | 152669b | 2017-03-02 13:00:53 +0000 | [diff] [blame] | 985 | rndis_filter_device_remove(hdev, nvdev); |
| 986 | |
Dexuan Cui | 152669b | 2017-03-02 13:00:53 +0000 | [diff] [blame] | 987 | ndev->mtu = mtu; |
| 988 | |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 989 | nvdev = rndis_filter_device_add(hdev, &device_info); |
| 990 | if (IS_ERR(nvdev)) { |
| 991 | ret = PTR_ERR(nvdev); |
| 992 | |
| 993 | /* Attempt rollback to original MTU */ |
| 994 | ndev->mtu = orig_mtu; |
stephen hemminger | 68d715f | 2017-08-09 17:46:06 -0700 | [diff] [blame] | 995 | nvdev = rndis_filter_device_add(hdev, &device_info); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 996 | |
| 997 | if (vf_netdev) |
| 998 | dev_set_mtu(vf_netdev, orig_mtu); |
stephen hemminger | 68d715f | 2017-08-09 17:46:06 -0700 | [diff] [blame] | 999 | |
| 1000 | if (IS_ERR(nvdev)) { |
| 1001 | netdev_err(ndev, "restoring mtu failed: %ld\n", |
| 1002 | PTR_ERR(nvdev)); |
| 1003 | return ret; |
| 1004 | } |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 1005 | } |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 1006 | |
stephen hemminger | ea383bf | 2017-07-19 11:53:15 -0700 | [diff] [blame] | 1007 | if (was_opened) |
| 1008 | rndis_filter_open(nvdev); |
| 1009 | |
| 1010 | netif_device_attach(ndev); |
stephen hemminger | 163891d | 2017-03-22 14:50:58 -0700 | [diff] [blame] | 1011 | |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1012 | /* We may have missed link change notifications */ |
| 1013 | schedule_delayed_work(&ndevctx->dwork, 0); |
| 1014 | |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 1015 | return ret; |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1018 | static void netvsc_get_vf_stats(struct net_device *net, |
| 1019 | struct netvsc_vf_pcpu_stats *tot) |
| 1020 | { |
| 1021 | struct net_device_context *ndev_ctx = netdev_priv(net); |
| 1022 | int i; |
| 1023 | |
| 1024 | memset(tot, 0, sizeof(*tot)); |
| 1025 | |
| 1026 | for_each_possible_cpu(i) { |
| 1027 | const struct netvsc_vf_pcpu_stats *stats |
| 1028 | = per_cpu_ptr(ndev_ctx->vf_stats, i); |
| 1029 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 1030 | unsigned int start; |
| 1031 | |
| 1032 | do { |
| 1033 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
| 1034 | rx_packets = stats->rx_packets; |
| 1035 | tx_packets = stats->tx_packets; |
| 1036 | rx_bytes = stats->rx_bytes; |
| 1037 | tx_bytes = stats->tx_bytes; |
| 1038 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
| 1039 | |
| 1040 | tot->rx_packets += rx_packets; |
| 1041 | tot->tx_packets += tx_packets; |
| 1042 | tot->rx_bytes += rx_bytes; |
| 1043 | tot->tx_bytes += tx_bytes; |
| 1044 | tot->tx_dropped += stats->tx_dropped; |
| 1045 | } |
| 1046 | } |
| 1047 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 1048 | static void netvsc_get_stats64(struct net_device *net, |
| 1049 | struct rtnl_link_stats64 *t) |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1050 | { |
| 1051 | struct net_device_context *ndev_ctx = netdev_priv(net); |
stephen hemminger | 776e726 | 2017-04-14 14:42:57 -0700 | [diff] [blame] | 1052 | struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1053 | struct netvsc_vf_pcpu_stats vf_tot; |
stephen hemminger | 89bb42b | 2017-08-09 17:46:08 -0700 | [diff] [blame] | 1054 | int i; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1055 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1056 | if (!nvdev) |
| 1057 | return; |
| 1058 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1059 | netdev_stats_to_stats64(t, &net->stats); |
| 1060 | |
| 1061 | netvsc_get_vf_stats(net, &vf_tot); |
| 1062 | t->rx_packets += vf_tot.rx_packets; |
| 1063 | t->tx_packets += vf_tot.tx_packets; |
| 1064 | t->rx_bytes += vf_tot.rx_bytes; |
| 1065 | t->tx_bytes += vf_tot.tx_bytes; |
| 1066 | t->tx_dropped += vf_tot.tx_dropped; |
| 1067 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1068 | for (i = 0; i < nvdev->num_chn; i++) { |
| 1069 | const struct netvsc_channel *nvchan = &nvdev->chan_table[i]; |
| 1070 | const struct netvsc_stats *stats; |
| 1071 | u64 packets, bytes, multicast; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1072 | unsigned int start; |
| 1073 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1074 | stats = &nvchan->tx_stats; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1075 | do { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1076 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
| 1077 | packets = stats->packets; |
| 1078 | bytes = stats->bytes; |
| 1079 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1080 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1081 | t->tx_bytes += bytes; |
| 1082 | t->tx_packets += packets; |
| 1083 | |
| 1084 | stats = &nvchan->rx_stats; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1085 | do { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1086 | start = u64_stats_fetch_begin_irq(&stats->syncp); |
| 1087 | packets = stats->packets; |
| 1088 | bytes = stats->bytes; |
| 1089 | multicast = stats->multicast + stats->broadcast; |
| 1090 | } while (u64_stats_fetch_retry_irq(&stats->syncp, start)); |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1091 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1092 | t->rx_bytes += bytes; |
| 1093 | t->rx_packets += packets; |
| 1094 | t->multicast += multicast; |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1095 | } |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1096 | } |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1097 | |
| 1098 | static int netvsc_set_mac_addr(struct net_device *ndev, void *p) |
| 1099 | { |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1100 | struct net_device_context *ndc = netdev_priv(ndev); |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame] | 1101 | struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1102 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1103 | struct sockaddr *addr = p; |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1104 | int err; |
| 1105 | |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame] | 1106 | err = eth_prepare_mac_addr_change(ndev, p); |
| 1107 | if (err) |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1108 | return err; |
| 1109 | |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1110 | if (!nvdev) |
| 1111 | return -ENODEV; |
| 1112 | |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame] | 1113 | if (vf_netdev) { |
| 1114 | err = dev_set_mac_address(vf_netdev, addr); |
| 1115 | if (err) |
| 1116 | return err; |
| 1117 | } |
| 1118 | |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1119 | err = rndis_filter_set_device_mac(nvdev, addr->sa_data); |
stephen hemminger | 16ba326 | 2017-08-09 17:46:05 -0700 | [diff] [blame] | 1120 | if (!err) { |
| 1121 | eth_commit_mac_addr_change(ndev, p); |
| 1122 | } else if (vf_netdev) { |
| 1123 | /* rollback change on VF */ |
| 1124 | memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN); |
| 1125 | dev_set_mac_address(vf_netdev, addr); |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | return err; |
| 1129 | } |
| 1130 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1131 | static const struct { |
| 1132 | char name[ETH_GSTRING_LEN]; |
| 1133 | u16 offset; |
| 1134 | } netvsc_stats[] = { |
| 1135 | { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) }, |
| 1136 | { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) }, |
| 1137 | { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) }, |
| 1138 | { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) }, |
| 1139 | { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) }, |
stephen hemminger | cad5c19 | 2017-08-09 17:46:12 -0700 | [diff] [blame] | 1140 | { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) }, |
| 1141 | { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) }, |
Simon Xiao | 09af87d | 2017-09-29 11:39:46 -0700 | [diff] [blame] | 1142 | { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) }, |
| 1143 | { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) }, |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1144 | }, vf_stats[] = { |
| 1145 | { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) }, |
| 1146 | { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) }, |
| 1147 | { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) }, |
| 1148 | { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) }, |
| 1149 | { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) }, |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1150 | }; |
| 1151 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1152 | #define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats) |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1153 | #define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats) |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1154 | |
| 1155 | /* 4 statistics per queue (rx/tx packets/bytes) */ |
| 1156 | #define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4) |
| 1157 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1158 | static int netvsc_get_sset_count(struct net_device *dev, int string_set) |
| 1159 | { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1160 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | fbd4c7e | 2017-06-07 15:53:47 -0700 | [diff] [blame] | 1161 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1162 | |
| 1163 | if (!nvdev) |
| 1164 | return -ENODEV; |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1165 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1166 | switch (string_set) { |
| 1167 | case ETH_SS_STATS: |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1168 | return NETVSC_GLOBAL_STATS_LEN |
| 1169 | + NETVSC_VF_STATS_LEN |
| 1170 | + NETVSC_QUEUE_STATS_LEN(nvdev); |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1171 | default: |
| 1172 | return -EINVAL; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | static void netvsc_get_ethtool_stats(struct net_device *dev, |
| 1177 | struct ethtool_stats *stats, u64 *data) |
| 1178 | { |
| 1179 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1180 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1181 | const void *nds = &ndc->eth_stats; |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1182 | const struct netvsc_stats *qstats; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1183 | struct netvsc_vf_pcpu_stats sum; |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1184 | unsigned int start; |
| 1185 | u64 packets, bytes; |
| 1186 | int i, j; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1187 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1188 | if (!nvdev) |
| 1189 | return; |
| 1190 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1191 | for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++) |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1192 | data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset); |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1193 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1194 | netvsc_get_vf_stats(dev, &sum); |
| 1195 | for (j = 0; j < NETVSC_VF_STATS_LEN; j++) |
| 1196 | data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset); |
| 1197 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1198 | for (j = 0; j < nvdev->num_chn; j++) { |
| 1199 | qstats = &nvdev->chan_table[j].tx_stats; |
| 1200 | |
| 1201 | do { |
| 1202 | start = u64_stats_fetch_begin_irq(&qstats->syncp); |
| 1203 | packets = qstats->packets; |
| 1204 | bytes = qstats->bytes; |
| 1205 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); |
| 1206 | data[i++] = packets; |
| 1207 | data[i++] = bytes; |
| 1208 | |
| 1209 | qstats = &nvdev->chan_table[j].rx_stats; |
| 1210 | do { |
| 1211 | start = u64_stats_fetch_begin_irq(&qstats->syncp); |
| 1212 | packets = qstats->packets; |
| 1213 | bytes = qstats->bytes; |
| 1214 | } while (u64_stats_fetch_retry_irq(&qstats->syncp, start)); |
| 1215 | data[i++] = packets; |
| 1216 | data[i++] = bytes; |
| 1217 | } |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 1221 | { |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1222 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1223 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1224 | u8 *p = data; |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1225 | int i; |
| 1226 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1227 | if (!nvdev) |
| 1228 | return; |
| 1229 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1230 | switch (stringset) { |
| 1231 | case ETH_SS_STATS: |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1232 | for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) { |
| 1233 | memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN); |
| 1234 | p += ETH_GSTRING_LEN; |
| 1235 | } |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1236 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1237 | for (i = 0; i < ARRAY_SIZE(vf_stats); i++) { |
| 1238 | memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN); |
| 1239 | p += ETH_GSTRING_LEN; |
| 1240 | } |
| 1241 | |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 1242 | for (i = 0; i < nvdev->num_chn; i++) { |
| 1243 | sprintf(p, "tx_queue_%u_packets", i); |
| 1244 | p += ETH_GSTRING_LEN; |
| 1245 | sprintf(p, "tx_queue_%u_bytes", i); |
| 1246 | p += ETH_GSTRING_LEN; |
| 1247 | sprintf(p, "rx_queue_%u_packets", i); |
| 1248 | p += ETH_GSTRING_LEN; |
| 1249 | sprintf(p, "rx_queue_%u_bytes", i); |
| 1250 | p += ETH_GSTRING_LEN; |
| 1251 | } |
| 1252 | |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1253 | break; |
| 1254 | } |
| 1255 | } |
| 1256 | |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1257 | static int |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1258 | netvsc_get_rss_hash_opts(struct net_device_context *ndc, |
| 1259 | struct ethtool_rxnfc *info) |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1260 | { |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1261 | const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3; |
| 1262 | |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1263 | info->data = RXH_IP_SRC | RXH_IP_DST; |
| 1264 | |
| 1265 | switch (info->flow_type) { |
| 1266 | case TCP_V4_FLOW: |
| 1267 | case TCP_V6_FLOW: |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1268 | info->data |= l4_flag; |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1269 | break; |
| 1270 | |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1271 | case UDP_V4_FLOW: |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1272 | if (ndc->l4_hash & HV_UDP4_L4HASH) |
| 1273 | info->data |= l4_flag; |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1274 | |
| 1275 | break; |
| 1276 | |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1277 | case UDP_V6_FLOW: |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1278 | if (ndc->l4_hash & HV_UDP6_L4HASH) |
| 1279 | info->data |= l4_flag; |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1280 | |
| 1281 | break; |
| 1282 | |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1283 | case IPV4_FLOW: |
| 1284 | case IPV6_FLOW: |
| 1285 | break; |
| 1286 | default: |
| 1287 | info->data = 0; |
| 1288 | break; |
| 1289 | } |
| 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | static int |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1295 | netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, |
| 1296 | u32 *rules) |
| 1297 | { |
| 1298 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1299 | struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1300 | |
| 1301 | if (!nvdev) |
| 1302 | return -ENODEV; |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1303 | |
| 1304 | switch (info->cmd) { |
| 1305 | case ETHTOOL_GRXRINGS: |
| 1306 | info->data = nvdev->num_chn; |
| 1307 | return 0; |
stephen hemminger | b5a5dc8 | 2017-01-24 13:06:01 -0800 | [diff] [blame] | 1308 | |
| 1309 | case ETHTOOL_GRXFH: |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1310 | return netvsc_get_rss_hash_opts(ndc, info); |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1311 | } |
| 1312 | return -EOPNOTSUPP; |
| 1313 | } |
| 1314 | |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1315 | static int netvsc_set_rss_hash_opts(struct net_device_context *ndc, |
| 1316 | struct ethtool_rxnfc *info) |
| 1317 | { |
| 1318 | if (info->data == (RXH_IP_SRC | RXH_IP_DST | |
| 1319 | RXH_L4_B_0_1 | RXH_L4_B_2_3)) { |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1320 | switch (info->flow_type) { |
| 1321 | case UDP_V4_FLOW: |
| 1322 | ndc->l4_hash |= HV_UDP4_L4HASH; |
| 1323 | break; |
| 1324 | |
| 1325 | case UDP_V6_FLOW: |
| 1326 | ndc->l4_hash |= HV_UDP6_L4HASH; |
| 1327 | break; |
| 1328 | |
| 1329 | default: |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1330 | return -EOPNOTSUPP; |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1331 | } |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | if (info->data == (RXH_IP_SRC | RXH_IP_DST)) { |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1337 | switch (info->flow_type) { |
| 1338 | case UDP_V4_FLOW: |
| 1339 | ndc->l4_hash &= ~HV_UDP4_L4HASH; |
| 1340 | break; |
| 1341 | |
| 1342 | case UDP_V6_FLOW: |
| 1343 | ndc->l4_hash &= ~HV_UDP6_L4HASH; |
| 1344 | break; |
| 1345 | |
| 1346 | default: |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1347 | return -EOPNOTSUPP; |
Haiyang Zhang | 486e398 | 2017-10-06 08:33:57 -0700 | [diff] [blame^] | 1348 | } |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | return -EOPNOTSUPP; |
| 1354 | } |
| 1355 | |
| 1356 | static int |
| 1357 | netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info) |
| 1358 | { |
| 1359 | struct net_device_context *ndc = netdev_priv(ndev); |
| 1360 | |
| 1361 | if (info->cmd == ETHTOOL_SRXFH) |
| 1362 | return netvsc_set_rss_hash_opts(ndc, info); |
| 1363 | |
| 1364 | return -EOPNOTSUPP; |
| 1365 | } |
| 1366 | |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1367 | #ifdef CONFIG_NET_POLL_CONTROLLER |
stephen hemminger | a5ecd43 | 2017-06-07 15:53:48 -0700 | [diff] [blame] | 1368 | static void netvsc_poll_controller(struct net_device *dev) |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1369 | { |
stephen hemminger | a5ecd43 | 2017-06-07 15:53:48 -0700 | [diff] [blame] | 1370 | struct net_device_context *ndc = netdev_priv(dev); |
| 1371 | struct netvsc_device *ndev; |
| 1372 | int i; |
| 1373 | |
| 1374 | rcu_read_lock(); |
| 1375 | ndev = rcu_dereference(ndc->nvdev); |
| 1376 | if (ndev) { |
| 1377 | for (i = 0; i < ndev->num_chn; i++) { |
| 1378 | struct netvsc_channel *nvchan = &ndev->chan_table[i]; |
| 1379 | |
| 1380 | napi_schedule(&nvchan->napi); |
| 1381 | } |
| 1382 | } |
| 1383 | rcu_read_unlock(); |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1384 | } |
| 1385 | #endif |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1386 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1387 | static u32 netvsc_get_rxfh_key_size(struct net_device *dev) |
| 1388 | { |
| 1389 | return NETVSC_HASH_KEYLEN; |
| 1390 | } |
| 1391 | |
| 1392 | static u32 netvsc_rss_indir_size(struct net_device *dev) |
| 1393 | { |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1394 | return ITAB_NUM; |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, |
| 1398 | u8 *hfunc) |
| 1399 | { |
| 1400 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 867047c | 2017-07-28 08:59:42 -0700 | [diff] [blame] | 1401 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1402 | struct rndis_device *rndis_dev; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1403 | int i; |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1404 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1405 | if (!ndev) |
| 1406 | return -ENODEV; |
| 1407 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1408 | if (hfunc) |
| 1409 | *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */ |
| 1410 | |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1411 | rndis_dev = ndev->extension; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1412 | if (indir) { |
| 1413 | for (i = 0; i < ITAB_NUM; i++) |
| 1414 | indir[i] = rndis_dev->ind_table[i]; |
| 1415 | } |
| 1416 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1417 | if (key) |
| 1418 | memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN); |
| 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir, |
| 1424 | const u8 *key, const u8 hfunc) |
| 1425 | { |
| 1426 | struct net_device_context *ndc = netdev_priv(dev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1427 | struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev); |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1428 | struct rndis_device *rndis_dev; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1429 | int i; |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1430 | |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1431 | if (!ndev) |
| 1432 | return -ENODEV; |
| 1433 | |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1434 | if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) |
| 1435 | return -EOPNOTSUPP; |
| 1436 | |
Colin Ian King | eb996ed | 2017-03-25 14:26:39 +0000 | [diff] [blame] | 1437 | rndis_dev = ndev->extension; |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1438 | if (indir) { |
| 1439 | for (i = 0; i < ITAB_NUM; i++) |
Haiyang Zhang | db3cd7a | 2017-09-01 14:30:07 -0700 | [diff] [blame] | 1440 | if (indir[i] >= ndev->num_chn) |
stephen hemminger | ff4a441 | 2017-01-24 13:06:04 -0800 | [diff] [blame] | 1441 | return -EINVAL; |
| 1442 | |
| 1443 | for (i = 0; i < ITAB_NUM; i++) |
| 1444 | rndis_dev->ind_table[i] = indir[i]; |
| 1445 | } |
| 1446 | |
| 1447 | if (!key) { |
| 1448 | if (!indir) |
| 1449 | return 0; |
| 1450 | |
| 1451 | key = rndis_dev->rss_key; |
| 1452 | } |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1453 | |
Haiyang Zhang | 715e2ec | 2017-09-01 14:30:04 -0700 | [diff] [blame] | 1454 | return rndis_filter_set_rss_param(rndis_dev, key); |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 1457 | /* Hyper-V RNDIS protocol does not have ring in the HW sense. |
| 1458 | * It does have pre-allocated receive area which is divided into sections. |
| 1459 | */ |
| 1460 | static void __netvsc_get_ringparam(struct netvsc_device *nvdev, |
| 1461 | struct ethtool_ringparam *ring) |
| 1462 | { |
| 1463 | u32 max_buf_size; |
| 1464 | |
| 1465 | ring->rx_pending = nvdev->recv_section_cnt; |
| 1466 | ring->tx_pending = nvdev->send_section_cnt; |
| 1467 | |
| 1468 | if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2) |
| 1469 | max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY; |
| 1470 | else |
| 1471 | max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE; |
| 1472 | |
| 1473 | ring->rx_max_pending = max_buf_size / nvdev->recv_section_size; |
| 1474 | ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE |
| 1475 | / nvdev->send_section_size; |
| 1476 | } |
| 1477 | |
| 1478 | static void netvsc_get_ringparam(struct net_device *ndev, |
| 1479 | struct ethtool_ringparam *ring) |
| 1480 | { |
| 1481 | struct net_device_context *ndevctx = netdev_priv(ndev); |
| 1482 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
| 1483 | |
| 1484 | if (!nvdev) |
| 1485 | return; |
| 1486 | |
| 1487 | __netvsc_get_ringparam(nvdev, ring); |
| 1488 | } |
| 1489 | |
| 1490 | static int netvsc_set_ringparam(struct net_device *ndev, |
| 1491 | struct ethtool_ringparam *ring) |
| 1492 | { |
| 1493 | struct net_device_context *ndevctx = netdev_priv(ndev); |
| 1494 | struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev); |
| 1495 | struct hv_device *hdev = ndevctx->device_ctx; |
| 1496 | struct netvsc_device_info device_info; |
| 1497 | struct ethtool_ringparam orig; |
| 1498 | u32 new_tx, new_rx; |
| 1499 | bool was_opened; |
| 1500 | int ret = 0; |
| 1501 | |
| 1502 | if (!nvdev || nvdev->destroy) |
| 1503 | return -ENODEV; |
| 1504 | |
| 1505 | memset(&orig, 0, sizeof(orig)); |
| 1506 | __netvsc_get_ringparam(nvdev, &orig); |
| 1507 | |
| 1508 | new_tx = clamp_t(u32, ring->tx_pending, |
| 1509 | NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending); |
| 1510 | new_rx = clamp_t(u32, ring->rx_pending, |
| 1511 | NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending); |
| 1512 | |
| 1513 | if (new_tx == orig.tx_pending && |
| 1514 | new_rx == orig.rx_pending) |
| 1515 | return 0; /* no change */ |
| 1516 | |
| 1517 | memset(&device_info, 0, sizeof(device_info)); |
| 1518 | device_info.num_chn = nvdev->num_chn; |
| 1519 | device_info.ring_size = ring_size; |
| 1520 | device_info.send_sections = new_tx; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 1521 | device_info.send_section_size = nvdev->send_section_size; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 1522 | device_info.recv_sections = new_rx; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 1523 | device_info.recv_section_size = nvdev->recv_section_size; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 1524 | |
| 1525 | netif_device_detach(ndev); |
| 1526 | was_opened = rndis_filter_opened(nvdev); |
| 1527 | if (was_opened) |
| 1528 | rndis_filter_close(nvdev); |
| 1529 | |
| 1530 | rndis_filter_device_remove(hdev, nvdev); |
| 1531 | |
| 1532 | nvdev = rndis_filter_device_add(hdev, &device_info); |
| 1533 | if (IS_ERR(nvdev)) { |
| 1534 | ret = PTR_ERR(nvdev); |
| 1535 | |
| 1536 | device_info.send_sections = orig.tx_pending; |
| 1537 | device_info.recv_sections = orig.rx_pending; |
| 1538 | nvdev = rndis_filter_device_add(hdev, &device_info); |
| 1539 | if (IS_ERR(nvdev)) { |
| 1540 | netdev_err(ndev, "restoring ringparam failed: %ld\n", |
| 1541 | PTR_ERR(nvdev)); |
| 1542 | return ret; |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | if (was_opened) |
| 1547 | rndis_filter_open(nvdev); |
| 1548 | netif_device_attach(ndev); |
| 1549 | |
| 1550 | /* We may have missed link change notifications */ |
| 1551 | ndevctx->last_reconfig = 0; |
| 1552 | schedule_delayed_work(&ndevctx->dwork, 0); |
| 1553 | |
| 1554 | return ret; |
| 1555 | } |
| 1556 | |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1557 | static const struct ethtool_ops ethtool_ops = { |
| 1558 | .get_drvinfo = netvsc_get_drvinfo, |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1559 | .get_link = ethtool_op_get_link, |
Stephen Hemminger | 4323b47 | 2016-08-23 12:17:57 -0700 | [diff] [blame] | 1560 | .get_ethtool_stats = netvsc_get_ethtool_stats, |
| 1561 | .get_sset_count = netvsc_get_sset_count, |
| 1562 | .get_strings = netvsc_get_strings, |
Andrew Schwartzmeyer | 5999537 | 2015-02-26 16:27:14 -0800 | [diff] [blame] | 1563 | .get_channels = netvsc_get_channels, |
Andrew Schwartzmeyer | b5960e6 | 2015-08-11 17:14:32 -0700 | [diff] [blame] | 1564 | .set_channels = netvsc_set_channels, |
sixiao@microsoft.com | 76d13b5 | 2016-02-17 16:43:59 -0800 | [diff] [blame] | 1565 | .get_ts_info = ethtool_op_get_ts_info, |
stephen hemminger | b448f4e | 2017-01-24 13:06:00 -0800 | [diff] [blame] | 1566 | .get_rxnfc = netvsc_get_rxnfc, |
Haiyang Zhang | 4823eb2 | 2017-08-21 19:22:39 -0700 | [diff] [blame] | 1567 | .set_rxnfc = netvsc_set_rxnfc, |
stephen hemminger | 962f3fe | 2017-01-24 13:06:02 -0800 | [diff] [blame] | 1568 | .get_rxfh_key_size = netvsc_get_rxfh_key_size, |
| 1569 | .get_rxfh_indir_size = netvsc_rss_indir_size, |
| 1570 | .get_rxfh = netvsc_get_rxfh, |
| 1571 | .set_rxfh = netvsc_set_rxfh, |
Philippe Reynes | 5e8456f | 2017-03-08 23:41:04 +0100 | [diff] [blame] | 1572 | .get_link_ksettings = netvsc_get_link_ksettings, |
| 1573 | .set_link_ksettings = netvsc_set_link_ksettings, |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 1574 | .get_ringparam = netvsc_get_ringparam, |
| 1575 | .set_ringparam = netvsc_set_ringparam, |
Stephen Hemminger | f82f4ad | 2010-05-04 09:58:57 -0700 | [diff] [blame] | 1576 | }; |
| 1577 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1578 | static const struct net_device_ops device_ops = { |
| 1579 | .ndo_open = netvsc_open, |
| 1580 | .ndo_stop = netvsc_close, |
| 1581 | .ndo_start_xmit = netvsc_start_xmit, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1582 | .ndo_set_rx_mode = netvsc_set_multicast_list, |
Haiyang Zhang | 4d447c9 | 2011-12-15 13:45:17 -0800 | [diff] [blame] | 1583 | .ndo_change_mtu = netvsc_change_mtu, |
Haiyang Zhang | b681b58 | 2010-08-03 19:15:31 +0000 | [diff] [blame] | 1584 | .ndo_validate_addr = eth_validate_addr, |
Haiyang Zhang | 1ce09e8 | 2012-07-10 07:19:22 +0000 | [diff] [blame] | 1585 | .ndo_set_mac_address = netvsc_set_mac_addr, |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1586 | .ndo_select_queue = netvsc_select_queue, |
sixiao@microsoft.com | 7eafd9b | 2015-05-14 01:00:25 -0700 | [diff] [blame] | 1587 | .ndo_get_stats64 = netvsc_get_stats64, |
Richard Weinberger | 316158f | 2014-07-09 16:23:59 +0200 | [diff] [blame] | 1588 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1589 | .ndo_poll_controller = netvsc_poll_controller, |
| 1590 | #endif |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1591 | }; |
| 1592 | |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1593 | /* |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1594 | * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link |
| 1595 | * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is |
| 1596 | * present send GARP packet to network peers with netif_notify_peers(). |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1597 | */ |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1598 | static void netvsc_link_change(struct work_struct *w) |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1599 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1600 | struct net_device_context *ndev_ctx = |
| 1601 | container_of(w, struct net_device_context, dwork.work); |
| 1602 | struct hv_device *device_obj = ndev_ctx->device_ctx; |
| 1603 | struct net_device *net = hv_get_drvdata(device_obj); |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1604 | struct netvsc_device *net_device; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1605 | struct rndis_device *rdev; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1606 | struct netvsc_reconfig *event = NULL; |
| 1607 | bool notify = false, reschedule = false; |
| 1608 | unsigned long flags, next_reconfig, delay; |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1609 | |
stephen hemminger | 9b4e946 | 2017-08-24 16:49:16 -0700 | [diff] [blame] | 1610 | /* if changes are happening, comeback later */ |
| 1611 | if (!rtnl_trylock()) { |
| 1612 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); |
| 1613 | return; |
| 1614 | } |
| 1615 | |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 1616 | net_device = rtnl_dereference(ndev_ctx->nvdev); |
| 1617 | if (!net_device) |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1618 | goto out_unlock; |
| 1619 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1620 | rdev = net_device->extension; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1621 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1622 | next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT; |
| 1623 | if (time_is_after_jiffies(next_reconfig)) { |
| 1624 | /* link_watch only sends one notification with current state |
| 1625 | * per second, avoid doing reconfig more frequently. Handle |
| 1626 | * wrap around. |
| 1627 | */ |
| 1628 | delay = next_reconfig - jiffies; |
| 1629 | delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT; |
| 1630 | schedule_delayed_work(&ndev_ctx->dwork, delay); |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1631 | goto out_unlock; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1632 | } |
| 1633 | ndev_ctx->last_reconfig = jiffies; |
| 1634 | |
| 1635 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
| 1636 | if (!list_empty(&ndev_ctx->reconfig_events)) { |
| 1637 | event = list_first_entry(&ndev_ctx->reconfig_events, |
| 1638 | struct netvsc_reconfig, list); |
| 1639 | list_del(&event->list); |
| 1640 | reschedule = !list_empty(&ndev_ctx->reconfig_events); |
| 1641 | } |
| 1642 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 1643 | |
| 1644 | if (!event) |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1645 | goto out_unlock; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1646 | |
| 1647 | switch (event->event) { |
| 1648 | /* Only the following events are possible due to the check in |
| 1649 | * netvsc_linkstatus_callback() |
| 1650 | */ |
| 1651 | case RNDIS_STATUS_MEDIA_CONNECT: |
| 1652 | if (rdev->link_state) { |
| 1653 | rdev->link_state = false; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1654 | netif_carrier_on(net); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1655 | netif_tx_wake_all_queues(net); |
| 1656 | } else { |
| 1657 | notify = true; |
Haiyang Zhang | 3a494e7 | 2014-06-19 18:34:36 -0700 | [diff] [blame] | 1658 | } |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1659 | kfree(event); |
| 1660 | break; |
| 1661 | case RNDIS_STATUS_MEDIA_DISCONNECT: |
| 1662 | if (!rdev->link_state) { |
| 1663 | rdev->link_state = true; |
| 1664 | netif_carrier_off(net); |
| 1665 | netif_tx_stop_all_queues(net); |
| 1666 | } |
| 1667 | kfree(event); |
| 1668 | break; |
| 1669 | case RNDIS_STATUS_NETWORK_CHANGE: |
| 1670 | /* Only makes sense if carrier is present */ |
| 1671 | if (!rdev->link_state) { |
| 1672 | rdev->link_state = true; |
| 1673 | netif_carrier_off(net); |
| 1674 | netif_tx_stop_all_queues(net); |
| 1675 | event->event = RNDIS_STATUS_MEDIA_CONNECT; |
| 1676 | spin_lock_irqsave(&ndev_ctx->lock, flags); |
Haiyang Zhang | 15cfd40 | 2016-04-21 16:13:01 -0700 | [diff] [blame] | 1677 | list_add(&event->list, &ndev_ctx->reconfig_events); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1678 | spin_unlock_irqrestore(&ndev_ctx->lock, flags); |
| 1679 | reschedule = true; |
| 1680 | } |
| 1681 | break; |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | rtnl_unlock(); |
| 1685 | |
| 1686 | if (notify) |
| 1687 | netdev_notify_peers(net); |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1688 | |
| 1689 | /* link_watch only sends one notification with current state per |
| 1690 | * second, handle next reconfig event in 2 seconds. |
| 1691 | */ |
| 1692 | if (reschedule) |
| 1693 | schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT); |
Vitaly Kuznetsov | 1bdcec8 | 2016-05-13 13:55:21 +0200 | [diff] [blame] | 1694 | |
| 1695 | return; |
| 1696 | |
| 1697 | out_unlock: |
| 1698 | rtnl_unlock(); |
Haiyang Zhang | c996edc | 2011-04-06 15:18:00 -0700 | [diff] [blame] | 1699 | } |
| 1700 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1701 | static struct net_device *get_netvsc_bymac(const u8 *mac) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1702 | { |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1703 | struct net_device *dev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1704 | |
Stephen Hemminger | 8737caa | 2016-08-23 12:17:44 -0700 | [diff] [blame] | 1705 | ASSERT_RTNL(); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1706 | |
| 1707 | for_each_netdev(&init_net, dev) { |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1708 | if (dev->netdev_ops != &device_ops) |
| 1709 | continue; /* not a netvsc device */ |
| 1710 | |
| 1711 | if (ether_addr_equal(mac, dev->perm_addr)) |
| 1712 | return dev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1713 | } |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1714 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1715 | return NULL; |
| 1716 | } |
| 1717 | |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1718 | static struct net_device *get_netvsc_byref(struct net_device *vf_netdev) |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1719 | { |
| 1720 | struct net_device *dev; |
| 1721 | |
| 1722 | ASSERT_RTNL(); |
| 1723 | |
| 1724 | for_each_netdev(&init_net, dev) { |
| 1725 | struct net_device_context *net_device_ctx; |
| 1726 | |
| 1727 | if (dev->netdev_ops != &device_ops) |
| 1728 | continue; /* not a netvsc device */ |
| 1729 | |
| 1730 | net_device_ctx = netdev_priv(dev); |
stephen hemminger | 79e8cbe | 2017-07-19 11:53:13 -0700 | [diff] [blame] | 1731 | if (!rtnl_dereference(net_device_ctx->nvdev)) |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1732 | continue; /* device is removed */ |
| 1733 | |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1734 | if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev) |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1735 | return dev; /* a match */ |
| 1736 | } |
| 1737 | |
| 1738 | return NULL; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1741 | /* Called when VF is injecting data into network stack. |
| 1742 | * Change the associated network device from VF to netvsc. |
| 1743 | * note: already called with rcu_read_lock |
| 1744 | */ |
| 1745 | static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb) |
| 1746 | { |
| 1747 | struct sk_buff *skb = *pskb; |
| 1748 | struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data); |
| 1749 | struct net_device_context *ndev_ctx = netdev_priv(ndev); |
| 1750 | struct netvsc_vf_pcpu_stats *pcpu_stats |
| 1751 | = this_cpu_ptr(ndev_ctx->vf_stats); |
| 1752 | |
| 1753 | skb->dev = ndev; |
| 1754 | |
| 1755 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 1756 | pcpu_stats->rx_packets++; |
| 1757 | pcpu_stats->rx_bytes += skb->len; |
| 1758 | u64_stats_update_end(&pcpu_stats->syncp); |
| 1759 | |
| 1760 | return RX_HANDLER_ANOTHER; |
| 1761 | } |
| 1762 | |
| 1763 | static int netvsc_vf_join(struct net_device *vf_netdev, |
| 1764 | struct net_device *ndev) |
| 1765 | { |
| 1766 | struct net_device_context *ndev_ctx = netdev_priv(ndev); |
| 1767 | int ret; |
| 1768 | |
| 1769 | ret = netdev_rx_handler_register(vf_netdev, |
| 1770 | netvsc_vf_handle_frame, ndev); |
| 1771 | if (ret != 0) { |
| 1772 | netdev_err(vf_netdev, |
| 1773 | "can not register netvsc VF receive handler (err = %d)\n", |
| 1774 | ret); |
| 1775 | goto rx_handler_failed; |
| 1776 | } |
| 1777 | |
David Ahern | 42ab19e | 2017-10-04 17:48:47 -0700 | [diff] [blame] | 1778 | ret = netdev_upper_dev_link(vf_netdev, ndev, NULL); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1779 | if (ret != 0) { |
| 1780 | netdev_err(vf_netdev, |
| 1781 | "can not set master device %s (err = %d)\n", |
| 1782 | ndev->name, ret); |
| 1783 | goto upper_link_failed; |
| 1784 | } |
| 1785 | |
| 1786 | /* set slave flag before open to prevent IPv6 addrconf */ |
| 1787 | vf_netdev->flags |= IFF_SLAVE; |
| 1788 | |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1789 | schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT); |
| 1790 | |
| 1791 | call_netdevice_notifiers(NETDEV_JOIN, vf_netdev); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1792 | |
| 1793 | netdev_info(vf_netdev, "joined to %s\n", ndev->name); |
| 1794 | return 0; |
| 1795 | |
| 1796 | upper_link_failed: |
| 1797 | netdev_rx_handler_unregister(vf_netdev); |
| 1798 | rx_handler_failed: |
| 1799 | return ret; |
| 1800 | } |
| 1801 | |
| 1802 | static void __netvsc_vf_setup(struct net_device *ndev, |
| 1803 | struct net_device *vf_netdev) |
| 1804 | { |
| 1805 | int ret; |
| 1806 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1807 | /* Align MTU of VF with master */ |
| 1808 | ret = dev_set_mtu(vf_netdev, ndev->mtu); |
| 1809 | if (ret) |
| 1810 | netdev_warn(vf_netdev, |
| 1811 | "unable to change mtu to %u\n", ndev->mtu); |
| 1812 | |
| 1813 | if (netif_running(ndev)) { |
| 1814 | ret = dev_open(vf_netdev); |
| 1815 | if (ret) |
| 1816 | netdev_warn(vf_netdev, |
| 1817 | "unable to open: %d\n", ret); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | /* Setup VF as slave of the synthetic device. |
| 1822 | * Runs in workqueue to avoid recursion in netlink callbacks. |
| 1823 | */ |
| 1824 | static void netvsc_vf_setup(struct work_struct *w) |
| 1825 | { |
| 1826 | struct net_device_context *ndev_ctx |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1827 | = container_of(w, struct net_device_context, vf_takeover.work); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1828 | struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx); |
| 1829 | struct net_device *vf_netdev; |
| 1830 | |
stephen hemminger | fb84af8 | 2017-08-04 12:14:00 -0700 | [diff] [blame] | 1831 | if (!rtnl_trylock()) { |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1832 | schedule_delayed_work(&ndev_ctx->vf_takeover, 0); |
stephen hemminger | fb84af8 | 2017-08-04 12:14:00 -0700 | [diff] [blame] | 1833 | return; |
| 1834 | } |
| 1835 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1836 | vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
| 1837 | if (vf_netdev) |
| 1838 | __netvsc_vf_setup(ndev, vf_netdev); |
| 1839 | |
| 1840 | rtnl_unlock(); |
| 1841 | } |
| 1842 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1843 | static int netvsc_register_vf(struct net_device *vf_netdev) |
| 1844 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1845 | struct net_device *ndev; |
| 1846 | struct net_device_context *net_device_ctx; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1847 | struct netvsc_device *netvsc_dev; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1848 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1849 | if (vf_netdev->addr_len != ETH_ALEN) |
| 1850 | return NOTIFY_DONE; |
| 1851 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1852 | /* |
| 1853 | * We will use the MAC address to locate the synthetic interface to |
| 1854 | * associate with the VF interface. If we don't find a matching |
| 1855 | * synthetic interface, move on. |
| 1856 | */ |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1857 | ndev = get_netvsc_bymac(vf_netdev->perm_addr); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1858 | if (!ndev) |
| 1859 | return NOTIFY_DONE; |
| 1860 | |
| 1861 | net_device_ctx = netdev_priv(ndev); |
stephen hemminger | 545a8e7 | 2017-03-22 14:51:00 -0700 | [diff] [blame] | 1862 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1863 | if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev)) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1864 | return NOTIFY_DONE; |
| 1865 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1866 | if (netvsc_vf_join(vf_netdev, ndev) != 0) |
| 1867 | return NOTIFY_DONE; |
| 1868 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1869 | netdev_info(ndev, "VF registering: %s\n", vf_netdev->name); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1870 | |
Stephen Hemminger | 07d0f00 | 2016-09-22 16:56:30 -0700 | [diff] [blame] | 1871 | dev_hold(vf_netdev); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1872 | rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1873 | return NOTIFY_OK; |
| 1874 | } |
| 1875 | |
Stephen Hemminger | 9a0c48d | 2017-08-31 16:16:12 -0700 | [diff] [blame] | 1876 | /* VF up/down change detected, schedule to change data path */ |
| 1877 | static int netvsc_vf_changed(struct net_device *vf_netdev) |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1878 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1879 | struct net_device_context *net_device_ctx; |
stephen hemminger | 7b83f52 | 2017-08-07 11:30:00 -0700 | [diff] [blame] | 1880 | struct netvsc_device *netvsc_dev; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1881 | struct net_device *ndev; |
Stephen Hemminger | 9a0c48d | 2017-08-31 16:16:12 -0700 | [diff] [blame] | 1882 | bool vf_is_up = netif_running(vf_netdev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1883 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1884 | ndev = get_netvsc_byref(vf_netdev); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1885 | if (!ndev) |
| 1886 | return NOTIFY_DONE; |
| 1887 | |
| 1888 | net_device_ctx = netdev_priv(ndev); |
stephen hemminger | 7b83f52 | 2017-08-07 11:30:00 -0700 | [diff] [blame] | 1889 | netvsc_dev = rtnl_dereference(net_device_ctx->nvdev); |
| 1890 | if (!netvsc_dev) |
| 1891 | return NOTIFY_DONE; |
| 1892 | |
Stephen Hemminger | 9a0c48d | 2017-08-31 16:16:12 -0700 | [diff] [blame] | 1893 | netvsc_switch_datapath(ndev, vf_is_up); |
| 1894 | netdev_info(ndev, "Data path switched %s VF: %s\n", |
| 1895 | vf_is_up ? "to" : "from", vf_netdev->name); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1896 | |
| 1897 | return NOTIFY_OK; |
| 1898 | } |
| 1899 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1900 | static int netvsc_unregister_vf(struct net_device *vf_netdev) |
| 1901 | { |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1902 | struct net_device *ndev; |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1903 | struct net_device_context *net_device_ctx; |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1904 | |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1905 | ndev = get_netvsc_byref(vf_netdev); |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1906 | if (!ndev) |
| 1907 | return NOTIFY_DONE; |
| 1908 | |
| 1909 | net_device_ctx = netdev_priv(ndev); |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1910 | cancel_delayed_work_sync(&net_device_ctx->vf_takeover); |
Stephen Hemminger | e8ff40d | 2016-09-22 16:56:32 -0700 | [diff] [blame] | 1911 | |
Vitaly Kuznetsov | 0a1275c | 2016-05-13 13:55:23 +0200 | [diff] [blame] | 1912 | netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1913 | |
Stephen Hemminger | ec158f7 | 2017-08-31 16:16:13 -0700 | [diff] [blame] | 1914 | netdev_rx_handler_unregister(vf_netdev); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1915 | netdev_upper_dev_unlink(vf_netdev, ndev); |
Stephen Hemminger | f207c10 | 2016-09-22 16:56:33 -0700 | [diff] [blame] | 1916 | RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL); |
Stephen Hemminger | 07d0f00 | 2016-09-22 16:56:30 -0700 | [diff] [blame] | 1917 | dev_put(vf_netdev); |
Stephen Hemminger | ec158f7 | 2017-08-31 16:16:13 -0700 | [diff] [blame] | 1918 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 1919 | return NOTIFY_OK; |
| 1920 | } |
| 1921 | |
K. Y. Srinivasan | 8494689 | 2011-09-13 10:59:38 -0700 | [diff] [blame] | 1922 | static int netvsc_probe(struct hv_device *dev, |
| 1923 | const struct hv_vmbus_device_id *dev_id) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1924 | { |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1925 | struct net_device *net = NULL; |
| 1926 | struct net_device_context *net_device_ctx; |
| 1927 | struct netvsc_device_info device_info; |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1928 | struct netvsc_device *nvdev; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1929 | int ret = -ENOMEM; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1930 | |
Haiyang Zhang | 5b54dac | 2014-04-21 10:20:28 -0700 | [diff] [blame] | 1931 | net = alloc_etherdev_mq(sizeof(struct net_device_context), |
stephen hemminger | 2b01888 | 2017-01-24 13:06:03 -0800 | [diff] [blame] | 1932 | VRSS_CHANNEL_MAX); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1933 | if (!net) |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1934 | goto no_net; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1935 | |
Haiyang Zhang | 1b07da5 | 2014-03-04 14:11:06 -0800 | [diff] [blame] | 1936 | netif_carrier_off(net); |
| 1937 | |
Haiyang Zhang | b37879e | 2016-08-04 10:42:14 -0700 | [diff] [blame] | 1938 | netvsc_init_settings(net); |
| 1939 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1940 | net_device_ctx = netdev_priv(net); |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 1941 | net_device_ctx->device_ctx = dev; |
Simon Xiao | 3f300ff | 2015-04-28 01:05:17 -0700 | [diff] [blame] | 1942 | net_device_ctx->msg_enable = netif_msg_init(debug, default_msg); |
| 1943 | if (netif_msg_probe(net_device_ctx)) |
| 1944 | netdev_dbg(net, "netvsc msg_enable: %d\n", |
| 1945 | net_device_ctx->msg_enable); |
| 1946 | |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 1947 | hv_set_drvdata(dev, net); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 1948 | |
Haiyang Zhang | 891de74 | 2014-02-12 16:54:27 -0800 | [diff] [blame] | 1949 | INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1950 | |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1951 | spin_lock_init(&net_device_ctx->lock); |
| 1952 | INIT_LIST_HEAD(&net_device_ctx->reconfig_events); |
stephen hemminger | 6123c66 | 2017-08-09 17:46:03 -0700 | [diff] [blame] | 1953 | INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1954 | |
| 1955 | net_device_ctx->vf_stats |
| 1956 | = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats); |
| 1957 | if (!net_device_ctx->vf_stats) |
| 1958 | goto no_stats; |
Vitaly Kuznetsov | 27a70af | 2015-11-27 11:39:55 +0100 | [diff] [blame] | 1959 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1960 | net->netdev_ops = &device_ops; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1961 | net->ethtool_ops = ðtool_ops; |
K. Y. Srinivasan | 9efd21e | 2011-04-29 13:45:10 -0700 | [diff] [blame] | 1962 | SET_NETDEV_DEV(net, &dev->device); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 1963 | |
Vitaly Kuznetsov | 14a03cf | 2016-02-05 17:29:08 +0100 | [diff] [blame] | 1964 | /* We always need headroom for rndis header */ |
| 1965 | net->needed_headroom = RNDIS_AND_PPI_SIZE; |
| 1966 | |
Haiyang Zhang | 6450f8f | 2017-09-22 15:31:38 -0700 | [diff] [blame] | 1967 | /* Initialize the number of queues to be 1, we may change it if more |
| 1968 | * channels are offered later. |
| 1969 | */ |
| 1970 | netif_set_real_num_tx_queues(net, 1); |
| 1971 | netif_set_real_num_rx_queues(net, 1); |
| 1972 | |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1973 | /* Notify the netvsc driver of the new device */ |
Andrew Schwartzmeyer | 8ebdcc5 | 2015-08-11 17:14:31 -0700 | [diff] [blame] | 1974 | memset(&device_info, 0, sizeof(device_info)); |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1975 | device_info.ring_size = ring_size; |
stephen hemminger | 3071ada | 2017-03-22 14:50:59 -0700 | [diff] [blame] | 1976 | device_info.num_chn = VRSS_CHANNEL_DEFAULT; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 1977 | device_info.send_sections = NETVSC_DEFAULT_TX; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 1978 | device_info.send_section_size = NETVSC_SEND_SECTION_SIZE; |
stephen hemminger | 8b53279 | 2017-08-09 17:46:11 -0700 | [diff] [blame] | 1979 | device_info.recv_sections = NETVSC_DEFAULT_RX; |
Alex Ng | 0ab09be | 2017-09-20 11:17:35 -0700 | [diff] [blame] | 1980 | device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE; |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 1981 | |
| 1982 | nvdev = rndis_filter_device_add(dev, &device_info); |
| 1983 | if (IS_ERR(nvdev)) { |
| 1984 | ret = PTR_ERR(nvdev); |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1985 | netdev_err(net, "unable to add netvsc device (ret %d)\n", ret); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1986 | goto rndis_failed; |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1987 | } |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 1988 | |
Haiyang Zhang | 692e084 | 2011-09-01 12:19:43 -0700 | [diff] [blame] | 1989 | memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN); |
| 1990 | |
stephen hemminger | 23312a3 | 2017-01-24 13:05:59 -0800 | [diff] [blame] | 1991 | /* hw_features computed in rndis_filter_device_add */ |
| 1992 | net->features = net->hw_features | |
| 1993 | NETIF_F_HIGHDMA | NETIF_F_SG | |
| 1994 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX; |
| 1995 | net->vlan_features = net->features; |
| 1996 | |
stephen hemminger | 9749fed | 2017-07-19 11:53:16 -0700 | [diff] [blame] | 1997 | netdev_lockdep_set_classes(net); |
| 1998 | |
Jarod Wilson | d0c2c99 | 2016-10-20 13:55:21 -0400 | [diff] [blame] | 1999 | /* MTU range: 68 - 1500 or 65521 */ |
| 2000 | net->min_mtu = NETVSC_MTU_MIN; |
| 2001 | if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2) |
| 2002 | net->max_mtu = NETVSC_MTU - ETH_HLEN; |
| 2003 | else |
| 2004 | net->max_mtu = ETH_DATA_LEN; |
| 2005 | |
Haiyang Zhang | a68f961 | 2013-12-20 16:52:31 -0800 | [diff] [blame] | 2006 | ret = register_netdev(net); |
| 2007 | if (ret != 0) { |
| 2008 | pr_err("Unable to register netdev.\n"); |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 2009 | goto register_failed; |
Haiyang Zhang | a68f961 | 2013-12-20 16:52:31 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2012 | return ret; |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 2013 | |
| 2014 | register_failed: |
| 2015 | rndis_filter_device_remove(dev, nvdev); |
| 2016 | rndis_failed: |
| 2017 | free_percpu(net_device_ctx->vf_stats); |
| 2018 | no_stats: |
| 2019 | hv_set_drvdata(dev, NULL); |
| 2020 | free_netdev(net); |
| 2021 | no_net: |
| 2022 | return ret; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 2025 | static int netvsc_remove(struct hv_device *dev) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2026 | { |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 2027 | struct net_device_context *ndev_ctx; |
Stephen Hemminger | ec158f7 | 2017-08-31 16:16:13 -0700 | [diff] [blame] | 2028 | struct net_device *vf_netdev; |
| 2029 | struct net_device *net; |
K. Y. Srinivasan | 2ddd5e5f | 2011-09-13 10:59:49 -0700 | [diff] [blame] | 2030 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 2031 | net = hv_get_drvdata(dev); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2032 | if (net == NULL) { |
K. Y. Srinivasan | 415b023 | 2011-04-29 13:45:12 -0700 | [diff] [blame] | 2033 | dev_err(&dev->device, "No net device to remove\n"); |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2034 | return 0; |
| 2035 | } |
| 2036 | |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 2037 | ndev_ctx = netdev_priv(net); |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 2038 | |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 2039 | netif_device_detach(net); |
Vitaly Kuznetsov | f580aec | 2016-05-13 13:55:20 +0200 | [diff] [blame] | 2040 | |
Haiyang Zhang | 122a5f6 | 2011-05-27 06:21:55 -0700 | [diff] [blame] | 2041 | cancel_delayed_work_sync(&ndev_ctx->dwork); |
| 2042 | |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2043 | /* |
| 2044 | * Call to the vsc driver to let it know that the device is being |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 2045 | * removed. Also blocks mtu and channel changes. |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2046 | */ |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 2047 | rtnl_lock(); |
Stephen Hemminger | ec158f7 | 2017-08-31 16:16:13 -0700 | [diff] [blame] | 2048 | vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev); |
| 2049 | if (vf_netdev) |
| 2050 | netvsc_unregister_vf(vf_netdev); |
| 2051 | |
Stephen Hemminger | 8195b13 | 2017-09-06 13:53:05 -0700 | [diff] [blame] | 2052 | unregister_netdevice(net); |
| 2053 | |
stephen hemminger | 79e8cbe | 2017-07-19 11:53:13 -0700 | [diff] [blame] | 2054 | rndis_filter_device_remove(dev, |
| 2055 | rtnl_dereference(ndev_ctx->nvdev)); |
stephen hemminger | a0be450 | 2017-03-22 14:51:01 -0700 | [diff] [blame] | 2056 | rtnl_unlock(); |
| 2057 | |
Vitaly Kuznetsov | 3d541ac | 2016-05-13 13:55:22 +0200 | [diff] [blame] | 2058 | hv_set_drvdata(dev, NULL); |
| 2059 | |
stephen hemminger | 0c19556 | 2017-08-01 19:58:53 -0700 | [diff] [blame] | 2060 | free_percpu(ndev_ctx->vf_stats); |
Simon Xiao | 6c80f3f | 2017-01-24 13:06:13 -0800 | [diff] [blame] | 2061 | free_netdev(net); |
Haiyang Zhang | df06bcf | 2011-05-23 09:03:47 -0700 | [diff] [blame] | 2062 | return 0; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 2065 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 2066 | /* Network guid */ |
K. Y. Srinivasan | 8f50594 | 2013-01-23 17:42:42 -0800 | [diff] [blame] | 2067 | { HV_NIC_GUID, }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 2068 | { }, |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 2069 | }; |
| 2070 | |
| 2071 | MODULE_DEVICE_TABLE(vmbus, id_table); |
| 2072 | |
K. Y. Srinivasan | f1542a6 | 2011-05-10 07:55:16 -0700 | [diff] [blame] | 2073 | /* The one and only one */ |
K. Y. Srinivasan | fde0ef9 | 2011-05-12 19:35:08 -0700 | [diff] [blame] | 2074 | static struct hv_driver netvsc_drv = { |
Haiyang Zhang | d31b20f | 2012-03-07 10:02:00 +0000 | [diff] [blame] | 2075 | .name = KBUILD_MODNAME, |
K. Y. Srinivasan | 345c4cc | 2011-08-25 09:48:34 -0700 | [diff] [blame] | 2076 | .id_table = id_table, |
K. Y. Srinivasan | fde0ef9 | 2011-05-12 19:35:08 -0700 | [diff] [blame] | 2077 | .probe = netvsc_probe, |
| 2078 | .remove = netvsc_remove, |
K. Y. Srinivasan | d489097 | 2011-05-10 07:55:17 -0700 | [diff] [blame] | 2079 | }; |
K. Y. Srinivasan | f1542a6 | 2011-05-10 07:55:16 -0700 | [diff] [blame] | 2080 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 2081 | /* |
| 2082 | * On Hyper-V, every VF interface is matched with a corresponding |
| 2083 | * synthetic interface. The synthetic interface is presented first |
| 2084 | * to the guest. When the corresponding VF instance is registered, |
| 2085 | * we will take care of switching the data path. |
| 2086 | */ |
| 2087 | static int netvsc_netdev_event(struct notifier_block *this, |
| 2088 | unsigned long event, void *ptr) |
| 2089 | { |
| 2090 | struct net_device *event_dev = netdev_notifier_info_to_dev(ptr); |
| 2091 | |
Stephen Hemminger | ee837a1 | 2016-09-22 16:56:31 -0700 | [diff] [blame] | 2092 | /* Skip our own events */ |
| 2093 | if (event_dev->netdev_ops == &device_ops) |
| 2094 | return NOTIFY_DONE; |
| 2095 | |
| 2096 | /* Avoid non-Ethernet type devices */ |
| 2097 | if (event_dev->type != ARPHRD_ETHER) |
| 2098 | return NOTIFY_DONE; |
| 2099 | |
Vitaly Kuznetsov | 0dbff14 | 2016-08-15 17:48:43 +0200 | [diff] [blame] | 2100 | /* Avoid Vlan dev with same MAC registering as VF */ |
Parav Pandit | d0d7b10 | 2017-02-04 11:00:49 -0600 | [diff] [blame] | 2101 | if (is_vlan_dev(event_dev)) |
Vitaly Kuznetsov | 0dbff14 | 2016-08-15 17:48:43 +0200 | [diff] [blame] | 2102 | return NOTIFY_DONE; |
| 2103 | |
| 2104 | /* Avoid Bonding master dev with same MAC registering as VF */ |
Stephen Hemminger | ee837a1 | 2016-09-22 16:56:31 -0700 | [diff] [blame] | 2105 | if ((event_dev->priv_flags & IFF_BONDING) && |
| 2106 | (event_dev->flags & IFF_MASTER)) |
Haiyang Zhang | cb2911f | 2016-06-02 12:02:04 -0700 | [diff] [blame] | 2107 | return NOTIFY_DONE; |
| 2108 | |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 2109 | switch (event) { |
| 2110 | case NETDEV_REGISTER: |
| 2111 | return netvsc_register_vf(event_dev); |
| 2112 | case NETDEV_UNREGISTER: |
| 2113 | return netvsc_unregister_vf(event_dev); |
| 2114 | case NETDEV_UP: |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 2115 | case NETDEV_DOWN: |
Stephen Hemminger | 9a0c48d | 2017-08-31 16:16:12 -0700 | [diff] [blame] | 2116 | return netvsc_vf_changed(event_dev); |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 2117 | default: |
| 2118 | return NOTIFY_DONE; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | static struct notifier_block netvsc_netdev_notifier = { |
| 2123 | .notifier_call = netvsc_netdev_event, |
| 2124 | }; |
| 2125 | |
K. Y. Srinivasan | a9869c9 | 2011-05-12 19:35:17 -0700 | [diff] [blame] | 2126 | static void __exit netvsc_drv_exit(void) |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 2127 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 2128 | unregister_netdevice_notifier(&netvsc_netdev_notifier); |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 2129 | vmbus_driver_unregister(&netvsc_drv); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
K. Y. Srinivasan | 1fde28c | 2011-05-12 19:35:16 -0700 | [diff] [blame] | 2132 | static int __init netvsc_drv_init(void) |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2133 | { |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 2134 | int ret; |
| 2135 | |
Haiyang Zhang | fa85a6c | 2012-07-25 08:08:41 +0000 | [diff] [blame] | 2136 | if (ring_size < RING_SIZE_MIN) { |
| 2137 | ring_size = RING_SIZE_MIN; |
| 2138 | pr_info("Increased ring_size to %d (min allowed)\n", |
| 2139 | ring_size); |
| 2140 | } |
KY Srinivasan | 84bf9ce | 2016-04-14 16:31:54 -0700 | [diff] [blame] | 2141 | ret = vmbus_driver_register(&netvsc_drv); |
| 2142 | |
| 2143 | if (ret) |
| 2144 | return ret; |
| 2145 | |
| 2146 | register_netdevice_notifier(&netvsc_netdev_notifier); |
| 2147 | return 0; |
Greg Kroah-Hartman | df2fff2 | 2009-08-31 21:11:12 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 2150 | MODULE_LICENSE("GPL"); |
Stephen Hemminger | 7880fc5 | 2010-05-04 09:58:52 -0700 | [diff] [blame] | 2151 | MODULE_DESCRIPTION("Microsoft Hyper-V network driver"); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 2152 | |
K. Y. Srinivasan | 1fde28c | 2011-05-12 19:35:16 -0700 | [diff] [blame] | 2153 | module_init(netvsc_drv_init); |
K. Y. Srinivasan | a9869c9 | 2011-05-12 19:35:17 -0700 | [diff] [blame] | 2154 | module_exit(netvsc_drv_exit); |