aboutsummaryrefslogtreecommitdiffstats
path: root/net/core/flow.c
blob: 0ab5234b17d8423e18c20ac9575178b01b25b6a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* flow.c: Generic flow cache.
 *
 * Copyright (C) 2003 Alexey N. Kuznetsov (kuznet@ms2.inr.ac.ru)
 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/jhash.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/smp.h>
#include <linux/completion.h>
#include <linux/percpu.h>
#include <linux/bitops.h>
#include <linux/notifier.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>
#include <linux/mutex.h>
#include <net/flow.h>
#include <asm/atomic.h>
#include <asm/semaphore.h>
#include <linux/security.h>

struct flow_cache_entry {
	struct flow_cache_entry	*next;
	u16			family;
	u8			dir;
	struct flowi		key;
	u32			genid;
	void			*object;
	atomic_t		*object_ref;
};

atomic_t flow_cache_genid = ATOMIC_INIT(0);

static u32 flow_hash_shift;
#define flow_hash_size	(1 << flow_hash_shift)
static DEFINE_PER_CPU(struct flow_cache_entry **, flow_tables) = { NULL };

#define flow_table(cpu) (per_cpu(flow_tables, cpu))

static struct kmem_cache *flow_cachep __read_mostly;

static int flow_lwm, flow_hwm;

struct flow_percpu_info {
	int hash_rnd_recalc;
	u32 hash_rnd;
	int count;
} ____cacheline_aligned;
static DEFINE_PER_CPU(struct flow_percpu_info, flow_hash_info) = { 0 };

#define flow_hash_rnd_recalc(cpu) \
	(per_cpu(flow_hash_info, cpu).hash_rnd_recalc)
#define flow_hash_rnd(cpu) \
	(per_cpu(flow_hash_info, cpu).hash_rnd)
#define flow_count(cpu) \
	(per_cpu(flow_hash_info, cpu).count)

static struct timer_list flow_hash_rnd_timer;

#define FLOW_HASH_RND_PERIOD	(10 * 60 * HZ)

struct flow_flush_info {
	atomic_t cpuleft;
	struct completion completion;
};
static DEFINE_PER_CPU(struct tasklet_struct, flow_flush_tasklets) = { NULL };

#define flow_flush_tasklet(cpu) (&per_cpu(flow_flush_tasklets, cpu))

static void flow_cache_new_hashrnd(unsigned long arg)
{
	int i;

	for_each_possible_cpu(i)
		flow_hash_rnd_recalc(i) = 1;

	flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
	add_timer(&flow_hash_rnd_timer);
}

static void flow_entry_kill(int cpu, struct flow_cache_entry *fle)
{
	if (fle->object)
		atomic_dec(fle->object_ref);
	kmem_cache_free(flow_cachep, fle);
	flow_count(cpu)--;
}

static void __flow_cache_shrink(int cpu, int shrink_to)
{
	struct flow_cache_entry *fle, **flp;
	int i;

	for (i = 0; i < flow_hash_size; i++) {
		int k = 0;

		flp = &flow_table(cpu)[i];
		while ((fle = *flp) != NULL && k < shrink_to) {
			k++;
			flp = &fle->next;
		}
		while ((fle = *flp) != NULL) {
			*flp = fle->next;
			flow_entry_kill(cpu, fle);
		}
	}
}

static void flow_cache_shrink(int cpu)
{
	int shrink_to = flow_lwm / flow_hash_size;

	__flow_cache_shrink(cpu, shrink_to);
}

static void flow_new_hash_rnd(int cpu)
{
	get_random_bytes(&flow_hash_rnd(cpu), sizeof(u32));
	flow_hash_rnd_recalc(cpu) = 0;

	__flow_cache_shrink(cpu, 0);
}

static u32 flow_hash_code(struct flowi *key, int cpu)
{
	u32 *k = (u32 *) key;

	return (jhash2(k, (sizeof(*key) / sizeof(u32)), flow_hash_rnd(cpu)) &
		(flow_hash_size - 1));
}

#if (BITS_PER_LONG == 64)
typedef u64 flow_compare_t;
#else
typedef u32 flow_compare_t;
#endif

extern void flowi_is_missized(void);

/* I hear what you're saying, use memcmp.  But memcmp cannot make
 * important assumptions that we can here, such as alignment and
 * constant size.
 */
static int flow_key_compare(struct flowi *key1, struct flowi *key2)
{
	flow_compare_t *k1, *k1_lim, *k2;
	const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t);

	if (sizeof(struct flowi) % sizeof(flow_compare_t))
		flowi_is_missized();

	k1 = (flow_compare_t *) key1;
	k1_lim = k1 + n_elem;

	k2 = (flow_compare_t *) key2;

	do {
		if (*k1++ != *k2++)
			return 1;
	} while (k1 < k1_lim);

	return 0;
}

void *flow_cache_lookup(struct flowi *key, u16 family, u8 dir,
			flow_resolve_t resolver)
{
	struct flow_cache_entry *fle, **head;
	unsigned int hash;
	int cpu;

	local_bh_disable();
	cpu = smp_processor_id();

	fle = NULL;
	/* Packet really early in init?  Making flow_cache_init a
	 * pre-smp initcall would solve this.  --RR */
	if (!flow_table(cpu))
		goto nocache;

	if (flow_hash_rnd_recalc(cpu))
		flow_new_hash_rnd(cpu);
	hash = flow_hash_code(key, cpu);

	head = &flow_table(cpu)[hash];
	for (fle = *head; fle; fle = fle->next) {
		if (fle->family == family &&
		    fle->dir == dir &&
		    flow_key_compare(key, &fle->key) == 0) {
			if (fle->genid == atomic_read(&flow_cache_genid)) {
				void *ret = fle->object;

				if (ret)
					atomic_inc(fle->object_ref);
				local_bh_enable();

				return ret;
			}
			break;
		}
	}

	if (!fle) {
		if (flow_count(cpu) > flow_hwm)
			flow_cache_shrink(cpu);

		fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC);
		if (fle) {
			fle->next = *head;
			*head = fle;
			fle->family = family;
			fle->dir = dir;
			memcpy(&fle->key, key, sizeof(*key));
			fle->object = NULL;
			flow_count(cpu)++;
		}
	}

nocache:
	{
		int err;
		void *obj;
		atomic_t *obj_ref;

		err = resolver(key, family, dir, &obj, &obj_ref);

		if (fle && !err) {
			fle->genid = atomic_read(&flow_cache_genid);

			if (fle->object)
				atomic_dec(fle->object_ref);

			fle->object = obj;
			fle->object_ref = obj_ref;
			if (obj)
				atomic_inc(fle->object_ref);
		}
		local_bh_enable();

		if (err)
			obj = ERR_PTR(err);
		return obj;
	}
}

static void flow_cache_flush_tasklet(unsigned long data)
{
	struct flow_flush_info *info = (void *)data;
	int i;
	int cpu;

	cpu = smp_processor_id();
	for (i = 0; i < flow_hash_size; i++) {
		struct flow_cache_entry *fle;

		fle = flow_table(cpu)[i];
		for (; fle; fle = fle->next) {
			unsigned genid = atomic_read(&flow_cache_genid);

			if (!fle->object || fle->genid == genid)
				continue;

			fle->object = NULL;
			atomic_dec(fle->object_ref);
		}
	}

	if (atomic_dec_and_test(&info->cpuleft))
		complete(&info->completion);
}

static void flow_cache_flush_per_cpu(void *) __attribute__((__unused__));
static void flow_cache_flush_per_cpu(void *data)
{
	struct flow_flush_info *info = data;
	int cpu;
	struct tasklet_struct *tasklet;

	cpu = smp_processor_id();

	tasklet = flow_flush_tasklet(cpu);
	tasklet->data = (unsigned long)info;
	tasklet_schedule(tasklet);
}

void flow_cache_flush(void)
{
	struct flow_flush_info info;
	static DEFINE_MUTEX(flow_flush_sem);

	/* Don't want cpus going down or up during this. */
	lock_cpu_hotplug();
	mutex_lock(&flow_flush_sem);
	atomic_set(&info.cpuleft, num_online_cpus());
	init_completion(&info.completion);

	local_bh_disable();
	smp_call_function(flow_cache_flush_per_cpu, &info, 1, 0);
	flow_cache_flush_tasklet((unsigned long)&info);
	local_bh_enable();

	wait_for_completion(&info.completion);
	mutex_unlock(&flow_flush_sem);
	unlock_cpu_hotplug();
}

static void __devinit flow_cache_cpu_prepare(int cpu)
{
	struct tasklet_struct *tasklet;
	unsigned long order;

	for (order = 0;
	     (PAGE_SIZE << order) <
		     (sizeof(struct flow_cache_entry *)*flow_hash_size);
	     order++)
		/* NOTHING */;

	flow_table(cpu) = (struct flow_cache_entry **)
		__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
	if (!flow_table(cpu))
		panic("NET: failed to allocate flow cache order %lu\n", order);

	flow_hash_rnd_recalc(cpu) = 1;
	flow_count(cpu) = 0;

	tasklet = flow_flush_tasklet(cpu);
	tasklet_init(tasklet, flow_cache_flush_tasklet, 0);
}

static int flow_cache_cpu(struct notifier_block *nfb,
			  unsigned long action,
			  void *hcpu)
{
	if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
		__flow_cache_shrink((unsigned long)hcpu, 0);
	return NOTIFY_OK;
}

static int __init flow_cache_init(void)
{
	int i;

	flow_cachep = kmem_cache_create("flow_cache",
					sizeof(struct flow_cache_entry),
					0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
					NULL);
	flow_hash_shift = 10;
	flow_lwm = 2 * flow_hash_size;
	flow_hwm = 4 * flow_hash_size;

	init_timer(&flow_hash_rnd_timer);
	flow_hash_rnd_timer.function = flow_cache_new_hashrnd;
	flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
	add_timer(&flow_hash_rnd_timer);

	for_each_possible_cpu(i)
		flow_cache_cpu_prepare(i);

	hotcpu_notifier(flow_cache_cpu, 0);
	return 0;
}

module_init(flow_cache_init);

EXPORT_SYMBOL(flow_cache_genid);
EXPORT_SYMBOL(flow_cache_lookup);
/span>pid = bswap_32(self->mmap.pid); self->mmap.tid = bswap_32(self->mmap.tid); self->mmap.start = bswap_64(self->mmap.start); self->mmap.len = bswap_64(self->mmap.len); self->mmap.pgoff = bswap_64(self->mmap.pgoff); } static void event__task_swap(event_t *self) { self->fork.pid = bswap_32(self->fork.pid); self->fork.tid = bswap_32(self->fork.tid); self->fork.ppid = bswap_32(self->fork.ppid); self->fork.ptid = bswap_32(self->fork.ptid); self->fork.time = bswap_64(self->fork.time); } static void event__read_swap(event_t *self) { self->read.pid = bswap_32(self->read.pid); self->read.tid = bswap_32(self->read.tid); self->read.value = bswap_64(self->read.value); self->read.time_enabled = bswap_64(self->read.time_enabled); self->read.time_running = bswap_64(self->read.time_running); self->read.id = bswap_64(self->read.id); } typedef void (*event__swap_op)(event_t *self); static event__swap_op event__swap_ops[] = { [PERF_RECORD_MMAP] = event__mmap_swap, [PERF_RECORD_COMM] = event__comm_swap, [PERF_RECORD_FORK] = event__task_swap, [PERF_RECORD_EXIT] = event__task_swap, [PERF_RECORD_LOST] = event__all64_swap, [PERF_RECORD_READ] = event__read_swap, [PERF_RECORD_SAMPLE] = event__all64_swap, [PERF_RECORD_MAX] = NULL, }; static int perf_session__process_event(struct perf_session *self, event_t *event, struct perf_event_ops *ops, u64 offset, u64 head) { trace_event(event); if (event->header.type < PERF_RECORD_MAX) { dump_printf("%#Lx [%#x]: PERF_RECORD_%s", offset + head, event->header.size, event__name[event->header.type]); ++event__total[0]; ++event__total[event->header.type]; } if (self->header.needs_swap && event__swap_ops[event->header.type]) event__swap_ops[event->header.type](event); switch (event->header.type) { case PERF_RECORD_SAMPLE: return ops->sample(event, self); case PERF_RECORD_MMAP: return ops->mmap(event, self); case PERF_RECORD_COMM: return ops->comm(event, self); case PERF_RECORD_FORK: return ops->fork(event, self); case PERF_RECORD_EXIT: return ops->exit(event, self); case PERF_RECORD_LOST: return ops->lost(event, self); case PERF_RECORD_READ: return ops->read(event, self); case PERF_RECORD_THROTTLE: return ops->throttle(event, self); case PERF_RECORD_UNTHROTTLE: return ops->unthrottle(event, self); default: self->unknown_events++; return -1; } } void perf_event_header__bswap(struct perf_event_header *self) { self->type = bswap_32(self->type); self->misc = bswap_16(self->misc); self->size = bswap_16(self->size); } int perf_header__read_build_ids(struct perf_header *self, int input, u64 offset, u64 size) { struct build_id_event bev; char filename[PATH_MAX]; u64 limit = offset + size; int err = -1; while (offset < limit) { struct dso *dso; ssize_t len; struct list_head *head = &dsos__user; if (read(input, &bev, sizeof(bev)) != sizeof(bev)) goto out; if (self->needs_swap) perf_event_header__bswap(&bev.header); len = bev.header.size - sizeof(bev); if (read(input, filename, len) != len) goto out; if (bev.header.misc & PERF_RECORD_MISC_KERNEL) head = &dsos__kernel; dso = __dsos__findnew(head, filename); if (dso != NULL) { dso__set_build_id(dso, &bev.build_id); if (head == &dsos__kernel && filename[0] == '[') dso->kernel = 1; } offset += bev.header.size; } err = 0; out: return err; } static struct thread *perf_session__register_idle_thread(struct perf_session *self) { struct thread *thread = perf_session__findnew(self, 0); if (thread == NULL || thread__set_comm(thread, "swapper")) { pr_err("problem inserting idle task.\n"); thread = NULL; } return thread; } int __perf_session__process_events(struct perf_session *self, u64 data_offset, u64 data_size, u64 file_size, struct perf_event_ops *ops) { int err, mmap_prot, mmap_flags; u64 head, shift; u64 offset = 0; size_t page_size; event_t *event; uint32_t size; char *buf; perf_event_ops__fill_defaults(ops); page_size = sysconf(_SC_PAGESIZE); head = data_offset; shift = page_size * (head / page_size); offset += shift; head -= shift; mmap_prot = PROT_READ; mmap_flags = MAP_SHARED; if (self->header.needs_swap) { mmap_prot |= PROT_WRITE; mmap_flags = MAP_PRIVATE; } remap: buf = mmap(NULL, page_size * self->mmap_window, mmap_prot, mmap_flags, self->fd, offset); if (buf == MAP_FAILED) { pr_err("failed to mmap file\n"); err = -errno; goto out_err; } more: event = (event_t *)(buf + head); if (self->header.needs_swap) perf_event_header__bswap(&event->header); size = event->header.size; if (size == 0) size = 8; if (head + event->header.size >= page_size * self->mmap_window) { int munmap_ret; shift = page_size * (head / page_size); munmap_ret = munmap(buf, page_size * self->mmap_window); assert(munmap_ret == 0); offset += shift; head -= shift; goto remap; } size = event->header.size; dump_printf("\n%#Lx [%#x]: event: %d\n", offset + head, event->header.size, event->header.type); if (size == 0 || perf_session__process_event(self, event, ops, offset, head) < 0) { dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", offset + head, event->header.size, event->header.type); /* * assume we lost track of the stream, check alignment, and * increment a single u64 in the hope to catch on again 'soon'. */ if (unlikely(head & 7)) head &= ~7ULL; size = 8; } head += size; if (offset + head >= data_offset + data_size) goto done; if (offset + head < file_size) goto more; done: err = 0; out_err: return err; } int perf_session__process_events(struct perf_session *self, struct perf_event_ops *ops) { int err; if (perf_session__register_idle_thread(self) == NULL) return -ENOMEM; if (!symbol_conf.full_paths) { char bf[PATH_MAX]; if (getcwd(bf, sizeof(bf)) == NULL) { err = -errno; out_getcwd_err: pr_err("failed to get the current directory\n"); goto out_err; } self->cwd = strdup(bf); if (self->cwd == NULL) { err = -ENOMEM; goto out_getcwd_err; } self->cwdlen = strlen(self->cwd); } err = __perf_session__process_events(self, self->header.data_offset, self->header.data_size, self->size, ops); out_err: return err; } bool perf_session__has_traces(struct perf_session *self, const char *msg) { if (!(self->sample_type & PERF_SAMPLE_RAW)) { pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); return false; } return true; } int perf_session__set_kallsyms_ref_reloc_sym(struct perf_session *self, const char *symbol_name, u64 addr) { char *bracket; enum map_type i; self->ref_reloc_sym.name = strdup(symbol_name); if (self->ref_reloc_sym.name == NULL) return -ENOMEM; bracket = strchr(self->ref_reloc_sym.name, ']'); if (bracket) *bracket = '\0'; self->ref_reloc_sym.addr = addr; for (i = 0; i < MAP__NR_TYPES; ++i) { struct kmap *kmap = map__kmap(self->vmlinux_maps[i]); kmap->ref_reloc_sym = &self->ref_reloc_sym; } return 0; } static u64 map__reloc_map_ip(struct map *map, u64 ip) { return ip + (s64)map->pgoff; } static u64 map__reloc_unmap_ip(struct map *map, u64 ip) { return ip - (s64)map->pgoff; } void map__reloc_vmlinux(struct map *self) { struct kmap *kmap = map__kmap(self); s64 reloc; if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr) return; reloc = (kmap->ref_reloc_sym->unrelocated_addr - kmap->ref_reloc_sym->addr); if (!reloc) return; self->map_ip = map__reloc_map_ip; self->unmap_ip = map__reloc_unmap_ip; self->pgoff = reloc; }