Merge Ubuntu DPDK packaging as of 15th June 2016
[deb_dpdk.git] / debian / patches / ubuntu-fix-vhost-user-socket-permission.patch
1 Description: eal: provide option to set vhost_user socket owner/permissions
2
3 The API doesn't hold a way to specify a owner/permission set for vhost_user
4 created sockets.
5
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/).
11
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.
15
16 Fixes LP: #1546565
17
18 *Update*
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
24    workaround.
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.
27
28 Forwarded: yes
29 Author: Christian Ehrhardt <christian.ehrhardt@canonical.com>
30 Last-Update: 2016-05-18
31
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       },
42         {0,                     0, NULL, 0                        }
43  };
44  
45 @@ -153,6 +155,8 @@ eal_reset_internal_config(struct interna
46  #endif
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;
51  }
52  
53  static int
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 */
64  
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"
74         OPT_XEN_DOM0_NUM,
75 +#define OPT_VHOST_OWNER       "vhost-owner"
76 +       OPT_VHOST_OWNER_NUM,
77 +#define OPT_VHOST_PERM        "vhost-perm"
78 +       OPT_VHOST_PERM_NUM,
79         OPT_LONG_MAX_NUM
80  };
81  
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
86 @@ -51,6 +51,8 @@
87  #include "vhost-net.h"
88  #include "virtio-net-user.h"
89  
90 +#include <rte_eal.h>
91 +
92  #define MAX_VIRTIO_BACKLOG 128
93  
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
96                 return -1;
97         }
98  
99 +       rte_eal_set_socket_permissions(path);
100 +
101         vserver->path = strdup(path);
102         vserver->fh = -1;
103  
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
108 @@ -53,6 +53,9 @@
109  #if defined(RTE_ARCH_X86)
110  #include <sys/io.h>
111  #endif
112 +#include <sys/types.h>
113 +#include <pwd.h>
114 +#include <grp.h>
115  
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"
124                "\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;
129                         break;
130  
131 +               case OPT_VHOST_OWNER_NUM:
132 +                       internal_config.vhost_sock_owner = optarg;
133 +                       break;
134 +
135 +               case OPT_VHOST_PERM_NUM:
136 +                       internal_config.vhost_sock_perm = optarg;
137 +                       break;
138 +
139                 default:
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 */
144         return 1;
145  }
146 +
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.  */
150 +static int
151 +enlarge_buffer(char **buf, size_t *sizep)
152 +{
153 +    size_t newsize = *sizep * 2;
154 +
155 +    if (newsize > *sizep) {
156 +        *buf = realloc(*buf, newsize);
157 +        *sizep = newsize;
158 +        return 1;
159 +    }
160 +
161 +    return 0;
162 +}
163 +
164 +static int
165 +get_owners_from_str(const char *user_spec, uid_t *uid, gid_t *gid)
166 +{
167 +       size_t bufsize = 4096;
168 +
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);
172 +
173 +       char *buf = NULL;
174 +       struct passwd pwd, *res;
175 +       int e;
176 +
177 +       buf = malloc(bufsize);
178 +       char *user_search = NULL;
179 +       if (len) {
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)) {
185 +                               break;
186 +                       }
187 +               }
188 +
189 +               if (e != 0) {
190 +                       RTE_LOG(ERR, EAL,"Failed to retrive user %s's uid (%s), aborting.",
191 +                               user_search, strerror(e));
192 +                       goto release;
193 +               }
194 +               if (res == NULL) {
195 +                       RTE_LOG(ERR, EAL,"user %s not found,  aborting.",
196 +                               user_search);
197 +                       e = -1;
198 +                       goto release;
199 +               }
200 +       } else {
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)) {
204 +                               break;
205 +                       }
206 +               }
207 +
208 +               if (e != 0) {
209 +                       RTE_LOG(ERR, EAL,"Failed to retrive current user's uid "
210 +                               "(%s), aborting.", strerror(e));
211 +                       goto release;
212 +               }
213 +               user_search = strdup(pwd.pw_name);
214 +       }
215 +
216 +       if (uid)
217 +               *uid = pwd.pw_uid;
218 +
219 +       free(buf);
220 +       buf = NULL;
221 +
222 +       if (pos) {
223 +               char *grpstr = pos + 1;
224 +               grpstr += strspn(grpstr, " \t\r\n");
225 +
226 +               if (*grpstr) {
227 +                       struct group grp, *res;
228 +
229 +                       bufsize = 4096;
230 +                       buf = malloc(bufsize);
231 +                       while ((e = getgrnam_r(grpstr, &grp, buf, bufsize, &res))
232 +                                        == ERANGE) {
233 +                               if (!enlarge_buffer(&buf, &bufsize)) {
234 +                                       break;
235 +                               }
236 +                       }
237 +
238 +                       if (e) {
239 +                               RTE_LOG(ERR, EAL,"Failed to get group entry for %s, "
240 +                                       "(%s), aborting.", grpstr,
241 +                                       strerror(e));
242 +                               goto release;
243 +                       }
244 +                       if (res == NULL) {
245 +                               RTE_LOG(ERR, EAL,"Group %s not found, aborting.",
246 +                                       grpstr);
247 +                               e = -1;
248 +                               goto release;
249 +                       }
250 +
251 +                       if (gid)
252 +                               *gid = grp.gr_gid;
253 +               }
254 +       }
255 +
256 + release:
257 +       free(buf);
258 +       free(user_search);
259 +       return e;
260 +}
261 +
262 +static void
263 +vhost_set_permissions(const char *vhost_sock_location)
264 +{
265 +       unsigned long int mode = strtoul(internal_config.vhost_sock_perm, NULL, 0);
266 +       int err = chmod(vhost_sock_location, (mode_t)mode);
267 +       if (err) {
268 +               RTE_LOG(ERR, EAL,"vhost-user socket cannot set"
269 +                       " permissions to %s (%s).\n",
270 +                       internal_config.vhost_sock_perm, strerror(err));
271 +               return;
272 +       }
273 +       RTE_LOG(INFO, EAL,"Socket %s changed permissions"
274 +                       " to %s\n", vhost_sock_location,
275 +                       internal_config.vhost_sock_perm);
276 +}
277 +
278 +static void
279 +vhost_set_ownership(const char *vhost_sock_location)
280 +{
281 +       uid_t vhuid=0;
282 +       gid_t vhgid=0;
283 +
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);
288 +               return;
289 +       }
290 +
291 +       int err = chown(vhost_sock_location, vhuid, vhgid);
292 +       if (err) {
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));
296 +               return;
297 +       }
298 +
299 +       RTE_LOG(INFO, EAL,"Socket %s changed ownership"
300 +                       " to %s.\n", vhost_sock_location,
301 +                       internal_config.vhost_sock_owner);
302 +}
303 +
304 +void
305 +rte_eal_set_socket_permissions(const char *path)
306 +{
307 +       if (internal_config.vhost_sock_perm) {
308 +               vhost_set_permissions(path);
309 +       }
310 +
311 +       if (internal_config.vhost_sock_owner) {
312 +               vhost_set_ownership(path);
313 +       }
314 +}
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);
321  }
322  
323 +/**
324 + * Set owner/permissions on sockets if requested on EAL commandline
325 + */
326 +void rte_eal_set_socket_permissions(const char *);
327 +
328  #ifdef __cplusplus
329  }
330  #endif
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
336  
337      Use malloc instead of hugetlbfs.
338  
339 +*   ``--vhost-owner``
340 +
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.
343 +
344 +    Examples::
345 +
346 +       --vhost-owner 'libvirt-qemu:kvm'
347 +       --vhost-owner 'libvirt-qemu'
348 +       --vhost-owner ':kvm'
349 +
350 +*   ``--vhost-perm``
351 +
352 +    When creating vhost_user sockets set them up with these permissions.
353 +
354 +    For example::
355 +
356 +       --vhost-perm '0664'
357 +
358  
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;
370  
371  } DPDK_2.1;
372