blob: 4a63cc95aa56bb73e00ea353c6ddb99d5bebd58c [file] [log] [blame]
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +01001package main
2
3import (
4 "context"
5 "flag"
Serge Bazanskic1515cb2018-10-04 10:36:29 +01006 "fmt"
Serge Bazanskic1515cb2018-10-04 10:36:29 +01007 "reflect"
8 "strconv"
9 "strings"
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010010
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010011 "github.com/golang/glog"
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010012 "github.com/ziutek/telnet"
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010013 "google.golang.org/grpc/codes"
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010014 "google.golang.org/grpc/status"
15
Serge Bazanski97b5cd72023-07-28 17:14:50 +000016 "code.hackerspace.pl/hscloud/dc/m6220-proxy/cli"
Sergiusz Bazanski61594bb2019-07-21 15:20:51 +020017 pb "code.hackerspace.pl/hscloud/dc/m6220-proxy/proto"
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +020018 dpb "code.hackerspace.pl/hscloud/dc/proto"
Serge Bazanski97b5cd72023-07-28 17:14:50 +000019 "code.hackerspace.pl/hscloud/go/mirko"
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010020)
21
22var (
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010023 flagSwitchAddress string
24 flagSwitchUsername string
25 flagSwitchPassword string
26)
27
28func init() {
29 flag.Set("logtostderr", "true")
30}
31
32type service struct {
33 connectionSemaphore chan int
34}
35
Serge Bazanski97b5cd72023-07-28 17:14:50 +000036func (s *service) connect() (*cli.Client, error) {
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010037 s.connectionSemaphore <- 1
38 conn, err := telnet.Dial("tcp", flagSwitchAddress)
39 if err != nil {
40 <-s.connectionSemaphore
41 return nil, err
42 }
43
Serge Bazanski97b5cd72023-07-28 17:14:50 +000044 cli := cli.NewClient(conn, flagSwitchUsername, flagSwitchPassword)
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010045 return cli, nil
46}
47
48func (s *service) disconnect() {
49 <-s.connectionSemaphore
50}
51
52func (s *service) RunCommand(ctx context.Context, req *pb.RunCommandRequest) (*pb.RunCommandResponse, error) {
53 if req.Command == "" {
54 return nil, status.Error(codes.InvalidArgument, "command cannot be null")
55 }
56
57 cli, err := s.connect()
58 if err != nil {
Serge Bazanskic1515cb2018-10-04 10:36:29 +010059 return nil, status.Error(codes.Unavailable, "could not connect to switch")
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010060 }
61 defer s.disconnect()
62
Serge Bazanski97b5cd72023-07-28 17:14:50 +000063 lines, effective, err := cli.RunCommand(ctx, req.Command)
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +010064 if err != nil {
65 return nil, err
66 }
67 res := &pb.RunCommandResponse{
68 EffectiveCommand: effective,
69 Lines: lines,
70 }
71 return res, nil
72}
73
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +020074func (s *service) parseInterfaceStatus(res *dpb.GetPortsResponse, lines []string) error {
Serge Bazanskic1515cb2018-10-04 10:36:29 +010075 if len(lines) < 4 {
76 return fmt.Errorf("need at least 4 lines of output, got %d", len(lines))
77 }
78 if lines[0] != "" {
79 return fmt.Errorf("expected first line to be empty, is %q", lines[0])
80 }
81 header1parts := strings.Fields(lines[1])
82 if want := []string{"Port", "Description", "Duplex", "Speed", "Neg", "Link", "Flow", "Control"}; !reflect.DeepEqual(want, header1parts) {
83 return fmt.Errorf("expected header1 to be %v, got %v", want, header1parts)
84 }
85
86 header2parts := strings.Fields(lines[2])
87 if want := []string{"State", "Status"}; !reflect.DeepEqual(want, header2parts) {
88 return fmt.Errorf("expected header2 to be %v, got %v", want, header2parts)
89 }
90
91 if lines[3][0] != '-' {
92 return fmt.Errorf("expected header3 to start with -, got %q", lines[3])
93 }
94
95 for _, line := range lines[4:] {
96 parts := strings.Fields(line)
97 if len(parts) < 6 {
98 break
99 }
100 portName := parts[0]
101 if strings.HasPrefix(portName, "Gi") && strings.HasPrefix(portName, "Ti") {
102 break
103 }
104
105 speedStr := parts[len(parts)-4]
106 stateStr := parts[len(parts)-2]
107
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200108 port := &dpb.SwitchPort{
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100109 Name: portName,
110 }
111 if speedStr == "100" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200112 port.Speed = dpb.SwitchPort_SPEED_100M
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100113 } else if speedStr == "1000" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200114 port.Speed = dpb.SwitchPort_SPEED_1G
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100115 } else if speedStr == "10000" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200116 port.Speed = dpb.SwitchPort_SPEED_10G
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100117 }
118 if stateStr == "Up" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200119 port.LinkState = dpb.SwitchPort_LINKSTATE_UP
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100120 } else if stateStr == "Down" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200121 port.LinkState = dpb.SwitchPort_LINKSTATE_DOWN
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100122 }
123
124 res.Ports = append(res.Ports, port)
125 }
126
127 return nil
128}
129
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200130func (s *service) parseInterfaceConfig(port *dpb.SwitchPort, lines []string) error {
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100131 glog.Infof("%+v", port)
132 for _, line := range lines {
133 glog.Infof("%s: %q", port.Name, line)
134 parts := strings.Fields(line)
135 if len(parts) < 1 {
136 continue
137 }
138
139 if len(parts) >= 2 && parts[0] == "switchport" {
140 if parts[1] == "mode" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200141 if port.PortMode != dpb.SwitchPort_PORTMODE_INVALID {
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100142 return fmt.Errorf("redefinition of switchport mode")
143 }
144 if parts[2] == "access" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200145 port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100146 } else if parts[2] == "trunk" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200147 port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100148 } else if parts[2] == "general" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200149 port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100150 } else {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200151 port.PortMode = dpb.SwitchPort_PORTMODE_MANGLED
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100152 }
153 }
154
155 if parts[1] == "access" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200156 if port.PortMode == dpb.SwitchPort_PORTMODE_INVALID {
157 port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100158 }
159 if len(parts) > 3 && parts[2] == "vlan" {
160 vlan, err := strconv.Atoi(parts[3])
161 if err != nil {
162 return fmt.Errorf("invalid vlan: %q", parts[3])
163 }
164 port.VlanNative = int32(vlan)
165 }
166 }
167
168 if parts[1] == "trunk" {
169 if len(parts) >= 5 && parts[2] == "allowed" && parts[3] == "vlan" {
170 vlans := strings.Split(parts[4], ",")
171 for _, vlan := range vlans {
172 vlanNum, err := strconv.Atoi(vlan)
173 if err != nil {
174 return fmt.Errorf("invalid vlan: %q", parts[3])
175 }
176 port.VlanTagged = append(port.VlanTagged, int32(vlanNum))
177 }
178 }
179 }
180 } else if len(parts) >= 2 && parts[0] == "mtu" {
181 mtu, err := strconv.Atoi(parts[1])
182 if err != nil {
183 return fmt.Errorf("invalid mtu: %q", parts[3])
184 }
185 port.Mtu = int32(mtu)
186 } else if len(parts) >= 2 && parts[0] == "spanning-tree" && parts[1] == "portfast" {
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200187 port.SpanningTreeMode = dpb.SwitchPort_SPANNING_TREE_MODE_PORTFAST
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100188 }
189 }
190
191 // no mode -> access
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200192 if port.PortMode == dpb.SwitchPort_PORTMODE_INVALID {
193 port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100194 }
195
196 // apply defaults
197 if port.Mtu == 0 {
198 port.Mtu = 1500
199 }
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200200 if port.SpanningTreeMode == dpb.SwitchPort_SPANNING_TREE_MODE_INVALID {
201 port.SpanningTreeMode = dpb.SwitchPort_SPANNING_TREE_MODE_AUTO_PORTFAST
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100202 }
203
204 // sanitize
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200205 if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED {
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100206 port.VlanTagged = []int32{}
207 port.Prefixes = []string{}
208 if port.VlanNative == 0 {
209 port.VlanNative = 1
210 }
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200211 } else if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED {
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100212 port.VlanNative = 0
213 port.Prefixes = []string{}
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200214 } else if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC {
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100215 port.Prefixes = []string{}
216 if port.VlanNative == 0 {
217 port.VlanNative = 1
218 }
219 }
220 return nil
221}
222
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200223func (s *service) GetPorts(ctx context.Context, req *dpb.GetPortsRequest) (*dpb.GetPortsResponse, error) {
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100224 cli, err := s.connect()
225 if err != nil {
226 return nil, status.Error(codes.Unavailable, "could not connect to switch")
227 }
228 defer s.disconnect()
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200229 res := &dpb.GetPortsResponse{}
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100230
Serge Bazanski97b5cd72023-07-28 17:14:50 +0000231 statusLines, _, err := cli.RunCommand(ctx, "show interface status")
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100232 if err != nil {
233 return nil, status.Error(codes.Unavailable, "could not get interface status from switch")
234 }
235
236 err = s.parseInterfaceStatus(res, statusLines)
237 if err != nil {
238 return nil, status.Errorf(codes.Unavailable, "could not parse interface status from switch: %v", err)
239 }
240
241 for _, port := range res.Ports {
Serge Bazanski97b5cd72023-07-28 17:14:50 +0000242 configLines, _, err := cli.RunCommand(ctx, "show run interface "+port.Name)
Serge Bazanskic1515cb2018-10-04 10:36:29 +0100243 if err != nil {
244 return nil, status.Error(codes.Unavailable, "could not get interface config from switch")
245 }
246 err = s.parseInterfaceConfig(port, configLines)
247 if err != nil {
248 return nil, status.Errorf(codes.Unavailable, "could not parse interface config from switch: %v", err)
249 }
250 }
251
252 return res, nil
253}
254
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +0100255func main() {
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +0100256 flag.StringVar(&flagSwitchAddress, "switch_address", "127.0.0.1:23", "Telnet address of M6220")
257 flag.StringVar(&flagSwitchUsername, "switch_username", "admin", "Switch login username")
258 flag.StringVar(&flagSwitchPassword, "switch_password", "admin", "Switch login password")
259 flag.Parse()
260
261 s := &service{
262 connectionSemaphore: make(chan int, 1),
263 }
264
Serge Bazanski0f04bfa2018-10-14 08:47:38 -0700265 m := mirko.New()
266 if err := m.Listen(); err != nil {
267 glog.Exitf("Listen(): %v", err)
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +0100268 }
269
Serge Bazanski0f04bfa2018-10-14 08:47:38 -0700270 pb.RegisterM6220ProxyServer(m.GRPC(), s)
Sergiusz Bazanski5b5b7c32019-07-21 16:07:49 +0200271 dpb.RegisterSwitchControlServer(m.GRPC(), s)
Serge Bazanski0f04bfa2018-10-14 08:47:38 -0700272
273 if err := m.Serve(); err != nil {
274 glog.Exitf("Serve(): %v", err)
275 }
Sergiusz Bazanskiff5af692018-08-29 19:20:46 +0100276
277 select {}
278}