github.com/slackhq/nebula@v1.9.0/overlay/tun.go (about)

     1  package overlay
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/sirupsen/logrus"
     7  	"github.com/slackhq/nebula/config"
     8  	"github.com/slackhq/nebula/util"
     9  )
    10  
    11  const DefaultMTU = 1300
    12  
    13  // TODO: We may be able to remove routines
    14  type DeviceFactory func(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error)
    15  
    16  func NewDeviceFromConfig(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
    17  	switch {
    18  	case c.GetBool("tun.disabled", false):
    19  		tun := newDisabledTun(tunCidr, c.GetInt("tun.tx_queue", 500), c.GetBool("stats.message_metrics", false), l)
    20  		return tun, nil
    21  
    22  	default:
    23  		return newTun(c, l, tunCidr, routines > 1)
    24  	}
    25  }
    26  
    27  func NewFdDeviceFromConfig(fd *int) DeviceFactory {
    28  	return func(c *config.C, l *logrus.Logger, tunCidr *net.IPNet, routines int) (Device, error) {
    29  		return newTunFromFd(c, l, *fd, tunCidr)
    30  	}
    31  }
    32  
    33  func getAllRoutesFromConfig(c *config.C, cidr *net.IPNet, initial bool) (bool, []Route, error) {
    34  	if !initial && !c.HasChanged("tun.routes") && !c.HasChanged("tun.unsafe_routes") {
    35  		return false, nil, nil
    36  	}
    37  
    38  	routes, err := parseRoutes(c, cidr)
    39  	if err != nil {
    40  		return true, nil, util.NewContextualError("Could not parse tun.routes", nil, err)
    41  	}
    42  
    43  	unsafeRoutes, err := parseUnsafeRoutes(c, cidr)
    44  	if err != nil {
    45  		return true, nil, util.NewContextualError("Could not parse tun.unsafe_routes", nil, err)
    46  	}
    47  
    48  	routes = append(routes, unsafeRoutes...)
    49  	return true, routes, nil
    50  }
    51  
    52  // findRemovedRoutes will return all routes that are not present in the newRoutes list and would affect the system route table.
    53  // Via is not used to evaluate since it does not affect the system route table.
    54  func findRemovedRoutes(newRoutes, oldRoutes []Route) []Route {
    55  	var removed []Route
    56  	has := func(entry Route) bool {
    57  		for _, check := range newRoutes {
    58  			if check.Equal(entry) {
    59  				return true
    60  			}
    61  		}
    62  		return false
    63  	}
    64  
    65  	for _, oldEntry := range oldRoutes {
    66  		if !has(oldEntry) {
    67  			removed = append(removed, oldEntry)
    68  		}
    69  	}
    70  
    71  	return removed
    72  }