Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include <boost/asio.hpp>
7 : #include <boost/bind/bind.hpp>
8 :
9 : #include <base/logging.h>
10 : #include <db/db_entry.h>
11 : #include <db/db_table.h>
12 : #include <db/db_table_partition.h>
13 : #include <ksync/ksync_index.h>
14 : #include <ksync/ksync_entry.h>
15 : #include <ksync/ksync_object.h>
16 : #include <ksync/ksync_netlink.h>
17 : #include <ksync/ksync_sock.h>
18 :
19 : #include "oper/interface_common.h"
20 : #include "oper/nexthop.h"
21 : #include "oper/vxlan.h"
22 : #include "oper/mirror_table.h"
23 :
24 : #include "vrouter/ksync/interface_ksync.h"
25 : #include "vrouter/ksync/nexthop_ksync.h"
26 : #include "vrouter/ksync/vxlan_ksync.h"
27 :
28 : #include "ksync_init.h"
29 :
30 : using namespace boost::placeholders;
31 :
32 5 : VxLanIdKSyncEntry::VxLanIdKSyncEntry(VxLanKSyncObject *obj,
33 : const VxLanIdKSyncEntry *entry,
34 5 : uint32_t index) :
35 5 : KSyncNetlinkDBEntry(index), ksync_obj_(obj), label_(entry->label_),
36 5 : nh_(NULL) {
37 5 : }
38 :
39 5 : VxLanIdKSyncEntry::VxLanIdKSyncEntry(VxLanKSyncObject *obj,
40 5 : const VxLanId *vxlan_id) :
41 5 : KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
42 5 : label_(vxlan_id->vxlan_id()), nh_(NULL) {
43 5 : }
44 :
45 20 : VxLanIdKSyncEntry::~VxLanIdKSyncEntry() {
46 20 : }
47 :
48 25 : KSyncDBObject *VxLanIdKSyncEntry::GetObject() const {
49 25 : return ksync_obj_;
50 : }
51 :
52 15 : bool VxLanIdKSyncEntry::IsLess(const KSyncEntry &rhs) const {
53 15 : const VxLanIdKSyncEntry &entry =
54 : static_cast<const VxLanIdKSyncEntry &>(rhs);
55 :
56 15 : return label_ < entry.label_;
57 : }
58 :
59 20 : std::string VxLanIdKSyncEntry::ToString() const {
60 20 : std::stringstream s;
61 20 : NHKSyncEntry *nexthop = nh();
62 :
63 20 : if (nexthop) {
64 20 : s << "VXLAN Label: " << label_ << " Index : "
65 20 : << GetIndex() << nexthop->ToString();
66 : } else {
67 0 : s << "VXLAN Label: " << label_ << " Index : "
68 0 : << GetIndex() << " NextHop : <null>";
69 : }
70 40 : return s.str();
71 20 : }
72 :
73 5 : bool VxLanIdKSyncEntry::Sync(DBEntry *e) {
74 5 : bool ret = false;
75 5 : const VxLanId *vxlan_id = static_cast<VxLanId *>(e);
76 :
77 5 : NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
78 5 : if (vxlan_id->nexthop() == NULL) {
79 0 : LOG(DEBUG, "nexthop in network-id label is null");
80 0 : assert(0);
81 : }
82 5 : NHKSyncEntry nexthop(nh_object, vxlan_id->nexthop());
83 5 : NHKSyncEntry *old_nh = nh();
84 :
85 5 : nh_ = nh_object->GetReference(&nexthop);
86 5 : if (old_nh != nh()) {
87 5 : ret = true;
88 : }
89 :
90 5 : return ret;
91 5 : };
92 :
93 10 : int VxLanIdKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
94 10 : vr_vxlan_req encoder;
95 : int encode_len;
96 10 : NHKSyncEntry *nexthop = nh();
97 :
98 10 : encoder.set_h_op(op);
99 10 : encoder.set_vxlanr_rid(0);
100 10 : encoder.set_vxlanr_vnid(label_);
101 10 : encoder.set_vxlanr_nhid(nexthop->nh_id());
102 10 : int error = 0;
103 10 : encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
104 10 : assert(error == 0);
105 10 : assert(encode_len <= buf_len);
106 10 : return encode_len;
107 10 : }
108 :
109 10 : void VxLanIdKSyncEntry::FillObjectLog(sandesh_op::type op,
110 : KSyncVxLanInfo &info) const {
111 10 : info.set_label(label_);
112 10 : info.set_nh(nh()->nh_id());
113 :
114 10 : if (op == sandesh_op::ADD) {
115 5 : info.set_operation("ADD/CHANGE");
116 : } else {
117 5 : info.set_operation("DELETE");
118 : }
119 10 : }
120 :
121 5 : int VxLanIdKSyncEntry::AddMsg(char *buf, int buf_len) {
122 5 : KSyncVxLanInfo info;
123 5 : FillObjectLog(sandesh_op::ADD, info);
124 5 : KSYNC_TRACE(VxLan, GetObject(), info);
125 :
126 10 : return Encode(sandesh_op::ADD, buf, buf_len);
127 5 : }
128 :
129 0 : int VxLanIdKSyncEntry::ChangeMsg(char *buf, int buf_len) {
130 0 : KSyncVxLanInfo info;
131 0 : FillObjectLog(sandesh_op::ADD, info);
132 0 : KSYNC_TRACE(VxLan, GetObject(), info);
133 :
134 0 : return Encode(sandesh_op::ADD, buf, buf_len);
135 0 : }
136 :
137 5 : int VxLanIdKSyncEntry::DeleteMsg(char *buf, int buf_len) {
138 5 : KSyncVxLanInfo info;
139 5 : FillObjectLog(sandesh_op::DEL, info);
140 5 : KSYNC_TRACE(VxLan, GetObject(), info);
141 :
142 10 : return Encode(sandesh_op::DEL, buf, buf_len);
143 5 : }
144 :
145 5 : KSyncEntry *VxLanIdKSyncEntry::UnresolvedReference() {
146 5 : NHKSyncEntry *nexthop = nh();
147 5 : if (!nexthop->IsResolved()) {
148 0 : return nexthop;
149 : }
150 5 : return NULL;
151 : }
152 :
153 1 : VxLanKSyncObject::VxLanKSyncObject(KSync *ksync)
154 1 : : KSyncDBObject("KSync VxLan"), ksync_(ksync) {
155 1 : }
156 :
157 2 : VxLanKSyncObject::~VxLanKSyncObject() {
158 2 : }
159 :
160 1 : void VxLanKSyncObject::RegisterDBClients() {
161 1 : RegisterDb(ksync_->agent()->vxlan_table());
162 1 : }
163 :
164 5 : KSyncEntry *VxLanKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
165 5 : const VxLanIdKSyncEntry *vxlan =
166 : static_cast<const VxLanIdKSyncEntry *>(entry);
167 5 : VxLanIdKSyncEntry *ksync = new VxLanIdKSyncEntry(this, vxlan, index);
168 5 : return static_cast<KSyncEntry *>(ksync);
169 : }
170 :
171 5 : KSyncEntry *VxLanKSyncObject::DBToKSyncEntry(const DBEntry *e) {
172 5 : const VxLanId *vxlan = static_cast<const VxLanId *>(e);
173 5 : VxLanIdKSyncEntry *key = new VxLanIdKSyncEntry(this, vxlan);
174 5 : return static_cast<KSyncEntry *>(key);
175 : }
176 :
177 10 : void vr_vxlan_req::Process(SandeshContext *context) {
178 10 : AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
179 10 : ioc->VxLanMsgHandler(this);
180 10 : }
181 :
|