aboutsummaryrefslogtreecommitdiffstats
path: root/src/VppRenderer.cpp
blob: b89259511a13cf8019d33aaf0e72396e19638a5f (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Implementation for VPPRenderer class
 *
 * 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/asio/placeholders.hpp>

#include <opflexagent/logging.h>

#include <vom/stat_reader.hpp>

#include "VppLogHandler.hpp"
#include "VppRenderer.hpp"

namespace VPP
{

using boost::asio::placeholders::error;
using boost::property_tree::ptree;
using opflex::ofcore::OFFramework;
using std::bind;

VppRendererPlugin::VppRendererPlugin()
{
}

std::unordered_set<std::string>
VppRendererPlugin::getNames() const
{
    return {"vpp"};
}

opflexagent::Renderer *
VppRendererPlugin::create(opflexagent::Agent &agent) const
{
    IdGenerator *idGen = new IdGenerator();
    VOM::HW::cmd_q *vppQ = new VOM::HW::cmd_q();
    VOM::stat_reader *vppSR = new stat_reader();
    VppManager *vppManager = new VppManager(agent, *idGen, vppQ, vppSR);
    return new VppRenderer(agent, *idGen, vppManager);
}

VPP::LogHandler vppLogHandler;

static VOM::log_level_t
agentLevelToVom(opflexagent::LogLevel level)
{
    switch (level)
    {
    case opflexagent::DEBUG:
        return (VOM::log_level_t::DEBUG);
    case opflexagent::INFO:
        return (VOM::log_level_t::INFO);
    case opflexagent::WARNING:
        return (VOM::log_level_t::WARNING);
    case opflexagent::ERROR:
        return (VOM::log_level_t::ERROR);
    case opflexagent::FATAL:
        return (VOM::log_level_t::CRITICAL);
    }
    return (VOM::log_level_t::INFO);
}

VppRenderer::VppRenderer(opflexagent::Agent &agent,
                         IdGenerator &idGen,
                         VppManager *vppManager)
    : Renderer(agent)
    , idGen(idGen)
    , vppManager(vppManager)
    , tunnelEpManager(&agent)
    , started(false)
{
    LOG(INFO) << "Vpp Renderer";

    /*
     * Register the call back handler for VOM logging and set the level
     * according to the agent's settings
     */
    VOM::logger().set(agentLevelToVom(opflexagent::logLevel));
    VOM::logger().set(&vppLogHandler);
}

VppRenderer::~VppRenderer()
{
    delete vppManager;
}

void
VppRenderer::setProperties(const boost::property_tree::ptree &properties)
{
    // Set configuration from property tree.  This configuration will
    // be from a "renderers": { "vpp" { } } block from the agent
    // configuration.  Multiple calls are possible; later calls are
    // merged with prior calls, overwriting any previously-set values.
    LOG(opflexagent::INFO) << "Setting configuration for vpp renderer";
    static const std::string ENCAP_VXLAN("encap.vxlan");
    static const std::string ENCAP_IVXLAN("encap.ivxlan");
    static const std::string ENCAP_VLAN("encap.vlan");
    static const std::string UPLINK_IFACE("uplink-iface");
    static const std::string UPLINK_SLAVES("uplink-slaves");
    static const std::string UPLINK_VLAN("uplink-vlan");
    static const std::string DHCP_OPTIONS("dhcp-opt");
    static const std::string ENCAP_IFACE("encap-iface");
    static const std::string REMOTE_IP("remote-ip");
    static const std::string REMOTE_PORT("remote-port");
    static const std::string VIRTUAL_ROUTER("forwarding"
                                            ".virtual-router.enabled");
    static const std::string VIRTUAL_ROUTER_MAC("forwarding"
                                                ".virtual-router.mac");
    static const std::string VIRTUAL_ROUTER_RA("forwarding.virtual-router"
                                               ".ipv6.router-advertisement");
    static const std::string CROSS_CONNECT("x-connect");
    static const std::string EAST("east");
    static const std::string WEST("west");
    static const std::string EIFACE("iface");
    static const std::string EVLAN("vlan");
    static const std::string EIP("ip-address");
    static const std::string ETAG_REWRITE("tag-rewrite");
    static const std::string WIFACE("iface");
    static const std::string WVLAN("vlan");
    static const std::string WIP("ip-address");

    auto vxlan = properties.get_child_optional(ENCAP_VXLAN);
    auto ivxlan = properties.get_child_optional(ENCAP_IVXLAN);
    auto vlan = properties.get_child_optional(ENCAP_VLAN);
    auto vr = properties.get_child_optional(VIRTUAL_ROUTER);
    auto x_connect = properties.get_child_optional(CROSS_CONNECT);

    if (vlan)
    {
        uplinkIface = vlan.get().get<std::string>(UPLINK_IFACE, "");
        uplinkVlan = vlan.get().get<uint16_t>(UPLINK_VLAN, 0);
        vppManager->uplink().set(uplinkIface,
                                 uplinkVlan,
                                 vlan.get().get<std::string>(ENCAP_IFACE, ""));
        auto slaves = vlan.get().get_child_optional(UPLINK_SLAVES);

        if (slaves)
        {
            for (auto s : slaves.get())
            {
                vppManager->uplink().insert_slave_ifaces(s.second.data());
                LOG(opflexagent::INFO) << s.second.data();
            }
        }
        auto dhcp_options = vlan.get().get_child_optional(DHCP_OPTIONS);
        if (dhcp_options)
        {
            for (auto d : dhcp_options.get())
            {
                vppManager->uplink().insert_dhcp_options(d.second.data());
                LOG(opflexagent::INFO) << d.second.data();
            }
        }
        encapType = encapTypeVlan;
    }
    else if (vxlan)
    {
        boost::asio::ip::address remote_ip;
        boost::system::error_code ec;

        remote_ip = boost::asio::ip::address::from_string(
            vxlan.get().get<std::string>(REMOTE_IP, ""));

        if (ec)
        {
            LOG(ERROR) << "Invalid tunnel destination IP: "
                       << vxlan.get().get<std::string>(REMOTE_IP, "") << ": "
                       << ec.message();
        }
        else
        {
            uplinkIface = vxlan.get().get<std::string>(UPLINK_IFACE, "");
            uplinkVlan = vxlan.get().get<uint16_t>(UPLINK_VLAN, 0);
            vppManager->uplink().set(
                uplinkIface,
                uplinkVlan,
                vxlan.get().get<std::string>(ENCAP_IFACE, ""),
                remote_ip,
                vxlan.get().get<uint16_t>(REMOTE_PORT, 4789));
            auto slaves = properties.get_child_optional(UPLINK_SLAVES);
        }
        encapType = encapTypeVxlan;
    }
    else if (ivxlan)
    {
        uplinkIface = ivxlan.get().get<std::string>(UPLINK_IFACE, "");
        uplinkVlan = ivxlan.get().get<uint16_t>(UPLINK_VLAN, 0);
        vppManager->uplink().set(
            uplinkIface,
            uplinkVlan,
            ivxlan.get().get<std::string>(ENCAP_IFACE, ""));
        auto slaves = ivxlan.get().get_child_optional(UPLINK_SLAVES);

        if (slaves)
        {
            for (auto s : slaves.get())
            {
                vppManager->uplink().insert_slave_ifaces(s.second.data());
                LOG(opflexagent::INFO) << s.second.data();
            }
        }
        auto dhcp_options = ivxlan.get().get_child_optional(DHCP_OPTIONS);
        if (dhcp_options)
        {
            for (auto d : dhcp_options.get())
            {
                vppManager->uplink().insert_dhcp_options(d.second.data());
                LOG(opflexagent::INFO) << d.second.data();
            }
        }
        encapType = encapTypeIvxlan;
    }
    if (vr)
    {
        vppManager->setVirtualRouter(
            vr.get().get<bool>(VIRTUAL_ROUTER, true),
            vr.get().get<bool>(VIRTUAL_ROUTER_RA, true),
            vr.get().get<std::string>(VIRTUAL_ROUTER_MAC, "00:22:bd:f8:19:ff"));
    }

    if (x_connect)
    {
        for (auto x : x_connect.get())
        {
            auto east = x.second.get_child_optional(EAST);
            auto west = x.second.get_child_optional(WEST);
            if (east && west)
            {
                std::string ename = east.get().get<std::string>(EIFACE, "");
                uint16_t evlan = east.get().get<uint16_t>(EVLAN, 0);
                std::string eip = east.get().get<std::string>(EIP, "0.0.0.0");
                std::string etag_rewrite =
                    east.get().get<std::string>(ETAG_REWRITE, "");
                std::string wname = west.get().get<std::string>(WIFACE, "");
                uint16_t wvlan = west.get().get<uint16_t>(WVLAN, 0);
                std::string wip = west.get().get<std::string>(WIP, "0.0.0.0");
                LOG(opflexagent::DEBUG) << "east:[" << ename << " , " << evlan
                                        << " , " << eip << " , " << etag_rewrite
                                        << "]";
                LOG(opflexagent::DEBUG) << "west:[" << wname << " , " << wvlan
                                        << " , " << wip << "]";
                if (!ename.empty() && !wname.empty())
                {
                    VPP::CrossConnect::xconnect_t xcon_east(
                        ename, evlan, eip, etag_rewrite);
                    VPP::CrossConnect::xconnect_t xcon_west(wname, wvlan, wip);
                    vppManager->crossConnect().insert_xconnect(
                        std::make_pair(xcon_east, xcon_west));
                }
            }
        }
    }

    /*
     * Are we opening an inspection socket?
     */
    auto inspect = properties.get<std::string>("inspect-socket", "");

    if (inspect.length())
    {
        inspector.reset(new VppInspect(inspect));
    }
}

void
VppRenderer::start()
{
    // Called during agent startup
    if (started) return;
    started = true;
    vppManager->start();
    vppManager->registerModbListeners();
    if ((encapType == encapTypeVxlan) || (encapType == encapTypeIvxlan))
    {
        tunnelEpManager.setUplinkIface(uplinkIface);
        tunnelEpManager.setUplinkVlan(uplinkVlan);
        tunnelEpManager.setParentRenderer(this);
        tunnelEpManager.start();
    }
    LOG(opflexagent::INFO) << "Starting vpp renderer plugin";
}

void
VppRenderer::stop()
{
    // Called during agent shutdown
    if (!started) return;
    started = false;
    LOG(opflexagent::INFO) << "Stopping vpp renderer plugin";
    if ((encapType == encapTypeVxlan) || (encapType == encapTypeIvxlan))
    {
        tunnelEpManager.stop();
    }
    vppManager->stop();
}

boost::asio::ip::address VppRenderer::getUplinkAddress()
{
    const boost::asio::ip::address addr =
        vppManager->uplink().local_address();
    return addr;
}

std::string
VppRenderer::getUplinkMac()
{
    return vppManager->uplink().uplink_l2_address();
}

} // namespace VPP

extern "C" const opflexagent::RendererPlugin *
init_renderer_plugin()
{
    // Return a plugin implementation, which can ini
    static const VPP::VppRendererPlugin vppPlugin;

    return &vppPlugin;
}