Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
a6cf40a4 MJ |
2 | * |
3 | * lttng-context-net-ns.c | |
4 | * | |
5 | * LTTng net namespace context. | |
6 | * | |
7 | * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * 2019 Michael Jeanson <mjeanson@efficios.com> | |
9 | * | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/sched.h> | |
7b1edb4c | 15 | #include <linux/nsproxy.h> |
a6cf40a4 MJ |
16 | #include <net/net_namespace.h> |
17 | #include <lttng-events.h> | |
18 | #include <wrapper/ringbuffer/frontend_types.h> | |
19 | #include <wrapper/vmalloc.h> | |
20 | #include <wrapper/namespace.h> | |
21 | #include <lttng-tracer.h> | |
22 | ||
23 | #if defined(CONFIG_NET_NS) && \ | |
24 | (LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)) | |
25 | ||
26 | static | |
27 | size_t net_ns_get_size(size_t offset) | |
28 | { | |
29 | size_t size = 0; | |
30 | ||
31 | size += lib_ring_buffer_align(offset, lttng_alignof(unsigned int)); | |
32 | size += sizeof(unsigned int); | |
33 | return size; | |
34 | } | |
35 | ||
36 | static | |
37 | void net_ns_record(struct lttng_ctx_field *field, | |
38 | struct lib_ring_buffer_ctx *ctx, | |
39 | struct lttng_channel *chan) | |
40 | { | |
41 | unsigned int net_ns_inum = 0; | |
42 | ||
43 | /* | |
44 | * nsproxy can be NULL when scheduled out of exit. | |
45 | * | |
46 | * As documented in 'linux/nsproxy.h' namespaces access rules, no | |
47 | * precautions should be taken when accessing the current task's | |
48 | * namespaces, just dereference the pointers. | |
49 | */ | |
50 | if (current->nsproxy) | |
51 | net_ns_inum = current->nsproxy->net_ns->lttng_ns_inum; | |
52 | ||
53 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(net_ns_inum)); | |
54 | chan->ops->event_write(ctx, &net_ns_inum, sizeof(net_ns_inum)); | |
55 | } | |
56 | ||
57 | static | |
58 | void net_ns_get_value(struct lttng_ctx_field *field, | |
59 | struct lttng_probe_ctx *lttng_probe_ctx, | |
60 | union lttng_ctx_value *value) | |
61 | { | |
62 | unsigned int net_ns_inum = 0; | |
63 | ||
64 | /* | |
65 | * nsproxy can be NULL when scheduled out of exit. | |
66 | * | |
67 | * As documented in 'linux/nsproxy.h' namespaces access rules, no | |
68 | * precautions should be taken when accessing the current task's | |
69 | * namespaces, just dereference the pointers. | |
70 | */ | |
71 | if (current->nsproxy) | |
72 | net_ns_inum = current->nsproxy->net_ns->lttng_ns_inum; | |
73 | ||
74 | value->s64 = net_ns_inum; | |
75 | } | |
76 | ||
77 | int lttng_add_net_ns_to_ctx(struct lttng_ctx **ctx) | |
78 | { | |
79 | struct lttng_ctx_field *field; | |
80 | ||
81 | field = lttng_append_context(ctx); | |
82 | if (!field) | |
83 | return -ENOMEM; | |
84 | if (lttng_find_context(*ctx, "net_ns")) { | |
85 | lttng_remove_context_field(ctx, field); | |
86 | return -EEXIST; | |
87 | } | |
88 | field->event_field.name = "net_ns"; | |
89 | field->event_field.type.atype = atype_integer; | |
90 | field->event_field.type.u.basic.integer.size = sizeof(unsigned int) * CHAR_BIT; | |
91 | field->event_field.type.u.basic.integer.alignment = lttng_alignof(unsigned int) * CHAR_BIT; | |
92 | field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(unsigned int); | |
93 | field->event_field.type.u.basic.integer.reverse_byte_order = 0; | |
94 | field->event_field.type.u.basic.integer.base = 10; | |
95 | field->event_field.type.u.basic.integer.encoding = lttng_encode_none; | |
96 | field->get_size = net_ns_get_size; | |
97 | field->record = net_ns_record; | |
98 | field->get_value = net_ns_get_value; | |
99 | lttng_context_update(*ctx); | |
100 | wrapper_vmalloc_sync_all(); | |
101 | return 0; | |
102 | } | |
103 | EXPORT_SYMBOL_GPL(lttng_add_net_ns_to_ctx); | |
104 | ||
105 | #endif |