go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/puntplugin/vppcalls/vpp2202/vppcalls_handler.go (about)

     1  //  Copyright (c) 2022 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 vpp2202
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	govppapi "go.fd.io/govpp/api"
    22  	"go.ligato.io/cn-infra/v2/logging"
    23  
    24  	vpp2202 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202"
    25  	vpp_ip "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip"
    26  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip_types"
    27  	vpp_punt "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/punt"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    29  	"go.ligato.io/vpp-agent/v3/plugins/vpp/puntplugin/vppcalls"
    30  )
    31  
    32  func init() {
    33  	var msgs []govppapi.Message
    34  	msgs = append(msgs, vpp_ip.AllMessages()...)
    35  	msgs = append(msgs, vpp_punt.AllMessages()...)
    36  
    37  	vppcalls.AddHandlerVersion(vpp2202.Version, msgs, NewPuntVppHandler)
    38  }
    39  
    40  // PuntVppHandler is accessor for punt-related vppcalls methods.
    41  type PuntVppHandler struct {
    42  	callsChannel govppapi.Channel
    43  	ifIndexes    ifaceidx.IfaceMetadataIndex
    44  	log          logging.Logger
    45  }
    46  
    47  // NewPuntVppHandler creates new instance of punt vppcalls handler
    48  func NewPuntVppHandler(
    49  	callsChan govppapi.Channel, ifIndexes ifaceidx.IfaceMetadataIndex, log logging.Logger,
    50  ) vppcalls.PuntVppAPI {
    51  	return &PuntVppHandler{
    52  		callsChannel: callsChan,
    53  		ifIndexes:    ifIndexes,
    54  		log:          log,
    55  	}
    56  }
    57  
    58  func ipToAddress(ipstr string) (addr ip_types.Address, err error) {
    59  	netIP := net.ParseIP(ipstr)
    60  	if netIP == nil {
    61  		return ip_types.Address{}, fmt.Errorf("invalid IP: %q", ipstr)
    62  	}
    63  	if ip4 := netIP.To4(); ip4 == nil {
    64  		addr.Af = ip_types.ADDRESS_IP6
    65  		var ip6addr ip_types.IP6Address
    66  		copy(ip6addr[:], netIP.To16())
    67  		addr.Un.SetIP6(ip6addr)
    68  	} else {
    69  		addr.Af = ip_types.ADDRESS_IP4
    70  		var ip4addr ip_types.IP4Address
    71  		copy(ip4addr[:], ip4)
    72  		addr.Un.SetIP4(ip4addr)
    73  	}
    74  	return
    75  }