debian: Avoid installing duplicate files in ovsdbmonitor package.
[sliver-openvswitch.git] / datapath / loop_counter.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2010, 2011 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/hardirq.h>
12 #include <linux/kernel.h>
13 #include <linux/percpu.h>
14 #include <linux/sched.h>
15
16 #include "loop_counter.h"
17
18 int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
19 {
20         if (net_ratelimit())
21                 pr_warn("%s: flow looped %d times, dropping\n",
22                         dp_name(dp), MAX_LOOPS);
23         actions->actions_len = 0;
24         return -ELOOP;
25 }
26
27 #ifndef CONFIG_PREEMPT_RT
28
29 /* We use a separate counter for each CPU for both interrupt and non-interrupt
30  * context in order to keep the limit deterministic for a given packet.
31  */
32 struct percpu_loop_counters {
33         struct loop_counter counters[2];
34 };
35
36 static DEFINE_PER_CPU(struct percpu_loop_counters, loop_counters);
37
38 struct loop_counter *loop_get_counter(void)
39 {
40         return &get_cpu_var(loop_counters).counters[!!in_interrupt()];
41 }
42
43 void loop_put_counter(void)
44 {
45         put_cpu_var(loop_counters);
46 }
47
48 #else /* !CONFIG_PREEMPT_RT */
49
50 struct loop_counter *loop_get_counter(void)
51 {
52         WARN_ON(in_interrupt());
53
54         /* Only two bits of the extra_flags field in struct task_struct are
55          * used and it's an unsigned int.  We hijack the most significant bits
56          * to be our counter structure.  On RT kernels softirqs always run in
57          * process context so we are guaranteed to have a valid task_struct.
58          */
59
60 #ifdef __LITTLE_ENDIAN
61         return (void *)(&current->extra_flags + 1) -
62                 sizeof(struct loop_counter);
63 #elif __BIG_ENDIAN
64         return (struct loop_counter *)&current->extra_flags;
65 #else
66 #error "Please fix <asm/byteorder.h>."
67 #endif
68 }
69
70 void loop_put_counter(void) { }
71
72 #endif /* CONFIG_PREEMPT_RT */