Revision 08a4230a

View differences:

Makefile.am
6 6
noinst_LIBRARIES = libml.a
7 7

  
8 8
libml_a_SOURCES = BUGS.txt ml.c util/stun.c \
9
	util/udpSocket.c
9
	util/udpSocket.c util/rateControl.c
10 10
# transmissionHandler.c
11 11

  
12 12
# testMessaginglayer: echoServer.c ml.h ml.c transmissionHandler.h transmissionHandler.c util/stun.h util/stun.c util/udpSocket.h util/udpSocket.c
13
#        ${COMPILER} -o echoServer.o echoServer.c ml.h ml.c transmissionHandler.h transmissionHandler.c util/stun.h util/stun.c util/udpSocket.h util/udpSocket.c ${EVENT} ${MATH} ${GDB}
13
#        ${COMPILER} -o echoServer.o echoServer.c ml.h ml.c transmissionHandler.h transmissionHandler.c util/stun.h util/stun.c util/udpSocket.h util/udpSocket.c util/rateControl.c ${EVENT} ${MATH} ${GDB}
14 14

  
15 15
#libml_a_LIBADD = $(top_builddir)/dclog/libdclog.a
include/ml.h
262 262
int mlInit(bool recv_data_cb,struct timeval timeout_value,const int port,const char *ipaddr,const int stun_port,const char *stun_ipaddr,receive_localsocketID_cb local_socketID_cb,void *arg);
263 263

  
264 264
/**
265
  * Configure the parameters for output rate control.
266
  * These values may also be set while packets are being transmitted.
267
  * @param bucketsize The size of the bucket in kbytes
268
  * @param drainrate The amount of kbytes draining in a second. If drainrate is <=0, then rateControl is completely disabled (all packets are passed).
269
*/
270
void mlSetThrottle(int bucketsize, int drainrate);
271

  
272
/**
265 273
 * @brief Register a received packet callback.
266 274
 * This function is to register a callback that is invoked when a messaging layer packet is received.
267 275
 * @param recv_pkt_inf_cb A function pointer to a callback function from the type get_recv_pkt_inf_cb
ml.c
339 339
					info("ML: sending message failed (to:%s conID:%d lconID:%d msgsize:%d offset:%d)\n", conid_to_string(con_id), ntohl(msg_h.remote_con_id), ntohl(msg_h.local_con_id), msg_len, offset);
340 340
					offset = msg_len; // exit the while
341 341
					break;
342
                                case THROTTLE:
343
                                    //    debug("THROTTLE on output"); 
344
					offset = msg_len; // exit the while
345
					break;
342 346
				case OK:
343 347
					//update
344 348
					offset += pkt_len;
......
1328 1332
	return create_socket(port, ipaddr);
1329 1333
}
1330 1334

  
1335
void mlSetThrottle(int bucketsize, int drainrate) {
1336
        setOutputRateParams(bucketsize, drainrate);
1337
}
1338
     
1339

  
1331 1340
/* register callbacks  */
1332 1341
void mlRegisterGetRecvPktInf(get_recv_pkt_inf_cb recv_pkt_inf_cb){
1333 1342

  
util/rateControl.c
1
/*
2
 *          Policy Management
3
 *
4
 *
5
 * This software was created by arpad.bakay@netvisor.hu
6
 *
7
 *     THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
8
 */
9

  
10
#include <sys/time.h>
11

  
12
#include "udpSocket.h"
13

  
14
static long bucket_size = 0;
15
static int drain_rate = 0;
16

  
17
static long bytes_in_bucket = 0;
18
struct timeval bib_when = { 0, 0};
19

  
20
int outputRateControl(int len) {
21
   struct timeval now;
22
   gettimeofday(&now, NULL);
23
   if(drain_rate <= 0) {
24
      bytes_in_bucket = 0;
25
      bib_when = now;
26
      return OK;
27
   }
28
   else {
29
       int total_drain_secs = bytes_in_bucket / drain_rate + 1; 
30
       if(now.tv_sec - bib_when.tv_sec - 1 < total_drain_secs) {
31
           bytes_in_bucket = 0;
32
       }
33
       else {
34
          long leaked = drain_rate * 1024 * (now.tv_sec - bib_when.tv_sec);
35
          leaked += drain_rate * (now.tv_usec - bib_when.tv_usec) / (1000000 / 1024); 
36
	  if(leaked > bytes_in_bucket) bytes_in_bucket = 0;
37
          else bytes_in_bucket -= leaked;
38
       }
39
       bib_when = now;
40
       if(bytes_in_bucket + len <= bucket_size) {
41
              bytes_in_bucket += len;
42
              return OK;
43
       }
44
       else  return THROTTLE;
45
   }
46
}
47

  
48
void setOutputRateParams(int bucketsize, int drainrate) {
49
     bucket_size = bucketsize * 1024;
50
     outputRateControl(0);
51
     drain_rate = drainrate; 
52
}
53

  
54

  
util/udpSocket.c
179 179
{
180 180
	int error, ret;
181 181
	struct msghdr msgh;
182
        
183
        if(outputRateControl(len) != OK) return THROTTLE;
182 184

  
183 185
	msgh.msg_name = socketaddr;
184 186
	msgh.msg_namelen = sizeof(struct sockaddr_in);
util/udpSocket.h
80 80
#define SO_EE_ORIGIN_ICMP6      3
81 81
/// @}
82 82

  
83
typedef enum {OK = 0, MSGLEN, FAILURE} error_codes;
83
typedef enum {OK = 0, MSGLEN, FAILURE, THROTTLE} error_codes;
84 84

  
85 85
/** 
86 86
 * A callback functions for received pmtu errors (icmp packets type 3 code 4) 
......
114 114
 */
115 115
int sendPacket(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr);
116 116

  
117
/** 
118
  * Decide if a packet should be throttled
119
  * The implementation follows a leaky bucket algorithm: 
120
  * if the packet would fill the bucket beyond its limit, it is to be discarded
121
  *
122
  * @param len The length of the packet to be sent
123
  * @return OK or THROTTLE 
124
*/
125
int outputRateControl(int len);
126

  
127
/**
128
  * Configure the parameters for output rate control.
129
  * These values may also be set while packets are being transmitted.
130
  * @param bucketsize The size of the bucket in kbytes
131
  * @param drainrate The amount of kbytes draining in a second. If drainrate is 0, then rateControl is completely disabled (all packets are passed).
132
*/
133
void setOutputRateParams(int bucketsize, int drainrate);
134

  
117 135
/**
118 136
 * Receive a udp packet
119 137
 * @param udpSocket The udpSocket file descriptor.

Also available in: Unified diff