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