OpenSDN source code
vxlan_ksync.cc
Go to the documentation of this file.
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 
27 
28 #include "ksync_init.h"
29 
30 using namespace boost::placeholders;
31 
33  const VxLanIdKSyncEntry *entry,
34  uint32_t index) :
35  KSyncNetlinkDBEntry(index), ksync_obj_(obj), label_(entry->label_),
36  nh_(NULL) {
37 }
38 
40  const VxLanId *vxlan_id) :
41  KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
42  label_(vxlan_id->vxlan_id()), nh_(NULL) {
43 }
44 
46 }
47 
49  return ksync_obj_;
50 }
51 
52 bool VxLanIdKSyncEntry::IsLess(const KSyncEntry &rhs) const {
53  const VxLanIdKSyncEntry &entry =
54  static_cast<const VxLanIdKSyncEntry &>(rhs);
55 
56  return label_ < entry.label_;
57 }
58 
59 std::string VxLanIdKSyncEntry::ToString() const {
60  std::stringstream s;
61  NHKSyncEntry *nexthop = nh();
62 
63  if (nexthop) {
64  s << "VXLAN Label: " << label_ << " Index : "
65  << GetIndex() << nexthop->ToString();
66  } else {
67  s << "VXLAN Label: " << label_ << " Index : "
68  << GetIndex() << " NextHop : <null>";
69  }
70  return s.str();
71 }
72 
74  bool ret = false;
75  const VxLanId *vxlan_id = static_cast<VxLanId *>(e);
76 
77  NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
78  if (vxlan_id->nexthop() == NULL) {
79  LOG(DEBUG, "nexthop in network-id label is null");
80  assert(0);
81  }
82  NHKSyncEntry nexthop(nh_object, vxlan_id->nexthop());
83  NHKSyncEntry *old_nh = nh();
84 
85  nh_ = nh_object->GetReference(&nexthop);
86  if (old_nh != nh()) {
87  ret = true;
88  }
89 
90  return ret;
91 };
92 
93 int VxLanIdKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
94  vr_vxlan_req encoder;
95  int encode_len;
96  NHKSyncEntry *nexthop = nh();
97 
98  encoder.set_h_op(op);
99  encoder.set_vxlanr_rid(0);
100  encoder.set_vxlanr_vnid(label_);
101  encoder.set_vxlanr_nhid(nexthop->nh_id());
102  int error = 0;
103  encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
104  assert(error == 0);
105  assert(encode_len <= buf_len);
106  return encode_len;
107 }
108 
110  KSyncVxLanInfo &info) const {
111  info.set_label(label_);
112  info.set_nh(nh()->nh_id());
113 
114  if (op == sandesh_op::ADD) {
115  info.set_operation("ADD/CHANGE");
116  } else {
117  info.set_operation("DELETE");
118  }
119 }
120 
121 int VxLanIdKSyncEntry::AddMsg(char *buf, int buf_len) {
122  KSyncVxLanInfo info;
123  FillObjectLog(sandesh_op::ADD, info);
124  KSYNC_TRACE(VxLan, GetObject(), info);
125 
126  return Encode(sandesh_op::ADD, buf, buf_len);
127 }
128 
129 int VxLanIdKSyncEntry::ChangeMsg(char *buf, int buf_len) {
130  KSyncVxLanInfo info;
131  FillObjectLog(sandesh_op::ADD, info);
132  KSYNC_TRACE(VxLan, GetObject(), info);
133 
134  return Encode(sandesh_op::ADD, buf, buf_len);
135 }
136 
137 int VxLanIdKSyncEntry::DeleteMsg(char *buf, int buf_len) {
138  KSyncVxLanInfo info;
139  FillObjectLog(sandesh_op::DEL, info);
140  KSYNC_TRACE(VxLan, GetObject(), info);
141 
142  return Encode(sandesh_op::DEL, buf, buf_len);
143 }
144 
146  NHKSyncEntry *nexthop = nh();
147  if (!nexthop->IsResolved()) {
148  return nexthop;
149  }
150  return NULL;
151 }
152 
154  : KSyncDBObject("KSync VxLan"), ksync_(ksync) {
155 }
156 
158 }
159 
162 }
163 
164 KSyncEntry *VxLanKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
165  const VxLanIdKSyncEntry *vxlan =
166  static_cast<const VxLanIdKSyncEntry *>(entry);
167  VxLanIdKSyncEntry *ksync = new VxLanIdKSyncEntry(this, vxlan, index);
168  return static_cast<KSyncEntry *>(ksync);
169 }
170 
172  const VxLanId *vxlan = static_cast<const VxLanId *>(e);
173  VxLanIdKSyncEntry *key = new VxLanIdKSyncEntry(this, vxlan);
174  return static_cast<KSyncEntry *>(key);
175 }
176 
177 void vr_vxlan_req::Process(SandeshContext *context) {
178  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
179  ioc->VxLanMsgHandler(this);
180 }
181 
virtual void VxLanMsgHandler(vr_vxlan_req *req)=0
VxLanTable * vxlan_table() const
Definition: agent.h:538
void RegisterDb(DBTableBase *table)
size_t GetIndex() const
Definition: ksync_entry.h:175
bool IsResolved()
KSyncEntry * GetReference(const KSyncEntry *key)
Agent * agent() const
Definition: ksync_init.h:40
NHKSyncObject * nh_ksync_obj() const
Definition: ksync_init.h:44
virtual std::string ToString() const
uint32_t nh_id() const
Definition: nexthop_ksync.h:61
VxLanKSyncObject * ksync_obj_
Definition: vxlan_ksync.h:48
KSyncDBObject * GetObject() const
Definition: vxlan_ksync.cc:48
virtual int ChangeMsg(char *buf, int buf_len)
Definition: vxlan_ksync.cc:129
virtual bool Sync(DBEntry *e)
Definition: vxlan_ksync.cc:73
virtual int AddMsg(char *buf, int buf_len)
Definition: vxlan_ksync.cc:121
virtual ~VxLanIdKSyncEntry()
Definition: vxlan_ksync.cc:45
virtual int DeleteMsg(char *buf, int buf_len)
Definition: vxlan_ksync.cc:137
virtual KSyncEntry * UnresolvedReference()
Definition: vxlan_ksync.cc:145
void FillObjectLog(sandesh_op::type op, KSyncVxLanInfo &info) const
Definition: vxlan_ksync.cc:109
virtual std::string ToString() const
Definition: vxlan_ksync.cc:59
VxLanIdKSyncEntry(VxLanKSyncObject *obj, const VxLanIdKSyncEntry *entry, uint32_t index)
Definition: vxlan_ksync.cc:32
KSyncEntryPtr nh_
Definition: vxlan_ksync.h:50
int Encode(sandesh_op::type op, char *buf, int buf_len)
Definition: vxlan_ksync.cc:93
virtual bool IsLess(const KSyncEntry &rhs) const
Definition: vxlan_ksync.cc:52
NHKSyncEntry * nh() const
Definition: vxlan_ksync.h:33
Definition: vxlan.h:14
const NextHop * nexthop() const
Definition: vxlan.h:28
VxLanKSyncObject(KSync *ksync)
Definition: vxlan_ksync.cc:153
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)
Definition: vxlan_ksync.cc:171
void RegisterDBClients()
Definition: vxlan_ksync.cc:160
virtual ~VxLanKSyncObject()
Definition: vxlan_ksync.cc:157
virtual KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Definition: vxlan_ksync.cc:164
KSync * ksync() const
Definition: vxlan_ksync.h:59
#define KSYNC_TRACE(obj, parent,...)
Definition: ksync_object.h:305
uint8_t type
Definition: load_balance.h:2
#define LOG(_Level, _Msg)
Definition: logging.h:35