-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.go
540 lines (478 loc) · 12.1 KB
/
core.go
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* Copyright 2016 Xiaomi Corporation. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*
* Authors: Yu Bo <[email protected]>
*/
package govs
import (
"encoding/json"
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"strconv"
"strings"
)
const (
URL = "/tmp/dpvs.sock"
/*
* IPVS Connection Flags
*/
VS_CONN_F_FWD_MASK = 0x000f /* mask for the fwd methods */
VS_CONN_F_MASQ = 0x0000 /* masquerading/NAT */
VS_CONN_F_LOCALNODE = 0x0001 /* local node */
VS_CONN_F_TUNNEL = 0x0002 /* tunneling */
VS_CONN_F_DROUTE = 0x0003 /* direct routing */
VS_CONN_F_BYPASS = 0x0004 /* cache bypass */
VS_CONN_F_FULLNAT = 0x0005 /* full nat */
VS_CONN_F_DSNAT = 0x0008 /* dsnat flag */
VS_CONN_F_SYNC = 0x0020 /* entry created by sync */
VS_CONN_F_HASHED = 0x0040 /* hashed entry */
VS_CONN_F_NOOUTPUT = 0x0080 /* no output packets */
VS_CONN_F_INACTIVE = 0x0100 /* not established */
VS_CONN_F_OUT_SEQ = 0x0200 /* must do output seq adjust */
VS_CONN_F_IN_SEQ = 0x0400 /* must do input seq adjust */
VS_CONN_F_SEQ_MASK = 0x0600 /* in/out sequence mask */
VS_CONN_F_NO_CPORT = 0x0800 /* no client port set yet */
VS_CONN_F_TEMPLATE = 0x1000 /* template, not connection */
VS_CONN_F_ONE_PACKET = 0x2000 /* forward only one packet */
VS_CONN_F_SYNPROXY = 0x8000 /* syn proxy flag */
VS_SVC_F_PERSISTENT = 0x0001 /* persistent port */
VS_SVC_F_HASHED = 0x0002 /* hashed entry */
VS_SVC_F_ONEPACKET = 0x0004 /* one-packet scheduling */
VS_SVC_F_SYNPROXY = VS_CONN_F_SYNPROXY /* synproxy flag */
VS_SVC_F_DSNAT = VS_CONN_F_DSNAT /* dsnat flag */
VS_SVC_F_MASK = (VS_SVC_F_PERSISTENT |
VS_SVC_F_ONEPACKET |
VS_SVC_F_SYNPROXY |
VS_SVC_F_DSNAT)
)
var (
client *rpc.Client
CmdOpt CmdOptions
FirstCmd FirstCommand
)
type CallOptions struct {
Opt CmdOptions
// Args []string
}
type CmdOptions struct {
/* status */
Typ string
Id int
/* service */
Addr Addr4
Nic uint
Protocol Protocol
TCP string
UDP string
Sched_name string
Flags uint
Number int
Timeout uint
Timeout_s string
Netmask Be32
/* dest */
Dnic uint
Daddr Addr4
Conn_flags uint
Weight int
U_threshold uint
L_threshold uint
/* local addr */
Lnic uint
Lip Be32
/* timeout */
Tcp_timeout int
Tcp_fin_timeout int
Udp_timeout int
/* others opt */
Print_detail bool
Print_all_worker bool
}
type FirstCommand struct {
ADD bool
EDIT bool
DEL bool
ADDDEST bool
EDITDEST bool
DELDEST bool
FLUSH bool
LIST bool
ZERO bool
USAGE bool
VERSION bool
ADDLADDR bool
DELLADDR bool
GETLADDR bool
TIMEOUT bool
STATUS bool
}
const (
CMD_NONE = iota
CMD_ADD
CMD_EDIT
CMD_DEL
CMD_ADDDEST
CMD_EDITDEST
CMD_DELDEST
CMD_FLUSH
CMD_LIST
CMD_ZERO
CMD_VERSION
CMD_ADDLADDR
CMD_DELLADDR
CMD_GETLADDR
CMD_TIMEOUT
CMD_STATUS
CMD_LAST
)
const NUMBER_OF_CMD = CMD_LAST - 1
const (
OPT_NONE = iota
OPT_SERVICE
OPT_NETMASK
OPT_SCHEDULER
OPT_FLAGS
OPT_REALSERVER
OPT_WEIGHT
OPT_UTHRESHOLD
OPT_LTHRESHOLD
OPT_LADDR
OPT_TYPE
OPT_ID
OPT_TIMEOUT
OPT_CONNFLAGS
OPT_PRINTDETAIL
OPT_PRINTALLWORKER
OPT_LAST
)
const NUMBER_OF_OPT = OPT_LAST - 1
var CMDNAMES = [NUMBER_OF_CMD]string{
"add-service",
"edit-service",
"delete-service",
"add-realserver",
"edit-realserver",
"delete-realserver",
"flush",
"list",
"zero",
"version",
"add-localaddr",
"del-localaddr",
"get-localaddr",
"timeout",
"status",
}
var OPTNAMES = [NUMBER_OF_OPT]string{
"service",
"netmask",
"scheduler",
"flags",
"real-server",
"weight",
"u-threshold",
"l-threshold",
"localaddr",
"type",
"id",
"timeout",
"conn-flags",
"print detail",
"print all cpu worker",
}
/*
* Table of legal combinations of commands and options.
* Key:
* '+' compulsory
* 'x' illegal
* ' ' optional
*/
var CMD_V_OPT = [NUMBER_OF_CMD][NUMBER_OF_OPT]byte{
/* svc -M -s flags -r -w -x -y -z type -i timeout connflags -detail -all -n*/
/*ADD */ {'+', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*EDIT */ {'+', ' ', ' ', ' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*DEL */ {'+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*ADDDEST */ {'+', 'x', 'x', 'x', '+', ' ', ' ', ' ', 'x', 'x', 'x', 'x', ' ', 'x', 'x'},
/*EDITDEST*/ {'+', 'x', 'x', 'x', '+', ' ', ' ', ' ', 'x', 'x', 'x', 'x', ' ', 'x', 'x'},
/*DELDEST */ {'+', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*FLUSH */ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*LIST */ {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', 'x', 'x', ' ', ' '},
/*ZERO */ {'+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*VERSION */ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*ADDLADDR*/ {'+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x'},
/*DELLADDR*/ {'+', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '+', 'x', 'x', 'x', 'x', 'x', 'x'},
/*GETLADDR*/ {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'},
/*TIMEOUT */ {'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', 'x', 'x', 'x'},
/*STATUS */ {' ', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', ' ', ' ', 'x', 'x', ' ', ' '},
}
type Be32 uint32
func (p *Be32) Set(value string) error {
if value == "" {
return nil
}
if ip := net.ParseIP(value).To4(); ip != nil {
*p = Htonl(ipToU32(ip))
}
return nil
}
func (p Be32) String() string {
return be32_to_addr(p)
}
func (p *Be32) UnmarshalJSON(data []byte) error {
var i int32
if err := json.Unmarshal(data, &i); err != nil {
return err
}
*p = Be32(uint32(i))
return nil
}
type Be16 uint16
func (p *Be16) Set(value string) error {
if value == "" {
return nil
}
port, err := strconv.Atoi(value)
if err != nil {
return err
}
*p = Htons(uint16(port))
return nil
}
func (p Be16) String() string {
return fmt.Sprintf("%d", Ntohs(p))
}
type Addr4 struct {
Ip Be32
Port Be16
}
func (p *Addr4) Set(value string) error {
if value == "" {
p.Ip = 0
p.Port = 0
return nil
}
fields := strings.Split(value, ":")
if len(fields) == 1 || len(fields) == 2 {
if ip := net.ParseIP(fields[0]).To4(); ip != nil {
p.Ip = Htonl(ipToU32(ip))
if len(fields) == 2 {
if port, err := strconv.Atoi(fields[1]); err != nil {
return errIpv4Addr
} else {
p.Port = Htons(uint16(port))
}
} else {
p.Port = 0
}
return nil
}
}
return errIpv4Addr
}
func (p *Addr4) String() string {
return fmt.Sprintf("%s:%d", be32_to_addr(p.Ip), Ntohs(p.Port))
}
type Vs_timeout_q struct {
Cmd int
Tcp_timeout int
Tcp_fin_timeout int
Udp_timeout int
}
func (t *Vs_timeout_q) Set(value string) (err error) {
fields := strings.Split(value, ",")
if len(fields) != 3 {
return errTimeout
}
if t.Tcp_timeout, err = strconv.Atoi(fields[0]); err != nil {
return err
}
if t.Tcp_fin_timeout, err = strconv.Atoi(fields[1]); err != nil {
return err
}
if t.Udp_timeout, err = strconv.Atoi(fields[2]); err != nil {
return err
}
return nil
}
type Vs_timeout_r struct {
Code int
Msg string
Tcp_timeout int
Tcp_fin_timeout int
Udp_timeout int
}
func (r Vs_timeout_r) String() string {
if r.Code == 0 {
return fmt.Sprintf("tcp:%d tcp_fin:%d udp:%d",
r.Tcp_timeout, r.Tcp_fin_timeout, r.Udp_timeout)
} else {
return fmt.Sprintf("%s:%s", Ecode(r.Code), r.Msg)
}
}
const (
VS_STATS_IO = iota
VS_STATS_WORKER
VS_ESTATS_WORKER
VS_STATS_DEV
VS_STATS_CTL
VS_STATS_MEM
VS_STATS_IP_VS_INFO
VS_STATS_DEBUG
__VS_STATS_MAX
)
const (
VS_CMD_UNSPEC = iota
VS_CMD_NEW_SERVICE /* add service */
VS_CMD_SET_SERVICE /* modify service */
VS_CMD_DEL_SERVICE /* delete service */
VS_CMD_GET_SERVICE /* get service info */
VS_CMD_GET_SERVICES /* get services info */
VS_CMD_NEW_DEST /* add destination */
VS_CMD_SET_DEST /* modify destination */
VS_CMD_DEL_DEST /* delete destination */
VS_CMD_GET_DEST /* get destination info */
VS_CMD_NEW_DAEMON /* start sync daemon */
VS_CMD_DEL_DAEMON /* stop sync daemon */
VS_CMD_GET_DAEMON /* get sync daemon status */
VS_CMD_SET_CONFIG /* set config settings */
VS_CMD_GET_CONFIG /* get config settings */
VS_CMD_SET_INFO /* only used in GET_INFO reply */
VS_CMD_GET_INFO /* get general IPVS info */
VS_CMD_ZERO /* zero all counters and stats */
VS_CMD_FLUSH /* flush services and dests */
VS_CMD_NEW_LADDR /* add local address */
VS_CMD_DEL_LADDR /* del local address */
VS_CMD_GET_LADDR /* dump local address */
__VS_CMD_MAX
)
const (
IPPROTO_IP = 0 /* Dummy protocol for TCP */
IPPROTO_ICMP = 1 /* Internet Control Message Protocol */
IPPROTO_IGMP = 2 /* Internet Group Management Protocol */
IPPROTO_IPIP = 4 /* IPIP tunnels (older KA9Q tunnels use 94) */
IPPROTO_TCP = 6 /* Transmission Control Protocol */
IPPROTO_EGP = 8 /* Exterior Gateway Protocol */
IPPROTO_PUP = 12 /* PUP protocol */
IPPROTO_UDP = 17 /* User Datagram Protocol */
IPPROTO_IDP = 22 /* XNS IDP protocol */
IPPROTO_DCCP = 33 /* Datagram Congestion Control Protocol */
IPPROTO_IPV6 = 41 /* IPv6-in-IPv4 tunnelling */
IPPROTO_RSVP = 46 /* RSVP protocol */
IPPROTO_GRE = 47 /* Cisco GRE tunnels (rfc 1701,1702) */
IPPROTO_ESP = 50 /* Encapsulation Security Payload protocol */
IPPROTO_AH = 51 /* Authentication Header protocol */
IPPROTO_BEETPH = 94 /* IP option pseudo header for BEET */
IPPROTO_PIM = 103 /* Protocol Independent Multicast */
IPPROTO_COMP = 108 /* Compression Header protocol */
IPPROTO_SCTP = 132 /* Stream Control Transport Protocol */
IPPROTO_UDPLITE = 136 /* UDP-Lite (RFC 3828) */
IPPROTO_RAW = 255 /* Raw IP packets */
)
type Protocol uint8
func (p *Protocol) Set(value string) error {
if value == "tcp" {
*p = IPPROTO_TCP
} else if value == "udp" {
*p = IPPROTO_UDP
} else {
return errProtocol
}
return nil
}
func (p *Protocol) String() string {
return get_protocol_name(uint8(*p))
}
type Vs_timeout_user struct {
Tcp_timeout int
Tcp_fin_timeout int
Udp_timeout int
}
type Vs_cmd_q struct {
Cmd int
}
type Vs_cmd_r struct {
Code int
Msg string
}
func (r Vs_cmd_r) String() string {
if r.Code == 0 {
return fmt.Sprintf("done")
} else {
return fmt.Sprintf("%s:%s", Ecode(r.Code), r.Msg)
}
}
type Vs_version_r struct {
Code int
Msg string
Version int
Size int
}
func (r Vs_version_r) String() string {
if r.Code != 0 {
return fmt.Sprintf("%s:%s", Ecode(r.Code), r.Msg)
}
return fmt.Sprintf("version\t\t%d.%d.%d\n"+
"conn table size\t%d",
(r.Version>>16)&0xff,
(r.Version>>8)&0xff,
(r.Version)&0xff,
r.Size)
}
func Vs_dial() error {
conn, err := net.Dial("unix", URL)
if err != nil {
return err
}
client = jsonrpc.NewClient(conn)
return nil
}
func Vs_close() {
if client != nil {
client.Close()
client = nil
}
}
func Get_version() (*Vs_version_r, error) {
var reply Vs_version_r
args := Vs_cmd_q{VS_CMD_GET_INFO}
err := client.Call("api", args, &reply)
return &reply, err
}
func Get_timeout(o *CmdOptions) (*Vs_timeout_r, error) {
args := Vs_timeout_q{Cmd: VS_CMD_GET_CONFIG}
reply := &Vs_timeout_r{}
err := client.Call("api", args, reply)
return reply, err
}
func Set_flush(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_cmd_q{VS_CMD_FLUSH}
err := client.Call("api", args, &reply)
return &reply, err
}
func Set_timeout(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_timeout_q{Cmd: VS_CMD_SET_CONFIG}
if err := args.Set(o.Timeout_s); err != nil {
return nil, err
}
err := client.Call("api", args, &reply)
return &reply, err
}
func Set_zero(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_service_q{
Cmd: VS_CMD_ZERO,
Service: Vs_service_user{
Addr: o.Addr.Ip,
Port: o.Addr.Port,
Protocol: uint8(o.Protocol),
},
}
err := client.Call("api", args, &reply)
return &reply, err
}