-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathservice.go
217 lines (192 loc) · 4.94 KB
/
service.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
/*
* 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 (
"fmt"
"strings"
)
type Vs_service_user struct {
Nic uint8
Protocol uint8
Addr Be32
Port Be16
Sched_name string
Flags uint
Timeout uint
Netmask Be32
Number int /* max list laddr/dests */
}
type Vs_service_user_r struct {
Protocol uint8
Addr Be32
Port Be16
Sched_name string
Flags uint32
Timeout uint32
Netmask Be32
Conns uint64
Inpkts uint64
Outpkts uint64
Inbytes uint64
Outbytes uint64
Num_dests uint32
Num_laddrs uint32
}
const (
fmt_svc_t = "%5s %21s %8s %8s %15s %6s %6s %5s %7s %10s %10s %10s %10s %12s"
fmt_svc = "%5s %21s %08x %8d %15s %6d %6d %5s %7d %10d %10d %10d %10d %12d"
fmt_svc_simple = "%5s %-21s %10s"
fmt_svc_simple_t = "%5s %-21s %10s"
)
func Svc_title(detail bool) string {
var res string
if detail == true {
res += fmt.Sprintf(fmt_svc_t,
"Proto", "Addr:Port ", "Flags",
"Timeout", "Netmask", "dests", "laddrs", "Sched",
"Conns", "Inpkts", "Outpkts", "Inbytes", "Outbytes", "Est_TimeOut")
} else {
res += fmt.Sprintf(fmt_svc_simple_t, "Prot", "Addr:Port", "Scheduler")
}
return res
}
func (svc *Vs_stats_svc) ListVsStats(detail bool, Coefficient uint64) {
var res string
if detail == true {
res += fmt.Sprintf(fmt_svc,
get_protocol_name(svc.Protocol),
fmt.Sprintf("%s:%s", svc.Addr.String(), svc.Port.String()), svc.Flags,
svc.Timeout, svc.Netmask.String(),
svc.Num_dests, svc.Num_laddrs, svc.Sched_name,
svc.Conns*Coefficient, svc.Inpkts*Coefficient, svc.Outpkts*Coefficient,
svc.Inbytes*Coefficient, svc.Outbytes*Coefficient, svc.Est_timeout)
} else {
res += fmt.Sprintf(fmt_svc_simple, get_protocol_name(svc.Protocol),
fmt.Sprintf("%s:%s", svc.Addr.String(), svc.Port.String()), svc.Sched_name)
}
fmt.Println(res)
}
func (svc Vs_service_user_r) String() string {
return fmt.Sprintf(fmt_svc,
get_protocol_name(svc.Protocol),
fmt.Sprintf("%s:%s", svc.Addr.String(), svc.Port.String()), svc.Flags,
svc.Timeout, svc.Netmask.String(),
svc.Num_dests, svc.Num_laddrs, svc.Sched_name,
svc.Conns, svc.Inpkts, svc.Outpkts,
svc.Inbytes, svc.Outbytes)
}
type Vs_list_q struct {
Cmd int
Num_services int
Service Vs_service_user
Dest Vs_dest_user
Laddr Vs_laddr_user
}
type Vs_list_service_r struct {
Code int
Msg string
Service Vs_service_user_r
}
func (r Vs_list_service_r) String() string {
if r.Code == 0 {
return r.Service.String()
} else {
return fmt.Sprintf("%s:%s", Ecode(r.Code), r.Msg)
}
}
type Vs_list_services_r struct {
Code int
Msg string
Num_services int
Services []Vs_service_user_r
}
func (r Vs_list_services_r) String() (s string) {
if r.Code != 0 {
return fmt.Sprintf("%s:%s", Ecode(r.Code), r.Msg)
}
for _, svc := range r.Services {
s += fmt.Sprintf("%s\n", svc)
}
return strings.TrimRight(s, "\n")
}
type Vs_service_q struct {
Cmd int
Service Vs_service_user
}
func Get_services(o *CmdOptions) (*Vs_list_services_r, error) {
var reply Vs_list_services_r
args := Vs_list_q{
Cmd: VS_CMD_GET_SERVICES,
}
err := client.Call("api", args, &reply)
return &reply, err
}
func Get_service(o *CmdOptions) (*Vs_list_service_r, error) {
var reply Vs_list_service_r
args := Vs_list_q{
Cmd: VS_CMD_GET_SERVICE,
Service: Vs_service_user{
Addr: o.Addr.Ip,
Port: o.Addr.Port,
Protocol: uint8(o.Protocol),
},
}
fmt.Printf("ip:%s, port:%d, protocol: %d\n",
args.Service.Addr.String(), args.Service.Port, args.Service.Protocol)
err := client.Call("api", args, &reply)
return &reply, err
}
func Set_add(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_service_q{
Cmd: VS_CMD_NEW_SERVICE,
Service: Vs_service_user{
Nic: uint8(o.Nic),
Protocol: uint8(o.Protocol),
Addr: o.Addr.Ip,
Port: o.Addr.Port,
Sched_name: o.Sched_name,
Flags: o.Flags,
Timeout: o.Timeout,
Netmask: o.Netmask,
},
}
err := client.Call("api", args, &reply)
return &reply, err
}
func Set_edit(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_service_q{
Cmd: VS_CMD_SET_SERVICE,
Service: Vs_service_user{
Nic: uint8(o.Nic),
Protocol: uint8(o.Protocol),
Addr: o.Addr.Ip,
Port: o.Addr.Port,
Sched_name: o.Sched_name,
Flags: o.Flags,
Timeout: o.Timeout,
Netmask: o.Netmask,
},
}
err := client.Call("api", args, &reply)
return &reply, err
}
func Set_del(o *CmdOptions) (*Vs_cmd_r, error) {
var reply Vs_cmd_r
args := Vs_service_q{
Cmd: VS_CMD_DEL_SERVICE,
Service: Vs_service_user{
Protocol: uint8(o.Protocol),
Addr: o.Addr.Ip,
Port: o.Addr.Port,
},
}
err := client.Call("api", args, &reply)
return &reply, err
}