go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/dnsplugin/vppcalls/vpp2210/dns.go (about)

     1  // Copyright (c) 2022 Pantheon.tech
     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 vpp2210
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	"github.com/go-errors/errors"
    22  	vpp_dns "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/dns"
    23  )
    24  
    25  // EnableDNS make act VPP as DNS cache server
    26  func (h *DNSVppHandler) EnableDNS() error {
    27  	h.log.Debug("Enabling DNS functionality of VPP")
    28  	if err := h.enableDisableDNS(true); err != nil {
    29  		return err
    30  	}
    31  	h.log.Debug("DNS functionality of VPP enabled.")
    32  	return nil
    33  }
    34  
    35  // DisableDNS disables functionality that makes VPP act as DNS cache server
    36  func (h *DNSVppHandler) DisableDNS() error {
    37  	h.log.Debug("Disabling DNS functionality of VPP")
    38  	if err := h.enableDisableDNS(false); err != nil {
    39  		return err
    40  	}
    41  	h.log.Debug("DNS functionality of VPP disabled.")
    42  	return nil
    43  }
    44  
    45  func (h *DNSVppHandler) enableDisableDNS(enable bool) error {
    46  	req := &vpp_dns.DNSEnableDisable{}
    47  	if enable {
    48  		req.Enable = 1
    49  	}
    50  	reply := &vpp_dns.DNSEnableDisableReply{}
    51  
    52  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    53  		return err
    54  	}
    55  	if reply.Retval != 0 {
    56  		return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
    57  	}
    58  	return nil
    59  }
    60  
    61  // AddUpstreamDNSServer adds new upstream DNS Server to the upstream DNS server list
    62  func (h *DNSVppHandler) AddUpstreamDNSServer(serverIPAddress net.IP) error {
    63  	h.log.Debug("Adding upstream DNS server with IP %s", serverIPAddress)
    64  	if err := h.addRemoveUpstreamDNSServer(true, serverIPAddress); err != nil {
    65  		return err
    66  	}
    67  	h.log.Debug("Upstream DNS server with IP %s was added", serverIPAddress)
    68  	return nil
    69  }
    70  
    71  // DeleteUpstreamDNSServer removes upstream DNS Server from the upstream DNS server list
    72  func (h *DNSVppHandler) DeleteUpstreamDNSServer(serverIPAddress net.IP) error {
    73  	h.log.Debug("Removing upstream DNS server with IP %s", serverIPAddress)
    74  	if err := h.addRemoveUpstreamDNSServer(false, serverIPAddress); err != nil {
    75  		return err
    76  	}
    77  	h.log.Debug("Upstream DNS server with IP %s was removed", serverIPAddress)
    78  	return nil
    79  }
    80  
    81  func (h *DNSVppHandler) addRemoveUpstreamDNSServer(addition bool, serverIPAddress net.IP) error {
    82  	if serverIPAddress == nil {
    83  		return errors.New("upstream DNS server IP address can't be nil")
    84  	}
    85  	req := &vpp_dns.DNSNameServerAddDel{
    86  		IsAdd: boolToUint(addition),
    87  	}
    88  	if serverIPAddress.To4() == nil { // IPv6
    89  		req.IsIP6 = 1
    90  		req.ServerAddress = serverIPAddress.To16()
    91  	} else {
    92  		req.ServerAddress = serverIPAddress.To4()
    93  	}
    94  	reply := &vpp_dns.DNSNameServerAddDelReply{}
    95  
    96  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    97  		return err
    98  	}
    99  	if reply.Retval != 0 {
   100  		return fmt.Errorf("vpp call %q returned: %d", reply.GetMessageName(), reply.Retval)
   101  	}
   102  	return nil
   103  }
   104  
   105  func boolToUint(input bool) uint8 {
   106  	if input {
   107  		return uint8(1)
   108  	}
   109  	return uint8(0)
   110  }