go.ligato.io/vpp-agent/v3@v3.5.0/tests/integration/vpp/100_gtpu_test.go (about)

     1  //  Copyright (c) 2021 EMnify
     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 vpp
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"go.ligato.io/cn-infra/v2/logging/logrus"
    22  
    23  	ifplugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    24  	interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    25  
    26  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin"
    27  )
    28  
    29  func TestGtpu(t *testing.T) {
    30  	ctx := setupVPP(t)
    31  	defer ctx.teardownVPP()
    32  
    33  	h := ifplugin_vppcalls.CompatibleInterfaceVppHandler(ctx.vppClient, logrus.NewLogger("test"))
    34  
    35  	tests := []struct {
    36  		name           string
    37  		gtpu           *interfaces.GtpuLink
    38  		mcastSwIfIndex uint32
    39  		isFail         bool
    40  	}{
    41  		{
    42  			name: "Create GTP-U tunnel (IP4)",
    43  			gtpu: &interfaces.GtpuLink{
    44  				SrcAddr:    "20.30.40.50",
    45  				DstAddr:    "50.40.30.20",
    46  				Teid:       101,
    47  				EncapVrfId: 0,
    48  			},
    49  			mcastSwIfIndex: 0xFFFFFFFF,
    50  			isFail:         false,
    51  		},
    52  		{
    53  			name: "Create GTP-U tunnel (IP6)",
    54  			gtpu: &interfaces.GtpuLink{
    55  				SrcAddr:    "2001:db8:0:1:1:1:1:1",
    56  				DstAddr:    "2002:db8:0:1:1:1:1:1",
    57  				Teid:       102,
    58  				EncapVrfId: 0,
    59  			},
    60  			mcastSwIfIndex: 0xFFFFFFFF,
    61  			isFail:         false,
    62  		},
    63  		{
    64  			name: "Create GTP-U tunnel (DecapNext: L2)",
    65  			gtpu: &interfaces.GtpuLink{
    66  				SrcAddr:    "20.30.40.50",
    67  				DstAddr:    "50.40.30.20",
    68  				Teid:       201,
    69  				EncapVrfId: 0,
    70  				//DecapNext:     interfaces.GtpuLink_L2,
    71  				DecapNextNode: 1,
    72  			},
    73  			mcastSwIfIndex: 0xFFFFFFFF,
    74  			isFail:         false,
    75  		},
    76  		{
    77  			name: "Create GTP-U tunnel (DecapNext: IP4)",
    78  			gtpu: &interfaces.GtpuLink{
    79  				SrcAddr:    "20.30.40.50",
    80  				DstAddr:    "50.40.30.20",
    81  				Teid:       202,
    82  				EncapVrfId: 0,
    83  				//DecapNext:     interfaces.GtpuLink_IP4,
    84  				DecapNextNode: 2,
    85  			},
    86  			mcastSwIfIndex: 0xFFFFFFFF,
    87  			isFail:         false,
    88  		},
    89  		{
    90  			name: "Create GTP-U tunnel (DecapNext: IP6)",
    91  			gtpu: &interfaces.GtpuLink{
    92  				SrcAddr:    "2001:db8:0:1:1:1:1:1",
    93  				DstAddr:    "2002:db8:0:1:1:1:1:1",
    94  				Teid:       203,
    95  				EncapVrfId: 0,
    96  				//DecapNext:     interfaces.GtpuLink_IP6,
    97  				DecapNextNode: 3,
    98  			},
    99  			mcastSwIfIndex: 0xFFFFFFFF,
   100  			isFail:         false,
   101  		},
   102  		{
   103  			name: "Create GTP-U tunnel with same source and destination",
   104  			gtpu: &interfaces.GtpuLink{
   105  				SrcAddr:    "20.30.40.50",
   106  				DstAddr:    "20.30.40.50",
   107  				Teid:       301,
   108  				EncapVrfId: 0,
   109  			},
   110  			mcastSwIfIndex: 0xFFFFFFFF,
   111  			isFail:         true,
   112  		},
   113  		{
   114  			name: "Create GTP-U tunnel with src and dst ip versions mismatch",
   115  			gtpu: &interfaces.GtpuLink{
   116  				SrcAddr:    "20.30.40.50",
   117  				DstAddr:    "::1",
   118  				Teid:       302,
   119  				EncapVrfId: 0,
   120  			},
   121  			mcastSwIfIndex: 0xFFFFFFFF,
   122  			isFail:         true,
   123  		},
   124  	}
   125  	for i, test := range tests {
   126  		t.Run(test.name, func(t *testing.T) {
   127  			ifName := fmt.Sprintf("test%d", i)
   128  			ifIdx, err := h.AddGtpuTunnel(ifName, test.gtpu, test.mcastSwIfIndex)
   129  			if err != nil {
   130  				if test.isFail {
   131  					return
   132  				}
   133  				t.Fatalf("create GTP-U tunnel failed: %v\n", err)
   134  			} else {
   135  				if test.isFail {
   136  					t.Fatal("create GTP-U tunnel must fail, but it's not")
   137  				}
   138  			}
   139  
   140  			ifaces, err := h.DumpInterfaces(ctx.Ctx)
   141  			if err != nil {
   142  				t.Fatalf("dumping interfaces failed: %v", err)
   143  			}
   144  			iface, ok := ifaces[ifIdx]
   145  			if !ok {
   146  				t.Fatalf("GTP-U interface was not found in dump")
   147  			}
   148  
   149  			if iface.Interface.GetType() != interfaces.Interface_GTPU_TUNNEL {
   150  				t.Fatalf("Interface is not a GTPU tunnel")
   151  			}
   152  
   153  			gtpu := iface.Interface.GetGtpu()
   154  			if test.gtpu.SrcAddr != gtpu.SrcAddr {
   155  				t.Fatalf("expected source address <%s>, got: <%s>", test.gtpu.SrcAddr, gtpu.SrcAddr)
   156  			}
   157  			if test.gtpu.DstAddr != gtpu.DstAddr {
   158  				t.Fatalf("expected destination address <%s>, got: <%s>", test.gtpu.DstAddr, gtpu.DstAddr)
   159  			}
   160  			if test.gtpu.Teid != gtpu.Teid {
   161  				t.Fatalf("expected TEID <%d>, got: <%d>", test.gtpu.Teid, gtpu.Teid)
   162  			}
   163  			if test.gtpu.Multicast != gtpu.Multicast {
   164  				t.Fatalf("expected multicast interface name <%s>, got: <%s>", test.gtpu.Multicast, gtpu.Multicast)
   165  			}
   166  			if test.gtpu.EncapVrfId != gtpu.EncapVrfId {
   167  				t.Fatalf("expected GTP-U EncapVrfId <%d>, got: <%d>", test.gtpu.EncapVrfId, gtpu.EncapVrfId)
   168  			}
   169  			testDecapNext := test.gtpu.DecapNextNode
   170  			if testDecapNext == uint32(interfaces.GtpuLink_DEFAULT) {
   171  				testDecapNext = uint32(interfaces.GtpuLink_L2)
   172  			}
   173  			if testDecapNext != gtpu.DecapNextNode {
   174  				t.Fatalf("expected GTP-U DecapNextNode <%v>, got: <%v>", testDecapNext, gtpu.DecapNextNode)
   175  			}
   176  
   177  			err = h.DelGtpuTunnel(ifName, test.gtpu)
   178  			if err != nil {
   179  				t.Fatalf("delete GTP-U tunnel failed: %v\n", err)
   180  			}
   181  
   182  			ifaces, err = h.DumpInterfaces(ctx.Ctx)
   183  			if err != nil {
   184  				t.Fatalf("dumping interfaces failed: %v", err)
   185  			}
   186  
   187  			if _, ok := ifaces[ifIdx]; ok {
   188  				t.Fatalf("GTP-U interface was found in dump after removing")
   189  			}
   190  		})
   191  	}
   192  }