<commit_before>/*
 * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
 */

#include <vector>
#include <bitset>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <base/os.h>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/unordered_map.hpp>
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include <sandesh/sandesh_trace.h>
#include <net/address_util.h>
#include <pkt/flow_table.h>
#include <vrouter/flow_stats/flow_stats_collector.h>
#include <vrouter/ksync/ksync_init.h>
#include <vrouter/ksync/ksync_flow_index_manager.h>

#include <route/route.h>
#include <cmn/agent_cmn.h>
#include <oper/interface_common.h>
#include <oper/nexthop.h>

#include <init/agent_param.h>
#include <cmn/agent_cmn.h>
#include <cmn/agent_stats.h>
#include <oper/route_common.h>
#include <oper/vrf.h>
#include <oper/vm.h>
#include <oper/sg.h>

#include <filter/packet_header.h>
#include <filter/acl.h>

#include <pkt/proto.h>
#include <pkt/proto_handler.h>
#include <pkt/pkt_handler.h>
#include <pkt/flow_proto.h>
#include <pkt/pkt_types.h>
#include <pkt/pkt_sandesh_flow.h>
#include <pkt/flow_mgmt.h>
#include <pkt/flow_event.h>
#include <pkt/flow_entry.h>
#include <uve/agent_uve.h>
#include <uve/vm_uve_table.h>
#include <uve/vn_uve_table.h>
#include <uve/vrouter_uve_entry.h>

const std::map<FlowEntry::FlowPolicyState, const char*>
    FlowEntry::FlowPolicyStateStr = boost::assign::map_list_of
        (NOT_EVALUATED,            "00000000-0000-0000-0000-000000000000")
        (IMPLICIT_ALLOW,           "00000000-0000-0000-0000-000000000001")
        (IMPLICIT_DENY,            "00000000-0000-0000-0000-000000000002")
        (DEFAULT_GW_ICMP_OR_DNS,   "00000000-0000-0000-0000-000000000003")
        (LINKLOCAL_FLOW,           "00000000-0000-0000-0000-000000000004")
        (MULTICAST_FLOW,           "00000000-0000-0000-0000-000000000005")
        (NON_IP_FLOW,              "00000000-0000-0000-0000-000000000006")
        (BGPROUTERSERVICE_FLOW,    "00000000-0000-0000-0000-000000000007");

const std::map<uint16_t, const char*>
    FlowEntry::FlowDropReasonStr = boost::assign::map_list_of
        ((uint16_t)DROP_UNKNOWN,                 "UNKNOWN")
        ((uint16_t)SHORT_UNAVIALABLE_INTERFACE,
         "SHORT_UNAVIALABLE_INTERFACE")
        ((uint16_t)SHORT_IPV4_FWD_DIS,       "SHORT_IPV4_FWD_DIS")
        ((uint16_t)SHORT_UNAVIALABLE_VRF,
         "SHORT_UNAVIALABLE_VRF")
        ((uint16_t)SHORT_NO_SRC_ROUTE,       "SHORT_NO_SRC_ROUTE")
        ((uint16_t)SHORT_NO_DST_ROUTE,       "SHORT_NO_DST_ROUTE")
        ((uint16_t)SHORT_AUDIT_ENTRY,        "SHORT_AUDIT_ENTRY")
        ((uint16_t)SHORT_VRF_CHANGE,         "SHORT_VRF_CHANGE")
        ((uint16_t)SHORT_NO_REVERSE_FLOW,    "SHORT_NO_REVERSE_FLOW")
        ((uint16_t)SHORT_REVERSE_FLOW_CHANGE,
         "SHORT_REVERSE_FLOW_CHANGE")
        ((uint16_t)SHORT_NAT_CHANGE,         "SHORT_NAT_CHANGE")
        ((uint16_t)SHORT_FLOW_LIMIT,         "SHORT_FLOW_LIMIT")
        ((uint16_t)SHORT_LINKLOCAL_SRC_NAT,
         "SHORT_LINKLOCAL_SRC_NAT")
        ((uint16_t)SHORT_FAILED_VROUTER_INSTALL,
         "SHORT_FAILED_VROUTER_INST")
        ((uint16_t)SHORT_INVALID_L2_FLOW,    "SHORT_INVALID_L2_FLOW")
        ((uint16_t)DROP_POLICY,              "DROP_POLICY")
        ((uint16_t)DROP_OUT_POLICY,          "DROP_OUT_POLICY")
        ((uint16_t)DROP_SG,                  "DROP_SG")
        ((uint16_t)DROP_OUT_SG,              "DROP_OUT_SG")
        ((uint16_t)DROP_REVERSE_SG,          "DROP_REVERSE_SG")
        ((uint16_t)DROP_REVERSE_OUT_SG,      "DROP_REVERSE_OUT_SG");

tbb::atomic<int> FlowEntry::alloc_count_;
SecurityGroupList FlowEntry::default_sg_list_;

/////////////////////////////////////////////////////////////////////////////
// VmFlowRef
/////////////////////////////////////////////////////////////////////////////
const int VmFlowRef::kInvalidFd;
VmFlowRef::VmFlowRef() : vm_(NULL), fd_(kInvalidFd), port_(0) {
}

VmFlowRef::VmFlowRef(const VmFlowRef &rhs) {
    // UPDATE on linklocal flows is not supported. So, fd_ should be invalid
    assert(fd_ == VmFlowRef::kInvalidFd);
    assert(rhs.fd_ == VmFlowRef::kInvalidFd);
    SetVm(rhs.vm_.get());
}

VmFlowRef:: ~VmFlowRef() {
    Reset();
}

void VmFlowRef::operator=(const VmFlowRef &rhs) {
    // UPDATE on linklocal flows is not supported. So, fd_ should be invalid
    assert(fd_ == VmFlowRef::kInvalidFd);
    assert(rhs.fd_ == VmFlowRef::kInvalidFd);
    SetVm(rhs.vm_.get());
}

void VmFlowRef::Reset() {
    FreeRef();
    FreeFd();
    vm_.reset(NULL);
}

void VmFlowRef::FreeRef() {
    if (vm_.get() == NULL)<commit_msg>Remove incorrect assert.<commit_after>/*
 * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
 */

#include <vector>
#include <bitset>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <base/os.h>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/unordered_map.hpp>
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include <sandesh/sandesh_trace.h>
#include <net/address_util.h>
#include <pkt/flow_table.h>
#include <vrouter/flow_stats/flow_stats_collector.h>
#include <vrouter/ksync/ksync_init.h>
#include <vrouter/ksync/ksync_flow_index_manager.h>

#include <route/route.h>
#include <cmn/agent_cmn.h>
#include <oper/interface_common.h>
#include <oper/nexthop.h>

#include <init/agent_param.h>
#include <cmn/agent_cmn.h>
#include <cmn/agent_stats.h>
#include <oper/route_common.h>
#include <oper/vrf.h>
#include <oper/vm.h>
#include <oper/sg.h>

#include <filter/packet_header.h>
#include <filter/acl.h>

#include <pkt/proto.h>
#include <pkt/proto_handler.h>
#include <pkt/pkt_handler.h>
#include <pkt/flow_proto.h>
#include <pkt/pkt_types.h>
#include <pkt/pkt_sandesh_flow.h>
#include <pkt/flow_mgmt.h>
#include <pkt/flow_event.h>
#include <pkt/flow_entry.h>
#include <uve/agent_uve.h>
#include <uve/vm_uve_table.h>
#include <uve/vn_uve_table.h>
#include <uve/vrouter_uve_entry.h>

const std::map<FlowEntry::FlowPolicyState, const char*>
    FlowEntry::FlowPolicyStateStr = boost::assign::map_list_of
        (NOT_EVALUATED,            "00000000-0000-0000-0000-000000000000")
        (IMPLICIT_ALLOW,           "00000000-0000-0000-0000-000000000001")
        (IMPLICIT_DENY,            "00000000-0000-0000-0000-000000000002")
        (DEFAULT_GW_ICMP_OR_DNS,   "00000000-0000-0000-0000-000000000003")
        (LINKLOCAL_FLOW,           "00000000-0000-0000-0000-000000000004")
        (MULTICAST_FLOW,           "00000000-0000-0000-0000-000000000005")
        (NON_IP_FLOW,              "00000000-0000-0000-0000-000000000006")
        (BGPROUTERSERVICE_FLOW,    "00000000-0000-0000-0000-000000000007");

const std::map<uint16_t, const char*>
    FlowEntry::FlowDropReasonStr = boost::assign::map_list_of
        ((uint16_t)DROP_UNKNOWN,                 "UNKNOWN")
        ((uint16_t)SHORT_UNAVIALABLE_INTERFACE,
         "SHORT_UNAVIALABLE_INTERFACE")
        ((uint16_t)SHORT_IPV4_FWD_DIS,       "SHORT_IPV4_FWD_DIS")
        ((uint16_t)SHORT_UNAVIALABLE_VRF,
         "SHORT_UNAVIALABLE_VRF")
        ((uint16_t)SHORT_NO_SRC_ROUTE,       "SHORT_NO_SRC_ROUTE")
        ((uint16_t)SHORT_NO_DST_ROUTE,       "SHORT_NO_DST_ROUTE")
        ((uint16_t)SHORT_AUDIT_ENTRY,        "SHORT_AUDIT_ENTRY")
        ((uint16_t)SHORT_VRF_CHANGE,         "SHORT_VRF_CHANGE")
        ((uint16_t)SHORT_NO_REVERSE_FLOW,    "SHORT_NO_REVERSE_FLOW")
        ((uint16_t)SHORT_REVERSE_FLOW_CHANGE,
         "SHORT_REVERSE_FLOW_CHANGE")
        ((uint16_t)SHORT_NAT_CHANGE,         "SHORT_NAT_CHANGE")
        ((uint16_t)SHORT_FLOW_LIMIT,         "SHORT_FLOW_LIMIT")
        ((uint16_t)SHORT_LINKLOCAL_SRC_NAT,
         "SHORT_LINKLOCAL_SRC_NAT")
        ((uint16_t)SHORT_FAILED_VROUTER_INSTALL,
         "SHORT_FAILED_VROUTER_INST")
        ((uint16_t)SHORT_INVALID_L2_FLOW,    "SHORT_INVALID_L2_FLOW")
        ((uint16_t)DROP_POLICY,              "DROP_POLICY")
        ((uint16_t)DROP_OUT_POLICY,          "DROP_OUT_POLICY")
        ((uint16_t)DROP_SG,                  "DROP_SG")
        ((uint16_t)DROP_OUT_SG,              "DROP_OUT_SG")
        ((uint16_t)DROP_REVERSE_SG,          "DROP_REVERSE_SG")
        ((uint16_t)DROP_REVERSE_OUT_SG,      "DROP_REVERSE_OUT_SG");

tbb::atomic<int> FlowEntry::alloc_count_;
SecurityGroupList FlowEntry::default_sg_list_;

/////////////////////////////////////////////////////////////////////////////
// VmFlowRef
/////////////////////////////////////////////////////////////////////////////
const int VmFlowRef::kInvalidFd;
VmFlowRef::VmFlowRef() : vm_(NULL), fd_(kInvalidFd), port_(0) {
}

VmFlowRef::VmFlowRef(const VmFlowRef &rhs) {
    // UPDATE on linklocal flows is not supported. So, fd_ should be invalid
    assert(fd_ == VmFlowRef::kInvalidFd);
    assert(rhs.fd_ == VmFlowRef::kInvalidFd);
    SetVm(rhs.vm_.get());
}

VmFlowRef:: ~VmFlowRef() {
    Reset();
}

void VmFlowRef::operator=(const VmFlowRef &rhs) {
    // When flow is evicted by vrouter, reuse the older fd and port
    FreeFd();
    fd_ = rhs.fd_;
    port_ = rhs.port_;
    SetVm(rhs.vm_.get());
}

void VmFlowRef::Reset() {
    FreeRef();
    FreeFd();
    vm_.reset(NULL);
}

void VmFlowRef::FreeRef() {
    if (vm_.get() == NULL)
{'fraction_non_alphanumeric': 0.16382841129553782, 'fraction_numerical': 0.06660918301358051, 'mean_word_length': 3.1703370786516856, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 32, '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': '6938371', 'n_tokens_mistral': 4039, 'n_tokens_neox': 3541, 'n_words': 462}