Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / rconn.h
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #ifndef RCONN_H
18 #define RCONN_H 1
19
20 #include "queue.h"
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <time.h>
24
25 /* A wrapper around vconn that provides queuing and optionally reliability.
26  *
27  * An rconn maintains a message transmission queue of bounded length specified
28  * by the caller.  The rconn does not guarantee reliable delivery of
29  * queued messages: all queued messages are dropped when reconnection becomes
30  * necessary.
31  *
32  * An rconn optionally provides reliable communication, in this sense: the
33  * rconn will re-connect, with exponential backoff, when the underlying vconn
34  * disconnects.
35  */
36
37 struct vconn;
38 struct rconn_packet_counter;
39
40 struct rconn *rconn_new(const char *name, 
41                         int inactivity_probe_interval, int max_backoff);
42 struct rconn *rconn_new_from_vconn(const char *name, struct vconn *);
43 struct rconn *rconn_create(int inactivity_probe_interval, int max_backoff);
44
45 void rconn_set_max_backoff(struct rconn *, int max_backoff);
46 int rconn_get_max_backoff(const struct rconn *);
47 void rconn_set_probe_interval(struct rconn *, int inactivity_probe_interval);
48 int rconn_get_probe_interval(const struct rconn *);
49
50 int rconn_connect(struct rconn *, const char *name);
51 void rconn_connect_unreliably(struct rconn *,
52                               const char *name, struct vconn *vconn);
53 void rconn_reconnect(struct rconn *);
54 void rconn_disconnect(struct rconn *);
55 void rconn_destroy(struct rconn *);
56
57 void rconn_run(struct rconn *);
58 void rconn_run_wait(struct rconn *);
59 struct ofpbuf *rconn_recv(struct rconn *);
60 void rconn_recv_wait(struct rconn *);
61 int rconn_send(struct rconn *, struct ofpbuf *, struct rconn_packet_counter *);
62 int rconn_send_with_limit(struct rconn *, struct ofpbuf *,
63                           struct rconn_packet_counter *, int queue_limit);
64 unsigned int rconn_packets_sent(const struct rconn *);
65 unsigned int rconn_packets_received(const struct rconn *);
66
67 void rconn_add_monitor(struct rconn *, struct vconn *);
68
69 const char *rconn_get_name(const struct rconn *);
70 bool rconn_is_alive(const struct rconn *);
71 bool rconn_is_connected(const struct rconn *);
72 int rconn_failure_duration(const struct rconn *);
73 bool rconn_is_connectivity_questionable(struct rconn *);
74
75 uint32_t rconn_get_ip(const struct rconn *);
76
77 const char *rconn_get_state(const struct rconn *);
78 unsigned int rconn_get_attempted_connections(const struct rconn *);
79 unsigned int rconn_get_successful_connections(const struct rconn *);
80 time_t rconn_get_last_connection(const struct rconn *);
81 time_t rconn_get_creation_time(const struct rconn *);
82 unsigned long int rconn_get_total_time_connected(const struct rconn *);
83 int rconn_get_backoff(const struct rconn *);
84 unsigned int rconn_get_state_elapsed(const struct rconn *);
85 unsigned int rconn_get_connection_seqno(const struct rconn *);
86
87 /* Counts the number of packets queued into an rconn by a given source. */
88 struct rconn_packet_counter {
89     int n;                      /* Number of packets queued. */
90     int ref_cnt;                /* Number of owners. */
91 };
92
93 struct rconn_packet_counter *rconn_packet_counter_create(void);
94 void rconn_packet_counter_destroy(struct rconn_packet_counter *);
95 void rconn_packet_counter_inc(struct rconn_packet_counter *);
96 void rconn_packet_counter_dec(struct rconn_packet_counter *);
97
98 static inline int
99 rconn_packet_counter_read(const struct rconn_packet_counter *counter)
100 {
101     return counter->n;
102 }
103
104 #endif /* rconn.h */