adapt packaging to the import of 16.07-rc1
[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: 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       },
42         {0,                     0, NULL, 0                        }
43  };
44  
45 @@ -157,6 +159,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: 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 */
64  
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"
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: 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;
88  };
89  
90 +#include <rte_eal.h>
91 +
92  #define MAX_VIRTIO_BACKLOG 128
93  
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
96         if (!vsocket)
97                 goto out;
98         memset(vsocket, 0, sizeof(struct vhost_user_socket));
99 +       rte_eal_set_socket_permissions(path);
100         vsocket->path = strdup(path);
101  
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
107 @@ -53,6 +53,9 @@
108  #if defined(RTE_ARCH_X86)
109  #include <sys/io.h>
110  #endif
111 +#include <sys/types.h>
112 +#include <pwd.h>
113 +#include <grp.h>
114  
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"
123                "\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;
128                         break;
129  
130 +               case OPT_VHOST_OWNER_NUM:
131 +                       internal_config.vhost_sock_owner = optarg;
132 +                       break;
133 +
134 +               case OPT_VHOST_PERM_NUM:
135 +                       internal_config.vhost_sock_perm = optarg;
136 +                       break;
137 +
138                 default:
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 */
143         return 1;
144  }
145 +
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.  */
149 +static int
150 +enlarge_buffer(char **buf, size_t *sizep)
151 +{
152 +    size_t newsize = *sizep * 2;
153 +
154 +    if (newsize > *sizep) {
155 +        *buf = realloc(*buf, newsize);
156 +        *sizep = newsize;
157 +        return 1;
158 +    }
159 +
160 +    return 0;
161 +}
162 +
163 +static int
164 +get_owners_from_str(const char *user_spec, uid_t *uid, gid_t *gid)
165 +{
166 +       size_t bufsize = 4096;
167 +
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);
171 +
172 +       char *buf = NULL;
173 +       struct passwd pwd, *res;
174 +       int e;
175 +
176 +       buf = malloc(bufsize);
177 +       char *user_search = NULL;
178 +       if (len) {
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)) {
184 +                               break;
185 +                       }
186 +               }
187 +
188 +               if (e != 0) {
189 +                       RTE_LOG(ERR, EAL,"Failed to retrive user %s's uid (%s), aborting.",
190 +                               user_search, strerror(e));
191 +                       goto release;
192 +               }
193 +               if (res == NULL) {
194 +                       RTE_LOG(ERR, EAL,"user %s not found,  aborting.",
195 +                               user_search);
196 +                       e = -1;
197 +                       goto release;
198 +               }
199 +       } else {
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)) {
203 +                               break;
204 +                       }
205 +               }
206 +
207 +               if (e != 0) {
208 +                       RTE_LOG(ERR, EAL,"Failed to retrive current user's uid "
209 +                               "(%s), aborting.", strerror(e));
210 +                       goto release;
211 +               }
212 +               user_search = strdup(pwd.pw_name);
213 +       }
214 +
215 +       if (uid)
216 +               *uid = pwd.pw_uid;
217 +
218 +       free(buf);
219 +       buf = NULL;
220 +
221 +       if (pos) {
222 +               char *grpstr = pos + 1;
223 +               grpstr += strspn(grpstr, " \t\r\n");
224 +
225 +               if (*grpstr) {
226 +                       struct group grp, *res;
227 +
228 +                       bufsize = 4096;
229 +                       buf = malloc(bufsize);
230 +                       while ((e = getgrnam_r(grpstr, &grp, buf, bufsize, &res))
231 +                                        == ERANGE) {
232 +                               if (!enlarge_buffer(&buf, &bufsize)) {
233 +                                       break;
234 +                               }
235 +                       }
236 +
237 +                       if (e) {
238 +                               RTE_LOG(ERR, EAL,"Failed to get group entry for %s, "
239 +                                       "(%s), aborting.", grpstr,
240 +                                       strerror(e));
241 +                               goto release;
242 +                       }
243 +                       if (res == NULL) {
244 +                               RTE_LOG(ERR, EAL,"Group %s not found, aborting.",
245 +                                       grpstr);
246 +                               e = -1;
247 +                               goto release;
248 +                       }
249 +
250 +                       if (gid)
251 +                               *gid = grp.gr_gid;
252 +               }
253 +       }
254 +
255 + release:
256 +       free(buf);
257 +       free(user_search);
258 +       return e;
259 +}
260 +
261 +static void
262 +vhost_set_permissions(const char *vhost_sock_location)
263 +{
264 +       unsigned long int mode = strtoul(internal_config.vhost_sock_perm, NULL, 0);
265 +       int err = chmod(vhost_sock_location, (mode_t)mode);
266 +       if (err) {
267 +               RTE_LOG(ERR, EAL,"vhost-user socket cannot set"
268 +                       " permissions to %s (%s).\n",
269 +                       internal_config.vhost_sock_perm, strerror(err));
270 +               return;
271 +       }
272 +       RTE_LOG(INFO, EAL,"Socket %s changed permissions"
273 +                       " to %s\n", vhost_sock_location,
274 +                       internal_config.vhost_sock_perm);
275 +}
276 +
277 +static void
278 +vhost_set_ownership(const char *vhost_sock_location)
279 +{
280 +       uid_t vhuid=0;
281 +       gid_t vhgid=0;
282 +
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);
287 +               return;
288 +       }
289 +
290 +       int err = chown(vhost_sock_location, vhuid, vhgid);
291 +       if (err) {
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));
295 +               return;
296 +       }
297 +
298 +       RTE_LOG(INFO, EAL,"Socket %s changed ownership"
299 +                       " to %s.\n", vhost_sock_location,
300 +                       internal_config.vhost_sock_owner);
301 +}
302 +
303 +void
304 +rte_eal_set_socket_permissions(const char *path)
305 +{
306 +       if (internal_config.vhost_sock_perm) {
307 +               vhost_set_permissions(path);
308 +       }
309 +
310 +       if (internal_config.vhost_sock_owner) {
311 +               vhost_set_ownership(path);
312 +       }
313 +}
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);
320  }
321  
322 +/**
323 + * Set owner/permissions on sockets if requested on EAL commandline
324 + */
325 +void rte_eal_set_socket_permissions(const char *);
326 +
327  #ifdef __cplusplus
328  }
329  #endif
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
335  
336      Use malloc instead of hugetlbfs.
337  
338 +*   ``--vhost-owner``
339 +
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.
342 +
343 +    Examples::
344 +
345 +       --vhost-owner 'libvirt-qemu:kvm'
346 +       --vhost-owner 'libvirt-qemu'
347 +       --vhost-owner ':kvm'
348 +
349 +*   ``--vhost-perm``
350 +
351 +    When creating vhost_user sockets set them up with these permissions.
352 +
353 +    For example::
354 +
355 +       --vhost-perm '0664'
356 +
357  
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;
369  
370  } DPDK_2.1;
371