Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 6 | "fmt" |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 7 | "reflect" |
| 8 | "strconv" |
| 9 | "strings" |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 10 | |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 11 | "github.com/golang/glog" |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 12 | "github.com/ziutek/telnet" |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 13 | "google.golang.org/grpc/codes" |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 14 | "google.golang.org/grpc/status" |
| 15 | |
Serge Bazanski | 97b5cd7 | 2023-07-28 17:14:50 +0000 | [diff] [blame^] | 16 | "code.hackerspace.pl/hscloud/dc/m6220-proxy/cli" |
Sergiusz Bazanski | 61594bb | 2019-07-21 15:20:51 +0200 | [diff] [blame] | 17 | pb "code.hackerspace.pl/hscloud/dc/m6220-proxy/proto" |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 18 | dpb "code.hackerspace.pl/hscloud/dc/proto" |
Serge Bazanski | 97b5cd7 | 2023-07-28 17:14:50 +0000 | [diff] [blame^] | 19 | "code.hackerspace.pl/hscloud/go/mirko" |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | var ( |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 23 | flagSwitchAddress string |
| 24 | flagSwitchUsername string |
| 25 | flagSwitchPassword string |
| 26 | ) |
| 27 | |
| 28 | func init() { |
| 29 | flag.Set("logtostderr", "true") |
| 30 | } |
| 31 | |
| 32 | type service struct { |
| 33 | connectionSemaphore chan int |
| 34 | } |
| 35 | |
Serge Bazanski | 97b5cd7 | 2023-07-28 17:14:50 +0000 | [diff] [blame^] | 36 | func (s *service) connect() (*cli.Client, error) { |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 37 | s.connectionSemaphore <- 1 |
| 38 | conn, err := telnet.Dial("tcp", flagSwitchAddress) |
| 39 | if err != nil { |
| 40 | <-s.connectionSemaphore |
| 41 | return nil, err |
| 42 | } |
| 43 | |
Serge Bazanski | 97b5cd7 | 2023-07-28 17:14:50 +0000 | [diff] [blame^] | 44 | cli := cli.NewClient(conn, flagSwitchUsername, flagSwitchPassword) |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 45 | return cli, nil |
| 46 | } |
| 47 | |
| 48 | func (s *service) disconnect() { |
| 49 | <-s.connectionSemaphore |
| 50 | } |
| 51 | |
| 52 | func (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 Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 59 | return nil, status.Error(codes.Unavailable, "could not connect to switch") |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 60 | } |
| 61 | defer s.disconnect() |
| 62 | |
Serge Bazanski | 97b5cd7 | 2023-07-28 17:14:50 +0000 | [diff] [blame^] | 63 | lines, effective, err := cli.RunCommand(ctx, req.Command) |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 64 | 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 Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 74 | func (s *service) parseInterfaceStatus(res *dpb.GetPortsResponse, lines []string) error { |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 75 | 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 Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 108 | port := &dpb.SwitchPort{ |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 109 | Name: portName, |
| 110 | } |
| 111 | if speedStr == "100" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 112 | port.Speed = dpb.SwitchPort_SPEED_100M |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 113 | } else if speedStr == "1000" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 114 | port.Speed = dpb.SwitchPort_SPEED_1G |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 115 | } else if speedStr == "10000" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 116 | port.Speed = dpb.SwitchPort_SPEED_10G |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 117 | } |
| 118 | if stateStr == "Up" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 119 | port.LinkState = dpb.SwitchPort_LINKSTATE_UP |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 120 | } else if stateStr == "Down" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 121 | port.LinkState = dpb.SwitchPort_LINKSTATE_DOWN |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | res.Ports = append(res.Ports, port) |
| 125 | } |
| 126 | |
| 127 | return nil |
| 128 | } |
| 129 | |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 130 | func (s *service) parseInterfaceConfig(port *dpb.SwitchPort, lines []string) error { |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 131 | 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 Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 141 | if port.PortMode != dpb.SwitchPort_PORTMODE_INVALID { |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 142 | return fmt.Errorf("redefinition of switchport mode") |
| 143 | } |
| 144 | if parts[2] == "access" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 145 | port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 146 | } else if parts[2] == "trunk" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 147 | port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 148 | } else if parts[2] == "general" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 149 | port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 150 | } else { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 151 | port.PortMode = dpb.SwitchPort_PORTMODE_MANGLED |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | if parts[1] == "access" { |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 156 | if port.PortMode == dpb.SwitchPort_PORTMODE_INVALID { |
| 157 | port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 158 | } |
| 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 Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 187 | port.SpanningTreeMode = dpb.SwitchPort_SPANNING_TREE_MODE_PORTFAST |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | // no mode -> access |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 192 | if port.PortMode == dpb.SwitchPort_PORTMODE_INVALID { |
| 193 | port.PortMode = dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // apply defaults |
| 197 | if port.Mtu == 0 { |
| 198 | port.Mtu = 1500 |
| 199 | } |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 200 | if port.SpanningTreeMode == dpb.SwitchPort_SPANNING_TREE_MODE_INVALID { |
| 201 | port.SpanningTreeMode = dpb.SwitchPort_SPANNING_TREE_MODE_AUTO_PORTFAST |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // sanitize |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 205 | if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_UNTAGGED { |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 206 | port.VlanTagged = []int32{} |
| 207 | port.Prefixes = []string{} |
| 208 | if port.VlanNative == 0 { |
| 209 | port.VlanNative = 1 |
| 210 | } |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 211 | } else if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_TAGGED { |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 212 | port.VlanNative = 0 |
| 213 | port.Prefixes = []string{} |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 214 | } else if port.PortMode == dpb.SwitchPort_PORTMODE_SWITCHPORT_GENERIC { |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 215 | port.Prefixes = []string{} |
| 216 | if port.VlanNative == 0 { |
| 217 | port.VlanNative = 1 |
| 218 | } |
| 219 | } |
| 220 | return nil |
| 221 | } |
| 222 | |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 223 | func (s *service) GetPorts(ctx context.Context, req *dpb.GetPortsRequest) (*dpb.GetPortsResponse, error) { |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 224 | 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 Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 229 | res := &dpb.GetPortsResponse{} |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 230 | |
Serge Bazanski | 97b5cd7 | 2023-07-28 17:14:50 +0000 | [diff] [blame^] | 231 | statusLines, _, err := cli.RunCommand(ctx, "show interface status") |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 232 | 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 Bazanski | 97b5cd7 | 2023-07-28 17:14:50 +0000 | [diff] [blame^] | 242 | configLines, _, err := cli.RunCommand(ctx, "show run interface "+port.Name) |
Serge Bazanski | c1515cb | 2018-10-04 10:36:29 +0100 | [diff] [blame] | 243 | 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 Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 255 | func main() { |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 256 | 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 Bazanski | 0f04bfa | 2018-10-14 08:47:38 -0700 | [diff] [blame] | 265 | m := mirko.New() |
| 266 | if err := m.Listen(); err != nil { |
| 267 | glog.Exitf("Listen(): %v", err) |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Serge Bazanski | 0f04bfa | 2018-10-14 08:47:38 -0700 | [diff] [blame] | 270 | pb.RegisterM6220ProxyServer(m.GRPC(), s) |
Sergiusz Bazanski | 5b5b7c3 | 2019-07-21 16:07:49 +0200 | [diff] [blame] | 271 | dpb.RegisterSwitchControlServer(m.GRPC(), s) |
Serge Bazanski | 0f04bfa | 2018-10-14 08:47:38 -0700 | [diff] [blame] | 272 | |
| 273 | if err := m.Serve(); err != nil { |
| 274 | glog.Exitf("Serve(): %v", err) |
| 275 | } |
Sergiusz Bazanski | ff5af69 | 2018-08-29 19:20:46 +0100 | [diff] [blame] | 276 | |
| 277 | select {} |
| 278 | } |