1 Description: eal: provide option to set vhost_user socket owner/permissions
3 The API doesn't hold a way to specify a owner/permission set for vhost_user
6 Projects consuming DPDK started to do 'their own workarounds' like openvswitch
7 https://patchwork.ozlabs.org/patch/559043/
8 https://patchwork.ozlabs.org/patch/559045/
9 But for this specific example they are blocked/stalled behind a bigger
10 rework (https://patchwork.ozlabs.org/patch/604898/).
12 We need something now for existing code linking against DPDK. That implies to
13 avoid changing API/ABI. So I created a DPDK EAL commandline option based ideas
14 in the former patches.
19 - with the split libs it now nees to be listed in
20 lib/librte_eal/linuxapp/eal/rte_eal_version.map to work on link steps
21 - please note that upstream gravitates towards not extending but creating a
22 new the API in DPDK as long term solution (will take a while)
23 - also as listed before most affected projects seem to create their own
25 So over time we have to check when we can drop it at the price of a config
26 transition - likely OVS 2.6 won't need it anymore.
29 Author: Christian Ehrhardt <christian.ehrhardt@canonical.com>
30 Last-Update: 2016-05-18
32 Index: dpdk/lib/librte_eal/common/eal_common_options.c
33 ===================================================================
34 --- dpdk.orig/lib/librte_eal/common/eal_common_options.c
35 +++ dpdk/lib/librte_eal/common/eal_common_options.c
36 @@ -95,6 +95,8 @@ eal_long_options[] = {
37 {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM },
38 {OPT_VMWARE_TSC_MAP, 0, NULL, OPT_VMWARE_TSC_MAP_NUM },
39 {OPT_XEN_DOM0, 0, NULL, OPT_XEN_DOM0_NUM },
40 + {OPT_VHOST_OWNER, 1, NULL, OPT_VHOST_OWNER_NUM },
41 + {OPT_VHOST_PERM, 1, NULL, OPT_VHOST_PERM_NUM },
45 @@ -153,6 +155,8 @@ eal_reset_internal_config(struct interna
47 internal_cfg->vmware_tsc_map = 0;
48 internal_cfg->create_uio_dev = 0;
49 + internal_cfg->vhost_sock_owner = NULL;
50 + internal_cfg->vhost_sock_perm = NULL;
54 Index: dpdk/lib/librte_eal/common/eal_internal_cfg.h
55 ===================================================================
56 --- dpdk.orig/lib/librte_eal/common/eal_internal_cfg.h
57 +++ dpdk/lib/librte_eal/common/eal_internal_cfg.h
58 @@ -83,6 +83,8 @@ struct internal_config {
59 volatile enum rte_intr_mode vfio_intr_mode;
60 const char *hugefile_prefix; /**< the base filename of hugetlbfs files */
61 const char *hugepage_dir; /**< specific hugetlbfs directory to use */
62 + const char *vhost_sock_owner; /**< owner:group of vhost_user sockets */
63 + const char *vhost_sock_perm; /**< permissions of vhost_user sockets */
65 unsigned num_hugepage_sizes; /**< how many sizes on this system */
66 struct hugepage_info hugepage_info[MAX_HUGEPAGE_SIZES];
67 Index: dpdk/lib/librte_eal/common/eal_options.h
68 ===================================================================
69 --- dpdk.orig/lib/librte_eal/common/eal_options.h
70 +++ dpdk/lib/librte_eal/common/eal_options.h
71 @@ -83,6 +83,10 @@ enum {
72 OPT_VMWARE_TSC_MAP_NUM,
73 #define OPT_XEN_DOM0 "xen-dom0"
75 +#define OPT_VHOST_OWNER "vhost-owner"
76 + OPT_VHOST_OWNER_NUM,
77 +#define OPT_VHOST_PERM "vhost-perm"
82 Index: dpdk/lib/librte_vhost/vhost_user/vhost-net-user.c
83 ===================================================================
84 --- dpdk.orig/lib/librte_vhost/vhost_user/vhost-net-user.c
85 +++ dpdk/lib/librte_vhost/vhost_user/vhost-net-user.c
87 #include "vhost-net.h"
88 #include "virtio-net-user.h"
92 #define MAX_VIRTIO_BACKLOG 128
94 static void vserver_new_vq_conn(int fd, void *data, int *remove);
95 @@ -486,6 +488,8 @@ rte_vhost_driver_register(const char *pa
99 + rte_eal_set_socket_permissions(path);
101 vserver->path = strdup(path);
104 Index: dpdk/lib/librte_eal/linuxapp/eal/eal.c
105 ===================================================================
106 --- dpdk.orig/lib/librte_eal/linuxapp/eal/eal.c
107 +++ dpdk/lib/librte_eal/linuxapp/eal/eal.c
109 #if defined(RTE_ARCH_X86)
112 +#include <sys/types.h>
116 #include <rte_common.h>
117 #include <rte_debug.h>
118 @@ -343,6 +346,8 @@ eal_usage(const char *prgname)
119 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n"
120 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n"
121 " --"OPT_XEN_DOM0" Support running on Xen dom0 without hugetlbfs\n"
122 + " --"OPT_VHOST_OWNER" Create vhost-user sockets with this owner:group\n"
123 + " --"OPT_VHOST_PERM" Create vhost-user sockets with these permissions\n"
125 /* Allow the application to print its usage message too if hook is set */
126 if ( rte_application_usage_hook ) {
127 @@ -618,6 +623,14 @@ eal_parse_args(int argc, char **argv)
128 internal_config.create_uio_dev = 1;
131 + case OPT_VHOST_OWNER_NUM:
132 + internal_config.vhost_sock_owner = optarg;
135 + case OPT_VHOST_PERM_NUM:
136 + internal_config.vhost_sock_perm = optarg;
140 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
141 RTE_LOG(ERR, EAL, "Option %c is not supported "
142 @@ -934,3 +947,172 @@ rte_eal_check_module(const char *module_
143 /* Module has been found */
147 +/* Try to double the size of '*buf', return true
148 + * if successful, and '*sizep' will be updated with
149 + * the new size. Otherwise, return false. */
151 +enlarge_buffer(char **buf, size_t *sizep)
153 + size_t newsize = *sizep * 2;
155 + if (newsize > *sizep) {
156 + *buf = realloc(*buf, newsize);
165 +get_owners_from_str(const char *user_spec, uid_t *uid, gid_t *gid)
167 + size_t bufsize = 4096;
169 + char *pos = strchr(user_spec, ':');
170 + user_spec += strspn(user_spec, " \t\r\n");
171 + size_t len = pos ? (size_t)(pos - user_spec) : strlen(user_spec);
174 + struct passwd pwd, *res;
177 + buf = malloc(bufsize);
178 + char *user_search = NULL;
180 + user_search = malloc(len + 1);
181 + memcpy(user_search, user_spec, len);
182 + user_search[len] = '\0';
183 + while ((e = getpwnam_r(user_search, &pwd, buf, bufsize, &res)) == ERANGE) {
184 + if (!enlarge_buffer(&buf, &bufsize)) {
190 + RTE_LOG(ERR, EAL,"Failed to retrive user %s's uid (%s), aborting.",
191 + user_search, strerror(e));
195 + RTE_LOG(ERR, EAL,"user %s not found, aborting.",
201 + /* User name is not specified, use current user. */
202 + while ((e = getpwuid_r(getuid(), &pwd, buf, bufsize, &res)) == ERANGE) {
203 + if (!enlarge_buffer(&buf, &bufsize)) {
209 + RTE_LOG(ERR, EAL,"Failed to retrive current user's uid "
210 + "(%s), aborting.", strerror(e));
213 + user_search = strdup(pwd.pw_name);
223 + char *grpstr = pos + 1;
224 + grpstr += strspn(grpstr, " \t\r\n");
227 + struct group grp, *res;
230 + buf = malloc(bufsize);
231 + while ((e = getgrnam_r(grpstr, &grp, buf, bufsize, &res))
233 + if (!enlarge_buffer(&buf, &bufsize)) {
239 + RTE_LOG(ERR, EAL,"Failed to get group entry for %s, "
240 + "(%s), aborting.", grpstr,
245 + RTE_LOG(ERR, EAL,"Group %s not found, aborting.",
263 +vhost_set_permissions(const char *vhost_sock_location)
265 + unsigned long int mode = strtoul(internal_config.vhost_sock_perm, NULL, 0);
266 + int err = chmod(vhost_sock_location, (mode_t)mode);
268 + RTE_LOG(ERR, EAL,"vhost-user socket cannot set"
269 + " permissions to %s (%s).\n",
270 + internal_config.vhost_sock_perm, strerror(err));
273 + RTE_LOG(INFO, EAL,"Socket %s changed permissions"
274 + " to %s\n", vhost_sock_location,
275 + internal_config.vhost_sock_perm);
279 +vhost_set_ownership(const char *vhost_sock_location)
284 + if (get_owners_from_str(internal_config.vhost_sock_owner, &vhuid, &vhgid)) {
285 + RTE_LOG(ERR, EAL,"vhost-user socket unable to get"
286 + " specified user/group: %s\n",
287 + internal_config.vhost_sock_owner);
291 + int err = chown(vhost_sock_location, vhuid, vhgid);
293 + RTE_LOG(ERR, EAL,"vhost-user socket unable to set"
294 + " ownership to %s (%s).\n",
295 + internal_config.vhost_sock_owner, strerror(err));
299 + RTE_LOG(INFO, EAL,"Socket %s changed ownership"
300 + " to %s.\n", vhost_sock_location,
301 + internal_config.vhost_sock_owner);
305 +rte_eal_set_socket_permissions(const char *path)
307 + if (internal_config.vhost_sock_perm) {
308 + vhost_set_permissions(path);
311 + if (internal_config.vhost_sock_owner) {
312 + vhost_set_ownership(path);
315 Index: dpdk/lib/librte_eal/common/include/rte_eal.h
316 ===================================================================
317 --- dpdk.orig/lib/librte_eal/common/include/rte_eal.h
318 +++ dpdk/lib/librte_eal/common/include/rte_eal.h
319 @@ -252,6 +252,11 @@ static inline int rte_gettid(void)
320 return RTE_PER_LCORE(_thread_id);
324 + * Set owner/permissions on sockets if requested on EAL commandline
326 +void rte_eal_set_socket_permissions(const char *);
331 Index: dpdk/doc/guides/testpmd_app_ug/run_app.rst
332 ===================================================================
333 --- dpdk.orig/doc/guides/testpmd_app_ug/run_app.rst
334 +++ dpdk/doc/guides/testpmd_app_ug/run_app.rst
335 @@ -156,6 +156,25 @@ See the DPDK Getting Started Guides for
337 Use malloc instead of hugetlbfs.
341 + When creating vhost_user sockets change owner and group to the specified value.
342 + This can be given as ``user:group``, but also only ``user`` or ``:group`` are supported.
346 + --vhost-owner 'libvirt-qemu:kvm'
347 + --vhost-owner 'libvirt-qemu'
348 + --vhost-owner ':kvm'
352 + When creating vhost_user sockets set them up with these permissions.
356 + --vhost-perm '0664'
359 Testpmd Command-line Options
360 ----------------------------
361 Index: dpdk/lib/librte_eal/linuxapp/eal/rte_eal_version.map
362 ===================================================================
363 --- dpdk.orig/lib/librte_eal/linuxapp/eal/rte_eal_version.map
364 +++ dpdk/lib/librte_eal/linuxapp/eal/rte_eal_version.map
365 @@ -138,6 +138,7 @@ DPDK_2.2 {
366 rte_keepalive_mark_alive;
367 rte_keepalive_register_core;
368 rte_xen_dom0_supported;
369 + rte_eal_set_socket_permissions;