Nathan Skrzypczak | 4cef6de | 2021-07-19 18:21:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | #include <fcntl.h> |
| 18 | #include <sched.h> |
| 19 | |
| 20 | #include <vppinfra/format.h> |
| 21 | |
| 22 | __clib_export int |
| 23 | clib_netns_open (u8 *netns_u8) |
| 24 | { |
| 25 | char *netns = (char *) netns_u8; |
| 26 | u8 *s = 0; |
| 27 | int fd; |
| 28 | |
| 29 | if ((NULL) == netns) |
| 30 | s = format (0, "/proc/self/ns/net"); |
| 31 | else if (strncmp (netns, "pid:", 4) == 0) |
| 32 | s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0); |
| 33 | else if (netns[0] == '/') |
| 34 | s = format (0, "%s%c", netns, 0); |
| 35 | else |
| 36 | s = format (0, "/var/run/netns/%s%c", netns, 0); |
| 37 | |
| 38 | fd = open ((char *) s, O_RDONLY); |
| 39 | vec_free (s); |
| 40 | return fd; |
| 41 | } |
| 42 | |
| 43 | __clib_export int |
| 44 | clib_setns (int nfd) |
| 45 | { |
| 46 | return setns (nfd, CLONE_NEWNET); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * fd.io coding-style-patch-verification: ON |
| 51 | * |
| 52 | * Local Variables: |
| 53 | * eval: (c-set-style "gnu") |
| 54 | * End: |
| 55 | */ |