blob: 0df17caa8af6cb82d022bc902602b64843a3fa54 [file] [log] [blame]
Simon Horman8be67a62010-08-22 21:37:54 +09001#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 */
14static LIST_HEAD(ip_vs_pe);
15
Julian Anastasov60b6aa32013-03-21 11:58:09 +020016/* semaphore for IPVS PEs. */
17static DEFINE_MUTEX(ip_vs_pe_mutex);
Simon Horman8be67a62010-08-22 21:37:54 +090018
Simon Horman8be67a62010-08-22 21:37:54 +090019/* Get pe in the pe list by name */
Hans Schillstromfe5e7a12010-11-19 14:25:12 +010020struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name)
Simon Horman8be67a62010-08-22 21:37:54 +090021{
22 struct ip_vs_pe *pe;
23
Hans Schillstromfe5e7a12010-11-19 14:25:12 +010024 IP_VS_DBG(10, "%s(): pe_name \"%s\"\n", __func__,
Simon Horman8be67a62010-08-22 21:37:54 +090025 pe_name);
26
Julian Anastasov60b6aa32013-03-21 11:58:09 +020027 rcu_read_lock();
28 list_for_each_entry_rcu(pe, &ip_vs_pe, n_list) {
Simon Horman8be67a62010-08-22 21:37:54 +090029 /* Test and get the modules atomically */
30 if (pe->module &&
31 !try_module_get(pe->module)) {
32 /* This pe is just deleted */
33 continue;
34 }
35 if (strcmp(pe_name, pe->name)==0) {
36 /* HIT */
Julian Anastasov60b6aa32013-03-21 11:58:09 +020037 rcu_read_unlock();
Simon Horman8be67a62010-08-22 21:37:54 +090038 return pe;
39 }
Markus Elfring982f4052014-11-18 20:37:05 +010040 module_put(pe->module);
Simon Horman8be67a62010-08-22 21:37:54 +090041 }
Julian Anastasov60b6aa32013-03-21 11:58:09 +020042 rcu_read_unlock();
Simon Horman8be67a62010-08-22 21:37:54 +090043
Simon Horman8be67a62010-08-22 21:37:54 +090044 return NULL;
45}
46
47/* Lookup pe and try to load it if it doesn't exist */
Simon Hormane9e5eee2010-11-08 20:05:57 +090048struct ip_vs_pe *ip_vs_pe_getbyname(const char *name)
Simon Horman8be67a62010-08-22 21:37:54 +090049{
50 struct ip_vs_pe *pe;
51
52 /* Search for the pe by name */
Simon Hormane9e5eee2010-11-08 20:05:57 +090053 pe = __ip_vs_pe_getbyname(name);
Simon Horman8be67a62010-08-22 21:37:54 +090054
55 /* If pe not found, load the module and search again */
56 if (!pe) {
57 request_module("ip_vs_pe_%s", name);
Simon Hormane9e5eee2010-11-08 20:05:57 +090058 pe = __ip_vs_pe_getbyname(name);
Simon Horman8be67a62010-08-22 21:37:54 +090059 }
60
61 return pe;
62}
63
Simon Horman8be67a62010-08-22 21:37:54 +090064/* Register a pe in the pe list */
65int register_ip_vs_pe(struct ip_vs_pe *pe)
66{
67 struct ip_vs_pe *tmp;
68
69 /* increase the module use count */
70 ip_vs_use_count_inc();
71
Julian Anastasov60b6aa32013-03-21 11:58:09 +020072 mutex_lock(&ip_vs_pe_mutex);
Simon Horman8be67a62010-08-22 21:37:54 +090073 /* Make sure that the pe with this name doesn't exist
74 * in the pe list.
75 */
76 list_for_each_entry(tmp, &ip_vs_pe, n_list) {
77 if (strcmp(tmp->name, pe->name) == 0) {
Julian Anastasov60b6aa32013-03-21 11:58:09 +020078 mutex_unlock(&ip_vs_pe_mutex);
Simon Horman8be67a62010-08-22 21:37:54 +090079 ip_vs_use_count_dec();
80 pr_err("%s(): [%s] pe already existed "
81 "in the system\n", __func__, pe->name);
82 return -EINVAL;
83 }
84 }
85 /* Add it into the d-linked pe list */
Julian Anastasov60b6aa32013-03-21 11:58:09 +020086 list_add_rcu(&pe->n_list, &ip_vs_pe);
87 mutex_unlock(&ip_vs_pe_mutex);
Simon Horman8be67a62010-08-22 21:37:54 +090088
89 pr_info("[%s] pe registered.\n", pe->name);
90
91 return 0;
92}
93EXPORT_SYMBOL_GPL(register_ip_vs_pe);
94
95/* Unregister a pe from the pe list */
96int unregister_ip_vs_pe(struct ip_vs_pe *pe)
97{
Julian Anastasov60b6aa32013-03-21 11:58:09 +020098 mutex_lock(&ip_vs_pe_mutex);
Simon Horman8be67a62010-08-22 21:37:54 +090099 /* Remove it from the d-linked pe list */
Julian Anastasov60b6aa32013-03-21 11:58:09 +0200100 list_del_rcu(&pe->n_list);
101 mutex_unlock(&ip_vs_pe_mutex);
Simon Horman8be67a62010-08-22 21:37:54 +0900102
103 /* decrease the module use count */
104 ip_vs_use_count_dec();
105
106 pr_info("[%s] pe unregistered.\n", pe->name);
107
108 return 0;
109}
110EXPORT_SYMBOL_GPL(unregister_ip_vs_pe);