go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/interface_handler_api.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package vppcalls 16 17 import ( 18 "context" 19 "errors" 20 "net" 21 22 "go.ligato.io/cn-infra/v2/logging" 23 24 "go.ligato.io/vpp-agent/v3/plugins/vpp" 25 interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces" 26 ) 27 28 var ( 29 // ErrIPIPUnsupported error is returned if IPIP interface is not supported on given VPP version. 30 ErrIPIPUnsupported = errors.New("IPIP interface not supported") 31 32 // ErrWireguardUnsupported error is returned if Wireguard interface is not supported on given VPP version. 33 ErrWireguardUnsupported = errors.New("Wireguard interface not supported") 34 35 // ErrRdmaUnsupported error is returned if RDMA interface is not supported on given VPP version. 36 ErrRdmaUnsupported = errors.New("RDMA interface not supported") 37 ) 38 39 // InterfaceDetails is the wrapper structure for the interface northbound API structure. 40 type InterfaceDetails struct { 41 Interface *interfaces.Interface `json:"interface"` 42 Meta *InterfaceMeta `json:"interface_meta"` 43 } 44 45 // InterfaceMeta is combination of proto-modelled Interface data and VPP provided metadata 46 type InterfaceMeta struct { 47 SwIfIndex uint32 `json:"sw_if_index"` 48 SupSwIfIndex uint32 `json:"sub_sw_if_index"` 49 L2Address net.HardwareAddr `json:"l2_address"` 50 InternalName string `json:"internal_name"` 51 DevType string `json:"dev_type"` 52 IsAdminStateUp bool `json:"is_admin_state_up"` 53 IsLinkStateUp bool `json:"is_link_state_up"` 54 LinkDuplex uint32 `json:"link_duplex"` 55 LinkMTU uint16 `json:"link_mtu"` 56 MTU []uint32 `json:"mtu"` 57 LinkSpeed uint32 `json:"link_speed"` 58 SubID uint32 `json:"sub_id"` 59 Tag string `json:"tag"` 60 61 // dhcp 62 Dhcp *Dhcp `json:"dhcp"` 63 64 // vrf 65 VrfIPv4 uint32 `json:"vrf_ipv4"` 66 VrfIPv6 uint32 `json:"vrf_ipv6"` 67 68 // wmxnet3 69 Pci uint32 `json:"pci"` 70 } 71 72 // InterfaceEvent represents interface event from VPP. 73 type InterfaceEvent struct { 74 SwIfIndex uint32 75 AdminState uint8 76 LinkState uint8 77 Deleted bool 78 } 79 80 // Dhcp is helper struct for DHCP metadata, split to client and lease (similar to VPP binary API) 81 type Dhcp struct { 82 Client *Client `json:"dhcp_client"` 83 Lease *Lease `json:"dhcp_lease"` 84 } 85 86 // Client is helper struct grouping DHCP client data 87 type Client struct { 88 SwIfIndex uint32 89 Hostname string 90 ID string 91 WantDhcpEvent bool 92 SetBroadcastFlag bool 93 PID uint32 94 } 95 96 // Lease is helper struct grouping DHCP lease data 97 type Lease struct { 98 SwIfIndex uint32 99 State uint8 100 Hostname string 101 IsIPv6 bool 102 MaskWidth uint8 103 HostAddress string 104 RouterAddress string 105 HostMac string 106 } 107 108 // InterfaceState is a helper function grouping interface state data. 109 type InterfaceState struct { 110 SwIfIndex uint32 111 InternalName string 112 PhysAddress net.HardwareAddr 113 114 AdminState interfaces.InterfaceState_Status 115 LinkState interfaces.InterfaceState_Status 116 LinkDuplex interfaces.InterfaceState_Duplex 117 LinkSpeed uint64 118 LinkMTU uint16 119 } 120 121 // InterfaceSpanDetails is a helper struct grouping SPAN data. 122 type InterfaceSpanDetails struct { 123 SwIfIndexFrom uint32 124 SwIfIndexTo uint32 125 Direction uint8 126 IsL2 bool 127 } 128 129 // InterfaceVppAPI provides methods for creating and managing interface plugin 130 type InterfaceVppAPI interface { 131 InterfaceVppRead 132 133 MemifAPI 134 Wmxnet3API 135 IP6ndVppAPI 136 RdmaAPI 137 138 // AddAfPacketInterface calls AfPacketCreate VPP binary API. 139 AddAfPacketInterface(ifName, hwAddr, targetHostIfName string) (swIndex uint32, err error) 140 // DeleteAfPacketInterface calls AfPacketDelete VPP binary API. 141 DeleteAfPacketInterface(ifName string, idx uint32, targetHostIfName string) error 142 143 // AddLoopbackInterface calls CreateLoopback bin API. 144 AddLoopbackInterface(ifName string) (swIndex uint32, err error) 145 // DeleteLoopbackInterface calls DeleteLoopback bin API. 146 DeleteLoopbackInterface(ifName string, idx uint32) error 147 148 // AddTapInterface calls TapConnect bin API. 149 AddTapInterface(ifName string, tapIf *interfaces.TapLink) (swIfIdx uint32, err error) 150 // DeleteTapInterface calls TapDelete bin API. 151 DeleteTapInterface(ifName string, idx uint32, version uint32) error 152 153 // AddVxLanTunnel calls AddDelVxLanTunnelReq with flag add=1. 154 AddVxLanTunnel(ifName string, vrf, multicastIf uint32, vxLan *interfaces.VxlanLink) (swIndex uint32, err error) 155 // DeleteVxLanTunnel calls AddDelVxLanTunnelReq with flag add=0. 156 DeleteVxLanTunnel(ifName string, idx, vrf uint32, vxLan *interfaces.VxlanLink) error 157 158 // AddVxLanGpeTunnel creates VxLAN-GPE tunnel. 159 AddVxLanGpeTunnel(ifName string, vrf, multicastIf uint32, vxLan *interfaces.VxlanLink) (uint32, error) 160 // DeleteVxLanGpeTunnel removes VxLAN-GPE tunnel. 161 DeleteVxLanGpeTunnel(ifName string, vxLan *interfaces.VxlanLink) error 162 163 // AddIPSecTunnelInterface adds a new IPSec tunnel interface 164 AddIPSecTunnelInterface(ctx context.Context, ifName string, ipSecLink *interfaces.IPSecLink) (uint32, error) 165 // DeleteIPSecTunnelInterface removes existing IPSec tunnel interface 166 DeleteIPSecTunnelInterface(ctx context.Context, ifName string, idx uint32, ipSecLink *interfaces.IPSecLink) error 167 168 // AddBondInterface configures bond interface. 169 AddBondInterface(ifName string, mac string, bondLink *interfaces.BondLink) (uint32, error) 170 // DeleteBondInterface removes bond interface. 171 DeleteBondInterface(ifName string, ifIdx uint32) error 172 173 // AddSpan creates new span record 174 AddSpan(ifIdxFrom, ifIdxTo uint32, direction uint8, isL2 bool) error 175 // DelSpan removes new span record 176 DelSpan(ifIdxFrom, ifIdxTo uint32, isL2 bool) error 177 178 // AddGreTunnel adds new GRE interface. 179 AddGreTunnel(ifName string, greLink *interfaces.GreLink) (uint32, error) 180 // DelGreTunnel removes GRE interface. 181 DelGreTunnel(ifName string, greLink *interfaces.GreLink) (uint32, error) 182 183 // AddGtpuTunnel adds new GTPU interface. 184 AddGtpuTunnel(ifName string, gtpuLink *interfaces.GtpuLink, multicastIf uint32) (uint32, error) 185 // DelGtpuTunnel removes GTPU interface. 186 DelGtpuTunnel(ifName string, gtpuLink *interfaces.GtpuLink) error 187 188 // AddIpipTunnel adds new IPIP tunnel interface. 189 AddIpipTunnel(ifName string, vrf uint32, ipipLink *interfaces.IPIPLink) (uint32, error) 190 // DelIpipTunnel removes IPIP tunnel interface. 191 DelIpipTunnel(ifName string, ifIdx uint32) error 192 193 // AddWireguardTunnel adds new wireguard tunnel interface. 194 AddWireguardTunnel(ifName string, wgLink *interfaces.WireguardLink) (uint32, error) 195 // DeleteWireguardTunnel removes wireguard tunnel interface. 196 DeleteWireguardTunnel(ifName string, ifIdx uint32) error 197 198 // CreateSubif creates sub interface. 199 CreateSubif(ifIdx, vlanID uint32) (swIfIdx uint32, err error) 200 // DeleteSubif deletes sub interface. 201 DeleteSubif(ifIdx uint32) error 202 203 // AttachInterfaceToBond adds interface as a slave to the bond interface. 204 AttachInterfaceToBond(ifIdx, bondIfIdx uint32, isPassive, isLongTimeout bool) error 205 // DetachInterfaceFromBond removes interface slave status from any bond interfaces. 206 DetachInterfaceFromBond(ifIdx uint32) error 207 208 // SetVLanTagRewrite sets VLan tag rewrite rule for given sub-interface 209 SetVLanTagRewrite(ifIdx uint32, subIf *interfaces.SubInterface) error 210 211 // InterfaceAdminDown calls binary API SwInterfaceSetFlagsReply with AdminUpDown=0. 212 InterfaceAdminDown(ctx context.Context, ifIdx uint32) error 213 // InterfaceAdminUp calls binary API SwInterfaceSetFlagsReply with AdminUpDown=1. 214 InterfaceAdminUp(ctx context.Context, ifIdx uint32) error 215 // SetInterfaceTag registers new interface index/tag pair 216 SetInterfaceTag(tag string, ifIdx uint32) error 217 // RemoveInterfaceTag un-registers new interface index/tag pair 218 RemoveInterfaceTag(tag string, ifIdx uint32) error 219 // SetInterfaceAsDHCPClient sets provided interface as a DHCP client 220 SetInterfaceAsDHCPClient(ifIdx uint32, hostName string) error 221 // UnsetInterfaceAsDHCPClient un-sets interface as DHCP client 222 UnsetInterfaceAsDHCPClient(ifIdx uint32, hostName string) error 223 // AddContainerIP calls IPContainerProxyAddDel VPP API with IsAdd=1 224 AddContainerIP(ifIdx uint32, addr string) error 225 // DelContainerIP calls IPContainerProxyAddDel VPP API with IsAdd=0 226 DelContainerIP(ifIdx uint32, addr string) error 227 // AddInterfaceIP calls SwInterfaceAddDelAddress bin API with IsAdd=1. 228 AddInterfaceIP(ifIdx uint32, addr *net.IPNet) error 229 // DelInterfaceIP calls SwInterfaceAddDelAddress bin API with IsAdd=00. 230 DelInterfaceIP(ifIdx uint32, addr *net.IPNet) error 231 // SetUnnumberedIP sets interface as un-numbered, linking IP address of the another interface (ifIdxWithIP) 232 SetUnnumberedIP(ctx context.Context, uIfIdx uint32, ifIdxWithIP uint32) error 233 // UnsetUnnumberedIP unset provided interface as un-numbered. IP address of the linked interface is removed 234 UnsetUnnumberedIP(ctx context.Context, uIfIdx uint32) error 235 // SetInterfaceMac calls SwInterfaceSetMacAddress bin API. 236 SetInterfaceMac(ifIdx uint32, macAddress string) error 237 // SetInterfaceMtu calls HwInterfaceSetMtu bin API with desired MTU value. 238 SetInterfaceMtu(ifIdx uint32, mtu uint32) error 239 // SetRxMode calls SwInterfaceSetRxMode bin API 240 SetRxMode(ifIdx uint32, rxMode *interfaces.Interface_RxMode) error 241 // SetRxPlacement configures rx-placement for interface 242 SetRxPlacement(ifIdx uint32, rxPlacement *interfaces.Interface_RxPlacement) error 243 // SetInterfaceVrf sets VRF table for the interface 244 SetInterfaceVrf(ifaceIndex, vrfID uint32) error 245 // SetInterfaceVrfIPv6 sets IPV6 VRF table for the interface 246 SetInterfaceVrfIPv6(ifaceIndex, vrfID uint32) error 247 } 248 249 type MemifAPI interface { 250 // AddMemifInterface calls MemifCreate bin API. 251 AddMemifInterface(ctx context.Context, ifName string, memIface *interfaces.MemifLink, socketID uint32) (swIdx uint32, err error) 252 // DeleteMemifInterface calls MemifDelete bin API. 253 DeleteMemifInterface(ctx context.Context, ifName string, idx uint32) error 254 // RegisterMemifSocketFilename registers new socket file name with provided ID. 255 RegisterMemifSocketFilename(ctx context.Context, filename string, id uint32) error 256 // DumpMemifSocketDetails dumps memif socket details from the VPP 257 DumpMemifSocketDetails(ctx context.Context) (map[string]uint32, error) 258 } 259 260 type Wmxnet3API interface { 261 // AddVmxNet3 configures vmxNet3 interface. Second parameter is optional in this case. 262 AddVmxNet3(ifName string, vmxNet3 *interfaces.VmxNet3Link) (uint32, error) 263 // DeleteVmxNet3 removes vmxNet3 interface 264 DeleteVmxNet3(ifName string, ifIdx uint32) error 265 } 266 267 type RdmaAPI interface { 268 // AddRdmaInterface adds new interface with RDMA driver. 269 AddRdmaInterface(ctx context.Context, ifName string, rdmaLink *interfaces.RDMALink) (swIdx uint32, err error) 270 // DeleteRdmaInterface removes interface with RDMA driver. 271 DeleteRdmaInterface(ctx context.Context, ifName string, ifIdx uint32) error 272 } 273 274 // InterfaceVppRead provides read methods for interface plugin 275 type InterfaceVppRead interface { 276 // DumpInterfaces dumps VPP interface data into the northbound API data structure 277 // map indexed by software interface index. 278 // 279 // LIMITATIONS: 280 // - there is no af_packet dump binary API. We relay on naming conventions of the internal VPP interface names 281 // 282 DumpInterfaces(ctx context.Context) (map[uint32]*InterfaceDetails, error) 283 // DumpInterfacesByType returns all VPP interfaces of the specified type 284 DumpInterfacesByType(ctx context.Context, reqType interfaces.Interface_Type) (map[uint32]*InterfaceDetails, error) 285 // DumpInterfaceStates dumps link and administrative state of every interface. 286 DumpInterfaceStates(ifIdxs ...uint32) (map[uint32]*InterfaceState, error) 287 // DumpSpan returns all records from span table. 288 DumpSpan() ([]*InterfaceSpanDetails, error) 289 // GetInterfaceVrf reads VRF table to interface 290 GetInterfaceVrf(ifIdx uint32) (vrfID uint32, err error) 291 // GetInterfaceVrfIPv6 reads IPv6 VRF table to interface 292 GetInterfaceVrfIPv6(ifIdx uint32) (vrfID uint32, err error) 293 // DumpDhcpClients dumps DHCP-related information for all interfaces. 294 DumpDhcpClients() (map[uint32]*Dhcp, error) 295 // WatchInterfaceEvents starts watching for interface events. 296 WatchInterfaceEvents(ctx context.Context, events chan<- *InterfaceEvent) error 297 // WatchDHCPLeases starts watching for DHCP leases. 298 WatchDHCPLeases(ctx context.Context, leases chan<- *Lease) error 299 } 300 301 // IP6ndVppAPI provides methods for managing IPv6 ND configuration. 302 type IP6ndVppAPI interface { 303 SetIP6ndAutoconfig(ctx context.Context, ifIdx uint32, enable, installDefaultRoutes bool) error 304 } 305 306 var Handler = vpp.RegisterHandler(vpp.HandlerDesc{ 307 Name: "interface", 308 HandlerAPI: (*InterfaceVppAPI)(nil), 309 NewFunc: (*NewHandlerFunc)(nil), 310 }) 311 312 type NewHandlerFunc func(vpp.Client, logging.Logger) InterfaceVppAPI 313 314 // CompatibleInterfaceVppHandler is helper for returning comptabile Handler. 315 func CompatibleInterfaceVppHandler(c vpp.Client, log logging.Logger) InterfaceVppAPI { 316 if v := Handler.FindCompatibleVersion(c); v != nil { 317 return v.NewHandler(c, log).(InterfaceVppAPI) 318 } 319 return nil 320 }