otg

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package otg provides an API to generate traffic using Open Traffic Generator.

The functions below are thin wrappers around the Gosnappi library provided by the Open Traffic Generator project. See the godoc Example for a demonstration of how to use the API.

Example

Example demonstrates how to use the OTG API.

package main

import (
	"testing"
	"time"

	"github.com/open-traffic-generator/snappi/gosnappi"
	"github.com/openconfig/ondatra"
	"github.com/openconfig/ondatra/gnmi"
	"github.com/openconfig/ondatra/gnmi/otg"
)

func main() {
	t := new(testing.T) // In a real test, *testing.T would be a parameter.

	const (
		eth1Name   = "intf1.eth"
		eth2Name   = "intf2.eth"
		ipv41Name  = "intf1.ipv4"
		ipv42Name  = "intf2.ipv4"
		ateSrcMAC  = "02:00:01:01:01:01"
		ateDstMAC  = "02:00:02:01:01:01"
		ateSrcIP   = "192.0.2.2"
		ateDstIP   = "192.0.2.6"
		ateSrcGway = "192.0.2.1"
		ateDstGway = "192.0.2.5"
	)

	ate := ondatra.ATE(t, "ate")
	ap1 := ate.Port(t, "port1")
	ap2 := ate.Port(t, "port2")

	// Create an empty OTG config and add the ports.
	cfg := gosnappi.NewConfig()
	cfg.Ports().Add().SetName(ap1.ID())
	cfg.Ports().Add().SetName(ap2.ID())

	// Configure an interface on port 1.
	intf1 := cfg.Devices().Add().SetName("intf1")
	eth1 := intf1.Ethernets().Add().
		SetName(eth1Name).
		SetMac(ateSrcMAC)
	eth1.Connection().SetPortName(ap1.ID())
	eth1.Ipv4Addresses().Add().
		SetName(ipv41Name).
		SetAddress(ateSrcIP).
		SetPrefix(30).
		SetGateway(ateSrcGway)

	// Configure an interface on port 2.
	intf2 := cfg.Devices().Add().SetName("intf2")
	eth2 := intf2.Ethernets().Add().
		SetName(eth2Name).
		SetMac(ateDstMAC)
	eth2.Connection().SetPortName(ap2.ID())
	eth2.Ipv4Addresses().Add().
		SetName(ipv42Name).
		SetAddress(ateDstIP).
		SetPrefix(30).
		SetGateway(ateDstGway)

	// Setup a traffic flow.
	flow := cfg.Flows().Add().SetName("flow1")
	flow.TxRx().Device().
		SetTxNames([]string{ipv41Name}).
		SetRxNames([]string{ipv42Name})
	flow.Metrics().SetEnable(true)
	eth := flow.Packet().Add().Ethernet()
	eth.Src().SetValue(ateSrcMAC)
	ipv4 := flow.Packet().Add().Ipv4()
	ipv4.Src().SetValue(ateSrcIP)
	ipv4.Dst().SetValue("20.21.21.21")

	// Push config and start control-plane protocols.
	ate.OTG().PushConfig(t, cfg)
	ate.OTG().StartProtocols(t)

	// Use gNMI to wait for the configured ports to be up.
	gnmi.Await(t, ate.OTG(), gnmi.OTG().Port(ap1.ID()).Link().State(), time.Minute, otg.Port_Link_UP)
	gnmi.Await(t, ate.OTG(), gnmi.OTG().Port(ap2.ID()).Link().State(), time.Minute, otg.Port_Link_UP)

	// Run data-plane traffic and then stop traffic and protocols.
	ate.OTG().StartTraffic(t)
	time.Sleep(30 * time.Second)
	ate.OTG().StopTraffic(t)
	ate.OTG().StopProtocols(t)

	// Gather flow statistics and assert that there was no traffic loss.
	flowMetrics := gnmi.Get(t, ate.OTG(), gnmi.OTG().Flow(flow.Name()).Counters().State())
	outPkts := flowMetrics.GetOutPkts()
	inPkts := flowMetrics.GetInPkts()
	lossPct := 100 * float64(outPkts-inPkts) / float64(outPkts)
	if lossPct < 0 || lossPct > 1 {
		t.Errorf("Unexpected loss percentage for traffic flow: %f", lossPct)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OTG

type OTG struct {
	// contains filtered or unexported fields
}

OTG provides the Open Traffic Generator API to an ATE. Tests should not construct this directly; call ate.OTG() instead.

func New

func New(ate binding.ATE) *OTG

New constructs a new OTG instance. Tests should not call this directly; call ate.OTG() instead.

func (*OTG) FetchConfig

func (o *OTG) FetchConfig(t testing.TB) gosnappi.Config

FetchConfig gets the current config. Deprecated: Use GetConfig instead.

func (*OTG) GNMIOpts

func (o *OTG) GNMIOpts() *gnmi.Opts

GNMIOpts returns a new set of options to customize gNMI queries.

func (*OTG) GetCapture added in v0.1.12

func (o *OTG) GetCapture(t testing.TB, req gosnappi.CaptureRequest) []byte

GetCapture gets the results of a port capture.

func (*OTG) GetConfig added in v0.1.12

func (o *OTG) GetConfig(t testing.TB) gosnappi.Config

GetConfig gets the current config.

func (*OTG) PushConfig

func (o *OTG) PushConfig(t testing.TB, cfg gosnappi.Config)

PushConfig pushes config to the ATE.

func (*OTG) SetControlAction added in v0.1.11

func (o *OTG) SetControlAction(t testing.TB, action gosnappi.ControlAction)

SetControlAction triggers actions against configured resources.

func (*OTG) SetControlState added in v0.1.11

func (o *OTG) SetControlState(t testing.TB, state gosnappi.ControlState)

SetControlState sets the operational state of configured resources.

func (*OTG) StartProtocols

func (o *OTG) StartProtocols(t testing.TB)

StartProtocols starts protocols on the ATE.

func (*OTG) StartTraffic

func (o *OTG) StartTraffic(t testing.TB)

StartTraffic starts traffic on the ATE.

func (*OTG) StopProtocols

func (o *OTG) StopProtocols(t testing.TB)

StopProtocols stops protocols on the ATE.

func (*OTG) StopTraffic

func (o *OTG) StopTraffic(t testing.TB)

StopTraffic stops traffic on the ATE.

func (*OTG) String

func (o *OTG) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL