2016-05-25 01:46:18 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-31 02:55:51 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-05-25 01:46:18 -04:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-06-12 20:28:28 -04:00
|
|
|
"path/filepath"
|
2016-05-25 01:46:18 -04:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-06-07 14:50:17 -04:00
|
|
|
"sync"
|
2016-06-05 16:45:52 -04:00
|
|
|
"syscall"
|
2016-05-25 01:46:18 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
|
|
"github.com/docker/libnetwork/iptables"
|
|
|
|
"github.com/docker/libnetwork/ipvs"
|
2016-06-12 20:28:28 -04:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2016-05-31 02:55:51 -04:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2016-05-25 01:46:18 -04:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
|
|
|
"github.com/vishvananda/netns"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reexec.Register("fwmarker", fwMarker)
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
func newService(name string, id string, ingressPorts []*PortConfig) *service {
|
2016-05-25 01:46:18 -04:00
|
|
|
return &service{
|
|
|
|
name: name,
|
|
|
|
id: id,
|
2016-05-31 02:55:51 -04:00
|
|
|
ingressPorts: ingressPorts,
|
2016-05-25 01:46:18 -04:00
|
|
|
loadBalancers: make(map[string]*loadBalancer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 19:40:54 -04:00
|
|
|
func (c *controller) addServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, aliases []string, ip net.IP) error {
|
2016-05-25 01:46:18 -04:00
|
|
|
var (
|
|
|
|
s *service
|
|
|
|
addService bool
|
|
|
|
)
|
|
|
|
|
|
|
|
n, err := c.NetworkByID(nid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:37:14 -04:00
|
|
|
skey := serviceKey{
|
|
|
|
id: sid,
|
|
|
|
ports: portConfigs(ingressPorts).String(),
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
c.Lock()
|
2016-06-24 19:37:14 -04:00
|
|
|
s, ok := c.serviceBindings[skey]
|
2016-05-25 01:46:18 -04:00
|
|
|
if !ok {
|
|
|
|
// Create a new service if we are seeing this service
|
|
|
|
// for the first time.
|
2016-05-31 02:55:51 -04:00
|
|
|
s = newService(name, sid, ingressPorts)
|
2016-06-24 19:37:14 -04:00
|
|
|
c.serviceBindings[skey] = s
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
2016-06-07 20:04:51 -04:00
|
|
|
// Add endpoint IP to special "tasks.svc_name" so that the
|
|
|
|
// applications have access to DNS RR.
|
|
|
|
n.(*network).addSvcRecords("tasks."+name, ip, nil, false)
|
2016-06-14 19:40:54 -04:00
|
|
|
for _, alias := range aliases {
|
|
|
|
n.(*network).addSvcRecords("tasks."+alias, ip, nil, false)
|
|
|
|
}
|
2016-06-07 20:04:51 -04:00
|
|
|
|
2016-06-09 18:44:58 -04:00
|
|
|
// Add service name to vip in DNS, if vip is valid. Otherwise resort to DNS RR
|
|
|
|
svcIP := vip
|
|
|
|
if len(svcIP) == 0 {
|
|
|
|
svcIP = ip
|
|
|
|
}
|
|
|
|
n.(*network).addSvcRecords(name, svcIP, nil, false)
|
2016-06-14 19:40:54 -04:00
|
|
|
for _, alias := range aliases {
|
|
|
|
n.(*network).addSvcRecords(alias, svcIP, nil, false)
|
|
|
|
}
|
2016-06-09 18:44:58 -04:00
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
s.Lock()
|
2016-06-07 20:04:51 -04:00
|
|
|
defer s.Unlock()
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
lb, ok := s.loadBalancers[nid]
|
|
|
|
if !ok {
|
|
|
|
// Create a new load balancer if we are seeing this
|
|
|
|
// network attachment on the service for the first
|
|
|
|
// time.
|
|
|
|
lb = &loadBalancer{
|
|
|
|
vip: vip,
|
|
|
|
fwMark: fwMarkCtr,
|
|
|
|
backEnds: make(map[string]net.IP),
|
2016-05-31 02:55:51 -04:00
|
|
|
service: s,
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fwMarkCtrMu.Lock()
|
|
|
|
fwMarkCtr++
|
|
|
|
fwMarkCtrMu.Unlock()
|
|
|
|
|
|
|
|
s.loadBalancers[nid] = lb
|
|
|
|
|
|
|
|
// Since we just created this load balancer make sure
|
|
|
|
// we add a new service service in IPVS rules.
|
|
|
|
addService = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lb.backEnds[eid] = ip
|
|
|
|
|
|
|
|
// Add loadbalancer service and backend in all sandboxes in
|
|
|
|
// the network only if vip is valid.
|
|
|
|
if len(vip) != 0 {
|
2016-05-31 02:55:51 -04:00
|
|
|
n.(*network).addLBBackend(ip, vip, lb.fwMark, ingressPorts, addService)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-14 19:40:54 -04:00
|
|
|
func (c *controller) rmServiceBinding(name, sid, nid, eid string, vip net.IP, ingressPorts []*PortConfig, aliases []string, ip net.IP) error {
|
2016-05-25 01:46:18 -04:00
|
|
|
var rmService bool
|
|
|
|
|
|
|
|
n, err := c.NetworkByID(nid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-24 19:37:14 -04:00
|
|
|
skey := serviceKey{
|
|
|
|
id: sid,
|
|
|
|
ports: portConfigs(ingressPorts).String(),
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
c.Lock()
|
2016-06-24 19:37:14 -04:00
|
|
|
s, ok := c.serviceBindings[skey]
|
2016-05-25 01:46:18 -04:00
|
|
|
if !ok {
|
|
|
|
c.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
2016-06-07 20:04:51 -04:00
|
|
|
// Delete the special "tasks.svc_name" backend record.
|
|
|
|
n.(*network).deleteSvcRecords("tasks."+name, ip, nil, false)
|
2016-06-14 19:40:54 -04:00
|
|
|
for _, alias := range aliases {
|
|
|
|
n.(*network).deleteSvcRecords("tasks."+alias, ip, nil, false)
|
|
|
|
}
|
2016-06-07 20:04:51 -04:00
|
|
|
|
2016-06-29 14:51:32 -04:00
|
|
|
// If we are doing DNS RR add the endpoint IP to DNS record
|
|
|
|
// right away.
|
|
|
|
if len(vip) == 0 {
|
|
|
|
n.(*network).deleteSvcRecords(name, ip, nil, false)
|
|
|
|
for _, alias := range aliases {
|
|
|
|
n.(*network).deleteSvcRecords(alias, ip, nil, false)
|
|
|
|
}
|
2016-06-14 19:40:54 -04:00
|
|
|
}
|
2016-06-09 18:44:58 -04:00
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
s.Lock()
|
|
|
|
lb, ok := s.loadBalancers[nid]
|
|
|
|
if !ok {
|
2016-06-29 14:51:32 -04:00
|
|
|
s.Unlock()
|
2016-05-25 01:46:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(lb.backEnds, eid)
|
|
|
|
if len(lb.backEnds) == 0 {
|
|
|
|
// All the backends for this service have been
|
|
|
|
// removed. Time to remove the load balancer and also
|
|
|
|
// remove the service entry in IPVS.
|
|
|
|
rmService = true
|
|
|
|
|
|
|
|
delete(s.loadBalancers, nid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.loadBalancers) == 0 {
|
|
|
|
// All loadbalancers for the service removed. Time to
|
|
|
|
// remove the service itself.
|
2016-06-24 19:37:14 -04:00
|
|
|
delete(c.serviceBindings, skey)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove loadbalancer service(if needed) and backend in all
|
|
|
|
// sandboxes in the network only if the vip is valid.
|
|
|
|
if len(vip) != 0 {
|
2016-05-31 02:55:51 -04:00
|
|
|
n.(*network).rmLBBackend(ip, vip, lb.fwMark, ingressPorts, rmService)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
2016-06-29 14:51:32 -04:00
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
// Remove the DNS record for VIP only if we are removing the service
|
|
|
|
if rmService && len(vip) != 0 {
|
|
|
|
n.(*network).deleteSvcRecords(name, vip, nil, false)
|
|
|
|
for _, alias := range aliases {
|
|
|
|
n.(*network).deleteSvcRecords(alias, vip, nil, false)
|
|
|
|
}
|
|
|
|
}
|
2016-05-25 01:46:18 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all loadbalancers on this network that is currently discovered
|
2016-05-31 02:55:51 -04:00
|
|
|
// on this node.
|
2016-05-25 01:46:18 -04:00
|
|
|
func (n *network) connectedLoadbalancers() []*loadBalancer {
|
|
|
|
c := n.getController()
|
|
|
|
|
2016-06-15 17:00:48 -04:00
|
|
|
serviceBindings := make([]*service, 0, len(c.serviceBindings))
|
2016-05-25 01:46:18 -04:00
|
|
|
c.Lock()
|
2016-06-15 17:00:48 -04:00
|
|
|
for _, s := range c.serviceBindings {
|
|
|
|
serviceBindings = append(serviceBindings, s)
|
|
|
|
}
|
|
|
|
c.Unlock()
|
2016-05-25 01:46:18 -04:00
|
|
|
|
|
|
|
var lbs []*loadBalancer
|
2016-06-15 17:00:48 -04:00
|
|
|
for _, s := range serviceBindings {
|
|
|
|
s.Lock()
|
2016-05-25 01:46:18 -04:00
|
|
|
if lb, ok := s.loadBalancers[n.ID()]; ok {
|
|
|
|
lbs = append(lbs, lb)
|
|
|
|
}
|
2016-06-15 17:00:48 -04:00
|
|
|
s.Unlock()
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return lbs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate all loadbalancers on the network that the passed endpoint
|
|
|
|
// belongs to, into this sandbox.
|
|
|
|
func (sb *sandbox) populateLoadbalancers(ep *endpoint) {
|
2016-05-31 02:55:51 -04:00
|
|
|
var gwIP net.IP
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
n := ep.getNetwork()
|
2016-05-31 02:55:51 -04:00
|
|
|
eIP := ep.Iface().Address()
|
|
|
|
|
|
|
|
if sb.ingress {
|
|
|
|
// For the ingress sandbox if this is not gateway
|
|
|
|
// endpoint do nothing.
|
|
|
|
if ep != sb.getGatewayEndpoint() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the gateway endpoint. Now get the ingress
|
|
|
|
// network and plumb the loadbalancers.
|
|
|
|
gwIP = ep.Iface().Address().IP
|
|
|
|
for _, ep := range sb.getConnectedEndpoints() {
|
|
|
|
if !ep.endpointInGWNetwork() {
|
|
|
|
n = ep.getNetwork()
|
|
|
|
eIP = ep.Iface().Address()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
for _, lb := range n.connectedLoadbalancers() {
|
|
|
|
// Skip if vip is not valid.
|
|
|
|
if len(lb.vip) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:00:48 -04:00
|
|
|
lb.service.Lock()
|
2016-05-25 01:46:18 -04:00
|
|
|
addService := true
|
|
|
|
for _, ip := range lb.backEnds {
|
2016-05-31 02:55:51 -04:00
|
|
|
sb.addLBBackend(ip, lb.vip, lb.fwMark, lb.service.ingressPorts,
|
|
|
|
eIP, gwIP, addService)
|
2016-05-25 01:46:18 -04:00
|
|
|
addService = false
|
|
|
|
}
|
2016-06-15 17:00:48 -04:00
|
|
|
lb.service.Unlock()
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add loadbalancer backend to all sandboxes which has a connection to
|
|
|
|
// this network. If needed add the service as well, as specified by
|
|
|
|
// the addService bool.
|
2016-05-31 02:55:51 -04:00
|
|
|
func (n *network) addLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, addService bool) {
|
2016-05-25 01:46:18 -04:00
|
|
|
n.WalkEndpoints(func(e Endpoint) bool {
|
|
|
|
ep := e.(*endpoint)
|
|
|
|
if sb, ok := ep.getSandbox(); ok {
|
2016-06-15 17:00:48 -04:00
|
|
|
if !sb.isEndpointPopulated(ep) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
var gwIP net.IP
|
|
|
|
if ep := sb.getGatewayEndpoint(); ep != nil {
|
|
|
|
gwIP = ep.Iface().Address().IP
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.addLBBackend(ip, vip, fwMark, ingressPorts, ep.Iface().Address(), gwIP, addService)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove loadbalancer backend from all sandboxes which has a
|
|
|
|
// connection to this network. If needed remove the service entry as
|
|
|
|
// well, as specified by the rmService bool.
|
2016-05-31 02:55:51 -04:00
|
|
|
func (n *network) rmLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, rmService bool) {
|
2016-05-25 01:46:18 -04:00
|
|
|
n.WalkEndpoints(func(e Endpoint) bool {
|
|
|
|
ep := e.(*endpoint)
|
|
|
|
if sb, ok := ep.getSandbox(); ok {
|
2016-06-15 17:00:48 -04:00
|
|
|
if !sb.isEndpointPopulated(ep) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
var gwIP net.IP
|
|
|
|
if ep := sb.getGatewayEndpoint(); ep != nil {
|
|
|
|
gwIP = ep.Iface().Address().IP
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.rmLBBackend(ip, vip, fwMark, ingressPorts, ep.Iface().Address(), gwIP, rmService)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add loadbalancer backend into one connected sandbox.
|
2016-05-31 02:55:51 -04:00
|
|
|
func (sb *sandbox) addLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, eIP *net.IPNet, gwIP net.IP, addService bool) {
|
2016-06-05 16:45:52 -04:00
|
|
|
if sb.osSbox == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
i, err := ipvs.New(sb.Key())
|
|
|
|
if err != nil {
|
2016-06-02 01:37:39 -04:00
|
|
|
logrus.Errorf("Failed to create an ipvs handle for sbox %s: %v", sb.Key(), err)
|
2016-05-25 01:46:18 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer i.Close()
|
|
|
|
|
|
|
|
s := &ipvs.Service{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
FWMark: fwMark,
|
|
|
|
SchedName: ipvs.RoundRobin,
|
|
|
|
}
|
|
|
|
|
|
|
|
if addService {
|
2016-05-31 02:55:51 -04:00
|
|
|
var iPorts []*PortConfig
|
|
|
|
if sb.ingress {
|
2016-06-24 19:37:14 -04:00
|
|
|
iPorts = filterPortConfigs(ingressPorts, false)
|
2016-05-31 02:55:51 -04:00
|
|
|
if err := programIngress(gwIP, iPorts, false); err != nil {
|
|
|
|
logrus.Errorf("Failed to add ingress: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Creating service for vip %s fwMark %d ingressPorts %#v", vip, fwMark, iPorts)
|
|
|
|
if err := invokeFWMarker(sb.Key(), vip, fwMark, iPorts, eIP, false); err != nil {
|
2016-05-25 01:46:18 -04:00
|
|
|
logrus.Errorf("Failed to add firewall mark rule in sbox %s: %v", sb.Key(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.NewService(s); err != nil {
|
|
|
|
logrus.Errorf("Failed to create a new service for vip %s fwmark %d: %v", vip, fwMark, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &ipvs.Destination{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
Address: ip,
|
|
|
|
Weight: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the sched name before using the service to add
|
|
|
|
// destination.
|
|
|
|
s.SchedName = ""
|
2016-06-05 16:45:52 -04:00
|
|
|
if err := i.NewDestination(s, d); err != nil && err != syscall.EEXIST {
|
2016-06-07 20:04:51 -04:00
|
|
|
logrus.Errorf("Failed to create real server %s for vip %s fwmark %d in sb %s: %v", ip, vip, fwMark, sb.containerID, err)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove loadbalancer backend from one connected sandbox.
|
2016-05-31 02:55:51 -04:00
|
|
|
func (sb *sandbox) rmLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, eIP *net.IPNet, gwIP net.IP, rmService bool) {
|
2016-06-05 16:45:52 -04:00
|
|
|
if sb.osSbox == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
i, err := ipvs.New(sb.Key())
|
|
|
|
if err != nil {
|
2016-06-02 01:37:39 -04:00
|
|
|
logrus.Errorf("Failed to create an ipvs handle for sbox %s: %v", sb.Key(), err)
|
2016-05-25 01:46:18 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer i.Close()
|
|
|
|
|
|
|
|
s := &ipvs.Service{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
FWMark: fwMark,
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &ipvs.Destination{
|
|
|
|
AddressFamily: nl.FAMILY_V4,
|
|
|
|
Address: ip,
|
|
|
|
Weight: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.DelDestination(s, d); err != nil {
|
2016-06-15 17:00:48 -04:00
|
|
|
logrus.Infof("Failed to delete real server %s for vip %s fwmark %d: %v", ip, vip, fwMark, err)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if rmService {
|
|
|
|
s.SchedName = ipvs.RoundRobin
|
|
|
|
if err := i.DelService(s); err != nil {
|
2016-06-15 17:00:48 -04:00
|
|
|
logrus.Errorf("Failed to delete a new service for vip %s fwmark %d: %v", vip, fwMark, err)
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
var iPorts []*PortConfig
|
|
|
|
if sb.ingress {
|
2016-06-24 19:37:14 -04:00
|
|
|
iPorts = filterPortConfigs(ingressPorts, true)
|
2016-05-31 02:55:51 -04:00
|
|
|
if err := programIngress(gwIP, iPorts, true); err != nil {
|
|
|
|
logrus.Errorf("Failed to delete ingress: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := invokeFWMarker(sb.Key(), vip, fwMark, iPorts, eIP, true); err != nil {
|
2016-05-25 01:46:18 -04:00
|
|
|
logrus.Errorf("Failed to add firewall mark rule in sbox %s: %v", sb.Key(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 14:50:17 -04:00
|
|
|
const ingressChain = "DOCKER-INGRESS"
|
|
|
|
|
|
|
|
var (
|
|
|
|
ingressOnce sync.Once
|
|
|
|
ingressProxyMu sync.Mutex
|
|
|
|
ingressProxyTbl = make(map[string]io.Closer)
|
2016-06-24 19:37:14 -04:00
|
|
|
portConfigMu sync.Mutex
|
|
|
|
portConfigTbl = make(map[PortConfig]int)
|
2016-06-07 14:50:17 -04:00
|
|
|
)
|
|
|
|
|
2016-06-24 19:37:14 -04:00
|
|
|
func filterPortConfigs(ingressPorts []*PortConfig, isDelete bool) []*PortConfig {
|
|
|
|
portConfigMu.Lock()
|
|
|
|
iPorts := make([]*PortConfig, 0, len(ingressPorts))
|
|
|
|
for _, pc := range ingressPorts {
|
|
|
|
if isDelete {
|
|
|
|
if cnt, ok := portConfigTbl[*pc]; ok {
|
|
|
|
// This is the last reference to this
|
|
|
|
// port config. Delete the port config
|
|
|
|
// and add it to filtered list to be
|
|
|
|
// plumbed.
|
|
|
|
if cnt == 1 {
|
|
|
|
delete(portConfigTbl, *pc)
|
|
|
|
iPorts = append(iPorts, pc)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
portConfigTbl[*pc] = cnt - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if cnt, ok := portConfigTbl[*pc]; ok {
|
|
|
|
portConfigTbl[*pc] = cnt + 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are adding it for the first time. Add it to the
|
|
|
|
// filter list to be plumbed.
|
|
|
|
portConfigTbl[*pc] = 1
|
|
|
|
iPorts = append(iPorts, pc)
|
|
|
|
}
|
|
|
|
portConfigMu.Unlock()
|
|
|
|
|
|
|
|
return iPorts
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
func programIngress(gwIP net.IP, ingressPorts []*PortConfig, isDelete bool) error {
|
2016-06-07 14:50:17 -04:00
|
|
|
addDelOpt := "-I"
|
2016-05-31 02:55:51 -04:00
|
|
|
if isDelete {
|
|
|
|
addDelOpt = "-D"
|
|
|
|
}
|
|
|
|
|
2016-06-07 14:50:17 -04:00
|
|
|
chainExists := iptables.ExistChain(ingressChain, iptables.Nat)
|
|
|
|
|
|
|
|
ingressOnce.Do(func() {
|
|
|
|
if chainExists {
|
|
|
|
// Flush ingress chain rules during init if it
|
|
|
|
// exists. It might contain stale rules from
|
|
|
|
// previous life.
|
|
|
|
if err := iptables.RawCombinedOutput("-t", "nat", "-F", ingressChain); err != nil {
|
|
|
|
logrus.Errorf("Could not flush ingress chain rules during init: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if !isDelete {
|
|
|
|
if !chainExists {
|
|
|
|
if err := iptables.RawCombinedOutput("-t", "nat", "-N", ingressChain); err != nil {
|
|
|
|
return fmt.Errorf("failed to create ingress chain: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !iptables.Exists(iptables.Nat, ingressChain, "-j", "RETURN") {
|
|
|
|
if err := iptables.RawCombinedOutput("-t", "nat", "-A", ingressChain, "-j", "RETURN"); err != nil {
|
|
|
|
return fmt.Errorf("failed to add return rule in ingress chain: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chain := range []string{"OUTPUT", "PREROUTING"} {
|
2016-06-17 12:57:58 -04:00
|
|
|
if !iptables.Exists(iptables.Nat, chain, "-m", "addrtype", "--dst-type", "LOCAL", "-j", ingressChain) {
|
|
|
|
if err := iptables.RawCombinedOutput("-t", "nat", "-I", chain, "-m", "addrtype", "--dst-type", "LOCAL", "-j", ingressChain); err != nil {
|
2016-06-07 14:50:17 -04:00
|
|
|
return fmt.Errorf("failed to add jump rule in %s to ingress chain: %v", chain, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-12 20:28:28 -04:00
|
|
|
|
|
|
|
oifName, err := findOIFName(gwIP)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to find gateway bridge interface name for %s: %v", gwIP, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join("/proc/sys/net/ipv4/conf", oifName, "route_localnet")
|
|
|
|
if err := ioutil.WriteFile(path, []byte{'1', '\n'}, 0644); err != nil {
|
|
|
|
return fmt.Errorf("could not write to %s: %v", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleArgs := strings.Fields(fmt.Sprintf("-m addrtype --src-type LOCAL -o %s -j MASQUERADE", oifName))
|
|
|
|
if !iptables.Exists(iptables.Nat, "POSTROUTING", ruleArgs...) {
|
|
|
|
if err := iptables.RawCombinedOutput(append([]string{"-t", "nat", "-I", "POSTROUTING"}, ruleArgs...)...); err != nil {
|
|
|
|
return fmt.Errorf("failed to add ingress localhost POSTROUTING rule for %s: %v", oifName, err)
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 14:50:17 -04:00
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
for _, iPort := range ingressPorts {
|
2016-06-07 14:50:17 -04:00
|
|
|
if iptables.ExistChain(ingressChain, iptables.Nat) {
|
|
|
|
rule := strings.Fields(fmt.Sprintf("-t nat %s %s -p %s --dport %d -j DNAT --to-destination %s:%d",
|
2016-06-13 17:11:18 -04:00
|
|
|
addDelOpt, ingressChain, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort, gwIP, iPort.PublishedPort))
|
2016-06-07 14:50:17 -04:00
|
|
|
if err := iptables.RawCombinedOutput(rule...); err != nil {
|
2016-06-15 17:00:48 -04:00
|
|
|
errStr := fmt.Sprintf("setting up rule failed, %v: %v", rule, err)
|
|
|
|
if !isDelete {
|
|
|
|
return fmt.Errorf("%s", errStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("%s", errStr)
|
2016-06-07 14:50:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := plumbProxy(iPort, isDelete); err != nil {
|
2016-06-15 17:00:48 -04:00
|
|
|
logrus.Warnf("failed to create proxy for port %d: %v", iPort.PublishedPort, err)
|
2016-05-31 02:55:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:28:28 -04:00
|
|
|
func findOIFName(ip net.IP) (string, error) {
|
|
|
|
nlh := ns.NlHandle()
|
|
|
|
|
|
|
|
routes, err := nlh.RouteGet(ip)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) == 0 {
|
|
|
|
return "", fmt.Errorf("no route to %s", ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick the first route(typically there is only one route). We
|
|
|
|
// don't support multipath.
|
|
|
|
link, err := nlh.LinkByIndex(routes[0].LinkIndex)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return link.Attrs().Name, nil
|
|
|
|
}
|
|
|
|
|
2016-06-07 14:50:17 -04:00
|
|
|
func plumbProxy(iPort *PortConfig, isDelete bool) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
l io.Closer
|
|
|
|
)
|
|
|
|
|
2016-06-13 17:11:18 -04:00
|
|
|
portSpec := fmt.Sprintf("%d/%s", iPort.PublishedPort, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]))
|
2016-06-07 14:50:17 -04:00
|
|
|
if isDelete {
|
|
|
|
ingressProxyMu.Lock()
|
|
|
|
if listener, ok := ingressProxyTbl[portSpec]; ok {
|
|
|
|
if listener != nil {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ingressProxyMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch iPort.Protocol {
|
|
|
|
case ProtocolTCP:
|
2016-06-13 17:11:18 -04:00
|
|
|
l, err = net.ListenTCP("tcp", &net.TCPAddr{Port: int(iPort.PublishedPort)})
|
2016-06-07 14:50:17 -04:00
|
|
|
case ProtocolUDP:
|
2016-06-13 17:11:18 -04:00
|
|
|
l, err = net.ListenUDP("udp", &net.UDPAddr{Port: int(iPort.PublishedPort)})
|
2016-06-07 14:50:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ingressProxyMu.Lock()
|
|
|
|
ingressProxyTbl[portSpec] = l
|
|
|
|
ingressProxyMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
// Invoke fwmarker reexec routine to mark vip destined packets with
|
|
|
|
// the passed firewall mark.
|
2016-05-31 02:55:51 -04:00
|
|
|
func invokeFWMarker(path string, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, eIP *net.IPNet, isDelete bool) error {
|
|
|
|
var ingressPortsFile string
|
|
|
|
if len(ingressPorts) != 0 {
|
|
|
|
f, err := ioutil.TempFile("", "port_configs")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := proto.Marshal(&EndpointRecord{
|
|
|
|
IngressPorts: ingressPorts,
|
|
|
|
})
|
|
|
|
|
|
|
|
n, err := f.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n < len(buf) {
|
|
|
|
f.Close()
|
|
|
|
return io.ErrShortWrite
|
|
|
|
}
|
|
|
|
|
|
|
|
ingressPortsFile = f.Name()
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
addDelOpt := "-A"
|
|
|
|
if isDelete {
|
|
|
|
addDelOpt = "-D"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: reexec.Self(),
|
2016-05-31 02:55:51 -04:00
|
|
|
Args: append([]string{"fwmarker"}, path, vip.String(), fmt.Sprintf("%d", fwMark), addDelOpt, ingressPortsFile, eIP.IP.String()),
|
2016-05-25 01:46:18 -04:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
}
|
2016-05-31 02:55:51 -04:00
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("reexec failed: %v", err)
|
|
|
|
}
|
2016-05-31 02:55:51 -04:00
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Firewall marker reexec function.
|
|
|
|
func fwMarker() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
if len(os.Args) < 7 {
|
2016-05-25 01:46:18 -04:00
|
|
|
logrus.Error("invalid number of arguments..")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
var ingressPorts []*PortConfig
|
|
|
|
if os.Args[5] != "" {
|
|
|
|
buf, err := ioutil.ReadFile(os.Args[5])
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to read ports config file: %v", err)
|
|
|
|
os.Exit(6)
|
|
|
|
}
|
|
|
|
|
|
|
|
var epRec EndpointRecord
|
|
|
|
err = proto.Unmarshal(buf, &epRec)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to unmarshal ports config data: %v", err)
|
|
|
|
os.Exit(7)
|
|
|
|
}
|
|
|
|
|
|
|
|
ingressPorts = epRec.IngressPorts
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
vip := os.Args[2]
|
|
|
|
fwMark, err := strconv.ParseUint(os.Args[3], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("bad fwmark value(%s) passed: %v", os.Args[3], err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
addDelOpt := os.Args[4]
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
rules := [][]string{}
|
|
|
|
for _, iPort := range ingressPorts {
|
|
|
|
rule := strings.Fields(fmt.Sprintf("-t nat %s PREROUTING -p %s --dport %d -j REDIRECT --to-port %d",
|
2016-06-13 17:11:18 -04:00
|
|
|
addDelOpt, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort, iPort.TargetPort))
|
2016-05-31 02:55:51 -04:00
|
|
|
rules = append(rules, rule)
|
|
|
|
|
|
|
|
rule = strings.Fields(fmt.Sprintf("-t mangle %s PREROUTING -p %s --dport %d -j MARK --set-mark %d",
|
2016-06-13 17:11:18 -04:00
|
|
|
addDelOpt, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort, fwMark))
|
2016-05-31 02:55:51 -04:00
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
ns, err := netns.GetFromPath(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("failed get network namespace %q: %v", os.Args[1], err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
defer ns.Close()
|
|
|
|
|
|
|
|
if err := netns.Set(ns); err != nil {
|
|
|
|
logrus.Errorf("setting into container net ns %v failed, %v", os.Args[1], err)
|
|
|
|
os.Exit(4)
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
if len(ingressPorts) != 0 && addDelOpt == "-A" {
|
|
|
|
ruleParams := strings.Fields(fmt.Sprintf("-m ipvs --ipvs -j SNAT --to-source %s", os.Args[6]))
|
|
|
|
if !iptables.Exists("nat", "POSTROUTING", ruleParams...) {
|
|
|
|
rule := append(strings.Fields("-t nat -A POSTROUTING"), ruleParams...)
|
|
|
|
rules = append(rules, rule)
|
|
|
|
|
|
|
|
err := ioutil.WriteFile("/proc/sys/net/ipv4/vs/conntrack", []byte{'1', '\n'}, 0644)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to write to /proc/sys/net/ipv4/vs/conntrack: %v", err)
|
|
|
|
os.Exit(8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:46:18 -04:00
|
|
|
rule := strings.Fields(fmt.Sprintf("-t mangle %s OUTPUT -d %s/32 -j MARK --set-mark %d", addDelOpt, vip, fwMark))
|
2016-05-31 02:55:51 -04:00
|
|
|
rules = append(rules, rule)
|
|
|
|
|
|
|
|
for _, rule := range rules {
|
|
|
|
if err := iptables.RawCombinedOutputNative(rule...); err != nil {
|
|
|
|
logrus.Errorf("setting up rule failed, %v: %v", rule, err)
|
|
|
|
os.Exit(5)
|
|
|
|
}
|
2016-05-25 01:46:18 -04:00
|
|
|
}
|
|
|
|
}
|