ipvs: convert locks used in persistence engines
[firefly-linux-kernel-4.4.55.git] / net / netfilter / ipvs / ip_vs_pe.c
1 #define KMSG_COMPONENT "IPVS"
2 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
3
4 #include <linux/module.h>
5 #include <linux/spinlock.h>
6 #include <linux/interrupt.h>
7 #include <asm/string.h>
8 #include <linux/kmod.h>
9 #include <linux/sysctl.h>
10
11 #include <net/ip_vs.h>
12
13 /* IPVS pe list */
14 static LIST_HEAD(ip_vs_pe);
15
16 /* semaphore for IPVS PEs. */
17 static DEFINE_MUTEX(ip_vs_pe_mutex);
18
19 /* Bind a service with a pe */
20 void ip_vs_bind_pe(struct ip_vs_service *svc, struct ip_vs_pe *pe)
21 {
22         svc->pe = pe;
23 }
24
25 /* Unbind a service from its pe */
26 void ip_vs_unbind_pe(struct ip_vs_service *svc)
27 {
28         svc->pe = NULL;
29 }
30
31 /* Get pe in the pe list by name */
32 struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name)
33 {
34         struct ip_vs_pe *pe;
35
36         IP_VS_DBG(10, "%s(): pe_name \"%s\"\n", __func__,
37                   pe_name);
38
39         rcu_read_lock();
40         list_for_each_entry_rcu(pe, &ip_vs_pe, n_list) {
41                 /* Test and get the modules atomically */
42                 if (pe->module &&
43                     !try_module_get(pe->module)) {
44                         /* This pe is just deleted */
45                         continue;
46                 }
47                 if (strcmp(pe_name, pe->name)==0) {
48                         /* HIT */
49                         rcu_read_unlock();
50                         return pe;
51                 }
52                 if (pe->module)
53                         module_put(pe->module);
54         }
55         rcu_read_unlock();
56
57         return NULL;
58 }
59
60 /* Lookup pe and try to load it if it doesn't exist */
61 struct ip_vs_pe *ip_vs_pe_getbyname(const char *name)
62 {
63         struct ip_vs_pe *pe;
64
65         /* Search for the pe by name */
66         pe = __ip_vs_pe_getbyname(name);
67
68         /* If pe not found, load the module and search again */
69         if (!pe) {
70                 request_module("ip_vs_pe_%s", name);
71                 pe = __ip_vs_pe_getbyname(name);
72         }
73
74         return pe;
75 }
76
77 /* Register a pe in the pe list */
78 int register_ip_vs_pe(struct ip_vs_pe *pe)
79 {
80         struct ip_vs_pe *tmp;
81
82         /* increase the module use count */
83         ip_vs_use_count_inc();
84
85         mutex_lock(&ip_vs_pe_mutex);
86         /* Make sure that the pe with this name doesn't exist
87          * in the pe list.
88          */
89         list_for_each_entry(tmp, &ip_vs_pe, n_list) {
90                 if (strcmp(tmp->name, pe->name) == 0) {
91                         mutex_unlock(&ip_vs_pe_mutex);
92                         ip_vs_use_count_dec();
93                         pr_err("%s(): [%s] pe already existed "
94                                "in the system\n", __func__, pe->name);
95                         return -EINVAL;
96                 }
97         }
98         /* Add it into the d-linked pe list */
99         list_add_rcu(&pe->n_list, &ip_vs_pe);
100         mutex_unlock(&ip_vs_pe_mutex);
101
102         pr_info("[%s] pe registered.\n", pe->name);
103
104         return 0;
105 }
106 EXPORT_SYMBOL_GPL(register_ip_vs_pe);
107
108 /* Unregister a pe from the pe list */
109 int unregister_ip_vs_pe(struct ip_vs_pe *pe)
110 {
111         mutex_lock(&ip_vs_pe_mutex);
112         /* Remove it from the d-linked pe list */
113         list_del_rcu(&pe->n_list);
114         mutex_unlock(&ip_vs_pe_mutex);
115
116         /* decrease the module use count */
117         ip_vs_use_count_dec();
118
119         pr_info("[%s] pe unregistered.\n", pe->name);
120
121         return 0;
122 }
123 EXPORT_SYMBOL_GPL(unregister_ip_vs_pe);