aboutsummaryrefslogtreecommitdiffstats
path: root/src/VppCrossConnect.cpp
blob: e10f03ce1a9ba0cdd9aa8067488fefb1bb6f7b7c (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
/* -*- 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 "VppCrossConnect.hpp"

#include <opflexagent/logging.h>

#include "vom/interface.hpp"
#include "vom/l2_xconnect.hpp"
#include "vom/sub_interface.hpp"
#include "vom/tap_interface.hpp"

using namespace VOM;

namespace VPP
{

static const std::string XCONNECT_KEY = "__xconnect__";

CrossConnect::xconnect_t::xconnect_t(const std::string &name,
                                     uint16_t vlan,
                                     std::string ip_address,
                                     std::string tag_rewrite)
    : name(name)
    , vlan(vlan)
    , ip(boost::asio::ip::address::from_string(ip_address))
    , tag_rewrite(tag_rewrite)
{
}

std::string
CrossConnect::xconnect_t::to_string() const
{
    std::ostringstream s;
    s << "[itf:" << name << " vlan:" << vlan << " ip:" << ip.to_string()
      << " tag-rewrite:" << tag_rewrite << "]";

    return (s.str());
}

CrossConnect::CrossConnect()
{
}

void
CrossConnect::insert_xconnect(CrossConnect::xconnect xconn)
{
    this->xconnects.push_back(xconn);
}

static VOM::interface::type_t
getIntfTypeFromName(std::string &name)
{
    if (name.find("Bond") != std::string::npos)
        return VOM::interface::type_t::BOND;
    else if (name.find("Ethernet") != std::string::npos)
        return VOM::interface::type_t::ETHERNET;
    else if (name.find("tapv2") != std::string::npos)
        return VOM::interface::type_t::TAPV2;

    return VOM::interface::type_t::AFPACKET;
}

void
CrossConnect::configure_xconnect()
{

    LOG(opflexagent::INFO) << "configure cross connect";

    for (auto it : xconnects)
    {
        std::shared_ptr<interface> itf_ptr, xitf_ptr;
        VOM::interface::type_t type = getIntfTypeFromName(it.first.name);
        if (type == VOM::interface::type_t::TAPV2)
        {
            VOM::route::prefix_t pfx(it.first.ip, 24);
            tap_interface itf(it.first.name, interface::admin_state_t::UP, pfx);
            OM::write(XCONNECT_KEY, itf);
            itf_ptr = itf.singular();
        }
        else
        {
            interface itf(it.first.name, type, interface::admin_state_t::UP);
            OM::write(XCONNECT_KEY, itf);
            itf_ptr = itf.singular();
        }
        if (it.first.vlan)
        {
            /*
             * now create the sub-interface on which control and data traffic
             * from
             * the upstream will arrive
             */
            sub_interface subitf(
                *itf_ptr, interface::admin_state_t::UP, it.first.vlan);
            OM::write(XCONNECT_KEY, subitf);
            itf_ptr = subitf.singular();
        }
        VOM::interface::type_t type2 = getIntfTypeFromName(it.second.name);
        if (type2 == VOM::interface::type_t::TAPV2)
        {
            VOM::route::prefix_t pfx(it.second.ip, 24);
            tap_interface xitf(
                it.second.name, interface::admin_state_t::UP, pfx);
            OM::write(XCONNECT_KEY, xitf);
            xitf_ptr = xitf.singular();
        }
        else
        {
            interface xitf(it.second.name, type2, interface::admin_state_t::UP);
            OM::write(XCONNECT_KEY, xitf);
            xitf_ptr = xitf.singular();
        }
        if (it.second.vlan)
        {
            /*
             * now create the sub-interface on which control and data traffic
             * from
             * the upstream will arrive
             */
            sub_interface xsubitf(
                *xitf_ptr, interface::admin_state_t::UP, it.second.vlan);
            OM::write(XCONNECT_KEY, xsubitf);
            xitf_ptr = xsubitf.singular();
        }
        VOM::l2_xconnect l2_xconn(*itf_ptr, *xitf_ptr);
        if ((type == VOM::interface::type_t::ETHERNET ||
             type == VOM::interface::type_t::AFPACKET) &&
            it.first.vlan &&
            (it.first.tag_rewrite.find("pop") != std::string::npos))
            l2_xconn.set(VOM::l2_vtr::option_t::POP_1, it.first.vlan);
        OM::write(XCONNECT_KEY, l2_xconn);
    }
}

} // namespace VPP

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