-
Notifications
You must be signed in to change notification settings - Fork 6
/
dest.go
167 lines (149 loc) · 3.58 KB
/
dest.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
/*
* 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 <yubo@xiaomi.com>
*/
package govs
import (
"fmt"
"strings"
)
type Vs_dest_user struct {
Nic uint8
Addr Be32
Port Be16
Conn_flags uint
Weight int
U_threshold uint32
L_threshold uint32
}
type Vs_dest_user_r struct {
Addr Be32
Port Be16
Conn_flags uint
Weight int
U_threshold uint32
L_threshold uint32
Activeconns uint32
Inactconns uint32
Persistent uint32
Conns uint64
Inpkts uint64
Outpkts uint64
Inbytes uint64
Outbytes uint64
}
const (
fmt_dest_t = "%5s %21s %8s %8s %15s %12s %12s %12s %7s %10s %10s %10s %10s"
fmt_dest = "%5s %21s %08x %8d %15s %12d %12d %12d %7d %10d %10d %10d %10d"
)
func Dest_title() string {
return fmt.Sprintf(fmt_dest_t,
"->", "Addr:Port", "Flags", "Weight", "threshold",
"Activeconns", "Inactconns", "Persistent",
"Conns", "Inpkts", "Outpkts", "Inbytes", "Outbytes")
}
func (d Vs_dest_user_r) String() string {
return fmt.Sprintf(fmt_dest,
"->", fmt.Sprintf("%s:%s", d.Addr.String(), d.Port.String()), d.Conn_flags,
d.Weight, fmt.Sprintf("%d-%d", d.L_threshold, d.U_threshold),
d.Activeconns, d.Inactconns, d.Persistent,
d.Conns, d.Inpkts, d.Outpkts, d.Inbytes, d.Outbytes)
}
type Vs_list_dests_r struct {
Code int
Msg string
Dests []Vs_dest_user_r
}
func (r Vs_list_dests_r) String() string {
var s string
if r.Code != 0 {
return fmt.Sprintf("%s:%s", Ecode(r.Code), r.Msg)
}
for _, dest := range r.Dests {
s += fmt.Sprintf("%s\n", dest)
}
return strings.TrimRight(s, "\n")
}
func Get_dests(o *CmdOptions) (*Vs_list_dests_r, error) {
var reply Vs_list_dests_r
args := Vs_list_q{
Cmd: VS_CMD_GET_DEST,
Service: Vs_service_user{
Addr: o.Addr.Ip,
Port: o.Addr.Port,
Protocol: uint8(o.Protocol),
Number: o.Number,
},
}
err := client.Call("api", args, &reply)
return &reply, err
}
type Vs_dest_q struct {
Cmd int
Service Vs_service_user
Dest Vs_dest_user
}
func Set_adddest(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_dest_q{
Cmd: VS_CMD_NEW_DEST,
Service: Vs_service_user{
Protocol: uint8(o.Protocol),
Addr: o.Addr.Ip,
Port: o.Addr.Port,
},
Dest: Vs_dest_user{
Nic: uint8(o.Dnic),
Addr: o.Daddr.Ip,
Port: o.Daddr.Port,
Conn_flags: o.Conn_flags | VS_CONN_F_FULLNAT,
Weight: o.Weight,
U_threshold: uint32(o.U_threshold),
L_threshold: uint32(o.L_threshold),
},
}
err := client.Call("api", args, &reply)
return &reply, err
}
func Set_editdest(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_dest_q{
Cmd: VS_CMD_SET_DEST,
Service: Vs_service_user{
Protocol: uint8(o.Protocol),
Addr: o.Addr.Ip,
Port: o.Addr.Port,
},
Dest: Vs_dest_user{
Nic: uint8(o.Dnic),
Addr: o.Daddr.Ip,
Port: o.Daddr.Port,
Conn_flags: o.Conn_flags | VS_CONN_F_FULLNAT,
Weight: o.Weight,
U_threshold: uint32(o.U_threshold),
L_threshold: uint32(o.L_threshold),
},
}
err := client.Call("api", args, &reply)
return &reply, err
}
func Set_deldest(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_dest_q{
Cmd: VS_CMD_DEL_DEST,
Service: Vs_service_user{
Protocol: uint8(o.Protocol),
Addr: o.Addr.Ip,
Port: o.Addr.Port,
},
Dest: Vs_dest_user{
Addr: o.Daddr.Ip,
Port: o.Daddr.Port,
},
}
err := client.Call("api", args, &reply)
return &reply, err
}