aboutsummaryrefslogtreecommitdiffstats
path: root/src/VppExtItfManager.cpp
blob: eb8fa9bd5228a9a2376361f7aeead3776f4959d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Copyright (c) 2018 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#include <boost/optional.hpp>

#include <opflexagent/PolicyManager.h>

#include <modelgbp/gbp/ExternalInterface.hpp>
#include <modelgbp/gbp/L3ExternalDomain.hpp>
#include <modelgbp/gbp/Subnet.hpp>

#include <vom/bridge_domain_arp_entry.hpp>
#include <vom/gbp_ext_itf.hpp>
#include <vom/gbp_subnet.hpp>
#include <vom/l3_binding.hpp>
#include <vom/route.hpp>

#include "VppEndPointGroupManager.hpp"
#include "VppExtItfManager.hpp"
#include "VppLog.hpp"
#include "VppRouteManager.hpp"
#include "VppUtil.hpp"

using namespace VOM;

namespace VPP
{
ExtItfManager::ExtItfManager(Runtime &runtime)
    : m_runtime(runtime)
{
}

void
ExtItfManager::handle_update(const opflex::modb::URI &uri)
{
    OM::mark_n_sweep ms(uri.toString());
    const std::string &uuid = uri.toString();

    boost::optional<std::shared_ptr<modelgbp::gbp::ExternalInterface>> ext_itf =
        modelgbp::gbp::ExternalInterface::resolve(
            m_runtime.agent.getFramework(), uri);

    if (!ext_itf)
    {
        VLOGD << "External-Interface; delete: " << uri;
        return;
    }
    VLOGD << "External-Interface; update: " << uri;

    boost::optional<std::shared_ptr<modelgbp::gbp::ExternalL3BridgeDomain>>
        op_bd = m_runtime.policy_manager().getBDForExternalInterface(uri);

    if (!op_bd)
    {
        VLOGE << "External-Interface; no ExternalBridgeDomain: " << uri;
        return;
    }

    boost::optional<std::shared_ptr<modelgbp::gbp::RoutingDomain>> op_rd =
        m_runtime.policy_manager().getRDForExternalInterface(uri);

    if (!op_rd)
    {
        VLOGE << "External-Interface; no RouteDomain: " << uri;
        return;
    }

    uint32_t rd_id = m_runtime.id_gen.get(
        modelgbp::gbp::RoutingDomain::CLASS_ID, op_rd.get()->getURI());

    route_domain rd(rd_id);
    OM::write(uuid, rd);

    uint32_t bd_id = m_runtime.id_gen.get(modelgbp::gbp::BridgeDomain::CLASS_ID,
                                          op_bd.get()->getURI());

    bridge_domain bd(bd_id, bridge_domain::learning_mode_t::OFF);
    OM::write(uuid, bd);

    /*
     * Create a BVI interface for the EPG and add it to the bridge-domain
     */
    std::shared_ptr<interface> bvi = EndPointGroupManager::mk_bvi(
        m_runtime, uuid, bd, rd, mac_from_modb(ext_itf.get()->getMac()));

    /*
     * Add the mcast tunnels for flooding
     */
    boost::optional<std::string> maddr =
        m_runtime.policy_manager().getBDMulticastIPForExternalInterface(uri);
    boost::optional<uint32_t> bd_vnid =
        m_runtime.policy_manager().getBDVnidForExternalInterface(uri);
    boost::optional<uint32_t> rd_vnid =
        m_runtime.policy_manager().getRDVnidForExternalInterface(uri);

    if (!(rd_vnid && bd_vnid && maddr))
    {
        VLOGE << "External-Interface; no VNI/mcast-address: " << uri;
        return;
    }

    std::shared_ptr<vxlan_tunnel> vt_mc = EndPointGroupManager::mk_mcast_tunnel(
        m_runtime, uuid, bd_vnid.get(), maddr.get());

    /*
     * there's no leanring of EPs in an external BD
     */
    gbp_bridge_domain gbd(
        bd, *bvi, {}, vt_mc, gbp_bridge_domain::flags_t::DO_NOT_LEARN);
    OM::write(uuid, gbd);
    std::shared_ptr<VOM::gbp_route_domain> grd =
        EndPointGroupManager::mk_gbp_rd(m_runtime, uuid, rd, rd_vnid.get());

    /*
     * the encap on the external-interface is a vlan ID
     */
    boost::optional<uint32_t> vlan_id = ext_itf.get()->getEncap();

    if (vlan_id)
        ;

    /*
     * Add the /32 to the BVI
     */
    boost::optional<const std::string &> s_addr = ext_itf.get()->getAddress();

    if (!s_addr)
    {
        VLOGI << "External-Interface; no prefix: " << uri;
        return;
    }
    boost::asio::ip::address p_addr =
        boost::asio::ip::address::from_string(s_addr.get());

    l3_binding l3b(*bvi, {p_addr});
    OM::write(uuid, l3b);

    bridge_domain_arp_entry bae(bd, p_addr, bvi->l2_address().to_mac());
    OM::write(uuid, bae);

    opflexagent::PolicyManager::subnet_vector_t subnets;

    m_runtime.policy_manager().getSubnetsForExternalInterface(uri, subnets);

    for (auto sn : subnets)
    {
        if (!sn->getPrefixLen() || !sn->getAddress()) continue;

        route::prefix_t pfx(sn->getAddress().get(), sn->getPrefixLen().get());

        route::ip_route ipr(rd, pfx, {route::path::special_t::DROP});
        OM::write(uuid, ipr);
    }

    /*
     * This BVI is the ExternalInterface
     */
    gbp_ext_itf gei(*bvi, gbd, *grd);
    OM::write(uuid, gei);

    /*
     * Add any external networks
     */
    boost::optional<std::shared_ptr<modelgbp::gbp::L3ExternalDomain>> ext_dom =
        m_runtime.policy_manager().getExternalDomainForExternalInterface(uri);

    if (!ext_dom)
    {
        VLOGI << "External-Interface; no ExternalDomain: " << uri;
        return;
    }

    RouteManager::mk_ext_nets(m_runtime, rd, uri, ext_dom.get());
}

}; // namepsace VPP

/*
 * Local Variables:
 * eval: (c-set-style "llvm.org")
 * End:
 */