act_bpf: properly support late binding of bpf action to a classifier
[linux/fpc-iii.git] / drivers / hv / hv_utils_transport.c
blobea7ba5ef16a90c2f834378c7675193033309671c
1 /*
2 * Kernel/userspace transport abstraction for Hyper-V util driver.
4 * Copyright (C) 2015, Vitaly Kuznetsov <vkuznets@redhat.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
18 #include <linux/slab.h>
19 #include <linux/fs.h>
20 #include <linux/poll.h>
22 #include "hyperv_vmbus.h"
23 #include "hv_utils_transport.h"
25 static DEFINE_SPINLOCK(hvt_list_lock);
26 static struct list_head hvt_list = LIST_HEAD_INIT(hvt_list);
28 static void hvt_reset(struct hvutil_transport *hvt)
30 mutex_lock(&hvt->outmsg_lock);
31 kfree(hvt->outmsg);
32 hvt->outmsg = NULL;
33 hvt->outmsg_len = 0;
34 mutex_unlock(&hvt->outmsg_lock);
35 if (hvt->on_reset)
36 hvt->on_reset();
39 static ssize_t hvt_op_read(struct file *file, char __user *buf,
40 size_t count, loff_t *ppos)
42 struct hvutil_transport *hvt;
43 int ret;
45 hvt = container_of(file->f_op, struct hvutil_transport, fops);
47 if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0))
48 return -EINTR;
50 mutex_lock(&hvt->outmsg_lock);
51 if (!hvt->outmsg) {
52 ret = -EAGAIN;
53 goto out_unlock;
56 if (count < hvt->outmsg_len) {
57 ret = -EINVAL;
58 goto out_unlock;
61 if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
62 ret = hvt->outmsg_len;
63 else
64 ret = -EFAULT;
66 kfree(hvt->outmsg);
67 hvt->outmsg = NULL;
68 hvt->outmsg_len = 0;
70 out_unlock:
71 mutex_unlock(&hvt->outmsg_lock);
72 return ret;
75 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
76 size_t count, loff_t *ppos)
78 struct hvutil_transport *hvt;
79 u8 *inmsg;
81 hvt = container_of(file->f_op, struct hvutil_transport, fops);
83 inmsg = kzalloc(count, GFP_KERNEL);
84 if (copy_from_user(inmsg, buf, count)) {
85 kfree(inmsg);
86 return -EFAULT;
88 if (hvt->on_msg(inmsg, count))
89 return -EFAULT;
90 kfree(inmsg);
92 return count;
95 static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
97 struct hvutil_transport *hvt;
99 hvt = container_of(file->f_op, struct hvutil_transport, fops);
101 poll_wait(file, &hvt->outmsg_q, wait);
102 if (hvt->outmsg_len > 0)
103 return POLLIN | POLLRDNORM;
105 return 0;
108 static int hvt_op_open(struct inode *inode, struct file *file)
110 struct hvutil_transport *hvt;
112 hvt = container_of(file->f_op, struct hvutil_transport, fops);
115 * Switching to CHARDEV mode. We switch bach to INIT when device
116 * gets released.
118 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
119 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
120 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
122 * We're switching from netlink communication to using char
123 * device. Issue the reset first.
125 hvt_reset(hvt);
126 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
127 } else
128 return -EBUSY;
130 return 0;
133 static int hvt_op_release(struct inode *inode, struct file *file)
135 struct hvutil_transport *hvt;
137 hvt = container_of(file->f_op, struct hvutil_transport, fops);
139 hvt->mode = HVUTIL_TRANSPORT_INIT;
141 * Cleanup message buffers to avoid spurious messages when the daemon
142 * connects back.
144 hvt_reset(hvt);
146 return 0;
149 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
151 struct hvutil_transport *hvt, *hvt_found = NULL;
153 spin_lock(&hvt_list_lock);
154 list_for_each_entry(hvt, &hvt_list, list) {
155 if (hvt->cn_id.idx == msg->id.idx &&
156 hvt->cn_id.val == msg->id.val) {
157 hvt_found = hvt;
158 break;
161 spin_unlock(&hvt_list_lock);
162 if (!hvt_found) {
163 pr_warn("hvt_cn_callback: spurious message received!\n");
164 return;
168 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
169 * opens the device.
171 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
172 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
174 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
175 hvt_found->on_msg(msg->data, msg->len);
176 else
177 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
180 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
182 struct cn_msg *cn_msg;
183 int ret = 0;
185 if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
186 return -EINVAL;
187 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
188 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
189 if (!msg)
190 return -ENOMEM;
191 cn_msg->id.idx = hvt->cn_id.idx;
192 cn_msg->id.val = hvt->cn_id.val;
193 cn_msg->len = len;
194 memcpy(cn_msg->data, msg, len);
195 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
196 kfree(cn_msg);
197 return ret;
199 /* HVUTIL_TRANSPORT_CHARDEV */
200 mutex_lock(&hvt->outmsg_lock);
201 if (hvt->outmsg) {
202 /* Previous message wasn't received */
203 ret = -EFAULT;
204 goto out_unlock;
206 hvt->outmsg = kzalloc(len, GFP_KERNEL);
207 memcpy(hvt->outmsg, msg, len);
208 hvt->outmsg_len = len;
209 wake_up_interruptible(&hvt->outmsg_q);
210 out_unlock:
211 mutex_unlock(&hvt->outmsg_lock);
212 return ret;
215 struct hvutil_transport *hvutil_transport_init(const char *name,
216 u32 cn_idx, u32 cn_val,
217 int (*on_msg)(void *, int),
218 void (*on_reset)(void))
220 struct hvutil_transport *hvt;
222 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
223 if (!hvt)
224 return NULL;
226 hvt->cn_id.idx = cn_idx;
227 hvt->cn_id.val = cn_val;
229 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
230 hvt->mdev.name = name;
232 hvt->fops.owner = THIS_MODULE;
233 hvt->fops.read = hvt_op_read;
234 hvt->fops.write = hvt_op_write;
235 hvt->fops.poll = hvt_op_poll;
236 hvt->fops.open = hvt_op_open;
237 hvt->fops.release = hvt_op_release;
239 hvt->mdev.fops = &hvt->fops;
241 init_waitqueue_head(&hvt->outmsg_q);
242 mutex_init(&hvt->outmsg_lock);
244 spin_lock(&hvt_list_lock);
245 list_add(&hvt->list, &hvt_list);
246 spin_unlock(&hvt_list_lock);
248 hvt->on_msg = on_msg;
249 hvt->on_reset = on_reset;
251 if (misc_register(&hvt->mdev))
252 goto err_free_hvt;
254 /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
255 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
256 cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
257 goto err_free_hvt;
259 return hvt;
261 err_free_hvt:
262 kfree(hvt);
263 return NULL;
266 void hvutil_transport_destroy(struct hvutil_transport *hvt)
268 spin_lock(&hvt_list_lock);
269 list_del(&hvt->list);
270 spin_unlock(&hvt_list_lock);
271 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
272 cn_del_callback(&hvt->cn_id);
273 misc_deregister(&hvt->mdev);
274 kfree(hvt->outmsg);
275 kfree(hvt);