go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/vpp-l3/main.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 main 16 17 import ( 18 "fmt" 19 "log" 20 "time" 21 22 "go.ligato.io/cn-infra/v2/agent" 23 24 "go.ligato.io/vpp-agent/v3/clientv2/linux/localclient" 25 "go.ligato.io/vpp-agent/v3/plugins/orchestrator" 26 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin" 27 "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin" 28 interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces" 29 l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3" 30 ) 31 32 /* 33 This example demonstrates example with VPP L3Plugin using KVScheduler. 34 */ 35 36 func main() { 37 ep := &ExamplePlugin{ 38 Orchestrator: &orchestrator.DefaultPlugin, 39 VPPIfPlugin: &ifplugin.DefaultPlugin, 40 VPPL3Plugin: &l3plugin.DefaultPlugin, 41 } 42 43 a := agent.NewAgent( 44 agent.AllPlugins(ep), 45 ) 46 if err := a.Run(); err != nil { 47 log.Fatal(err) 48 } 49 } 50 51 // ExamplePlugin is the main plugin which 52 // handles resync and changes in this example. 53 type ExamplePlugin struct { 54 VPPIfPlugin *ifplugin.IfPlugin 55 VPPL3Plugin *l3plugin.L3Plugin 56 Orchestrator *orchestrator.Plugin 57 } 58 59 // String returns plugin name 60 func (p *ExamplePlugin) String() string { 61 return "vpp-l3-example" 62 } 63 64 // Init handles initialization phase. 65 func (p *ExamplePlugin) Init() error { 66 return nil 67 } 68 69 // AfterInit handles phase after initialization. 70 func (p *ExamplePlugin) AfterInit() error { 71 go testLocalClientWithScheduler() 72 return nil 73 } 74 75 // Close cleans up the resources. 76 func (p *ExamplePlugin) Close() error { 77 return nil 78 } 79 80 func testLocalClientWithScheduler() { 81 // initial resync 82 time.Sleep(time.Second * 2) 83 fmt.Println("=== RESYNC ===") 84 85 txn := localclient.DataResyncRequest("example") 86 err := txn. 87 VppInterface(memif0). 88 VppInterface(memif0_10). 89 StaticRoute(route0). 90 StaticRoute(route1). 91 Arp(arp0). 92 ProxyArp(proxyArp). 93 IPScanNeighbor(ipScanNeighbor). 94 Send().ReceiveReply() 95 if err != nil { 96 fmt.Println(err) 97 return 98 } 99 100 // data change 101 time.Sleep(time.Second * 10) 102 fmt.Println("=== CHANGE ===") 103 104 route0.OutgoingInterface = "" 105 arp0.PhysAddress = "22:22:22:22:22:22" 106 proxyArp.Ranges = append(proxyArp.Ranges, &l3.ProxyARP_Range{ 107 FirstIpAddr: "10.10.2.1", LastIpAddr: "10.10.2.255", 108 }) 109 proxyArp.Interfaces = nil 110 111 txn2 := localclient.DataChangeRequest("example") 112 err = txn2. 113 Put(). 114 VppInterface(memif0_10). 115 StaticRoute(route0). 116 Delete(). 117 VppInterface(memif0.Name). 118 StaticRoute(route1.OutgoingInterface, route1.VrfId, route1.DstNetwork, route1.NextHopAddr). 119 Put(). 120 Arp(arp0). 121 ProxyArp(proxyArp). 122 Send().ReceiveReply() 123 if err != nil { 124 fmt.Println(err) 125 return 126 } 127 } 128 129 var ( 130 memif0 = &interfaces.Interface{ 131 Name: "memif0", 132 Enabled: true, 133 Type: interfaces.Interface_MEMIF, 134 IpAddresses: []string{"3.3.0.1/16"}, 135 Link: &interfaces.Interface_Memif{ 136 Memif: &interfaces.MemifLink{ 137 Id: 1, 138 Master: true, 139 Secret: "secret", 140 SocketFilename: "/tmp/memif1.sock", 141 }, 142 }, 143 } 144 memif0_10 = &interfaces.Interface{ 145 Name: "memif0/10", 146 Enabled: true, 147 Type: interfaces.Interface_SUB_INTERFACE, 148 IpAddresses: []string{"3.10.0.10/32"}, 149 Link: &interfaces.Interface_Sub{ 150 Sub: &interfaces.SubInterface{ 151 ParentName: "memif0", 152 SubId: 10, 153 }, 154 }, 155 } 156 route0 = &l3.Route{ 157 DstNetwork: "10.10.1.0/24", 158 OutgoingInterface: "memif0", 159 Weight: 200, 160 } 161 route1 = &l3.Route{ 162 DstNetwork: "2001:DB8::0001/32", 163 OutgoingInterface: "memif0", 164 Weight: 100, 165 } 166 arp0 = &l3.ARPEntry{ 167 Interface: "memif0", 168 PhysAddress: "33:33:33:33:33:33", 169 IpAddress: "3.3.3.3", 170 Static: true, 171 } 172 proxyArp = &l3.ProxyARP{ 173 Ranges: []*l3.ProxyARP_Range{ 174 {FirstIpAddr: "10.10.1.1", LastIpAddr: "10.10.1.255"}, 175 }, 176 Interfaces: []*l3.ProxyARP_Interface{ 177 {Name: "memif0"}, 178 }, 179 } 180 ipScanNeighbor = &l3.IPScanNeighbor{ 181 Mode: l3.IPScanNeighbor_IPV4, 182 ScanInterval: 1, 183 ScanIntDelay: 1, 184 MaxProcTime: 20, 185 MaxUpdate: 0, 186 StaleThreshold: 4, 187 } 188 )