forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_vs_sched.c
194 lines (160 loc) · 4.22 KB
/
ip_vs_sched.c
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
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <rte_spinlock.h>
#include "list.h"
#include "ipvs/sched.h"
#include "ipvs/rr.h"
#include "ipvs/wrr.h"
#include "ipvs/wlc.h"
/*
* IPVS scheduler list
*/
static struct list_head dp_vs_schedulers;
/* lock for service table */
static rte_rwlock_t __dp_vs_sched_lock;
/*
* Bind a service with a scheduler
*/
int dp_vs_bind_scheduler(struct dp_vs_service *svc,
struct dp_vs_scheduler *scheduler)
{
int ret;
if (svc == NULL) {
return EDPVS_INVAL;
}
if (scheduler == NULL) {
return EDPVS_INVAL;
}
svc->scheduler = scheduler;
if (scheduler->init_service) {
ret = scheduler->init_service(svc);
if (ret) {
return ret;
}
}
return EDPVS_OK;
}
/*
* Unbind a service with its scheduler
*/
int dp_vs_unbind_scheduler(struct dp_vs_service *svc)
{
struct dp_vs_scheduler *sched;
if (svc == NULL) {
return EDPVS_INVAL;;
}
sched = svc->scheduler;
if (sched == NULL) {
return EDPVS_INVAL;
}
if (sched->exit_service) {
if (sched->exit_service(svc) != 0) {
return EDPVS_INVAL;
}
}
svc->scheduler = NULL;
return EDPVS_OK;
}
/*
* Lookup scheduler and try to load it if it doesn't exist
*/
struct dp_vs_scheduler *dp_vs_scheduler_get(const char *sched_name)
{
struct dp_vs_scheduler *sched;
//IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
rte_rwlock_read_lock(&__dp_vs_sched_lock);
list_for_each_entry(sched, &dp_vs_schedulers, n_list) {
if (strcmp(sched_name, sched->name) == 0) {
/* HIT */
rte_rwlock_read_unlock(&__dp_vs_sched_lock);
return sched;
}
}
rte_rwlock_read_unlock(&__dp_vs_sched_lock);
return NULL;
}
/*
* Register a scheduler in the scheduler list
*/
int register_dp_vs_scheduler(struct dp_vs_scheduler *scheduler)
{
struct dp_vs_scheduler *sched;
if (!scheduler) {
return EDPVS_INVAL;
}
if (!scheduler->name) {
return EDPVS_INVAL;
}
rte_rwlock_write_lock(&__dp_vs_sched_lock);
if (!list_empty(&scheduler->n_list)) {
rte_rwlock_write_unlock(&__dp_vs_sched_lock);
return EDPVS_EXIST;
}
/*
* Make sure that the scheduler with this name doesn't exist
* in the scheduler list.
*/
list_for_each_entry(sched, &dp_vs_schedulers, n_list) {
if (strcmp(scheduler->name, sched->name) == 0) {
rte_rwlock_write_unlock(&__dp_vs_sched_lock);
return EDPVS_EXIST;
}
}
/*
* Add it into the d-linked scheduler list
*/
list_add(&scheduler->n_list, &dp_vs_schedulers);
rte_rwlock_write_unlock(&__dp_vs_sched_lock);
return EDPVS_OK;
}
/*
* Unregister a scheduler from the scheduler list
*/
int unregister_dp_vs_scheduler(struct dp_vs_scheduler *scheduler)
{
if (!scheduler) {
return EDPVS_INVAL;
}
rte_rwlock_write_lock(&__dp_vs_sched_lock);
if (list_empty(&scheduler->n_list)) {
rte_rwlock_write_unlock(&__dp_vs_sched_lock);
return EDPVS_NOTEXIST;
}
/*
* Remove it from the d-linked scheduler list
*/
list_del(&scheduler->n_list);
rte_rwlock_write_unlock(&__dp_vs_sched_lock);
return EDPVS_OK;
}
int dp_vs_sched_init(void)
{
INIT_LIST_HEAD(&dp_vs_schedulers);
rte_rwlock_init(&__dp_vs_sched_lock);
dp_vs_rr_init();
dp_vs_wrr_init();
dp_vs_wlc_init();
return EDPVS_OK;
}
int dp_vs_sched_term(void)
{
dp_vs_rr_term();
dp_vs_wrr_term();
dp_vs_wlc_term();
return EDPVS_OK;
}