summaryrefslogtreecommitdiffstats
path: root/bgpd/bgp_engine.c
blob: 3e297849fb063347af11cea199aa7526a5e1d9cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* BGP Engine pThread -- functions
 * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <zebra.h>

#include "bgpd/bgp_engine.h"

#include "lib/memory.h"
#include "lib/symtab.h"

/*==============================================================================
 * The BGP Engine pThread contains:
 *
 *   * the BGP Finite State Machine (FSM) for BGP Sessions
 *   * the encoding and decoding of BGP protocol messages
 *   * all related socket handling and I/O
 *   * all related timers
 *
 * The BGP Engine communicates with the BGP Routeing pthread(s) via two Message
 * Queues.
 *
 * There are also shared pools of data:
 *
 *   * the BGP session structures, which ...
 *   * the Attributes Store
 *   * the Prefixes Store
 *   ...
 *
 * Other pthread-safe facilities used:
 *
 *   * logging
 *   * privilege raising/lowering
 *   ...
 *
 * At the heart of the BGP Engine pthread is a select for the socket I/O, a
 * Message Queue reader and a timer handler.  The Message Queue uses SIGUSR2 to
 * kick the pthread into action if it is stopped on the select.
 *
 */

/*==============================================================================
 * The qpnexus for the BGP Engine.
 *
 *
 */

qpn_nexus p_bgp_engine ;

static struct qpn_nexus  bgp_engine ;


/*==============================================================================
 * Start the BGP Engine Thread.
 *
 * Initialise the Engine Thread qpnexus
 *
 */

static void* bgp_engine_loop(void* arg) ;

extern void
bgp_engine_start(void)
{
  p_bgp_engine = qpn_init_new(&bgp_engine) ;

  p_bgp_engine->start     = bgp_engine_loop ;

  p_bgp_engine->thread_id = qpt_thread_self() ;

  p_bgp_engine->selection = qps_selection_init_new(NULL) ;
  p_bgp_engine->pile      = qtimer_pile_init_new(NULL) ;
  p_bgp_engine->queue     = mqueue_init_new(NULL, mqt_signal_broadcast) ;
  p_bgp_engine->mts       = mqueue_thread_signal_init(NULL,
                                           p_bgp_engine->thread_id, SIGMQUEUE) ;

  qpn_exec(p_bgp_engine) ;
} ;

/*==============================================================================
 * The BGP Engine Thread main loop
 *
 * Processes:
 *
 *   1) connections with pending work -- local queue.
 *
 *      When a connection fills its output buffers, any further messages
 *      requiring output are placed on the connection's pending queue.
 *
 *      When the output buffers empty sufficiently, some of those messages
 *      can (finally) be processed.
 *
 *      So this is done first.
 *
 *      [This is also where stopped connections are finally reaped.]
 *
 *   2) messages coming from the Routeing Engine -- mqueue_queue.
 *
 *      These will mostly be BGP UPDATE messages, which will either be
 *      processed into the relevant connection's output buffers, or end up
 *      on its pending queue.
 *
 *      There is a flow control mechanism to prevent the Routeing Engine from
 *      flooding the BGP Engine with UPDATE messages.
 *
 *      Other messages start/stop sessions and so on.
 *
 *   3) I/O -- qpselect
 *
 *      This deals with all active sockets for read/write/connect/accept.
 *
 *      Each time a socket is readable, one message is read and dispatched to
 *      the Routeing Engine (or otherwise dealt with by the FSM).
 *
 *      Each time a socket is writable, as much as possible is written to it
 *      from the connection's write buffer.  When the write buffer is drained,
 *      the connection goes back onto the local queue.
 *
 *   4) Timers -- qtimers
 *
 *      Which generate FSM events.
 *
 *
 */








/*==============================================================================
 * The qpnexus for the BGP Engine.
 *
 *
 */



/*==============================================================================
 * The write queue for the BGP Engine
 *
 * Each connection has a single message buffer.  When that has yet to be
 *
 *
 */