tagging vde-2 version 2.3.2
[vde.git] / 2.3.2 / src / vde_l3 / pfifo.c
blob2a4c0e28523f1b6c762e17f35bef43657935ff9f
1 /*
2 * tc pfifo module
3 * Usage: tc set <dev> pfifo limit <packets>
5 * */
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
10 #include <config.h>
11 #include <vde.h>
12 #include <vdecommon.h>
13 #include "vde_buff.h"
14 #include "vde_l3.h"
16 /** Private per-interface structure
19 struct tc_pfifo
21 uint32_t qlen;
22 uint32_t limit;
23 uint32_t dropped;
26 #define pfifo_tcpriv(x) (struct tc_pfifo*)(tcpriv(x))
29 * Enqueue function. Try to add the packet 'vdb' to the output queue
30 * of the interface 'vif'
32 * return value: 1 = packet was enqueued, 0 = packet was rejected
34 int pfifo_enqueue(struct vde_buff *vdb, struct vde_iface *vif)
36 struct tc_pfifo *pfifo = pfifo_tcpriv(vif);
37 if (pfifo->qlen < pfifo->limit){
38 pfifo->qlen++;
39 ufifo_enqueue(vdb,vif);
40 return 1;
41 }else{
42 /* Queue Full: dropping. */
43 free(vdb);
44 pfifo->dropped++;
45 return 0;
49 /* Dequeue function. Interface is ready to send the packet.
52 int pfifo_dequeue(struct vde_iface *vif)
54 struct tc_pfifo *pfifo = pfifo_tcpriv(vif);
55 (void)ufifo_dequeue(vif);
56 if(pfifo->qlen > 0)
57 pfifo->qlen--;
58 return (pfifo->qlen > 0);
63 /* Function to initialize the queue on the given interface.
65 int pfifo_init(struct vde_iface *vif, char *args)
67 struct tc_pfifo *pfifo=(struct tc_pfifo *)malloc(sizeof(struct tc_pfifo));
68 int arglen = strlen(args) - 1;
70 if ((arglen < 6) || strncmp(args,"limit ",6) || (sscanf(args+6, "%u",&(pfifo->limit)) < 1) )
71 return 0;
73 pfifo->qlen = 0;
74 pfifo->dropped = 0;
75 vif->policy_name="pfifo";
76 memcpy(vif->tc_priv, pfifo, sizeof(struct tc_pfifo));
77 return 1;
80 char *pfifo_tc_stats(struct vde_iface *vif)
82 struct tc_pfifo *pfifo = pfifo_tcpriv(vif);
83 char *statistics=(char*)malloc(256);
84 snprintf(statistics,255,"Limit: %u packets. Dropped: %u packets.", pfifo->limit, pfifo->dropped);
85 return statistics;
91 * Module symbol to load into module list.
94 struct routing_policy module_routing_policy=
96 .name="pfifo",
97 .help="Packet Fifo queue\nUsage: tc set <dev> pfifo limit <packets>\n",
98 .policy_init = pfifo_init,
99 .enqueue = pfifo_enqueue,
100 .dequeue = pfifo_dequeue,
101 .tc_stats = pfifo_tc_stats
104 static void
105 __attribute__ ((constructor))
106 init (void)
108 fprintf(stderr,"Loading library: pfifo.so\n");
112 static void
113 __attribute__ ((destructor))
114 fini (void)