| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> |
| 6 | * |
| 7 | * LTTng UST net namespace context. |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include <limits.h> |
| 12 | #include <stddef.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <unistd.h> |
| 16 | #include <lttng/ust-events.h> |
| 17 | #include <lttng/ust-tracer.h> |
| 18 | #include <lttng/ust-ringbuffer-context.h> |
| 19 | #include "common/compat/tid.h" |
| 20 | #include <urcu/tls-compat.h> |
| 21 | |
| 22 | #include "context-internal.h" |
| 23 | #include "lttng-tracer-core.h" |
| 24 | #include "common/ns.h" |
| 25 | |
| 26 | /* |
| 27 | * We cache the result to ensure we don't stat(2) the proc filesystem on |
| 28 | * each event. |
| 29 | */ |
| 30 | static DEFINE_URCU_TLS_INIT(ino_t, cached_net_ns, NS_INO_UNINITIALIZED); |
| 31 | |
| 32 | static |
| 33 | ino_t get_net_ns(void) |
| 34 | { |
| 35 | struct stat sb; |
| 36 | ino_t net_ns; |
| 37 | |
| 38 | net_ns = CMM_LOAD_SHARED(URCU_TLS(cached_net_ns)); |
| 39 | |
| 40 | /* |
| 41 | * If the cache is populated, do nothing and return the |
| 42 | * cached inode number. |
| 43 | */ |
| 44 | if (caa_likely(net_ns != NS_INO_UNINITIALIZED)) |
| 45 | return net_ns; |
| 46 | |
| 47 | /* |
| 48 | * At this point we have to populate the cache, set the initial |
| 49 | * value to NS_INO_UNAVAILABLE (0), if we fail to get the inode |
| 50 | * number from the proc filesystem, this is the value we will |
| 51 | * cache. |
| 52 | */ |
| 53 | net_ns = NS_INO_UNAVAILABLE; |
| 54 | |
| 55 | /* |
| 56 | * /proc/thread-self was introduced in kernel v3.17 |
| 57 | */ |
| 58 | if (stat("/proc/thread-self/ns/net", &sb) == 0) { |
| 59 | net_ns = sb.st_ino; |
| 60 | } else { |
| 61 | char proc_ns_path[LTTNG_PROC_NS_PATH_MAX]; |
| 62 | |
| 63 | if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX, |
| 64 | "/proc/self/task/%d/ns/net", |
| 65 | lttng_gettid()) >= 0) { |
| 66 | |
| 67 | if (stat(proc_ns_path, &sb) == 0) { |
| 68 | net_ns = sb.st_ino; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * And finally, store the inode number in the cache. |
| 75 | */ |
| 76 | CMM_STORE_SHARED(URCU_TLS(cached_net_ns), net_ns); |
| 77 | |
| 78 | return net_ns; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * The net namespace can change for 3 reasons |
| 83 | * * clone(2) called with CLONE_NEWNET |
| 84 | * * setns(2) called with the fd of a different net ns |
| 85 | * * unshare(2) called with CLONE_NEWNET |
| 86 | */ |
| 87 | void lttng_context_net_ns_reset(void) |
| 88 | { |
| 89 | CMM_STORE_SHARED(URCU_TLS(cached_net_ns), NS_INO_UNINITIALIZED); |
| 90 | } |
| 91 | |
| 92 | static |
| 93 | size_t net_ns_get_size(void *priv __attribute__((unused)), |
| 94 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
| 95 | size_t offset) |
| 96 | { |
| 97 | size_t size = 0; |
| 98 | |
| 99 | size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(ino_t)); |
| 100 | size += sizeof(ino_t); |
| 101 | return size; |
| 102 | } |
| 103 | |
| 104 | static |
| 105 | void net_ns_record(void *priv __attribute__((unused)), |
| 106 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
| 107 | struct lttng_ust_ring_buffer_ctx *ctx, |
| 108 | struct lttng_ust_channel_buffer *chan) |
| 109 | { |
| 110 | ino_t net_ns; |
| 111 | |
| 112 | net_ns = get_net_ns(); |
| 113 | chan->ops->event_write(ctx, &net_ns, sizeof(net_ns), lttng_ust_rb_alignof(net_ns)); |
| 114 | } |
| 115 | |
| 116 | static |
| 117 | void net_ns_get_value(void *priv __attribute__((unused)), |
| 118 | struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)), |
| 119 | struct lttng_ust_ctx_value *value) |
| 120 | { |
| 121 | value->u.u64 = get_net_ns(); |
| 122 | } |
| 123 | |
| 124 | static const struct lttng_ust_ctx_field *ctx_field = lttng_ust_static_ctx_field( |
| 125 | lttng_ust_static_event_field("net_ns", |
| 126 | lttng_ust_static_type_integer(sizeof(ino_t) * CHAR_BIT, |
| 127 | lttng_ust_rb_alignof(ino_t) * CHAR_BIT, |
| 128 | lttng_ust_is_signed_type(ino_t), |
| 129 | LTTNG_UST_BYTE_ORDER, 10), |
| 130 | false, false), |
| 131 | net_ns_get_size, |
| 132 | net_ns_record, |
| 133 | net_ns_get_value, |
| 134 | NULL, NULL); |
| 135 | |
| 136 | int lttng_add_net_ns_to_ctx(struct lttng_ust_ctx **ctx) |
| 137 | { |
| 138 | int ret; |
| 139 | |
| 140 | if (lttng_find_context(*ctx, ctx_field->event_field->name)) { |
| 141 | ret = -EEXIST; |
| 142 | goto error_find_context; |
| 143 | } |
| 144 | ret = lttng_ust_context_append(ctx, ctx_field); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | return 0; |
| 148 | |
| 149 | error_find_context: |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Force a read (imply TLS allocation for dlopen) of TLS variables. |
| 155 | */ |
| 156 | void lttng_ust_net_ns_init_thread(int flags) |
| 157 | { |
| 158 | __asm__ __volatile__ ("" : : "m" (URCU_TLS(cached_net_ns))); |
| 159 | if (flags & LTTNG_UST_INIT_THREAD_CONTEXT_CACHE) |
| 160 | (void)get_net_ns(); |
| 161 | } |