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: deb_dpdk/lib/librte_eal/common/eal_common_options.c
33 ===================================================================
34 --- deb_dpdk.orig/lib/librte_eal/common/eal_common_options.c
35 +++ deb_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 @@ -157,6 +159,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: deb_dpdk/lib/librte_eal/common/eal_internal_cfg.h
55 ===================================================================
56 --- deb_dpdk.orig/lib/librte_eal/common/eal_internal_cfg.h
57 +++ deb_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: deb_dpdk/lib/librte_eal/common/eal_options.h
68 ===================================================================
69 --- deb_dpdk.orig/lib/librte_eal/common/eal_options.h
70 +++ deb_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: deb_dpdk/lib/librte_vhost/vhost_user/vhost-net-user.c
83 ===================================================================
84 --- deb_dpdk.orig/lib/librte_vhost/vhost_user/vhost-net-user.c
85 +++ deb_dpdk/lib/librte_vhost/vhost_user/vhost-net-user.c
86 @@ -77,6 +77,8 @@ struct vhost_user {
87 pthread_mutex_t mutex;
92 #define MAX_VIRTIO_BACKLOG 128
94 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
95 @@ -629,6 +631,7 @@ rte_vhost_driver_register(const char *pa
98 memset(vsocket, 0, sizeof(struct vhost_user_socket));
99 + rte_eal_set_socket_permissions(path);
100 vsocket->path = strdup(path);
102 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
103 Index: deb_dpdk/lib/librte_eal/linuxapp/eal/eal.c
104 ===================================================================
105 --- deb_dpdk.orig/lib/librte_eal/linuxapp/eal/eal.c
106 +++ deb_dpdk/lib/librte_eal/linuxapp/eal/eal.c
108 #if defined(RTE_ARCH_X86)
111 +#include <sys/types.h>
115 #include <rte_common.h>
116 #include <rte_debug.h>
117 @@ -343,6 +346,8 @@ eal_usage(const char *prgname)
118 " --"OPT_CREATE_UIO_DEV" Create /dev/uioX (usually done by hotplug)\n"
119 " --"OPT_VFIO_INTR" Interrupt mode for VFIO (legacy|msi|msix)\n"
120 " --"OPT_XEN_DOM0" Support running on Xen dom0 without hugetlbfs\n"
121 + " --"OPT_VHOST_OWNER" Create vhost-user sockets with this owner:group\n"
122 + " --"OPT_VHOST_PERM" Create vhost-user sockets with these permissions\n"
124 /* Allow the application to print its usage message too if hook is set */
125 if ( rte_application_usage_hook ) {
126 @@ -600,6 +605,14 @@ eal_parse_args(int argc, char **argv)
127 internal_config.create_uio_dev = 1;
130 + case OPT_VHOST_OWNER_NUM:
131 + internal_config.vhost_sock_owner = optarg;
134 + case OPT_VHOST_PERM_NUM:
135 + internal_config.vhost_sock_perm = optarg;
139 if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
140 RTE_LOG(ERR, EAL, "Option %c is not supported "
141 @@ -910,3 +923,172 @@ rte_eal_check_module(const char *module_
142 /* Module has been found */
146 +/* Try to double the size of '*buf', return true
147 + * if successful, and '*sizep' will be updated with
148 + * the new size. Otherwise, return false. */
150 +enlarge_buffer(char **buf, size_t *sizep)
152 + size_t newsize = *sizep * 2;
154 + if (newsize > *sizep) {
155 + *buf = realloc(*buf, newsize);
164 +get_owners_from_str(const char *user_spec, uid_t *uid, gid_t *gid)
166 + size_t bufsize = 4096;
168 + char *pos = strchr(user_spec, ':');
169 + user_spec += strspn(user_spec, " \t\r\n");
170 + size_t len = pos ? (size_t)(pos - user_spec) : strlen(user_spec);
173 + struct passwd pwd, *res;
176 + buf = malloc(bufsize);
177 + char *user_search = NULL;
179 + user_search = malloc(len + 1);
180 + memcpy(user_search, user_spec, len);
181 + user_search[len] = '\0';
182 + while ((e = getpwnam_r(user_search, &pwd, buf, bufsize, &res)) == ERANGE) {
183 + if (!enlarge_buffer(&buf, &bufsize)) {
189 + RTE_LOG(ERR, EAL,"Failed to retrive user %s's uid (%s), aborting.",
190 + user_search, strerror(e));
194 + RTE_LOG(ERR, EAL,"user %s not found, aborting.",
200 + /* User name is not specified, use current user. */
201 + while ((e = getpwuid_r(getuid(), &pwd, buf, bufsize, &res)) == ERANGE) {
202 + if (!enlarge_buffer(&buf, &bufsize)) {
208 + RTE_LOG(ERR, EAL,"Failed to retrive current user's uid "
209 + "(%s), aborting.", strerror(e));
212 + user_search = strdup(pwd.pw_name);
222 + char *grpstr = pos + 1;
223 + grpstr += strspn(grpstr, " \t\r\n");
226 + struct group grp, *res;
229 + buf = malloc(bufsize);
230 + while ((e = getgrnam_r(grpstr, &grp, buf, bufsize, &res))
232 + if (!enlarge_buffer(&buf, &bufsize)) {
238 + RTE_LOG(ERR, EAL,"Failed to get group entry for %s, "
239 + "(%s), aborting.", grpstr,
244 + RTE_LOG(ERR, EAL,"Group %s not found, aborting.",
262 +vhost_set_permissions(const char *vhost_sock_location)
264 + unsigned long int mode = strtoul(internal_config.vhost_sock_perm, NULL, 0);
265 + int err = chmod(vhost_sock_location, (mode_t)mode);
267 + RTE_LOG(ERR, EAL,"vhost-user socket cannot set"
268 + " permissions to %s (%s).\n",
269 + internal_config.vhost_sock_perm, strerror(err));
272 + RTE_LOG(INFO, EAL,"Socket %s changed permissions"
273 + " to %s\n", vhost_sock_location,
274 + internal_config.vhost_sock_perm);
278 +vhost_set_ownership(const char *vhost_sock_location)
283 + if (get_owners_from_str(internal_config.vhost_sock_owner, &vhuid, &vhgid)) {
284 + RTE_LOG(ERR, EAL,"vhost-user socket unable to get"
285 + " specified user/group: %s\n",
286 + internal_config.vhost_sock_owner);
290 + int err = chown(vhost_sock_location, vhuid, vhgid);
292 + RTE_LOG(ERR, EAL,"vhost-user socket unable to set"
293 + " ownership to %s (%s).\n",
294 + internal_config.vhost_sock_owner, strerror(err));
298 + RTE_LOG(INFO, EAL,"Socket %s changed ownership"
299 + " to %s.\n", vhost_sock_location,
300 + internal_config.vhost_sock_owner);
304 +rte_eal_set_socket_permissions(const char *path)
306 + if (internal_config.vhost_sock_perm) {
307 + vhost_set_permissions(path);
310 + if (internal_config.vhost_sock_owner) {
311 + vhost_set_ownership(path);
314 Index: deb_dpdk/lib/librte_eal/common/include/rte_eal.h
315 ===================================================================
316 --- deb_dpdk.orig/lib/librte_eal/common/include/rte_eal.h
317 +++ deb_dpdk/lib/librte_eal/common/include/rte_eal.h
318 @@ -252,6 +252,11 @@ static inline int rte_gettid(void)
319 return RTE_PER_LCORE(_thread_id);
323 + * Set owner/permissions on sockets if requested on EAL commandline
325 +void rte_eal_set_socket_permissions(const char *);
330 Index: deb_dpdk/doc/guides/testpmd_app_ug/run_app.rst
331 ===================================================================
332 --- deb_dpdk.orig/doc/guides/testpmd_app_ug/run_app.rst
333 +++ deb_dpdk/doc/guides/testpmd_app_ug/run_app.rst
334 @@ -156,6 +156,25 @@ See the DPDK Getting Started Guides for
336 Use malloc instead of hugetlbfs.
340 + When creating vhost_user sockets change owner and group to the specified value.
341 + This can be given as ``user:group``, but also only ``user`` or ``:group`` are supported.
345 + --vhost-owner 'libvirt-qemu:kvm'
346 + --vhost-owner 'libvirt-qemu'
347 + --vhost-owner ':kvm'
351 + When creating vhost_user sockets set them up with these permissions.
355 + --vhost-perm '0664'
358 Testpmd Command-line Options
359 ----------------------------
360 Index: deb_dpdk/lib/librte_eal/linuxapp/eal/rte_eal_version.map
361 ===================================================================
362 --- deb_dpdk.orig/lib/librte_eal/linuxapp/eal/rte_eal_version.map
363 +++ deb_dpdk/lib/librte_eal/linuxapp/eal/rte_eal_version.map
364 @@ -138,6 +138,7 @@ DPDK_2.2 {
365 rte_keepalive_mark_alive;
366 rte_keepalive_register_core;
367 rte_xen_dom0_supported;
368 + rte_eal_set_socket_permissions;