<commit_before>// Copyright (c) 2016 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package intdataplane

import (
	"fmt"
	log "github.com/Sirupsen/logrus"
	"github.com/projectcalico/felix/go/felix/ifacemonitor"
	"github.com/projectcalico/felix/go/felix/ip"
	"github.com/projectcalico/felix/go/felix/iptables"
	"github.com/projectcalico/felix/go/felix/proto"
	"github.com/projectcalico/felix/go/felix/routetable"
	"github.com/projectcalico/felix/go/felix/rules"
	"github.com/projectcalico/felix/go/felix/set"
	"io"
	"net"
	"os"
	"reflect"
	"regexp"
	"strings"
)

// endpointManager manages the dataplane resources that belong to each endpoint as well as
// the "dispatch chains" that fan out packets to the right per-endpoint chain.
//
// It programs the relevant iptables chains (via the iptables.Table objects) along with
// per-endpoint routes (via the RouteTable).
//
// Since calculating the dispatch chains is fairly expensive, the main OnUpdate method
// simply records the pending state of each interface and defers the actual calculation
// to CompleteDeferredWork().  This is also the basis of our failure handling; updates
// that fail are left in the pending state so they can be retried later.
type endpointManager struct {
	ipVersion      int
	wlIfacesRegexp *regexp.Regexp

	// Our dependencies.
	filterTable  *iptables.Table
	ruleRenderer rules.RuleRenderer
	routeTable   *routetable.RouteTable

	// Active state, updated in CompleteDeferredWork.
	activeEndpoints      map[proto.WorkloadEndpointID]*proto.WorkloadEndpoint
	activeUpIfaces       set.Set
	activeIdToChains     map[proto.WorkloadEndpointID][]*iptables.Chain
	activeDispatchChains []*iptables.Chain

	activeIfacesNeedingConfig set.Set

	// Pending updates, cleared in CompleteDeferredWork.
	pendingEndpointUpdates map[proto.WorkloadEndpointID]*proto.WorkloadEndpoint
	pendingIfaceUpdates    map[string]ifacemonitor.State

	// Host endpoint processing.
	ifaceAddrs               map[string][]string
	rawHostEndpoints         map[proto.HostEndpointID]*proto.HostEndpoint
	dirtyHostEndpoints       bool
	activeHostIdToChains     map[*proto.HostEndpointID][]*iptables.Chain
	activeHostDispatchChains []*iptables.Chain
}

func newEndpointManager(
	filterTable *iptables.Table,
	ruleRenderer rules.RuleRenderer,
	routeTable *routetable.RouteTable,
	ipVersion int,
	ourInterfacePrefixes []string,
) *endpointManager {
	wlIfacesPattern := "^(" + strings.Join(ourInterfacePrefixes, "|") + ").*"
	wlIfacesRegexp := regexp.MustCompile(wlIfacesPattern)

	return &endpointManager{
		ipVersion:      ipVersion,
		wlIfacesRegexp: wlIfacesRegexp,

		filterTable:  filterTable,
		ruleRenderer: ruleRenderer,
		routeTable:   routeTable,

		activeEndpoints:  nil,
		activeUpIfaces:   set.New(),
		activeIdToChains: nil,

		activeIfacesNeedingConfig: set.New(),

		pendingEndpointUpdates:   make(map[proto.WorkloadEndpointID]*proto.WorkloadEndpoint),
		pendingIfaceUpdates:      make(map[string]ifacemonitor.State),
		ifaceAddrs:               make(map[string][]string),
		rawHostEndpoints:         make(map[proto.HostEndpointID]*proto.HostEndpoint),
		dirtyHostEndpoints:       true,
		activeHostIdToChains:     make(map[*proto.HostEndpointID][]*iptables.Chain),
		activeHostDispatchChains: nil,
	}
}

func (m *endpointManager) OnUpdate(protoBufMsg interface{}) {
	switch msg := protoBufMsg.(type) {
	case *proto.WorkloadEndpointUpdate:
		m.pendingEndpointUpdates[*msg.Id] = msg.Endpoint
	case *proto.WorkloadEndpointRemove:
		m.pendingEndpointUpdates[*msg.Id] = nil
	case *proto.HostEndpointUpdate:
		log.WithField("msg", msg).Debug("Host endpoint update")
		m.rawHostEndpoints[*msg.Id] = msg.Endpoint
		m.dirtyHostEndpoints = true
	case *proto.HostEndpointRemove:
		log.WithField("msg", msg).Debug("Host endpoint removed")
		delete(m.rawHostEndpoints, *msg.Id)
		m.dirtyHostEndpoints = true
	case *ifaceUpdate:<commit_msg>Revert incorrect empty map initializers<commit_after>// Copyright (c) 2016 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package intdataplane

import (
	"fmt"
	log "github.com/Sirupsen/logrus"
	"github.com/projectcalico/felix/go/felix/ifacemonitor"
	"github.com/projectcalico/felix/go/felix/ip"
	"github.com/projectcalico/felix/go/felix/iptables"
	"github.com/projectcalico/felix/go/felix/proto"
	"github.com/projectcalico/felix/go/felix/routetable"
	"github.com/projectcalico/felix/go/felix/rules"
	"github.com/projectcalico/felix/go/felix/set"
	"io"
	"net"
	"os"
	"reflect"
	"regexp"
	"strings"
)

// endpointManager manages the dataplane resources that belong to each endpoint as well as
// the "dispatch chains" that fan out packets to the right per-endpoint chain.
//
// It programs the relevant iptables chains (via the iptables.Table objects) along with
// per-endpoint routes (via the RouteTable).
//
// Since calculating the dispatch chains is fairly expensive, the main OnUpdate method
// simply records the pending state of each interface and defers the actual calculation
// to CompleteDeferredWork().  This is also the basis of our failure handling; updates
// that fail are left in the pending state so they can be retried later.
type endpointManager struct {
	ipVersion      int
	wlIfacesRegexp *regexp.Regexp

	// Our dependencies.
	filterTable  *iptables.Table
	ruleRenderer rules.RuleRenderer
	routeTable   *routetable.RouteTable

	// Active state, updated in CompleteDeferredWork.
	activeEndpoints      map[proto.WorkloadEndpointID]*proto.WorkloadEndpoint
	activeUpIfaces       set.Set
	activeIdToChains     map[proto.WorkloadEndpointID][]*iptables.Chain
	activeDispatchChains []*iptables.Chain

	activeIfacesNeedingConfig set.Set

	// Pending updates, cleared in CompleteDeferredWork.
	pendingEndpointUpdates map[proto.WorkloadEndpointID]*proto.WorkloadEndpoint
	pendingIfaceUpdates    map[string]ifacemonitor.State

	// Host endpoint processing.
	ifaceAddrs               map[string][]string
	rawHostEndpoints         map[proto.HostEndpointID]*proto.HostEndpoint
	dirtyHostEndpoints       bool
	activeHostIdToChains     map[*proto.HostEndpointID][]*iptables.Chain
	activeHostDispatchChains []*iptables.Chain
}

func newEndpointManager(
	filterTable *iptables.Table,
	ruleRenderer rules.RuleRenderer,
	routeTable *routetable.RouteTable,
	ipVersion int,
	ourInterfacePrefixes []string,
) *endpointManager {
	wlIfacesPattern := "^(" + strings.Join(ourInterfacePrefixes, "|") + ").*"
	wlIfacesRegexp := regexp.MustCompile(wlIfacesPattern)

	return &endpointManager{
		ipVersion:      ipVersion,
		wlIfacesRegexp: wlIfacesRegexp,

		filterTable:  filterTable,
		ruleRenderer: ruleRenderer,
		routeTable:   routeTable,

		activeEndpoints:  map[proto.WorkloadEndpointID]*proto.WorkloadEndpoint{},
		activeUpIfaces:   set.New(),
		activeIdToChains: map[proto.WorkloadEndpointID][]*iptables.Chain{},

		activeIfacesNeedingConfig: set.New(),

		pendingEndpointUpdates:   map[proto.WorkloadEndpointID]*proto.WorkloadEndpoint{},
		pendingIfaceUpdates:      map[string]ifacemonitor.State{},
		ifaceAddrs:               map[string][]string{},
		rawHostEndpoints:         map[proto.HostEndpointID]*proto.HostEndpoint{},
		dirtyHostEndpoints:       true,
		activeHostIdToChains:     map[*proto.HostEndpointID][]*iptables.Chain{},
		activeHostDispatchChains: nil,
	}
}

func (m *endpointManager) OnUpdate(protoBufMsg interface{}) {
	switch msg := protoBufMsg.(type) {
	case *proto.WorkloadEndpointUpdate:
		m.pendingEndpointUpdates[*msg.Id] = msg.Endpoint
	case *proto.WorkloadEndpointRemove:
		m.pendingEndpointUpdates[*msg.Id] = nil
	case *proto.HostEndpointUpdate:
		log.WithField("msg", msg).Debug("Host endpoint update")
		m.rawHostEndpoints[*msg.Id] = msg.Endpoint
		m.dirtyHostEndpoints = true
	case *proto.HostEndpointRemove:
		log.WithField("msg", msg).Debug("Host endpoint removed")
		delete(m.rawHostEndpoints, *msg.Id)
		m.dirtyHostEndpoints = true
	case *ifaceUpdate:
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.0017857142857142857, 'mean_word_length': 4.6111458985597995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2349056', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2708, 'n_words': 683}