1 // L2TPNS Clustering Stuff
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <sys/ioctl.h>
31 * All cluster packets have the same format.
33 * One or more instances of
35 * a 32 bit 'extra' data dependant on the 'type'.
36 * zero or more bytes of structure data, dependant on the type.
41 extern int cluster_sockfd
; // The filedescriptor for the cluster communications port.
43 in_addr_t my_address
= 0; // The network address of my ethernet port.
44 static int walk_session_number
= 0; // The next session to send when doing the slow table walk.
45 static int walk_bundle_number
= 0; // The next bundle to send when doing the slow table walk.
46 static int walk_tunnel_number
= 0; // The next tunnel to send when doing the slow table walk.
47 int forked
= 0; // Sanity check: CLI must not diddle with heartbeat table
49 #define MAX_HEART_SIZE (8192) // Maximum size of heartbeat packet. Must be less than max IP packet size :)
50 #define MAX_CHANGES (MAX_HEART_SIZE/(sizeof(sessiont) + sizeof(int) ) - 2) // Assumes a session is the biggest type!
55 } cluster_changes
[MAX_CHANGES
]; // Queue of changed structures that need to go out when next heartbeat.
60 uint8_t data
[MAX_HEART_SIZE
];
61 } past_hearts
[HB_HISTORY_SIZE
]; // Ring buffer of heartbeats that we've recently sent out. Needed so
62 // we can re-transmit if needed.
69 } peers
[CLUSTER_MAX_SIZE
]; // List of all the peers we've heard from.
70 static int num_peers
; // Number of peers in list.
72 static int rle_decompress(uint8_t **src_p
, int ssize
, uint8_t *dst
, int dsize
);
73 static int rle_compress(uint8_t **src_p
, int ssize
, uint8_t *dst
, int dsize
);
76 // Create a listening socket
78 // This joins the cluster multi-cast group.
82 struct sockaddr_in addr
;
83 struct sockaddr_in interface_addr
;
88 config
->cluster_undefined_sessions
= MAXSESSION
-1;
89 config
->cluster_undefined_bundles
= MAXBUNDLE
-1;
90 config
->cluster_undefined_tunnels
= MAXTUNNEL
-1;
92 if (!config
->cluster_address
)
94 if (!*config
->cluster_interface
)
97 cluster_sockfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
99 memset(&addr
, 0, sizeof(addr
));
100 addr
.sin_family
= AF_INET
;
101 addr
.sin_port
= htons(CLUSTERPORT
);
102 addr
.sin_addr
.s_addr
= INADDR_ANY
;
103 setsockopt(cluster_sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &addr
, sizeof(addr
));
105 opt
= fcntl(cluster_sockfd
, F_GETFL
, 0);
106 fcntl(cluster_sockfd
, F_SETFL
, opt
| O_NONBLOCK
);
108 if (bind(cluster_sockfd
, (void *) &addr
, sizeof(addr
)) < 0)
110 LOG(0, 0, 0, "Failed to bind cluster socket: %s\n", strerror(errno
));
114 strcpy(ifr
.ifr_name
, config
->cluster_interface
);
115 if (ioctl(cluster_sockfd
, SIOCGIFADDR
, &ifr
) < 0)
117 LOG(0, 0, 0, "Failed to get interface address for (%s): %s\n", config
->cluster_interface
, strerror(errno
));
121 memcpy(&interface_addr
, &ifr
.ifr_addr
, sizeof(interface_addr
));
122 my_address
= interface_addr
.sin_addr
.s_addr
;
124 // Join multicast group.
125 mreq
.imr_multiaddr
.s_addr
= config
->cluster_address
;
126 mreq
.imr_interface
= interface_addr
.sin_addr
;
129 opt
= 0; // Turn off multicast loopback.
130 setsockopt(cluster_sockfd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &opt
, sizeof(opt
));
132 if (config
->cluster_mcast_ttl
!= 1)
135 if (config
->cluster_mcast_ttl
> 0)
136 ttl
= config
->cluster_mcast_ttl
< 256 ? config
->cluster_mcast_ttl
: 255;
138 setsockopt(cluster_sockfd
, IPPROTO_IP
, IP_MULTICAST_TTL
, &ttl
, sizeof(ttl
));
141 if (setsockopt(cluster_sockfd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
)) < 0)
143 LOG(0, 0, 0, "Failed to setsockopt (join mcast group): %s\n", strerror(errno
));
147 if (setsockopt(cluster_sockfd
, IPPROTO_IP
, IP_MULTICAST_IF
, &interface_addr
, sizeof(interface_addr
)) < 0)
149 LOG(0, 0, 0, "Failed to setsockopt (set mcast interface): %s\n", strerror(errno
));
153 config
->cluster_last_hb
= TIME
;
154 config
->cluster_seq_number
= -1;
156 return cluster_sockfd
;
161 // Send a chunk of data to the entire cluster (usually via the multicast
165 static int cluster_send_data(void *data
, int datalen
)
167 struct sockaddr_in addr
= {0};
169 if (!cluster_sockfd
) return -1;
170 if (!config
->cluster_address
) return 0;
172 addr
.sin_addr
.s_addr
= config
->cluster_address
;
173 addr
.sin_port
= htons(CLUSTERPORT
);
174 addr
.sin_family
= AF_INET
;
176 LOG(5, 0, 0, "Cluster send data: %d bytes\n", datalen
);
178 if (sendto(cluster_sockfd
, data
, datalen
, MSG_NOSIGNAL
, (void *) &addr
, sizeof(addr
)) < 0)
180 LOG(0, 0, 0, "sendto: %s\n", strerror(errno
));
188 // Add a chunk of data to a heartbeat packet.
189 // Maintains the format. Assumes that the caller
190 // has passed in a big enough buffer!
192 static void add_type(uint8_t **p
, int type
, int more
, uint8_t *data
, int size
)
194 *((uint32_t *) (*p
)) = type
;
195 *p
+= sizeof(uint32_t);
197 *((uint32_t *)(*p
)) = more
;
198 *p
+= sizeof(uint32_t);
200 if (data
&& size
> 0) {
201 memcpy(*p
, data
, size
);
206 // advertise our presence via BGP or gratuitous ARP
207 static void advertise_routes(void)
211 bgp_enable_routing(1);
214 if (config
->send_garp
)
215 send_garp(config
->bind_address
); // Start taking traffic.
218 // withdraw our routes (BGP only)
219 static void withdraw_routes(void)
223 bgp_enable_routing(0);
227 static void cluster_uptodate(void)
229 if (config
->cluster_iam_uptodate
)
232 if (config
->cluster_undefined_sessions
|| config
->cluster_undefined_tunnels
|| config
->cluster_undefined_bundles
)
235 config
->cluster_iam_uptodate
= 1;
237 LOG(0, 0, 0, "Now uptodate with master.\n");
242 // Send a unicast UDP packet to a peer with 'data' as the
245 static int peer_send_data(in_addr_t peer
, uint8_t *data
, int size
)
247 struct sockaddr_in addr
= {0};
249 if (!cluster_sockfd
) return -1;
250 if (!config
->cluster_address
) return 0;
255 addr
.sin_addr
.s_addr
= peer
;
256 addr
.sin_port
= htons(CLUSTERPORT
);
257 addr
.sin_family
= AF_INET
;
259 LOG_HEX(5, "Peer send", data
, size
);
261 if (sendto(cluster_sockfd
, data
, size
, MSG_NOSIGNAL
, (void *) &addr
, sizeof(addr
)) < 0)
263 LOG(0, 0, 0, "sendto: %s\n", strerror(errno
));
271 // Send a structured message to a peer with a single element of type 'type'.
273 static int peer_send_message(in_addr_t peer
, int type
, int more
, uint8_t *data
, int size
)
275 uint8_t buf
[65536]; // Vast overkill.
278 LOG(4, 0, 0, "Sending message to peer (type %d, more %d, size %d)\n", type
, more
, size
);
279 add_type(&p
, type
, more
, data
, size
);
281 return peer_send_data(peer
, buf
, (p
-buf
) );
284 // send a packet to the master
285 static int _forward_packet(uint8_t *data
, int size
, in_addr_t addr
, int port
, int type
)
287 uint8_t buf
[65536]; // Vast overkill.
290 if (!config
->cluster_master_address
) // No election has been held yet. Just skip it.
293 LOG(4, 0, 0, "Forwarding packet from %s to master (size %d)\n", fmtaddr(addr
, 0), size
);
296 add_type(&p
, type
, addr
, (uint8_t *) &port
, sizeof(port
)); // ick. should be uint16_t
297 memcpy(p
, data
, size
);
300 return peer_send_data(config
->cluster_master_address
, buf
, (p
- buf
));
304 // Forward a state changing packet to the master.
306 // The master just processes the payload as if it had
307 // received it off the tun device.
308 //(note: THIS ROUTINE WRITES TO pack[-6]).
309 int master_forward_packet(uint8_t *data
, int size
, in_addr_t addr
, uint16_t port
, uint16_t indexudp
)
311 uint8_t *p
= data
- (3 * sizeof(uint32_t));
313 uint32_t indexandport
= port
| ((indexudp
<< 16) & 0xFFFF0000);
315 if (!config
->cluster_master_address
) // No election has been held yet. Just skip it.
318 LOG(4, 0, 0, "Forwarding packet from %s to master (size %d)\n", fmtaddr(addr
, 0), size
);
321 add_type(&p
, C_FORWARD
, addr
, (uint8_t *) &indexandport
, sizeof(indexandport
));
323 return peer_send_data(config
->cluster_master_address
, psave
, size
+ (3 * sizeof(uint32_t)));
326 // Forward PPPOE packet to the master.
327 //(note: THIS ROUTINE WRITES TO pack[-4]).
328 int master_forward_pppoe_packet(uint8_t *data
, int size
, uint8_t codepad
)
330 uint8_t *p
= data
- (2 * sizeof(uint32_t));
333 if (!config
->cluster_master_address
) // No election has been held yet. Just skip it.
336 LOG(4, 0, 0, "Forward PPPOE packet to master, code %s (size %d)\n", get_string_codepad(codepad
), size
);
339 add_type(&p
, C_PPPOE_FORWARD
, codepad
, NULL
, 0);
341 return peer_send_data(config
->cluster_master_address
, psave
, size
+ (2 * sizeof(uint32_t)));
344 // Forward a DAE RADIUS packet to the master.
345 int master_forward_dae_packet(uint8_t *data
, int size
, in_addr_t addr
, int port
)
347 return _forward_packet(data
, size
, addr
, port
, C_FORWARD_DAE
);
351 // Forward a throttled packet to the master for handling.
353 // The master just drops the packet into the appropriate
354 // token bucket queue, and lets normal processing take care
357 int master_throttle_packet(int tbfid
, uint8_t *data
, int size
)
359 uint8_t buf
[65536]; // Vast overkill.
362 if (!config
->cluster_master_address
) // No election has been held yet. Just skip it.
365 LOG(4, 0, 0, "Throttling packet master (size %d, tbfid %d)\n", size
, tbfid
);
367 add_type(&p
, C_THROTTLE
, tbfid
, data
, size
);
369 return peer_send_data(config
->cluster_master_address
, buf
, (p
-buf
) );
374 // Forward a walled garden packet to the master for handling.
376 // The master just writes the packet straight to the tun
377 // device (where is will normally loop through the
378 // firewall rules, and come back in on the tun device)
380 // (Note that this must be called with the tun header
381 // as the start of the data).
382 int master_garden_packet(sessionidt s
, uint8_t *data
, int size
)
384 uint8_t buf
[65536]; // Vast overkill.
387 if (!config
->cluster_master_address
) // No election has been held yet. Just skip it.
390 LOG(4, 0, 0, "Walled garden packet to master (size %d)\n", size
);
392 add_type(&p
, C_GARDEN
, s
, data
, size
);
394 return peer_send_data(config
->cluster_master_address
, buf
, (p
-buf
));
399 // Forward a MPPP packet to the master for handling.
401 // (Note that this must be called with the tun header
402 // as the start of the data).
403 // (i.e. this routine writes to data[-8]).
404 int master_forward_mppp_packet(sessionidt s
, uint8_t *data
, int size
)
406 uint8_t *p
= data
- (2 * sizeof(uint32_t));
409 if (!config
->cluster_master_address
) // No election has been held yet. Just skip it.
412 LOG(4, 0, 0, "Forward MPPP packet to master (size %d)\n", size
);
414 add_type(&p
, C_MPPP_FORWARD
, s
, NULL
, 0);
416 return peer_send_data(config
->cluster_master_address
, psave
, size
+ (2 * sizeof(uint32_t)));
421 // Send a chunk of data as a heartbeat..
422 // We save it in the history buffer as we do so.
424 static void send_heartbeat(int seq
, uint8_t *data
, int size
)
428 if (size
> sizeof(past_hearts
[0].data
))
430 LOG(0, 0, 0, "Tried to heartbeat something larger than the maximum packet!\n");
434 i
= seq
% HB_HISTORY_SIZE
;
435 past_hearts
[i
].seq
= seq
;
436 past_hearts
[i
].size
= size
;
437 memcpy(&past_hearts
[i
].data
, data
, size
); // Save it.
438 cluster_send_data(data
, size
);
442 // Send an 'i am alive' message to every machine in the cluster.
444 void cluster_send_ping(time_t basetime
)
446 uint8_t buff
[100 + sizeof(pingt
)];
450 if (config
->cluster_iam_master
&& basetime
) // We're heartbeating so no need to ping.
453 LOG(5, 0, 0, "Sending cluster ping...\n");
456 x
.addr
= config
->bind_address
;
457 x
.undef
= config
->cluster_undefined_sessions
+ config
->cluster_undefined_tunnels
+ config
->cluster_undefined_bundles
;
458 x
.basetime
= basetime
;
460 add_type(&p
, C_PING
, basetime
, (uint8_t *) &x
, sizeof(x
));
461 cluster_send_data(buff
, (p
-buff
) );
465 // Walk the session counters looking for non-zero ones to send
466 // to the master. We send up to 600 of them at one time.
467 // We examine a maximum of 3000 sessions.
468 // (50k max session should mean that we normally
469 // examine the entire session table every 25 seconds).
471 #define MAX_B_RECS (600)
472 void master_update_counts(void)
475 bytest b
[MAX_B_RECS
+1];
477 if (config
->cluster_iam_master
) // Only happens on the slaves.
480 if (!config
->cluster_master_address
) // If we don't have a master, skip it for a while.
483 i
= MAX_B_RECS
* 5; // Examine max 3000 sessions;
484 if (config
->cluster_highest_sessionid
> i
)
485 i
= config
->cluster_highest_sessionid
;
487 for ( c
= 0; i
> 0 ; --i
) {
488 // Next session to look at.
489 walk_session_number
++;
490 if ( walk_session_number
> config
->cluster_highest_sessionid
)
491 walk_session_number
= 1;
493 if (!sess_local
[walk_session_number
].cin
&& !sess_local
[walk_session_number
].cout
)
494 continue; // Unchanged. Skip it.
496 b
[c
].sid
= walk_session_number
;
497 b
[c
].pin
= sess_local
[walk_session_number
].pin
;
498 b
[c
].pout
= sess_local
[walk_session_number
].pout
;
499 b
[c
].cin
= sess_local
[walk_session_number
].cin
;
500 b
[c
].cout
= sess_local
[walk_session_number
].cout
;
503 sess_local
[walk_session_number
].pin
= sess_local
[walk_session_number
].pout
= 0;
504 sess_local
[walk_session_number
].cin
= sess_local
[walk_session_number
].cout
= 0;
506 if (++c
> MAX_B_RECS
) // Send a max of 600 elements in a packet.
510 if (!c
) // Didn't find any that changes. Get out of here!
514 // Forward the data to the master.
515 LOG(4, 0, 0, "Sending byte counters to master (%d elements)\n", c
);
516 peer_send_message(config
->cluster_master_address
, C_BYTES
, c
, (uint8_t *) &b
, sizeof(b
[0]) * c
);
521 // On the master, check how our slaves are going. If
522 // one of them's not up-to-date we'll heartbeat faster.
523 // If we don't have any of them, then we need to turn
524 // on our own packet handling!
526 void cluster_check_slaves(void)
529 static int have_peers
= 0;
530 int had_peers
= have_peers
;
533 if (!config
->cluster_iam_master
)
534 return; // Only runs on the master...
536 config
->cluster_iam_uptodate
= 1; // cleared in loop below
538 for (i
= have_peers
= 0; i
< num_peers
; i
++)
540 if ((peers
[i
].timestamp
+ config
->cluster_hb_timeout
) < t
)
541 continue; // Stale peer! Skip them.
543 if (!peers
[i
].basetime
)
544 continue; // Shutdown peer! Skip them.
546 if (peers
[i
].uptodate
)
549 config
->cluster_iam_uptodate
= 0; // Start fast heartbeats
552 // in a cluster, withdraw/add routes when we get a peer/lose peers
553 if (have_peers
!= had_peers
)
555 if (had_peers
< config
->cluster_master_min_adv
&&
556 have_peers
>= config
->cluster_master_min_adv
)
559 else if (had_peers
>= config
->cluster_master_min_adv
&&
560 have_peers
< config
->cluster_master_min_adv
)
566 // Check that we have a master. If it's been too
567 // long since we heard from a master then hold an election.
569 void cluster_check_master(void)
571 int i
, count
, high_unique_id
= 0;
574 static int probed
= 0;
577 if (config
->cluster_iam_master
)
578 return; // Only runs on the slaves...
580 // If the master is late (missed 2 hearbeats by a second and a
581 // hair) it may be that the switch has dropped us from the
582 // multicast group, try unicasting probes to the master
583 // which will hopefully respond with a unicast heartbeat that
584 // will allow us to limp along until the querier next runs.
585 if (config
->cluster_master_address
586 && TIME
> (config
->cluster_last_hb
+ 2 * config
->cluster_hb_interval
+ 11))
588 if (!probed
|| (TIME
> (probed
+ 2 * config
->cluster_hb_interval
)))
591 LOG(1, 0, 0, "Heartbeat from master %.1fs late, probing...\n",
592 0.1 * (TIME
- (config
->cluster_last_hb
+ config
->cluster_hb_interval
)));
594 peer_send_message(config
->cluster_master_address
,
595 C_LASTSEEN
, config
->cluster_seq_number
, NULL
, 0);
597 } else { // We got a recent heartbeat; reset the probe flag.
601 if (TIME
< (config
->cluster_last_hb
+ config
->cluster_hb_timeout
))
602 return; // Everything's ok!
604 config
->cluster_last_hb
= TIME
+ 1; // Just the one election thanks.
605 config
->cluster_master_address
= 0;
607 LOG(0, 0, 0, "Master timed out! Holding election...\n");
609 // In the process of shutting down, can't be master
613 for (i
= have_peers
= 0; i
< num_peers
; i
++)
615 if ((peers
[i
].timestamp
+ config
->cluster_hb_timeout
) < t
)
616 continue; // Stale peer! Skip them.
618 if (!peers
[i
].basetime
)
619 continue; // Shutdown peer! Skip them.
621 if (peers
[i
].basetime
< basetime
) {
622 LOG(1, 0, 0, "Expecting %s to become master\n", fmtaddr(peers
[i
].peer
, 0));
623 return; // They'll win the election. Get out of here.
626 if (peers
[i
].basetime
== basetime
&&
627 peers
[i
].peer
> my_address
) {
628 LOG(1, 0, 0, "Expecting %s to become master\n", fmtaddr(peers
[i
].peer
, 0));
629 return; // They'll win the election. Wait for them to come up.
632 if (peers
[i
].uptodate
)
636 // Wow. it's been ages since I last heard a heartbeat
637 // and I'm better than an of my peers so it's time
638 // to become a master!!!
640 config
->cluster_iam_master
= 1;
641 pppoe_send_garp(); // gratuitous arp of the pppoe interface
643 LOG(0, 0, 0, "I am declaring myself the master!\n");
645 if (have_peers
< config
->cluster_master_min_adv
)
650 if (config
->cluster_seq_number
== -1)
651 config
->cluster_seq_number
= 0;
654 // Go through and mark all the tunnels as defined.
655 // Count the highest used tunnel number as well.
657 config
->cluster_highest_tunnelid
= 0;
658 for (i
= 0; i
< MAXTUNNEL
; ++i
) {
659 if (tunnel
[i
].state
== TUNNELUNDEF
)
660 tunnel
[i
].state
= TUNNELFREE
;
662 if (tunnel
[i
].state
!= TUNNELFREE
&& i
> config
->cluster_highest_tunnelid
)
663 config
->cluster_highest_tunnelid
= i
;
667 // Go through and mark all the bundles as defined.
668 // Count the highest used bundle number as well.
670 config
->cluster_highest_bundleid
= 0;
671 for (i
= 0; i
< MAXBUNDLE
; ++i
) {
672 if (bundle
[i
].state
== BUNDLEUNDEF
)
673 bundle
[i
].state
= BUNDLEFREE
;
675 if (bundle
[i
].state
!= BUNDLEFREE
&& i
> config
->cluster_highest_bundleid
)
676 config
->cluster_highest_bundleid
= i
;
680 // Go through and mark all the sessions as being defined.
681 // reset the idle timeouts.
682 // add temporary byte counters to permanent ones.
683 // Re-string the free list.
684 // Find the ID of the highest session.
687 config
->cluster_highest_sessionid
= 0;
688 for (i
= 0, count
= 0; i
< MAXSESSION
; ++i
) {
689 if (session
[i
].tunnel
== T_UNDEF
) {
690 session
[i
].tunnel
= T_FREE
;
694 if (!session
[i
].opened
) { // Unused session. Add to free list.
695 memset(&session
[i
], 0, sizeof(session
[i
]));
696 session
[i
].tunnel
= T_FREE
;
697 session
[last_free
].next
= i
;
703 // Reset idle timeouts..
704 session
[i
].last_packet
= session
[i
].last_data
= time_now
;
706 // Reset die relative to our uptime rather than the old master's
707 if (session
[i
].die
) session
[i
].die
= TIME
;
709 // Accumulate un-sent byte/packet counters.
710 increment_counter(&session
[i
].cin
, &session
[i
].cin_wrap
, sess_local
[i
].cin
);
711 increment_counter(&session
[i
].cout
, &session
[i
].cout_wrap
, sess_local
[i
].cout
);
712 session
[i
].cin_delta
+= sess_local
[i
].cin
;
713 session
[i
].cout_delta
+= sess_local
[i
].cout
;
715 session
[i
].pin
+= sess_local
[i
].pin
;
716 session
[i
].pout
+= sess_local
[i
].pout
;
718 sess_local
[i
].cin
= sess_local
[i
].cout
= 0;
719 sess_local
[i
].pin
= sess_local
[i
].pout
= 0;
721 sess_local
[i
].radius
= 0; // Reset authentication as the radius blocks aren't up to date.
723 if (session
[i
].unique_id
>= high_unique_id
) // This is different to the index into the session table!!!
724 high_unique_id
= session
[i
].unique_id
+1;
726 session
[i
].tbf_in
= session
[i
].tbf_out
= 0; // Remove stale pointers from old master.
727 throttle_session(i
, session
[i
].throttle_in
, session
[i
].throttle_out
);
729 config
->cluster_highest_sessionid
= i
;
732 session
[last_free
].next
= 0; // End of chain.
733 last_id
= high_unique_id
; // Keep track of the highest used session ID.
737 rebuild_address_pool();
739 // If we're not the very first master, this is a big issue!
741 LOG(0, 0, 0, "Warning: Fixed %d uninitialized sessions in becoming master!\n", count
);
743 config
->cluster_undefined_sessions
= 0;
744 config
->cluster_undefined_bundles
= 0;
745 config
->cluster_undefined_tunnels
= 0;
746 config
->cluster_iam_uptodate
= 1; // assume all peers are up-to-date
748 // FIXME. We need to fix up the tunnel control message
749 // queue here! There's a number of other variables we
750 // should also update.
755 // Check that our session table is validly matching what the
756 // master has in mind.
758 // In particular, if we have too many sessions marked 'undefined'
759 // we fix it up here, and we ensure that the 'first free session'
762 static void cluster_check_sessions(int highsession
, int freesession_ptr
, int highbundle
, int hightunnel
)
766 sessionfree
= freesession_ptr
; // Keep the freesession ptr valid.
768 if (config
->cluster_iam_uptodate
)
771 if (highsession
> config
->cluster_undefined_sessions
&& highbundle
> config
->cluster_undefined_bundles
&& hightunnel
> config
->cluster_undefined_tunnels
)
774 // Clear out defined sessions, counting the number of
776 config
->cluster_undefined_sessions
= 0;
777 for (i
= 1 ; i
< MAXSESSION
; ++i
) {
778 if (i
> highsession
) {
779 if (session
[i
].tunnel
== T_UNDEF
) session
[i
].tunnel
= T_FREE
; // Defined.
783 if (session
[i
].tunnel
== T_UNDEF
)
784 ++config
->cluster_undefined_sessions
;
787 // Clear out defined bundles, counting the number of
789 config
->cluster_undefined_bundles
= 0;
790 for (i
= 1 ; i
< MAXBUNDLE
; ++i
) {
791 if (i
> highbundle
) {
792 if (bundle
[i
].state
== BUNDLEUNDEF
) bundle
[i
].state
= BUNDLEFREE
; // Defined.
796 if (bundle
[i
].state
== BUNDLEUNDEF
)
797 ++config
->cluster_undefined_bundles
;
800 // Clear out defined tunnels, counting the number of
802 config
->cluster_undefined_tunnels
= 0;
803 for (i
= 1 ; i
< MAXTUNNEL
; ++i
) {
804 if (i
> hightunnel
) {
805 if (tunnel
[i
].state
== TUNNELUNDEF
) tunnel
[i
].state
= TUNNELFREE
; // Defined.
809 if (tunnel
[i
].state
== TUNNELUNDEF
)
810 ++config
->cluster_undefined_tunnels
;
814 if (config
->cluster_undefined_sessions
|| config
->cluster_undefined_tunnels
|| config
->cluster_undefined_bundles
) {
815 LOG(2, 0, 0, "Cleared undefined sessions/bundles/tunnels. %d sess (high %d), %d bund (high %d), %d tunn (high %d)\n",
816 config
->cluster_undefined_sessions
, highsession
, config
->cluster_undefined_bundles
, highbundle
, config
->cluster_undefined_tunnels
, hightunnel
);
820 // Are we up to date?
822 if (!config
->cluster_iam_uptodate
)
826 static int hb_add_type(uint8_t **p
, int type
, int id
)
829 case C_CSESSION
: { // Compressed C_SESSION.
830 uint8_t c
[sizeof(sessiont
) * 2]; // Bigger than worst case.
831 uint8_t *d
= (uint8_t *) &session
[id
];
835 size
= rle_compress( &d
, sizeof(sessiont
), c
, sizeof(c
) );
837 // Did we compress the full structure, and is the size actually
839 if ( (d
- orig
) == sizeof(sessiont
) && size
< sizeof(sessiont
) ) {
840 add_type(p
, C_CSESSION
, id
, c
, size
);
843 // Failed to compress : Fall through.
846 add_type(p
, C_SESSION
, id
, (uint8_t *) &session
[id
], sizeof(sessiont
));
849 case C_CBUNDLE
: { // Compressed C_BUNDLE
850 uint8_t c
[sizeof(bundlet
) * 2]; // Bigger than worst case.
851 uint8_t *d
= (uint8_t *) &bundle
[id
];
855 size
= rle_compress( &d
, sizeof(bundlet
), c
, sizeof(c
) );
857 // Did we compress the full structure, and is the size actually
859 if ( (d
- orig
) == sizeof(bundlet
) && size
< sizeof(bundlet
) ) {
860 add_type(p
, C_CBUNDLE
, id
, c
, size
);
863 // Failed to compress : Fall through.
867 add_type(p
, C_BUNDLE
, id
, (uint8_t *) &bundle
[id
], sizeof(bundlet
));
870 case C_CTUNNEL
: { // Compressed C_TUNNEL
871 uint8_t c
[sizeof(tunnelt
) * 2]; // Bigger than worst case.
872 uint8_t *d
= (uint8_t *) &tunnel
[id
];
876 size
= rle_compress( &d
, sizeof(tunnelt
), c
, sizeof(c
) );
878 // Did we compress the full structure, and is the size actually
880 if ( (d
- orig
) == sizeof(tunnelt
) && size
< sizeof(tunnelt
) ) {
881 add_type(p
, C_CTUNNEL
, id
, c
, size
);
884 // Failed to compress : Fall through.
887 add_type(p
, C_TUNNEL
, id
, (uint8_t *) &tunnel
[id
], sizeof(tunnelt
));
890 LOG(0, 0, 0, "Found an invalid type in heart queue! (%d)\n", type
);
898 // Send a heartbeat, incidently sending out any queued changes..
900 void cluster_heartbeat()
902 int i
, count
= 0, tcount
= 0, bcount
= 0;
903 uint8_t buff
[MAX_HEART_SIZE
+ sizeof(heartt
) + sizeof(int) ];
907 if (!config
->cluster_iam_master
) // Only the master does this.
910 config
->cluster_table_version
+= config
->cluster_num_changes
;
912 // Fill out the heartbeat header.
913 memset(&h
, 0, sizeof(h
));
915 h
.version
= HB_VERSION
;
916 h
.seq
= config
->cluster_seq_number
;
917 h
.basetime
= basetime
;
918 h
.clusterid
= config
->bind_address
; // Will this do??
919 h
.basetime
= basetime
;
920 h
.highsession
= config
->cluster_highest_sessionid
;
921 h
.freesession
= sessionfree
;
922 h
.hightunnel
= config
->cluster_highest_tunnelid
;
923 h
.highbundle
= config
->cluster_highest_bundleid
;
924 h
.size_sess
= sizeof(sessiont
); // Just in case.
925 h
.size_bund
= sizeof(bundlet
);
926 h
.size_tunn
= sizeof(tunnelt
);
927 h
.interval
= config
->cluster_hb_interval
;
928 h
.timeout
= config
->cluster_hb_timeout
;
929 h
.table_version
= config
->cluster_table_version
;
931 add_type(&p
, C_HEARTBEAT
, HB_VERSION
, (uint8_t *) &h
, sizeof(h
));
933 for (i
= 0; i
< config
->cluster_num_changes
; ++i
) {
934 hb_add_type(&p
, cluster_changes
[i
].type
, cluster_changes
[i
].id
);
937 if (p
> (buff
+ sizeof(buff
))) { // Did we somehow manage to overun the buffer?
938 LOG(0, 0, 0, "FATAL: Overran the heartbeat buffer! This is fatal. Exiting. (size %d)\n", (int) (p
- buff
));
944 // Fill out the packet with sessions from the session table...
945 // (not forgetting to leave space so we can get some tunnels in too )
946 while ( (p
+ sizeof(uint32_t) * 2 + sizeof(sessiont
) * 2 ) < (buff
+ MAX_HEART_SIZE
) ) {
948 if (!walk_session_number
) // session #0 isn't valid.
949 ++walk_session_number
;
951 if (count
>= config
->cluster_highest_sessionid
) // If we're a small cluster, don't go wild.
954 hb_add_type(&p
, C_CSESSION
, walk_session_number
);
955 walk_session_number
= (1+walk_session_number
)%(config
->cluster_highest_sessionid
+1); // +1 avoids divide by zero.
957 ++count
; // Count the number of extra sessions we're sending.
961 // Fill out the packet with tunnels from the tunnel table...
962 // This effectively means we walk the tunnel table more quickly
963 // than the session table. This is good because stuffing up a
964 // tunnel is a much bigger deal than stuffing up a session.
966 while ( (p
+ sizeof(uint32_t) * 2 + sizeof(tunnelt
) ) < (buff
+ MAX_HEART_SIZE
) ) {
968 if (!walk_tunnel_number
) // tunnel #0 isn't valid.
969 ++walk_tunnel_number
;
971 if (tcount
>= config
->cluster_highest_tunnelid
)
974 hb_add_type(&p
, C_CTUNNEL
, walk_tunnel_number
);
975 walk_tunnel_number
= (1+walk_tunnel_number
)%(config
->cluster_highest_tunnelid
+1); // +1 avoids divide by zero.
981 // Fill out the packet with bundles from the bundle table...
982 while ( (p
+ sizeof(uint32_t) * 2 + sizeof(bundlet
) ) < (buff
+ MAX_HEART_SIZE
) ) {
984 if (!walk_bundle_number
) // bundle #0 isn't valid.
985 ++walk_bundle_number
;
987 if (bcount
>= config
->cluster_highest_bundleid
)
990 hb_add_type(&p
, C_CBUNDLE
, walk_bundle_number
);
991 walk_bundle_number
= (1+walk_bundle_number
)%(config
->cluster_highest_bundleid
+1); // +1 avoids divide by zero.
996 // Did we do something wrong?
997 if (p
> (buff
+ sizeof(buff
))) { // Did we somehow manage to overun the buffer?
998 LOG(0, 0, 0, "Overran the heartbeat buffer now! This is fatal. Exiting. (size %d)\n", (int) (p
- buff
));
1003 LOG(4, 0, 0, "Sending v%d heartbeat #%d, change #%" PRIu64
" with %d changes "
1004 "(%d x-sess, %d x-bundles, %d x-tunnels, %d highsess, %d highbund, %d hightun, size %d)\n",
1005 HB_VERSION
, h
.seq
, h
.table_version
, config
->cluster_num_changes
,
1006 count
, bcount
, tcount
, config
->cluster_highest_sessionid
, config
->cluster_highest_bundleid
,
1007 config
->cluster_highest_tunnelid
, (int) (p
- buff
));
1009 config
->cluster_num_changes
= 0;
1011 send_heartbeat(h
.seq
, buff
, (p
-buff
) ); // Send out the heartbeat to the cluster, keeping a copy of it.
1013 config
->cluster_seq_number
= (config
->cluster_seq_number
+1)%HB_MAX_SEQ
; // Next seq number to use.
1017 // A structure of type 'type' has changed; Add it to the queue to send.
1019 static int type_changed(int type
, int id
)
1023 for (i
= 0 ; i
< config
->cluster_num_changes
; ++i
)
1025 if ( cluster_changes
[i
].id
== id
&& cluster_changes
[i
].type
== type
)
1027 // Already marked for change, remove it
1028 --config
->cluster_num_changes
;
1029 memmove(&cluster_changes
[i
],
1030 &cluster_changes
[i
+1],
1031 (config
->cluster_num_changes
- i
) * sizeof(cluster_changes
[i
]));
1036 cluster_changes
[config
->cluster_num_changes
].type
= type
;
1037 cluster_changes
[config
->cluster_num_changes
].id
= id
;
1038 ++config
->cluster_num_changes
;
1040 if (config
->cluster_num_changes
> MAX_CHANGES
)
1041 cluster_heartbeat(); // flush now
1046 // A particular session has been changed!
1047 int cluster_send_session(int sid
)
1049 if (!config
->cluster_iam_master
) {
1050 LOG(0, sid
, 0, "I'm not a master, but I just tried to change a session!\n");
1055 LOG(0, sid
, 0, "cluster_send_session called from child process!\n");
1059 return type_changed(C_CSESSION
, sid
);
1062 // A particular bundle has been changed!
1063 int cluster_send_bundle(int bid
)
1065 if (!config
->cluster_iam_master
) {
1066 LOG(0, 0, bid
, "I'm not a master, but I just tried to change a bundle!\n");
1070 return type_changed(C_CBUNDLE
, bid
);
1073 // A particular tunnel has been changed!
1074 int cluster_send_tunnel(int tid
)
1076 if (!config
->cluster_iam_master
) {
1077 LOG(0, 0, tid
, "I'm not a master, but I just tried to change a tunnel!\n");
1081 return type_changed(C_CTUNNEL
, tid
);
1086 // We're a master, and a slave has just told us that it's
1087 // missed a packet. We'll resend it every packet since
1088 // the last one it's seen.
1090 static int cluster_catchup_slave(int seq
, in_addr_t slave
)
1095 LOG(1, 0, 0, "Slave %s sent LASTSEEN with seq %d\n", fmtaddr(slave
, 0), seq
);
1096 if (!config
->cluster_iam_master
) {
1097 LOG(1, 0, 0, "Got LASTSEEN but I'm not a master! Redirecting it to %s.\n",
1098 fmtaddr(config
->cluster_master_address
, 0));
1100 peer_send_message(slave
, C_MASTER
, config
->cluster_master_address
, NULL
, 0);
1104 diff
= config
->cluster_seq_number
- seq
; // How many packet do we need to send?
1108 if (diff
>= HB_HISTORY_SIZE
) { // Ouch. We don't have the packet to send it!
1109 LOG(0, 0, 0, "A slave asked for message %d when our seq number is %d. Killing it.\n",
1110 seq
, config
->cluster_seq_number
);
1111 return peer_send_message(slave
, C_KILL
, seq
, NULL
, 0);// Kill the slave. Nothing else to do.
1114 LOG(1, 0, 0, "Sending %d catchup packets to slave %s\n", diff
, fmtaddr(slave
, 0) );
1116 // Now resend every packet that it missed, in order.
1117 while (seq
!= config
->cluster_seq_number
) {
1118 s
= seq
% HB_HISTORY_SIZE
;
1119 if (seq
!= past_hearts
[s
].seq
) {
1120 LOG(0, 0, 0, "Tried to re-send heartbeat for %s but %d doesn't match %d! (%d,%d)\n",
1121 fmtaddr(slave
, 0), seq
, past_hearts
[s
].seq
, s
, config
->cluster_seq_number
);
1122 return -1; // What to do here!?
1124 peer_send_data(slave
, past_hearts
[s
].data
, past_hearts
[s
].size
);
1125 seq
= (seq
+1)%HB_MAX_SEQ
; // Increment to next seq number.
1127 return 0; // All good!
1131 // We've heard from another peer! Add it to the list
1132 // that we select from at election time.
1134 static int cluster_add_peer(in_addr_t peer
, time_t basetime
, pingt
*pp
, int size
)
1137 in_addr_t clusterid
;
1140 // Allow for backward compatability.
1141 // Just the ping packet into a new structure to allow
1142 // for the possibility that we might have received
1143 // more or fewer elements than we were expecting.
1144 if (size
> sizeof(p
))
1147 memset( (void *) &p
, 0, sizeof(p
) );
1148 memcpy( (void *) &p
, (void *) pp
, size
);
1151 if (clusterid
!= config
->bind_address
)
1154 LOG(4, 0, 0, "Skipping ping from %s (different cluster)\n", fmtaddr(peer
, 0));
1158 for (i
= 0; i
< num_peers
; ++i
)
1160 if (peers
[i
].peer
!= peer
)
1163 // This peer already exists. Just update the timestamp.
1164 peers
[i
].basetime
= basetime
;
1165 peers
[i
].timestamp
= TIME
;
1166 peers
[i
].uptodate
= !p
.undef
;
1170 // Is this the master shutting down??
1171 if (peer
== config
->cluster_master_address
) {
1172 LOG(3, 0, 0, "Master %s %s\n", fmtaddr(config
->cluster_master_address
, 0),
1173 basetime
? "has restarted!" : "shutting down...");
1175 config
->cluster_master_address
= 0;
1176 config
->cluster_last_hb
= 0; // Force an election.
1177 cluster_check_master();
1182 LOG(4, 0, 0, "Adding %s as a peer\n", fmtaddr(peer
, 0));
1184 // Not found. Is there a stale slot to re-use?
1185 for (i
= 0; i
< num_peers
; ++i
)
1187 if (!peers
[i
].basetime
) // Shutdown
1190 if ((peers
[i
].timestamp
+ config
->cluster_hb_timeout
* 10) < TIME
) // Stale.
1194 if (i
>= CLUSTER_MAX_SIZE
)
1197 LOG(0, 0, 0, "Tried to add %s as a peer, but I already have %d of them!\n", fmtaddr(peer
, 0), i
);
1201 peers
[i
].peer
= peer
;
1202 peers
[i
].basetime
= basetime
;
1203 peers
[i
].timestamp
= TIME
;
1204 peers
[i
].uptodate
= !p
.undef
;
1208 LOG(1, 0, 0, "Added %s as a new peer. Now %d peers\n", fmtaddr(peer
, 0), num_peers
);
1214 // A slave responds with C_MASTER when it gets a message which should have gone to a master.
1215 static int cluster_set_master(in_addr_t peer
, in_addr_t master
)
1217 if (config
->cluster_iam_master
) // Sanity...
1220 LOG(3, 0, 0, "Peer %s set the master to %s...\n", fmtaddr(peer
, 0),
1221 fmtaddr(master
, 1));
1223 config
->cluster_master_address
= master
;
1226 // catchup with new master
1227 peer_send_message(master
, C_LASTSEEN
, config
->cluster_seq_number
, NULL
, 0);
1229 // delay next election
1230 config
->cluster_last_hb
= TIME
;
1233 // run election (or reset "probed" if master was set)
1234 cluster_check_master();
1238 /* Handle the slave updating the byte counters for the master. */
1240 // Note that we don't mark the session as dirty; We rely on
1241 // the slow table walk to propogate this back out to the slaves.
1243 static int cluster_handle_bytes(uint8_t *data
, int size
)
1247 b
= (bytest
*) data
;
1249 LOG(3, 0, 0, "Got byte counter update (size %d)\n", size
);
1251 /* Loop around, adding the byte
1252 counts to each of the sessions. */
1254 while (size
>= sizeof(*b
) ) {
1255 if (b
->sid
> MAXSESSION
) {
1256 LOG(0, 0, 0, "Got C_BYTES with session #%d!\n", b
->sid
);
1257 return -1; /* Abort processing */
1260 session
[b
->sid
].pin
+= b
->pin
;
1261 session
[b
->sid
].pout
+= b
->pout
;
1263 increment_counter(&session
[b
->sid
].cin
, &session
[b
->sid
].cin_wrap
, b
->cin
);
1264 increment_counter(&session
[b
->sid
].cout
, &session
[b
->sid
].cout_wrap
, b
->cout
);
1266 session
[b
->sid
].cin_delta
+= b
->cin
;
1267 session
[b
->sid
].cout_delta
+= b
->cout
;
1270 session
[b
->sid
].last_packet
= session
[b
->sid
].last_data
= time_now
;
1272 session
[b
->sid
].last_data
= time_now
;
1279 LOG(0, 0, 0, "Got C_BYTES with %d bytes of trailing junk!\n", size
);
1285 // Handle receiving a session structure in a heartbeat packet.
1287 static int cluster_recv_session(int more
, uint8_t *p
)
1289 if (more
>= MAXSESSION
) {
1290 LOG(0, 0, 0, "DANGER: Received a heartbeat session id > MAXSESSION!\n");
1294 if (session
[more
].tunnel
== T_UNDEF
) {
1295 if (config
->cluster_iam_uptodate
) { // Sanity.
1296 LOG(0, 0, 0, "I thought I was uptodate but I just found an undefined session!\n");
1298 --config
->cluster_undefined_sessions
;
1302 load_session(more
, (sessiont
*) p
); // Copy session into session table..
1304 LOG(5, more
, 0, "Received session update (%d undef)\n", config
->cluster_undefined_sessions
);
1306 if (!config
->cluster_iam_uptodate
)
1307 cluster_uptodate(); // Check to see if we're up to date.
1312 static int cluster_recv_bundle(int more
, uint8_t *p
)
1314 if (more
>= MAXBUNDLE
) {
1315 LOG(0, 0, 0, "DANGER: Received a bundle id > MAXBUNDLE!\n");
1319 if (bundle
[more
].state
== BUNDLEUNDEF
) {
1320 if (config
->cluster_iam_uptodate
) { // Sanity.
1321 LOG(0, 0, 0, "I thought I was uptodate but I just found an undefined bundle!\n");
1323 --config
->cluster_undefined_bundles
;
1327 memcpy(&bundle
[more
], p
, sizeof(bundle
[more
]) );
1329 LOG(5, 0, more
, "Received bundle update\n");
1331 if (!config
->cluster_iam_uptodate
)
1332 cluster_uptodate(); // Check to see if we're up to date.
1337 static int cluster_recv_tunnel(int more
, uint8_t *p
)
1339 if (more
>= MAXTUNNEL
) {
1340 LOG(0, 0, 0, "DANGER: Received a tunnel session id > MAXTUNNEL!\n");
1344 if (tunnel
[more
].state
== TUNNELUNDEF
) {
1345 if (config
->cluster_iam_uptodate
) { // Sanity.
1346 LOG(0, 0, 0, "I thought I was uptodate but I just found an undefined tunnel!\n");
1348 --config
->cluster_undefined_tunnels
;
1352 memcpy(&tunnel
[more
], p
, sizeof(tunnel
[more
]) );
1355 // Clear tunnel control messages. These are dynamically allocated.
1356 // If we get unlucky, this may cause the tunnel to drop!
1358 tunnel
[more
].controls
= tunnel
[more
].controle
= NULL
;
1359 tunnel
[more
].controlc
= 0;
1361 LOG(5, 0, more
, "Received tunnel update\n");
1363 if (!config
->cluster_iam_uptodate
)
1364 cluster_uptodate(); // Check to see if we're up to date.
1370 // pre v6 heartbeat session structure
1391 uint32_t cin_wrap
, cout_wrap
;
1392 uint32_t cin_delta
, cout_delta
;
1393 uint16_t throttle_in
;
1394 uint16_t throttle_out
;
1400 uint32_t session_timeout
;
1401 uint32_t idle_timeout
;
1404 in_addr_t dns1
, dns2
;
1405 routet route
[MAXROUTE
];
1408 int random_vector_length
;
1409 uint8_t random_vector
[MAXTEL
];
1411 char called
[MAXTEL
];
1412 char calling
[MAXTEL
];
1413 uint32_t tx_connect_speed
;
1414 uint32_t rx_connect_speed
;
1421 uint16_t snoop_port
;
1422 uint8_t walled_garden
;
1423 uint8_t ipv6prefixlen
;
1424 struct in6_addr ipv6route
;
1425 char reserved_3
[11];
1428 static uint8_t *convert_session(struct oldsession
*old
)
1430 static sessiont
new;
1433 memset(&new, 0, sizeof(new));
1435 new.next
= old
->next
;
1437 new.tunnel
= old
->tunnel
;
1438 new.flags
= old
->flags
;
1439 new.ppp
.phase
= old
->ppp
.phase
;
1440 new.ppp
.lcp
= old
->ppp
.lcp
;
1441 new.ppp
.ipcp
= old
->ppp
.ipcp
;
1442 new.ppp
.ipv6cp
= old
->ppp
.ipv6cp
;
1443 new.ppp
.ccp
= old
->ppp
.ccp
;
1445 new.ip_pool_index
= old
->ip_pool_index
;
1446 new.unique_id
= old
->unique_id
;
1447 new.magic
= old
->magic
;
1449 new.pout
= old
->pout
;
1451 new.cout
= old
->cout
;
1452 new.cin_wrap
= old
->cin_wrap
;
1453 new.cout_wrap
= old
->cout_wrap
;
1454 new.cin_delta
= old
->cin_delta
;
1455 new.cout_delta
= old
->cout_delta
;
1456 new.throttle_in
= old
->throttle_in
;
1457 new.throttle_out
= old
->throttle_out
;
1458 new.filter_in
= old
->filter_in
;
1459 new.filter_out
= old
->filter_out
;
1461 new.opened
= old
->opened
;
1463 new.session_timeout
= old
->session_timeout
;
1464 new.idle_timeout
= old
->idle_timeout
;
1465 new.last_packet
= old
->last_packet
;
1466 new.last_data
= old
->last_data
;
1467 new.dns1
= old
->dns1
;
1468 new.dns2
= old
->dns2
;
1469 new.tbf_in
= old
->tbf_in
;
1470 new.tbf_out
= old
->tbf_out
;
1471 new.random_vector_length
= old
->random_vector_length
;
1472 new.tx_connect_speed
= old
->tx_connect_speed
;
1473 new.rx_connect_speed
= old
->rx_connect_speed
;
1474 new.timeout
= old
->timeout
;
1475 new.mrru
= old
->mrru
;
1476 new.mssf
= old
->mssf
;
1477 new.epdis
= old
->epdis
;
1478 new.bundle
= old
->bundle
;
1479 new.snoop_ip
= old
->snoop_ip
;
1480 new.snoop_port
= old
->snoop_port
;
1481 new.walled_garden
= old
->walled_garden
;
1482 new.ipv6prefixlen
= old
->ipv6prefixlen
;
1483 new.ipv6route
= old
->ipv6route
;
1485 memcpy(new.random_vector
, old
->random_vector
, sizeof(new.random_vector
));
1486 memcpy(new.user
, old
->user
, sizeof(new.user
));
1487 memcpy(new.called
, old
->called
, sizeof(new.called
));
1488 memcpy(new.calling
, old
->calling
, sizeof(new.calling
));
1490 for (i
= 0; i
< MAXROUTE
; i
++)
1491 memcpy(&new.route
[i
], &old
->route
[i
], sizeof(new.route
[i
]));
1493 return (uint8_t *) &new;
1497 // Process a heartbeat..
1499 // v6: added RADIUS class attribute, re-ordered session structure
1500 // v7: added tunnelt attribute at the end of struct (tunnelt size change)
1501 static int cluster_process_heartbeat(uint8_t *data
, int size
, int more
, uint8_t *p
, in_addr_t addr
)
1504 int s
= size
- (p
-data
);
1509 # error "need to update cluster_process_heartbeat()"
1512 // we handle versions 5 through 8
1513 if (hb_ver
< 5 || hb_ver
> HB_VERSION
) {
1514 LOG(0, 0, 0, "Received a heartbeat version that I don't support (%d)!\n", hb_ver
);
1515 return -1; // Ignore it??
1518 if (size
> sizeof(past_hearts
[0].data
)) {
1519 LOG(0, 0, 0, "Received an oversize heartbeat from %s (%d)!\n", fmtaddr(addr
, 0), size
);
1530 if (h
->clusterid
!= config
->bind_address
)
1531 return -1; // It's not part of our cluster.
1533 if (config
->cluster_iam_master
) { // Sanity...
1534 // Note that this MUST match the election process above!
1536 LOG(0, 0, 0, "I just got a heartbeat from master %s, but _I_ am the master!\n", fmtaddr(addr
, 0));
1538 LOG(0, 0, 0, "Heartbeat with zero basetime! Ignoring\n");
1539 return -1; // Skip it.
1542 if (h
->table_version
> config
->cluster_table_version
) {
1543 LOG(0, 0, 0, "They've seen more state changes (%" PRIu64
" vs my %" PRIu64
") so I'm gone!\n",
1544 h
->table_version
, config
->cluster_table_version
);
1550 if (h
->table_version
< config
->cluster_table_version
)
1553 if (basetime
> h
->basetime
) {
1554 LOG(0, 0, 0, "They're an older master than me so I'm gone!\n");
1559 if (basetime
< h
->basetime
)
1562 if (my_address
< addr
) { // Tie breaker.
1563 LOG(0, 0, 0, "They're a higher IP address than me, so I'm gone!\n");
1569 // Send it a unicast heartbeat to see give it a chance to die.
1570 // NOTE: It's actually safe to do seq-number - 1 without checking
1573 cluster_catchup_slave(config
->cluster_seq_number
- 1, addr
);
1575 return -1; // Skip it.
1579 // Try and guard against a stray master appearing.
1581 // Ignore heartbeats received from another master before the
1582 // timeout (less a smidgen) for the old master has elapsed.
1584 // Note that after a clean failover, the cluster_master_address
1585 // is cleared, so this doesn't run.
1587 if (config
->cluster_master_address
&& addr
!= config
->cluster_master_address
) {
1588 LOG(0, 0, 0, "Ignoring stray heartbeat from %s, current master %s has not yet timed out (last heartbeat %.1f seconds ago).\n",
1589 fmtaddr(addr
, 0), fmtaddr(config
->cluster_master_address
, 1),
1590 0.1 * (TIME
- config
->cluster_last_hb
));
1591 return -1; // ignore
1594 if (config
->cluster_seq_number
== -1) // Don't have one. Just align to the master...
1595 config
->cluster_seq_number
= h
->seq
;
1597 config
->cluster_last_hb
= TIME
; // Reset to ensure that we don't become master!!
1598 config
->cluster_last_hb_ver
= hb_ver
; // remember what cluster version the master is using
1600 if (config
->cluster_seq_number
!= h
->seq
) { // Out of sequence heartbeat!
1601 static int lastseen_seq
= 0;
1602 static time_t lastseen_time
= 0;
1604 // limit to once per second for a particular seq#
1605 int ask
= (config
->cluster_seq_number
!= lastseen_seq
|| time_now
!= lastseen_time
);
1607 LOG(1, 0, 0, "HB: Got seq# %d but was expecting %d. %s.\n",
1608 h
->seq
, config
->cluster_seq_number
,
1609 ask
? "Asking for resend" : "Ignoring");
1613 lastseen_seq
= config
->cluster_seq_number
;
1614 lastseen_time
= time_now
;
1615 peer_send_message(addr
, C_LASTSEEN
, config
->cluster_seq_number
, NULL
, 0);
1618 config
->cluster_last_hb
= TIME
; // Reset to ensure that we don't become master!!
1620 // Just drop the packet. The master will resend it as part of the catchup.
1624 // Save the packet in our buffer.
1625 // This is needed in case we become the master.
1626 config
->cluster_seq_number
= (h
->seq
+1)%HB_MAX_SEQ
;
1627 i
= h
->seq
% HB_HISTORY_SIZE
;
1628 past_hearts
[i
].seq
= h
->seq
;
1629 past_hearts
[i
].size
= size
;
1630 memcpy(&past_hearts
[i
].data
, data
, size
); // Save it.
1633 // Check that we don't have too many undefined sessions, and
1634 // that the free session pointer is correct.
1635 cluster_check_sessions(h
->highsession
, h
->freesession
, h
->highbundle
, h
->hightunnel
);
1637 if (h
->interval
!= config
->cluster_hb_interval
)
1639 LOG(2, 0, 0, "Master set ping/heartbeat interval to %u (was %u)\n",
1640 h
->interval
, config
->cluster_hb_interval
);
1642 config
->cluster_hb_interval
= h
->interval
;
1645 if (h
->timeout
!= config
->cluster_hb_timeout
)
1647 LOG(2, 0, 0, "Master set heartbeat timeout to %u (was %u)\n",
1648 h
->timeout
, config
->cluster_hb_timeout
);
1650 config
->cluster_hb_timeout
= h
->timeout
;
1653 // Ok. process the packet...
1656 type
= *((uint32_t *) p
);
1657 p
+= sizeof(uint32_t);
1658 s
-= sizeof(uint32_t);
1660 more
= *((uint32_t *) p
);
1661 p
+= sizeof(uint32_t);
1662 s
-= sizeof(uint32_t);
1665 case C_CSESSION
: { // Compressed session structure.
1666 uint8_t c
[ sizeof(sessiont
) + 2];
1668 uint8_t *orig_p
= p
;
1670 size
= rle_decompress((uint8_t **) &p
, s
, c
, sizeof(c
) );
1673 // session struct changed with v5
1676 if (size
!= sizeof(struct oldsession
)) {
1677 LOG(0, 0, 0, "DANGER: Received a v%d CSESSION that didn't decompress correctly!\n", hb_ver
);
1678 // Now what? Should exit! No-longer up to date!
1681 cluster_recv_session(more
, convert_session((struct oldsession
*) c
));
1685 if (size
!= sizeof(sessiont
)) { // Ouch! Very very bad!
1686 if ((hb_ver
< HB_VERSION
) && (size
< sizeof(sessiont
)))
1688 // set to 0 the unused variables
1689 memset(&c
[size
], 0, (sizeof(sessiont
) - size
));
1690 LOG(3, 0, 0, "WARNING: Received a CSESSION from %s hb_version %d != %d current version !\n", fmtaddr(addr
, 2), hb_ver
, HB_VERSION
);
1691 // New feature not activated until the master has not been upgraded.
1695 LOG(0, 0, 0, "DANGER: Received a CSESSION that didn't decompress correctly!\n");
1696 // Now what? Should exit! No-longer up to date!
1701 cluster_recv_session(more
, c
);
1707 if (s
< sizeof(struct oldsession
))
1710 cluster_recv_session(more
, convert_session((struct oldsession
*) p
));
1712 p
+= sizeof(struct oldsession
);
1713 s
-= sizeof(struct oldsession
);
1717 if ( s
< sizeof(session
[more
]))
1720 cluster_recv_session(more
, p
);
1722 p
+= sizeof(session
[more
]);
1723 s
-= sizeof(session
[more
]);
1726 case C_CTUNNEL
: { // Compressed tunnel structure.
1727 uint8_t c
[ sizeof(tunnelt
) + 2];
1729 uint8_t *orig_p
= p
;
1731 size
= rle_decompress((uint8_t **) &p
, s
, c
, sizeof(c
));
1734 if ( ((hb_ver
>= HB_VERSION
) && (size
!= sizeof(tunnelt
))) ||
1735 ((hb_ver
< HB_VERSION
) && (size
> sizeof(tunnelt
))) )
1736 { // Ouch! Very very bad!
1737 LOG(0, 0, 0, "DANGER: Received a CTUNNEL that didn't decompress correctly!\n");
1738 // Now what? Should exit! No-longer up to date!
1742 cluster_recv_tunnel(more
, c
);
1747 if ( s
< sizeof(tunnel
[more
]))
1750 cluster_recv_tunnel(more
, p
);
1752 p
+= sizeof(tunnel
[more
]);
1753 s
-= sizeof(tunnel
[more
]);
1756 case C_CBUNDLE
: { // Compressed bundle structure.
1757 uint8_t c
[ sizeof(bundlet
) + 2];
1759 uint8_t *orig_p
= p
;
1761 size
= rle_decompress((uint8_t **) &p
, s
, c
, sizeof(c
));
1764 if (size
!= sizeof(bundlet
) ) { // Ouch! Very very bad!
1765 LOG(0, 0, 0, "DANGER: Received a CBUNDLE that didn't decompress correctly!\n");
1766 // Now what? Should exit! No-longer up to date!
1770 cluster_recv_bundle(more
, c
);
1775 if ( s
< sizeof(bundle
[more
]))
1778 cluster_recv_bundle(more
, p
);
1780 p
+= sizeof(bundle
[more
]);
1781 s
-= sizeof(bundle
[more
]);
1784 LOG(0, 0, 0, "DANGER: I received a heartbeat element where I didn't understand the type! (%d)\n", type
);
1785 return -1; // can't process any more of the packet!!
1789 if (config
->cluster_master_address
!= addr
)
1791 LOG(0, 0, 0, "My master just changed from %s to %s!\n",
1792 fmtaddr(config
->cluster_master_address
, 0), fmtaddr(addr
, 1));
1794 config
->cluster_master_address
= addr
;
1797 config
->cluster_last_hb
= TIME
; // Successfully received a heartbeat!
1798 config
->cluster_table_version
= h
->table_version
;
1802 LOG(0, 0, 0, "I got an incomplete heartbeat packet! This means I'm probably out of sync!!\n");
1807 // We got a packet on the cluster port!
1808 // Handle pings, lastseens, and heartbeats!
1810 int processcluster(uint8_t *data
, int size
, in_addr_t addr
)
1816 if (addr
== my_address
)
1817 return -1; // Ignore it. Something looped back the multicast!
1819 LOG(5, 0, 0, "Process cluster: %d bytes from %s\n", size
, fmtaddr(addr
, 0));
1821 if (s
<= 0) // Any data there??
1827 type
= *((uint32_t *) p
);
1828 p
+= sizeof(uint32_t);
1829 s
-= sizeof(uint32_t);
1831 more
= *((uint32_t *) p
);
1832 p
+= sizeof(uint32_t);
1833 s
-= sizeof(uint32_t);
1837 case C_PING
: // Update the peers table.
1838 return cluster_add_peer(addr
, more
, (pingt
*) p
, s
);
1840 case C_MASTER
: // Our master is wrong
1841 return cluster_set_master(addr
, more
);
1843 case C_LASTSEEN
: // Catch up a slave (slave missed a packet).
1844 return cluster_catchup_slave(more
, addr
);
1846 case C_FORWARD
: // Forwarded control packet. pass off to processudp.
1847 case C_FORWARD_DAE
: // Forwarded DAE packet. pass off to processdae.
1848 if (!config
->cluster_iam_master
)
1850 LOG(0, 0, 0, "I'm not the master, but I got a C_FORWARD%s from %s?\n",
1851 type
== C_FORWARD_DAE
? "_DAE" : "", fmtaddr(addr
, 0));
1857 struct sockaddr_in a
;
1859 a
.sin_addr
.s_addr
= more
;
1861 a
.sin_port
= (*(int *) p
) & 0xFFFF;
1862 indexudp
= ((*(int *) p
) >> 16) & 0xFFFF;
1866 LOG(4, 0, 0, "Got a forwarded %spacket... (%s:%d)\n",
1867 type
== C_FORWARD_DAE
? "DAE " : "", fmtaddr(more
, 0), a
.sin_port
);
1870 if (type
== C_FORWARD_DAE
)
1872 struct in_addr local
;
1873 local
.s_addr
= config
->bind_address
? config
->bind_address
: my_address
;
1874 processdae(p
, s
, &a
, sizeof(a
), &local
);
1877 processudp(p
, s
, &a
, indexudp
);
1881 case C_PPPOE_FORWARD
:
1882 if (!config
->cluster_iam_master
)
1884 LOG(0, 0, 0, "I'm not the master, but I got a C_PPPOE_FORWARD from %s?\n", fmtaddr(addr
, 0));
1889 pppoe_process_forward(p
, s
, addr
);
1893 case C_MPPP_FORWARD
:
1894 // Receive a MPPP packet from a slave.
1895 if (!config
->cluster_iam_master
) {
1896 LOG(0, 0, 0, "I'm not the master, but I got a C_MPPP_FORWARD from %s?\n", fmtaddr(addr
, 0));
1903 case C_THROTTLE
: { // Receive a forwarded packet from a slave.
1904 if (!config
->cluster_iam_master
) {
1905 LOG(0, 0, 0, "I'm not the master, but I got a C_THROTTLE from %s?\n", fmtaddr(addr
, 0));
1909 tbf_queue_packet(more
, p
, s
); // The TBF id tells wether it goes in or out.
1913 // Receive a walled garden packet from a slave.
1914 if (!config
->cluster_iam_master
) {
1915 LOG(0, 0, 0, "I'm not the master, but I got a C_GARDEN from %s?\n", fmtaddr(addr
, 0));
1923 if (!config
->cluster_iam_master
) {
1924 LOG(0, 0, 0, "I'm not the master, but I got a C_BYTES from %s?\n", fmtaddr(addr
, 0));
1928 return cluster_handle_bytes(p
, s
);
1930 case C_KILL
: // The master asked us to die!? (usually because we're too out of date).
1931 if (config
->cluster_iam_master
) {
1932 LOG(0, 0, 0, "_I_ am master, but I received a C_KILL from %s! (Seq# %d)\n", fmtaddr(addr
, 0), more
);
1935 if (more
!= config
->cluster_seq_number
) {
1936 LOG(0, 0, 0, "The master asked us to die but the seq number didn't match!?\n");
1940 if (addr
!= config
->cluster_master_address
) {
1941 LOG(0, 0, 0, "Received a C_KILL from %s which doesn't match config->cluster_master_address (%s)\n",
1942 fmtaddr(addr
, 0), fmtaddr(config
->cluster_master_address
, 1));
1943 // We can only warn about it. The master might really have switched!
1946 LOG(0, 0, 0, "Received a valid C_KILL: I'm going to die now.\n");
1948 exit(0); // Lets be paranoid;
1949 return -1; // Just signalling the compiler.
1952 LOG(4, 0, 0, "Got a heartbeat from %s\n", fmtaddr(addr
, 0));
1953 return cluster_process_heartbeat(data
, size
, more
, p
, addr
);
1956 LOG(0, 0, 0, "Strange type packet received on cluster socket (%d)\n", type
);
1962 LOG(0, 0, 0, "I got a _short_ cluster heartbeat packet! This means I'm probably out of sync!!\n");
1966 //====================================================================================================
1968 int cmd_show_cluster(struct cli_def
*cli
, const char *command
, char **argv
, int argc
)
1972 if (CLI_HELP_REQUESTED
)
1973 return CLI_HELP_NO_ARGS
;
1975 cli_print(cli
, "Cluster status : %s", config
->cluster_iam_master
? "Master" : "Slave" );
1976 cli_print(cli
, "My address : %s", fmtaddr(my_address
, 0));
1977 cli_print(cli
, "VIP address : %s", fmtaddr(config
->bind_address
, 0));
1978 cli_print(cli
, "Multicast address: %s", fmtaddr(config
->cluster_address
, 0));
1979 cli_print(cli
, "Multicast i'face : %s", config
->cluster_interface
);
1981 if (!config
->cluster_iam_master
) {
1982 cli_print(cli
, "My master : %s (last heartbeat %.1f seconds old)",
1983 config
->cluster_master_address
1984 ? fmtaddr(config
->cluster_master_address
, 0)
1986 0.1 * (TIME
- config
->cluster_last_hb
));
1987 cli_print(cli
, "Uptodate : %s", config
->cluster_iam_uptodate
? "Yes" : "No");
1988 cli_print(cli
, "Table version # : %" PRIu64
, config
->cluster_table_version
);
1989 cli_print(cli
, "Next sequence number expected: %d", config
->cluster_seq_number
);
1990 cli_print(cli
, "%d sessions undefined of %d", config
->cluster_undefined_sessions
, config
->cluster_highest_sessionid
);
1991 cli_print(cli
, "%d bundles undefined of %d", config
->cluster_undefined_bundles
, config
->cluster_highest_bundleid
);
1992 cli_print(cli
, "%d tunnels undefined of %d", config
->cluster_undefined_tunnels
, config
->cluster_highest_tunnelid
);
1994 cli_print(cli
, "Table version # : %" PRIu64
, config
->cluster_table_version
);
1995 cli_print(cli
, "Next heartbeat # : %d", config
->cluster_seq_number
);
1996 cli_print(cli
, "Highest session : %d", config
->cluster_highest_sessionid
);
1997 cli_print(cli
, "Highest bundle : %d", config
->cluster_highest_bundleid
);
1998 cli_print(cli
, "Highest tunnel : %d", config
->cluster_highest_tunnelid
);
1999 cli_print(cli
, "%d changes queued for sending", config
->cluster_num_changes
);
2001 cli_print(cli
, "%d peers.", num_peers
);
2004 cli_print(cli
, "%20s %10s %8s", "Address", "Basetime", "Age");
2005 for (i
= 0; i
< num_peers
; ++i
) {
2006 cli_print(cli
, "%20s %10u %8d", fmtaddr(peers
[i
].peer
, 0),
2007 peers
[i
].basetime
, TIME
- peers
[i
].timestamp
);
2013 // Simple run-length-encoding compression.
2015 // 1 byte < 128 = count of non-zero bytes following. // Not legal to be zero.
2016 // n non-zero bytes;
2018 // 1 byte > 128 = (count - 128) run of zero bytes. //
2020 // count == 0 indicates end of compressed stream.
2022 // Compress from 'src' into 'dst'. return number of bytes
2024 // Updates *src_p to indicate 1 past last bytes used.
2026 // We could get an extra byte in the zero runs by storing (count-1)
2027 // but I'm playing it safe.
2029 // Worst case is a 50% expansion in space required (trying to
2030 // compress { 0x00, 0x01 } * N )
2031 static int rle_compress(uint8_t **src_p
, int ssize
, uint8_t *dst
, int dsize
)
2034 int orig_dsize
= dsize
;
2038 while (ssize
> 0 && dsize
> 2) {
2040 x
= dst
++; --dsize
; // Reserve space for count byte..
2042 if (*src
) { // Copy a run of non-zero bytes.
2043 while (*src
&& count
< 127 && ssize
> 0 && dsize
> 1) { // Count number of non-zero bytes.
2048 *x
= count
; // Store number of non-zero bytes. Guarenteed to be non-zero!
2050 } else { // Compress a run of zero bytes.
2051 while (*src
== 0 && count
< 127 && ssize
> 0) {
2060 *dst
++ = 0x0; // Add Stop byte.
2064 return (orig_dsize
- dsize
);
2068 // Decompress the buffer into **p.
2069 // 'psize' is the size of the decompression buffer available.
2071 // Returns the number of bytes decompressed.
2073 // Decompresses from '*src_p' into 'dst'.
2074 // Return the number of dst bytes used.
2075 // Updates the 'src_p' pointer to point to the
2076 // first un-used byte.
2077 static int rle_decompress(uint8_t **src_p
, int ssize
, uint8_t *dst
, int dsize
)
2080 int orig_dsize
= dsize
;
2081 uint8_t *src
= *src_p
;
2083 while (ssize
>0 && dsize
> 0) { // While there's more to decompress, and there's room in the decompress buffer...
2084 count
= *src
++; --ssize
; // get the count byte from the source.
2085 if (count
== 0x0) // End marker reached? If so, finish.
2088 if (count
& 0x80) { // Decompress a run of zeros
2089 for (count
&= 0x7f ; count
> 0 && dsize
> 0; --count
) {
2093 } else { // Copy run of non-zero bytes.
2094 for ( ; count
> 0 && ssize
&& dsize
; --count
) { // Copy non-zero bytes across.
2101 return (orig_dsize
- dsize
);