Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An implementation of key value pair (KVP) functionality for Linux. |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2010, Novell, Inc. |
| 6 | * Author : K. Y. Srinivasan <ksrinivasan@novell.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 15 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 16 | * details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/net.h> |
| 26 | #include <linux/nls.h> |
| 27 | #include <linux/connector.h> |
| 28 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 29 | #include <linux/hyperv.h> |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 30 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | /* |
| 34 | * Global state maintained for transaction that is being processed. |
| 35 | * Note that only one transaction can be active at any point in time. |
| 36 | * |
| 37 | * This state is set when we receive a request from the host; we |
| 38 | * cleanup this state when the transaction is completed - when we respond |
| 39 | * to the host with the key value. |
| 40 | */ |
| 41 | |
| 42 | static struct { |
| 43 | bool active; /* transaction status - active or not */ |
| 44 | int recv_len; /* number of bytes received. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 45 | struct hv_kvp_msg *kvp_msg; /* current message */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 46 | struct vmbus_channel *recv_channel; /* chn we got the request */ |
| 47 | u64 recv_req_id; /* request ID. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 48 | void *kvp_context; /* for the channel callback */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 49 | } kvp_transaction; |
| 50 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Before we can accept KVP messages from the host, we need |
| 53 | * to handshake with the user level daemon. This state tracks |
| 54 | * if we are in the handshake phase. |
| 55 | */ |
| 56 | static bool in_hand_shake = true; |
| 57 | |
| 58 | /* |
| 59 | * This state maintains the version number registered by the daemon. |
| 60 | */ |
| 61 | static int dm_reg_value; |
| 62 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 63 | static void kvp_send_key(struct work_struct *dummy); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 64 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 65 | |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 66 | static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 67 | static void kvp_work_func(struct work_struct *dummy); |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 68 | static void kvp_register(int); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 69 | |
| 70 | static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 71 | static DECLARE_WORK(kvp_sendkey_work, kvp_send_key); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 72 | |
| 73 | static struct cb_id kvp_id = { CN_KVP_IDX, CN_KVP_VAL }; |
| 74 | static const char kvp_name[] = "kvp_kernel_module"; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 75 | static u8 *recv_buffer; |
| 76 | /* |
| 77 | * Register the kernel component with the user-level daemon. |
| 78 | * As part of this registration, pass the LIC version number. |
| 79 | */ |
| 80 | |
| 81 | static void |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 82 | kvp_register(int reg_value) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 83 | { |
| 84 | |
| 85 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 86 | struct hv_kvp_msg *kvp_msg; |
| 87 | char *version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 88 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 89 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg), GFP_ATOMIC); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 90 | |
| 91 | if (msg) { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 92 | kvp_msg = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame] | 93 | version = kvp_msg->body.kvp_register.version; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 94 | msg->id.idx = CN_KVP_IDX; |
| 95 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 96 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 97 | kvp_msg->kvp_hdr.operation = reg_value; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 98 | strcpy(version, HV_DRV_VERSION); |
| 99 | msg->len = sizeof(struct hv_kvp_msg); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 100 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 101 | kfree(msg); |
| 102 | } |
| 103 | } |
| 104 | static void |
| 105 | kvp_work_func(struct work_struct *dummy) |
| 106 | { |
| 107 | /* |
| 108 | * If the timer fires, the user-mode component has not responded; |
| 109 | * process the pending transaction. |
| 110 | */ |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 111 | kvp_respond_to_host(NULL, HV_E_FAIL); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 112 | } |
| 113 | |
K. Y. Srinivasan | 9ac5d53 | 2014-07-07 16:34:25 -0700 | [diff] [blame] | 114 | static void poll_channel(struct vmbus_channel *channel) |
| 115 | { |
| 116 | unsigned long flags; |
| 117 | |
| 118 | spin_lock_irqsave(&channel->inbound_lock, flags); |
| 119 | hv_kvp_onchannelcallback(channel); |
| 120 | spin_unlock_irqrestore(&channel->inbound_lock, flags); |
| 121 | } |
| 122 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 123 | static int kvp_handle_handshake(struct hv_kvp_msg *msg) |
| 124 | { |
| 125 | int ret = 1; |
| 126 | |
| 127 | switch (msg->kvp_hdr.operation) { |
| 128 | case KVP_OP_REGISTER: |
| 129 | dm_reg_value = KVP_OP_REGISTER; |
| 130 | pr_info("KVP: IP injection functionality not available\n"); |
| 131 | pr_info("KVP: Upgrade the KVP daemon\n"); |
| 132 | break; |
| 133 | case KVP_OP_REGISTER1: |
| 134 | dm_reg_value = KVP_OP_REGISTER1; |
| 135 | break; |
| 136 | default: |
| 137 | pr_info("KVP: incompatible daemon\n"); |
| 138 | pr_info("KVP: KVP version: %d, Daemon version: %d\n", |
| 139 | KVP_OP_REGISTER1, msg->kvp_hdr.operation); |
| 140 | ret = 0; |
| 141 | } |
| 142 | |
| 143 | if (ret) { |
| 144 | /* |
| 145 | * We have a compatible daemon; complete the handshake. |
| 146 | */ |
| 147 | pr_info("KVP: user-mode registering done.\n"); |
| 148 | kvp_register(dm_reg_value); |
| 149 | kvp_transaction.active = false; |
| 150 | if (kvp_transaction.kvp_context) |
K. Y. Srinivasan | 9ac5d53 | 2014-07-07 16:34:25 -0700 | [diff] [blame] | 151 | poll_channel(kvp_transaction.kvp_context); |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 152 | } |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 157 | /* |
| 158 | * Callback when data is received from user mode. |
| 159 | */ |
| 160 | |
| 161 | static void |
| 162 | kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp) |
| 163 | { |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 164 | struct hv_kvp_msg *message; |
| 165 | struct hv_kvp_msg_enumerate *data; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 166 | int error = 0; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 167 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 168 | message = (struct hv_kvp_msg *)msg->data; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * If we are negotiating the version information |
| 172 | * with the daemon; handle that first. |
| 173 | */ |
| 174 | |
| 175 | if (in_hand_shake) { |
| 176 | if (kvp_handle_handshake(message)) |
| 177 | in_hand_shake = false; |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Based on the version of the daemon, we propagate errors from the |
| 183 | * daemon differently. |
| 184 | */ |
| 185 | |
| 186 | data = &message->body.kvp_enum_data; |
| 187 | |
| 188 | switch (dm_reg_value) { |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 189 | case KVP_OP_REGISTER: |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 190 | /* |
| 191 | * Null string is used to pass back error condition. |
| 192 | */ |
| 193 | if (data->data.key[0] == 0) |
| 194 | error = HV_S_CONT; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 195 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 196 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 197 | case KVP_OP_REGISTER1: |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 198 | /* |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 199 | * We use the message header information from |
| 200 | * the user level daemon to transmit errors. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 201 | */ |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 202 | error = message->error; |
| 203 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 204 | } |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * Complete the transaction by forwarding the key value |
| 208 | * to the host. But first, cancel the timeout. |
| 209 | */ |
| 210 | if (cancel_delayed_work_sync(&kvp_work)) |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 211 | kvp_respond_to_host(message, error); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 212 | } |
| 213 | |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 214 | |
| 215 | static int process_ob_ipinfo(void *in_msg, void *out_msg, int op) |
| 216 | { |
| 217 | struct hv_kvp_msg *in = in_msg; |
| 218 | struct hv_kvp_ip_msg *out = out_msg; |
| 219 | int len; |
| 220 | |
| 221 | switch (op) { |
| 222 | case KVP_OP_GET_IP_INFO: |
| 223 | /* |
| 224 | * Transform all parameters into utf16 encoding. |
| 225 | */ |
| 226 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr, |
| 227 | strlen((char *)in->body.kvp_ip_val.ip_addr), |
| 228 | UTF16_HOST_ENDIAN, |
| 229 | (wchar_t *)out->kvp_ip_val.ip_addr, |
| 230 | MAX_IP_ADDR_SIZE); |
| 231 | if (len < 0) |
| 232 | return len; |
| 233 | |
| 234 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net, |
| 235 | strlen((char *)in->body.kvp_ip_val.sub_net), |
| 236 | UTF16_HOST_ENDIAN, |
| 237 | (wchar_t *)out->kvp_ip_val.sub_net, |
| 238 | MAX_IP_ADDR_SIZE); |
| 239 | if (len < 0) |
| 240 | return len; |
| 241 | |
| 242 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way, |
| 243 | strlen((char *)in->body.kvp_ip_val.gate_way), |
| 244 | UTF16_HOST_ENDIAN, |
| 245 | (wchar_t *)out->kvp_ip_val.gate_way, |
| 246 | MAX_GATEWAY_SIZE); |
| 247 | if (len < 0) |
| 248 | return len; |
| 249 | |
| 250 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr, |
| 251 | strlen((char *)in->body.kvp_ip_val.dns_addr), |
| 252 | UTF16_HOST_ENDIAN, |
| 253 | (wchar_t *)out->kvp_ip_val.dns_addr, |
| 254 | MAX_IP_ADDR_SIZE); |
| 255 | if (len < 0) |
| 256 | return len; |
| 257 | |
| 258 | len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id, |
| 259 | strlen((char *)in->body.kvp_ip_val.adapter_id), |
| 260 | UTF16_HOST_ENDIAN, |
| 261 | (wchar_t *)out->kvp_ip_val.adapter_id, |
| 262 | MAX_IP_ADDR_SIZE); |
| 263 | if (len < 0) |
| 264 | return len; |
| 265 | |
| 266 | out->kvp_ip_val.dhcp_enabled = |
| 267 | in->body.kvp_ip_val.dhcp_enabled; |
K. Y. Srinivasan | a500e0e | 2012-09-04 17:54:13 -0700 | [diff] [blame] | 268 | out->kvp_ip_val.addr_family = |
| 269 | in->body.kvp_ip_val.addr_family; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static void process_ib_ipinfo(void *in_msg, void *out_msg, int op) |
| 276 | { |
| 277 | struct hv_kvp_ip_msg *in = in_msg; |
| 278 | struct hv_kvp_msg *out = out_msg; |
| 279 | |
| 280 | switch (op) { |
| 281 | case KVP_OP_SET_IP_INFO: |
| 282 | /* |
| 283 | * Transform all parameters into utf8 encoding. |
| 284 | */ |
| 285 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr, |
| 286 | MAX_IP_ADDR_SIZE, |
| 287 | UTF16_LITTLE_ENDIAN, |
| 288 | (__u8 *)out->body.kvp_ip_val.ip_addr, |
| 289 | MAX_IP_ADDR_SIZE); |
| 290 | |
| 291 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net, |
| 292 | MAX_IP_ADDR_SIZE, |
| 293 | UTF16_LITTLE_ENDIAN, |
| 294 | (__u8 *)out->body.kvp_ip_val.sub_net, |
| 295 | MAX_IP_ADDR_SIZE); |
| 296 | |
| 297 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way, |
| 298 | MAX_GATEWAY_SIZE, |
| 299 | UTF16_LITTLE_ENDIAN, |
| 300 | (__u8 *)out->body.kvp_ip_val.gate_way, |
| 301 | MAX_GATEWAY_SIZE); |
| 302 | |
| 303 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr, |
| 304 | MAX_IP_ADDR_SIZE, |
| 305 | UTF16_LITTLE_ENDIAN, |
| 306 | (__u8 *)out->body.kvp_ip_val.dns_addr, |
| 307 | MAX_IP_ADDR_SIZE); |
| 308 | |
| 309 | out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled; |
| 310 | |
| 311 | default: |
| 312 | utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id, |
| 313 | MAX_ADAPTER_ID_SIZE, |
| 314 | UTF16_LITTLE_ENDIAN, |
| 315 | (__u8 *)out->body.kvp_ip_val.adapter_id, |
| 316 | MAX_ADAPTER_ID_SIZE); |
| 317 | |
| 318 | out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 325 | static void |
| 326 | kvp_send_key(struct work_struct *dummy) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 327 | { |
| 328 | struct cn_msg *msg; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 329 | struct hv_kvp_msg *message; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 330 | struct hv_kvp_msg *in_msg; |
| 331 | __u8 operation = kvp_transaction.kvp_msg->kvp_hdr.operation; |
| 332 | __u8 pool = kvp_transaction.kvp_msg->kvp_hdr.pool; |
| 333 | __u32 val32; |
| 334 | __u64 val64; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 335 | |
| 336 | msg = kzalloc(sizeof(*msg) + sizeof(struct hv_kvp_msg) , GFP_ATOMIC); |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 337 | if (!msg) |
| 338 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 339 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 340 | msg->id.idx = CN_KVP_IDX; |
| 341 | msg->id.val = CN_KVP_VAL; |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 342 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 343 | message = (struct hv_kvp_msg *)msg->data; |
| 344 | message->kvp_hdr.operation = operation; |
| 345 | message->kvp_hdr.pool = pool; |
| 346 | in_msg = kvp_transaction.kvp_msg; |
| 347 | |
| 348 | /* |
| 349 | * The key/value strings sent from the host are encoded in |
| 350 | * in utf16; convert it to utf8 strings. |
| 351 | * The host assures us that the utf16 strings will not exceed |
| 352 | * the max lengths specified. We will however, reserve room |
| 353 | * for the string terminating character - in the utf16s_utf8s() |
| 354 | * function we limit the size of the buffer where the converted |
| 355 | * string is placed to HV_KVP_EXCHANGE_MAX_*_SIZE -1 to gaurantee |
| 356 | * that the strings can be properly terminated! |
| 357 | */ |
| 358 | |
| 359 | switch (message->kvp_hdr.operation) { |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 360 | case KVP_OP_SET_IP_INFO: |
| 361 | process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO); |
| 362 | break; |
| 363 | case KVP_OP_GET_IP_INFO: |
| 364 | process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO); |
| 365 | break; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 366 | case KVP_OP_SET: |
| 367 | switch (in_msg->body.kvp_set.data.value_type) { |
| 368 | case REG_SZ: |
| 369 | /* |
| 370 | * The value is a string - utf16 encoding. |
| 371 | */ |
| 372 | message->body.kvp_set.data.value_size = |
| 373 | utf16s_to_utf8s( |
| 374 | (wchar_t *)in_msg->body.kvp_set.data.value, |
| 375 | in_msg->body.kvp_set.data.value_size, |
| 376 | UTF16_LITTLE_ENDIAN, |
| 377 | message->body.kvp_set.data.value, |
| 378 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE - 1) + 1; |
| 379 | break; |
| 380 | |
| 381 | case REG_U32: |
| 382 | /* |
| 383 | * The value is a 32 bit scalar. |
| 384 | * We save this as a utf8 string. |
| 385 | */ |
| 386 | val32 = in_msg->body.kvp_set.data.value_u32; |
| 387 | message->body.kvp_set.data.value_size = |
| 388 | sprintf(message->body.kvp_set.data.value, |
| 389 | "%d", val32) + 1; |
| 390 | break; |
| 391 | |
| 392 | case REG_U64: |
| 393 | /* |
| 394 | * The value is a 64 bit scalar. |
| 395 | * We save this as a utf8 string. |
| 396 | */ |
| 397 | val64 = in_msg->body.kvp_set.data.value_u64; |
| 398 | message->body.kvp_set.data.value_size = |
| 399 | sprintf(message->body.kvp_set.data.value, |
| 400 | "%llu", val64) + 1; |
| 401 | break; |
| 402 | |
| 403 | } |
| 404 | case KVP_OP_GET: |
| 405 | message->body.kvp_set.data.key_size = |
| 406 | utf16s_to_utf8s( |
| 407 | (wchar_t *)in_msg->body.kvp_set.data.key, |
| 408 | in_msg->body.kvp_set.data.key_size, |
| 409 | UTF16_LITTLE_ENDIAN, |
| 410 | message->body.kvp_set.data.key, |
| 411 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 412 | break; |
| 413 | |
| 414 | case KVP_OP_DELETE: |
| 415 | message->body.kvp_delete.key_size = |
| 416 | utf16s_to_utf8s( |
| 417 | (wchar_t *)in_msg->body.kvp_delete.key, |
| 418 | in_msg->body.kvp_delete.key_size, |
| 419 | UTF16_LITTLE_ENDIAN, |
| 420 | message->body.kvp_delete.key, |
| 421 | HV_KVP_EXCHANGE_MAX_KEY_SIZE - 1) + 1; |
| 422 | break; |
| 423 | |
| 424 | case KVP_OP_ENUMERATE: |
| 425 | message->body.kvp_enum_data.index = |
| 426 | in_msg->body.kvp_enum_data.index; |
| 427 | break; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 428 | } |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 429 | |
| 430 | msg->len = sizeof(struct hv_kvp_msg); |
| 431 | cn_netlink_send(msg, 0, GFP_ATOMIC); |
| 432 | kfree(msg); |
| 433 | |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 434 | return; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Send a response back to the host. |
| 439 | */ |
| 440 | |
| 441 | static void |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 442 | kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 443 | { |
| 444 | struct hv_kvp_msg *kvp_msg; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 445 | struct hv_kvp_exchg_msg_value *kvp_data; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 446 | char *key_name; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 447 | char *value; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 448 | struct icmsg_hdr *icmsghdrp; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 449 | int keylen = 0; |
| 450 | int valuelen = 0; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 451 | u32 buf_len; |
| 452 | struct vmbus_channel *channel; |
| 453 | u64 req_id; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 454 | int ret; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * If a transaction is not active; log and return. |
| 458 | */ |
| 459 | |
| 460 | if (!kvp_transaction.active) { |
| 461 | /* |
| 462 | * This is a spurious call! |
| 463 | */ |
Hank Janssen | af7a5b6 | 2011-03-29 13:58:49 -0700 | [diff] [blame] | 464 | pr_warn("KVP: Transaction not active\n"); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | /* |
| 468 | * Copy the global state for completing the transaction. Note that |
| 469 | * only one transaction can be active at a time. |
| 470 | */ |
| 471 | |
| 472 | buf_len = kvp_transaction.recv_len; |
| 473 | channel = kvp_transaction.recv_channel; |
| 474 | req_id = kvp_transaction.recv_req_id; |
| 475 | |
K. Y. Srinivasan | 76e5f81 | 2011-10-04 14:00:02 -0700 | [diff] [blame] | 476 | kvp_transaction.active = false; |
| 477 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 478 | icmsghdrp = (struct icmsg_hdr *) |
| 479 | &recv_buffer[sizeof(struct vmbuspipe_hdr)]; |
| 480 | |
K. Y. Srinivasan | 4e65f6e | 2011-09-18 10:31:34 -0700 | [diff] [blame] | 481 | if (channel->onchannel_callback == NULL) |
| 482 | /* |
| 483 | * We have raced with util driver being unloaded; |
| 484 | * silently return. |
| 485 | */ |
| 486 | return; |
| 487 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 488 | icmsghdrp->status = error; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 491 | * If the error parameter is set, terminate the host's enumeration |
| 492 | * on this pool. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 493 | */ |
| 494 | if (error) { |
| 495 | /* |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 496 | * Something failed or we have timedout; |
| 497 | * terminate the current host-side iteration. |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 498 | */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 499 | goto response_done; |
| 500 | } |
| 501 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 502 | kvp_msg = (struct hv_kvp_msg *) |
| 503 | &recv_buffer[sizeof(struct vmbuspipe_hdr) + |
| 504 | sizeof(struct icmsg_hdr)]; |
| 505 | |
| 506 | switch (kvp_transaction.kvp_msg->kvp_hdr.operation) { |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 507 | case KVP_OP_GET_IP_INFO: |
| 508 | ret = process_ob_ipinfo(msg_to_host, |
| 509 | (struct hv_kvp_ip_msg *)kvp_msg, |
| 510 | KVP_OP_GET_IP_INFO); |
| 511 | if (ret < 0) |
| 512 | icmsghdrp->status = HV_E_FAIL; |
| 513 | |
| 514 | goto response_done; |
| 515 | case KVP_OP_SET_IP_INFO: |
| 516 | goto response_done; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 517 | case KVP_OP_GET: |
| 518 | kvp_data = &kvp_msg->body.kvp_get.data; |
| 519 | goto copy_value; |
| 520 | |
| 521 | case KVP_OP_SET: |
| 522 | case KVP_OP_DELETE: |
| 523 | goto response_done; |
| 524 | |
| 525 | default: |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | kvp_data = &kvp_msg->body.kvp_enum_data.data; |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 530 | key_name = msg_to_host->body.kvp_enum_data.data.key; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 531 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 532 | /* |
| 533 | * The windows host expects the key/value pair to be encoded |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 534 | * in utf16. Ensure that the key/value size reported to the host |
| 535 | * will be less than or equal to the MAX size (including the |
| 536 | * terminating character). |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 537 | */ |
Alan Stern | 0720a06 | 2011-11-17 16:42:19 -0500 | [diff] [blame] | 538 | keylen = utf8s_to_utf16s(key_name, strlen(key_name), UTF16_HOST_ENDIAN, |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 539 | (wchar_t *) kvp_data->key, |
| 540 | (HV_KVP_EXCHANGE_MAX_KEY_SIZE / 2) - 2); |
| 541 | kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 542 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 543 | copy_value: |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 544 | value = msg_to_host->body.kvp_enum_data.data.value; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 545 | valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN, |
| 546 | (wchar_t *) kvp_data->value, |
| 547 | (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2); |
| 548 | kvp_data->value_size = 2*(valuelen + 1); /* utf16 encoding */ |
| 549 | |
| 550 | /* |
| 551 | * If the utf8s to utf16s conversion failed; notify host |
| 552 | * of the error. |
| 553 | */ |
| 554 | if ((keylen < 0) || (valuelen < 0)) |
| 555 | icmsghdrp->status = HV_E_FAIL; |
| 556 | |
| 557 | kvp_data->value_type = REG_SZ; /* all our values are strings */ |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 558 | |
| 559 | response_done: |
| 560 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE; |
| 561 | |
| 562 | vmbus_sendpacket(channel, recv_buffer, buf_len, req_id, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 563 | VM_PKT_DATA_INBAND, 0); |
K. Y. Srinivasan | 9ac5d53 | 2014-07-07 16:34:25 -0700 | [diff] [blame] | 564 | poll_channel(channel); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 565 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
| 569 | * This callback is invoked when we get a KVP message from the host. |
| 570 | * The host ensures that only one KVP transaction can be active at a time. |
| 571 | * KVP implementation in Linux needs to forward the key to a user-mde |
| 572 | * component to retrive the corresponding value. Consequently, we cannot |
| 573 | * respond to the host in the conext of this callback. Since the host |
| 574 | * guarantees that at most only one transaction can be active at a time, |
| 575 | * we stash away the transaction state in a set of global variables. |
| 576 | */ |
| 577 | |
| 578 | void hv_kvp_onchannelcallback(void *context) |
| 579 | { |
| 580 | struct vmbus_channel *channel = context; |
| 581 | u32 recvlen; |
| 582 | u64 requestid; |
| 583 | |
| 584 | struct hv_kvp_msg *kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 585 | |
| 586 | struct icmsg_hdr *icmsghdrp; |
| 587 | struct icmsg_negotiate *negop = NULL; |
| 588 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 589 | if (kvp_transaction.active) { |
| 590 | /* |
| 591 | * We will defer processing this callback once |
| 592 | * the current transaction is complete. |
| 593 | */ |
| 594 | kvp_transaction.kvp_context = context; |
| 595 | return; |
| 596 | } |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 597 | |
K. Y. Srinivasan | 9ac5d53 | 2014-07-07 16:34:25 -0700 | [diff] [blame] | 598 | vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 4, &recvlen, |
K. Y. Srinivasan | 03db772 | 2012-08-16 18:32:12 -0700 | [diff] [blame] | 599 | &requestid); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 600 | |
| 601 | if (recvlen > 0) { |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 602 | icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ |
| 603 | sizeof(struct vmbuspipe_hdr)]; |
| 604 | |
| 605 | if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) { |
K. Y. Srinivasan | c836d0a | 2012-05-12 13:44:58 -0700 | [diff] [blame] | 606 | vmbus_prep_negotiate_resp(icmsghdrp, negop, |
| 607 | recv_buffer, MAX_SRV_VER, MAX_SRV_VER); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 608 | } else { |
| 609 | kvp_msg = (struct hv_kvp_msg *)&recv_buffer[ |
| 610 | sizeof(struct vmbuspipe_hdr) + |
| 611 | sizeof(struct icmsg_hdr)]; |
| 612 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 613 | /* |
| 614 | * Stash away this global state for completing the |
| 615 | * transaction; note transactions are serialized. |
| 616 | */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 617 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 618 | kvp_transaction.recv_len = recvlen; |
| 619 | kvp_transaction.recv_channel = channel; |
| 620 | kvp_transaction.recv_req_id = requestid; |
| 621 | kvp_transaction.active = true; |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 622 | kvp_transaction.kvp_msg = kvp_msg; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * Get the information from the |
| 626 | * user-mode component. |
| 627 | * component. This transaction will be |
| 628 | * completed when we get the value from |
| 629 | * the user-mode component. |
| 630 | * Set a timeout to deal with |
| 631 | * user-mode not responding. |
| 632 | */ |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 633 | schedule_work(&kvp_sendkey_work); |
| 634 | schedule_delayed_work(&kvp_work, 5*HZ); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 635 | |
| 636 | return; |
| 637 | |
| 638 | } |
| 639 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 640 | icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION |
| 641 | | ICMSGHDRFLAG_RESPONSE; |
| 642 | |
| 643 | vmbus_sendpacket(channel, recv_buffer, |
| 644 | recvlen, requestid, |
Haiyang Zhang | 415f228 | 2011-01-26 12:12:13 -0800 | [diff] [blame] | 645 | VM_PKT_DATA_INBAND, 0); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | } |
| 649 | |
| 650 | int |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 651 | hv_kvp_init(struct hv_util_service *srv) |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 652 | { |
| 653 | int err; |
| 654 | |
| 655 | err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback); |
| 656 | if (err) |
| 657 | return err; |
K. Y. Srinivasan | a29b643 | 2011-09-18 10:31:33 -0700 | [diff] [blame] | 658 | recv_buffer = srv->recv_buffer; |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 659 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 660 | /* |
| 661 | * When this driver loads, the user level daemon that |
| 662 | * processes the host requests may not yet be running. |
| 663 | * Defer processing channel callbacks until the daemon |
| 664 | * has registered. |
| 665 | */ |
| 666 | kvp_transaction.active = true; |
| 667 | |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | void hv_kvp_deinit(void) |
| 672 | { |
| 673 | cn_del_callback(&kvp_id); |
| 674 | cancel_delayed_work_sync(&kvp_work); |
K. Y. Srinivasan | e2bb653 | 2011-10-09 19:42:28 -0700 | [diff] [blame] | 675 | cancel_work_sync(&kvp_sendkey_work); |
Ky Srinivasan | 245ba56 | 2010-12-16 18:54:16 -0700 | [diff] [blame] | 676 | } |