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