go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/span/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  
    27  	linux_ifplugin "go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin"
    28  	linux_nsplugin "go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin"
    29  	vpp_ifplugin "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin"
    30  	linux_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/linux/interfaces"
    31  	linux_ns "go.ligato.io/vpp-agent/v3/proto/ligato/linux/namespace"
    32  	vpp_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    33  )
    34  
    35  /*
    36  	This example demonstrates usage of SPAN feature from VPPIfPlugin.
    37  */
    38  
    39  func main() {
    40  	// Set inter-dependency between VPP & Linux plugins
    41  	vpp_ifplugin.DefaultPlugin.LinuxIfPlugin = &linux_ifplugin.DefaultPlugin
    42  	vpp_ifplugin.DefaultPlugin.NsPlugin = &linux_nsplugin.DefaultPlugin
    43  	linux_ifplugin.DefaultPlugin.VppIfPlugin = &vpp_ifplugin.DefaultPlugin
    44  
    45  	ep := &ExamplePlugin{
    46  		Orchestrator:  &orchestrator.DefaultPlugin,
    47  		LinuxIfPlugin: &linux_ifplugin.DefaultPlugin,
    48  		VPPIfPlugin:   &vpp_ifplugin.DefaultPlugin,
    49  	}
    50  
    51  	a := agent.NewAgent(
    52  		agent.AllPlugins(ep),
    53  	)
    54  	if err := a.Run(); err != nil {
    55  		log.Fatal(err)
    56  	}
    57  }
    58  
    59  // ExamplePlugin is the main plugin which
    60  // handles resync and changes in this example.
    61  type ExamplePlugin struct {
    62  	LinuxIfPlugin *linux_ifplugin.IfPlugin
    63  	VPPIfPlugin   *vpp_ifplugin.IfPlugin
    64  	Orchestrator  *orchestrator.Plugin
    65  }
    66  
    67  // String returns plugin name
    68  func (p *ExamplePlugin) String() string {
    69  	return "span-example"
    70  }
    71  
    72  // Init handles initialization phase.
    73  func (p *ExamplePlugin) Init() error {
    74  	return nil
    75  }
    76  
    77  // AfterInit handles phase after initialization.
    78  func (p *ExamplePlugin) AfterInit() error {
    79  	go testLocalClientWithScheduler()
    80  	return nil
    81  }
    82  
    83  // Close cleans up the resources.
    84  func (p *ExamplePlugin) Close() error {
    85  	return nil
    86  }
    87  
    88  func testLocalClientWithScheduler() {
    89  	time.Sleep(time.Second * 2)
    90  	fmt.Println("=== RESYNC ===")
    91  
    92  	txn := localclient.DataResyncRequest("span-example")
    93  	err := txn.
    94  		LinuxInterface(hostLinuxTap).
    95  		LinuxInterface(clientLinuxTap).
    96  		VppInterface(hostVPPTap).
    97  		VppInterface(clientVPPTap).
    98  		Span(spanRx).
    99  		Send().ReceiveReply()
   100  	if err != nil {
   101  		fmt.Println(err)
   102  		return
   103  	}
   104  
   105  	time.Sleep(time.Second * 20)
   106  	fmt.Println("=== CHANGE ===")
   107  	txn2 := localclient.DataChangeRequest("span-change")
   108  	err = txn2.Delete().Span(spanRx).Send().ReceiveReply()
   109  	if err != nil {
   110  		fmt.Println(err)
   111  		return
   112  	}
   113  
   114  	time.Sleep(time.Second * 20)
   115  	err = txn2.Put().Span(spanBoth).Send().ReceiveReply()
   116  	if err != nil {
   117  		fmt.Println(err)
   118  		return
   119  	}
   120  
   121  	time.Sleep(time.Second * 20)
   122  	err = txn2.Put().Span(spanRx).Send().ReceiveReply()
   123  	if err != nil {
   124  		fmt.Println(err)
   125  		return
   126  	}
   127  
   128  }
   129  
   130  var (
   131  	/* host <-> VPP */
   132  
   133  	hostLinuxTap = &linux_interfaces.Interface{
   134  		Name:    "linux_span_tap1",
   135  		Type:    linux_interfaces.Interface_TAP_TO_VPP,
   136  		Enabled: true,
   137  		IpAddresses: []string{
   138  			"10.10.1.1/24",
   139  		},
   140  		HostIfName: "linux_span_tap1",
   141  		Link: &linux_interfaces.Interface_Tap{
   142  			Tap: &linux_interfaces.TapLink{
   143  				VppTapIfName: "vpp_span_tap1",
   144  			},
   145  		},
   146  	}
   147  	hostVPPTap = &vpp_interfaces.Interface{
   148  		Name:    "vpp_span_tap1",
   149  		Type:    vpp_interfaces.Interface_TAP,
   150  		Enabled: true,
   151  		IpAddresses: []string{
   152  			"10.10.1.2/24",
   153  		},
   154  		Link: &vpp_interfaces.Interface_Tap{
   155  			Tap: &vpp_interfaces.TapLink{
   156  				Version: 2,
   157  			},
   158  		},
   159  	}
   160  
   161  	/* host <-> VPP */
   162  
   163  	clientLinuxTap = &linux_interfaces.Interface{
   164  		Name:    "linux_span_tap2",
   165  		Type:    linux_interfaces.Interface_TAP_TO_VPP,
   166  		Enabled: true,
   167  		IpAddresses: []string{
   168  			"10.20.1.1/24",
   169  		},
   170  		HostIfName: "linux_span_tap2",
   171  		Link: &linux_interfaces.Interface_Tap{
   172  			Tap: &linux_interfaces.TapLink{
   173  				VppTapIfName: "vpp_span_tap2",
   174  			},
   175  		},
   176  		Namespace: &linux_ns.NetNamespace{
   177  			Type:      linux_ns.NetNamespace_MICROSERVICE,
   178  			Reference: "microservice-client",
   179  		},
   180  	}
   181  	clientVPPTap = &vpp_interfaces.Interface{
   182  		Name:    "vpp_span_tap2",
   183  		Type:    vpp_interfaces.Interface_TAP,
   184  		Enabled: true,
   185  		IpAddresses: []string{
   186  			"10.20.1.2/24",
   187  		},
   188  		Link: &vpp_interfaces.Interface_Tap{
   189  			Tap: &vpp_interfaces.TapLink{
   190  				Version:        2,
   191  				ToMicroservice: "microservice-client",
   192  			},
   193  		},
   194  	}
   195  
   196  	spanRx = &vpp_interfaces.Span{
   197  		InterfaceFrom: "vpp_span_tap1",
   198  		InterfaceTo:   "vpp_span_tap2",
   199  		Direction:     vpp_interfaces.Span_RX,
   200  	}
   201  
   202  	spanBoth = &vpp_interfaces.Span{
   203  		InterfaceFrom: "vpp_span_tap1",
   204  		InterfaceTo:   "vpp_span_tap2",
   205  		Direction:     vpp_interfaces.Span_BOTH,
   206  	}
   207  )