OpenSDN source code
controller_vrf_export.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/uuid/uuid_io.hpp>
7 
8 #include <cmn/agent_cmn.h>
9 #include <oper/route_common.h>
11 #include <oper/vrf.h>
12 #include <oper/mirror_table.h>
13 
19 #include <controller/controller_types.h>
20 
21 VrfExport::State::State() : DBState(), exported_(false),
22  force_chg_(false), rt_export_(), last_sequence_number_(0) {
23 };
24 
26 };
27 
28 bool VrfExport::State::IsExportable(uint64_t sequence_number) {
29  // Sequence number passed in argument is of channel to which this state
30  // belongs. Once channel sends the vrf subscription after flap/fresh
31  // connection, state's sequence number will be updated to that of channel.
32  // After this all routes in this VRF become eligible for export.
33  // This is needed as control-node mandates that VRF is subscribed before any
34  // route is seen in same else it will again flap the
35  // connection(intentionally).
36  // Note: In case of flaps etc CN removes VRF subscription.
37  // This sequence number macthing helps in making sure that VRF sub is
38  // sent before any route is published.
39  return (last_sequence_number_ != sequence_number);
40 }
41 
42 void VrfExport::Notify(const Agent *agent, AgentXmppChannel *bgp_xmpp_peer,
43  DBTablePartBase *partition, DBEntryBase *e) {
44 
45  BgpPeer *bgp_peer = static_cast<BgpPeer *>(bgp_xmpp_peer->bgp_peer_id());
46  VrfEntry *vrf = static_cast<VrfEntry *>(e);
47  //PBB VRF is implictly created, agent is not supposed to send RI
48  //subscription message since control-node will not be aware of this RI
49  //We still want to set a state and subscribe for bridge table for
50  //building ingress replication tree
51  bool send_subscribe = vrf->ShouldExportRoute();
52 
53  uint32_t instance_id = vrf->rd();
54  //Instance ID being zero is possible because of VN unavailability and VRF
55  //ending up with NULL VRF. Reason being config sequence.
56  //So seeing 0 instance_id delete the state so that resubscribe can be done
57  //with new id and then re-export all route.
58  //Note: Assumption is that instance id will never change from non zero to
59  //some other non zero value.
60  //Also Instance ID check is for TSN and TA only.
61  bool deleted = (vrf->IsDeleted()) || (instance_id == VrfEntry::kInvalidIndex);
62 
63  if (deleted) {
64  bgp_peer->DeleteVrfState(partition, e);
65  if (!AgentXmppChannel::IsXmppChannelActive(agent, bgp_xmpp_peer)) {
66  return;
67  }
68  if (bgp_peer) {
69  CONTROLLER_TRACE(Trace, bgp_peer->GetName(), vrf->GetName(),
70  "VRF deleted, remove state");
71  bgp_peer->DeleteVrfState(partition, e);
72  }
73  return;
74  }
75 
76  if (!AgentXmppChannel::IsBgpPeerActive(agent, bgp_xmpp_peer))
77  return;
78 
80  State *state = static_cast<State *>(vrf->GetState(partition->parent(), id));
81  uint8_t table_type;
82  if (state == NULL) {
83  state = new State();
84  state->exported_ = false;
85  state->mcast_exported_ = false;
86  state->force_chg_ = true;
87  vrf->SetState(partition->parent(), id, state);
88 
89  // Dont export routes belonging to Fabric VRF table
90  for (table_type = (Agent::INVALID + 1);
91  table_type < Agent::ROUTE_TABLE_MAX; table_type++) {
92  state->rt_export_[table_type] =
94  static_cast<AgentRouteTable *>
95  (vrf->GetRouteTable(table_type)),
96  bgp_xmpp_peer);
97  }
98  }
99 
100  if (state->last_sequence_number_ == bgp_xmpp_peer->sequence_number()) {
101  state->force_chg_ = false;
102  } else {
103  state->last_sequence_number_ = bgp_xmpp_peer->sequence_number();
104  }
105 
106  if (send_subscribe == false) {
107  state->force_chg_ = false;
108  }
109 
110  if (send_subscribe && ((state->exported_ == false) ||
111  (state->force_chg_ == true))) {
112  if (AgentXmppChannel::ControllerSendSubscribe(bgp_xmpp_peer, vrf,
113  true)) {
114  CONTROLLER_TRACE(Trace, bgp_peer->GetName(), vrf->GetName(),
115  "Subscribe");
116 
117  state->exported_ = true;
118  if (state->force_chg_ == true) {
119  bgp_peer->route_walker()->StartRouteWalk(vrf, true,
121  state->force_chg_ = false;
122 
123  if (Agent::GetInstance()->mulitcast_builder() == bgp_xmpp_peer) {
124  state->mcast_exported_ = true;
125  } else {
126  state->mcast_exported_ = false;
127  }
128  }
129  return;
130  }
131  } else {
132  if (send_subscribe &&
133  (Agent::GetInstance()->mulitcast_builder() == bgp_xmpp_peer) &&
134  !state->mcast_exported_) {
135  // When Agent switches to a new multicast builder, we have to
136  // send multicast subscribe to the new multicast builder. Otherwise,
137  // Agent can get stuck with old multicast tree affecting BUM
138  // traffic.
139  bgp_peer->route_walker()->StartRouteWalk(vrf, true,
141  state->mcast_exported_ = true;
142  } else if (send_subscribe &&
143  Agent::GetInstance()->mulitcast_builder() != bgp_xmpp_peer) {
144  // If it is not the multicast builder, mark the flag false so that
145  // if it becomes multicast builder we notify mutlcast routes
146  state->mcast_exported_ = false;
147  } else {
148  CONTROLLER_TRACE(Trace, bgp_peer->GetName(), vrf->GetName(),
149  "Already subscribed");
150  }
151  }
152 }
Agent supports multiple route tables - Inet-unicast (IPv4/IPv6), Inet-multicast, bridge,...
Definition: agent_route.h:110
uint64_t sequence_number() const
static bool IsBgpPeerActive(const Agent *agent, AgentXmppChannel *peer)
static bool IsXmppChannelActive(const Agent *agent, AgentXmppChannel *peer)
static bool ControllerSendSubscribe(AgentXmppChannel *peer, VrfEntry *vrf, bool subscribe)
BgpPeer * bgp_peer_id()
Definition: agent.h:361
@ ROUTE_TABLE_MAX
Definition: agent.h:426
@ INVALID
Definition: agent.h:419
static Agent * GetInstance()
Definition: agent.h:439
DBTableBase::ListenerId GetVrfExportListenerId()
Definition: peer.h:172
ControllerRouteWalker * route_walker() const
Definition: peer.cc:243
void DeleteVrfState(DBTablePartBase *partition, DBEntryBase *entry)
Definition: peer.cc:260
void StartRouteWalk(VrfEntry *vrf, bool associate, Type type)
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:38
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:23
int ListenerId
Definition: db_table.h:63
DBTableBase * parent()
const std::string & GetName() const
Definition: peer.h:87
static RouteExport * Init(AgentRouteTable *table, AgentXmppChannel *bgp_xmpp_peer)
Definition: trace.h:289
Definition: vrf.h:90
const string & GetName() const
Definition: vrf.h:104
int rd() const
Definition: vrf.h:224
bool ShouldExportRoute() const
Definition: vrf.h:136
static const uint32_t kInvalidIndex
Definition: vrf.h:92
AgentRouteTable * GetRouteTable(uint8_t table_type) const
Definition: vrf.cc:286
static void Notify(const Agent *agent, AgentXmppChannel *, DBTablePartBase *partition, DBEntryBase *e)
#define CONTROLLER_TRACE(obj,...)
bool IsExportable(uint64_t sequence_number)
RouteExport * rt_export_[Agent::ROUTE_TABLE_MAX]