Debian Codegraph @

common/atm_vsar.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2007 Christophe Fillot ([email protected])
4 *
5 * ATM Virtual Segmentation & Reassembly Engine.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <pthread.h>
13 #include <errno.h>
14 #include <sys/select.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <sys/uio.h>
18
19 #include "utils.h"
20 #include "registry.h"
21 #include "crc.h"
22 #include "atm.h"
23 #include "net_io.h"
24 #include "atm_vsar.h"
25
26 /* Segmentation Context */
27 struct atm_seg_context {
28 netio_desc_t *nio;
29
30 m_uint8_t txfifo_cell[ATM_CELL_SIZE];
31 size_t txfifo_pos,txfifo_avail;
32 size_t aal5_len;
33 m_uint32_t aal5_crc;
34 m_uint32_t atm_hdr;
35
36 char *buffer;
37 size_t buf_len;
38 };
39
40 /* Send the ATM cell in FIFO */
41 static void atm_send_cell(struct atm_seg_context *asc)
42 {
43 m_hton32(asc->txfifo_cell,asc->atm_hdr);
44 atm_insert_hec(asc->txfifo_cell);
45 netio_send(asc->nio,asc->txfifo_cell,ATM_CELL_SIZE);
46 }
47
48 /* Clear the TX fifo */
49 static void atm_clear_tx_fifo(struct atm_seg_context *asc)
50 {
51 asc->txfifo_avail = ATM_PAYLOAD_SIZE;
52 asc->txfifo_pos = ATM_HDR_SIZE;
53 memset(asc->txfifo_cell,0,ATM_CELL_SIZE);
54 }
55
56 /* Add padding to the FIFO */
57 static void atm_add_tx_padding(struct atm_seg_context *asc,size_t len)
58 {
59 if (len > asc->txfifo_avail)
60 len = asc->txfifo_avail;
61
62 memset(&asc->txfifo_cell[asc->txfifo_pos],0,len);
63 asc->txfifo_pos += len;
64 asc->txfifo_avail -= len;
65 }
66
67 /* Send the TX fifo if it is empty */
68 static void atm_send_fifo(struct atm_seg_context *asc)
69 {
70 if (!asc->txfifo_avail) {
71 asc->aal5_crc = crc32_compute(~asc->aal5_crc,
72 &asc->txfifo_cell[ATM_HDR_SIZE],
73 ATM_PAYLOAD_SIZE);
74 atm_send_cell(asc);
75 atm_clear_tx_fifo(asc);
76 }
77 }
78
79 /* Store a packet in the TX FIFO */
80 static int atm_store_fifo(struct atm_seg_context *asc)
81 {
82 size_t len;
83
84 len = m_min(asc->buf_len,asc->txfifo_avail);
85
86 memcpy(&asc->txfifo_cell[asc->txfifo_pos],asc->buffer,len);
87 asc->buffer += len;
88 asc->buf_len -= len;
89 asc->txfifo_pos += len;
90 asc->txfifo_avail -= len;
91
92 if (!asc->txfifo_avail) {
93 atm_send_fifo(asc);
94 return(TRUE);
95 }
96
97 return(FALSE);
98 }
99
100 /* Add the AAL5 trailer to the TX FIFO */
101 static void atm_aal5_add_trailer(struct atm_seg_context *asc)
102 {
103 m_uint8_t *trailer;
104
105 trailer = &asc->txfifo_cell[ATM_AAL5_TRAILER_POS];
106
107 /* Control field + Length */
108 m_hton32(trailer,asc->aal5_len);
109
110 /* Final CRC-32 computation */
111 asc->aal5_crc = crc32_compute(~asc->aal5_crc,
112 &asc->txfifo_cell[ATM_HDR_SIZE],
113 ATM_PAYLOAD_SIZE - 4);
114
115 m_hton32(trailer+4,asc->aal5_crc);
116
117 /* Consider the FIFO as full */
118 asc->txfifo_avail = 0;
119 }
120
121 /* Send an AAL5 packet through an NIO (segmentation) */
122 int atm_aal5_send(netio_desc_t *nio,u_int vpi,u_int vci,
123 struct iovec *iov,int iovcnt)
124 {
125 struct atm_seg_context asc;
126 int i;
127
128 asc.nio = nio;
129 asc.aal5_len = 0;
130 asc.aal5_crc = 0; /* will be inverted by first CRC update */
131 atm_clear_tx_fifo(&asc);
132
133 /* prepare the atm header */
134 asc.atm_hdr = vpi << ATM_HDR_VPI_SHIFT;
135 asc.atm_hdr |= vci << ATM_HDR_VCI_SHIFT;
136
137 for(i=0;i<iovcnt;i++) {
138 asc.buffer = iov[i].iov_base;
139 asc.buf_len = iov[i].iov_len;
140 asc.aal5_len += iov[i].iov_len;
141
142 while(asc.buf_len > 0)
143 atm_store_fifo(&asc);
144 }
145
146 /*
147 * Add the PDU trailer. If we have enough room, add it in the last cell,
148 * otherwise create a new one.
149 */
150 if (asc.txfifo_avail < ATM_AAL5_TRAILER_SIZE) {
151 atm_add_tx_padding(&asc,asc.txfifo_avail);
152 atm_send_fifo(&asc);
153 }
154
155 /* Set AAL5 end of packet in ATM header (PTI field) */
156 asc.atm_hdr |= ATM_PTI_EOP;
157
158 atm_add_tx_padding(&asc,asc.txfifo_avail - ATM_AAL5_TRAILER_SIZE);
159 atm_aal5_add_trailer(&asc);
160 atm_send_cell(&asc);
161 return(0);
162 }
163
164 /* Reset a receive context */
165 void atm_aal5_recv_reset(struct atm_reas_context *arc)
166 {
167 arc->buf_pos = 0;
168 arc->len = 0;
169 }
170
171 /* Receive an ATM cell and process reassembly */
172 int atm_aal5_recv(struct atm_reas_context *arc,m_uint8_t *cell)
173 {
174 m_uint32_t atm_hdr;
175
176 /* Check buffer boundary */
177 if ((arc->buf_pos + ATM_PAYLOAD_SIZE) > ATM_REAS_MAX_SIZE) {
178 atm_aal5_recv_reset(arc);
179 return(-1);
180 }
181
182 /* Get the PTI field: we cannot handle "network" traffic */
183 atm_hdr = m_ntoh32(cell);
184
185 if (atm_hdr & ATM_PTI_NETWORK)
186 return(2);
187
188 /* Copy the payload */
189 memcpy(&arc->buffer[arc->buf_pos],&cell[ATM_HDR_SIZE],ATM_PAYLOAD_SIZE);
190 arc->buf_pos += ATM_PAYLOAD_SIZE;
191
192 /*
193 * If this is the last cell of the packet, get the real length (the
194 * trailer is at the end).
195 */
196 if (atm_hdr & ATM_PTI_EOP) {
197 arc->len = m_ntoh16(&cell[ATM_AAL5_TRAILER_POS+2]);
198 return((arc->len <= arc->buf_pos) ? 1 : -2);
199 }
200
201 return(0);
202 }
203
204 /* Send a packet through a rfc1483 bridge encap */
205 int atm_aal5_send_rfc1483b(netio_desc_t *nio,u_int vpi,u_int vci,
206 void *pkt,size_t len)
207 {
208 struct iovec vec[2];
209
210 vec[0].iov_base = (void *)atm_rfc1483b_header;
211 vec[0].iov_len = ATM_RFC1483B_HLEN;
212 vec[1].iov_base = pkt;
213 vec[1].iov_len = len;
214
215 return(atm_aal5_send(nio,vpi,vci,vec,2));
216 }