1package libnetwork 2 3import ( 4 "fmt" 5 "net" 6 "sync" 7 8 "github.com/docker/libnetwork/internal/setmatrix" 9) 10 11var ( 12 // A global monotonic counter to assign firewall marks to 13 // services. 14 fwMarkCtr uint32 = 256 15 fwMarkCtrMu sync.Mutex 16) 17 18type portConfigs []*PortConfig 19 20func (p portConfigs) String() string { 21 if len(p) == 0 { 22 return "" 23 } 24 25 pc := p[0] 26 str := fmt.Sprintf("%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)]) 27 for _, pc := range p[1:] { 28 str = str + fmt.Sprintf(",%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)]) 29 } 30 31 return str 32} 33 34type serviceKey struct { 35 id string 36 ports string 37} 38 39type service struct { 40 name string // Service Name 41 id string // Service ID 42 43 // Map of loadbalancers for the service one-per attached 44 // network. It is keyed with network ID. 45 loadBalancers map[string]*loadBalancer 46 47 // List of ingress ports exposed by the service 48 ingressPorts portConfigs 49 50 // Service aliases 51 aliases []string 52 53 // This maps tracks for each IP address the list of endpoints ID 54 // associated with it. At stable state the endpoint ID expected is 1 55 // but during transition and service change it is possible to have 56 // temporary more than 1 57 ipToEndpoint setmatrix.SetMatrix 58 59 deleted bool 60 61 sync.Mutex 62} 63 64// assignIPToEndpoint inserts the mapping between the IP and the endpoint identifier 65// returns true if the mapping was not present, false otherwise 66// returns also the number of endpoints associated to the IP 67func (s *service) assignIPToEndpoint(ip, eID string) (bool, int) { 68 return s.ipToEndpoint.Insert(ip, eID) 69} 70 71// removeIPToEndpoint removes the mapping between the IP and the endpoint identifier 72// returns true if the mapping was deleted, false otherwise 73// returns also the number of endpoints associated to the IP 74func (s *service) removeIPToEndpoint(ip, eID string) (bool, int) { 75 return s.ipToEndpoint.Remove(ip, eID) 76} 77 78func (s *service) printIPToEndpoint(ip string) (string, bool) { 79 return s.ipToEndpoint.String(ip) 80} 81 82type lbBackend struct { 83 ip net.IP 84 disabled bool 85} 86 87type loadBalancer struct { 88 vip net.IP 89 fwMark uint32 90 91 // Map of backend IPs backing this loadbalancer on this 92 // network. It is keyed with endpoint ID. 93 backEnds map[string]*lbBackend 94 95 // Back pointer to service to which the loadbalancer belongs. 96 service *service 97 sync.Mutex 98} 99