netbuf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. * -
  33. * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
  34. *
  35. * This file is distributed in the hope that it will be useful, but WITHOUT
  36. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  37. * FITNESS FOR A PARTICULAR PURPOSE.
  38. *
  39. * You can redistribute this file and/or modify it under the terms of the GNU
  40. * General Public License (GPL) as published by the Free Software Foundation;
  41. * either version 2 of the License, or (at your discretion) any later version.
  42. * See the accompanying file "copying-gpl.txt" for more details.
  43. *
  44. * As a special exception to the GPL, permission is granted for additional
  45. * uses of the text contained in this file. See the accompanying file
  46. * "copying-liquorice.txt" for details.
  47. * -
  48. * Portions Copyright (c) 1983, 1993 by
  49. * The Regents of the University of California. All rights reserved.
  50. *
  51. * Redistribution and use in source and binary forms, with or without
  52. * modification, are permitted provided that the following conditions
  53. * are met:
  54. * 1. Redistributions of source code must retain the above copyright
  55. * notice, this list of conditions and the following disclaimer.
  56. * 2. Redistributions in binary form must reproduce the above copyright
  57. * notice, this list of conditions and the following disclaimer in the
  58. * documentation and/or other materials provided with the distribution.
  59. * 3. Neither the name of the University nor the names of its contributors
  60. * may be used to endorse or promote products derived from this software
  61. * without specific prior written permission.
  62. *
  63. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  64. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  66. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  67. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  68. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  69. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  70. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  71. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  73. * SUCH DAMAGE.
  74. * -
  75. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  76. *
  77. * Permission to use, copy, modify, and distribute this software for any
  78. * purpose with or without fee is hereby granted, provided that the above
  79. * copyright notice and this permission notice appear in all copies, and that
  80. * the name of Digital Equipment Corporation not be used in advertising or
  81. * publicity pertaining to distribution of the document or software without
  82. * specific, written prior permission.
  83. *
  84. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  85. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  86. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  87. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  88. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  89. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  90. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  91. * SOFTWARE.
  92. */
  93. /*
  94. * $Log$
  95. * Revision 1.6 2008/08/11 06:59:42 haraldkipp
  96. * BSD types replaced by stdint types (feature request #1282721).
  97. *
  98. * Revision 1.5 2008/08/09 17:35:32 haraldkipp
  99. * Simplified NETBUF allocation and release.
  100. *
  101. * Revision 1.4 2008/04/18 13:31:59 haraldkipp
  102. * Changed size parameter from uint16_t to int, which is easier to handle
  103. * for 32-bit targets. You need to recompile your ARM code. No impact on
  104. * AVR expected
  105. * I changed u_int to int at some places to avoid some warnings during
  106. * compilation of Nut/Net.
  107. * libs.
  108. *
  109. * Revision 1.3 2005/04/30 16:42:41 chaac
  110. * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
  111. * is defined in NutConf, it will make effect where it is used.
  112. *
  113. * Revision 1.2 2004/01/16 07:51:43 drsung
  114. * Bugfix for reallocating smaller network buffers. Thx to Mike Cornelius.
  115. *
  116. * Revision 1.1.1.1 2003/05/09 14:40:46 haraldkipp
  117. * Initial using 3.2.1
  118. *
  119. * Revision 1.12 2003/03/31 14:53:07 harald
  120. * Prepare release 3.1
  121. *
  122. * Revision 1.11 2003/02/04 17:50:54 harald
  123. * Version 3 released
  124. *
  125. * Revision 1.10 2002/06/26 17:29:08 harald
  126. * First pre-release with 2.4 stack
  127. *
  128. */
  129. #include <cfg/os.h>
  130. #include <string.h>
  131. #include <sys/heap.h>
  132. #include <dev/netbuf.h>
  133. #include <sys/nutdebug.h>
  134. /*!
  135. * \addtogroup xgnetbuf
  136. */
  137. /*@{*/
  138. static int NutNetBufAllocData(NBDATA * nbd, int size, int offs)
  139. {
  140. nbd->vp = NutHeapAlloc(size + offs);
  141. if (nbd->vp) {
  142. nbd->vp = (void *) ((uint8_t *) nbd->vp + offs);
  143. nbd->sz = size;
  144. return 0;
  145. }
  146. return -1;
  147. }
  148. /*!
  149. * \brief Allocate or re-allocate a network buffer part.
  150. *
  151. * \param nb Points to an existing network buffer structure or NULL,
  152. * if a new structure should be created. An existing buffer
  153. * must not be used any further if this function returns
  154. * a null pointer.
  155. * \param type Part of the buffer to be allocated. This can be any of
  156. * the following:
  157. * - NBAF_DATALINK + 0 <= OFFSET <= 0xf
  158. * - NBAF_NETWORK
  159. * - NBAF_TRANSPORT
  160. * - NBAF_APPLICATION
  161. * \param size Size of the part to be allocated.
  162. *
  163. * \return Pointer to the allocated network buffer structure. A null
  164. * pointer is returned if not enough memory is available and
  165. * the whole structure is released.
  166. */
  167. NETBUF *NutNetBufAlloc(NETBUF * nb, uint8_t type, int size)
  168. {
  169. NBDATA * nbd;
  170. int offs = type & ~NBAF_ALL;
  171. if ((type & NBAF_ALL) == NBAF_DATALINK)
  172. type = NBAF_DATALINK;
  173. NUTASSERT(size > 0);
  174. NUTASSERT(type == NBAF_DATALINK ||
  175. type == NBAF_NETWORK ||
  176. type == NBAF_TRANSPORT ||
  177. type == NBAF_APPLICATION);
  178. /* Allocate a new buffer, if the caller don't provide one. */
  179. if (nb == NULL) {
  180. nb = NutHeapAllocClear(sizeof(NETBUF));
  181. }
  182. /* Make sure, that the allocation above was successful. */
  183. if (nb) {
  184. /* Determine the data type. */
  185. switch(type) {
  186. case NBAF_DATALINK:
  187. nbd = &nb->nb_dl;
  188. break;
  189. case NBAF_NETWORK:
  190. nbd = &nb->nb_nw;
  191. break;
  192. case NBAF_TRANSPORT:
  193. nbd = &nb->nb_tp;
  194. break;
  195. default:
  196. nbd = &nb->nb_ap;
  197. break;
  198. }
  199. /* Check previously allocated buffer. */
  200. if (nb->nb_flags & type) {
  201. if (nbd->sz < size) {
  202. /* Existing buffer is too small. */
  203. NutHeapFree((void *)NUTMEM_BOTTOM_ALIGN((uintptr_t) nbd->vp));
  204. nbd->sz = 0;
  205. } else {
  206. /*
  207. * Reduce the size. This is actually a bad idea, because
  208. * we may waste too much memory. One option would be to
  209. * use the new NutHeapRealloc, but not sure if it will
  210. * work for 32-bit CPUs after some crashes after its
  211. * introduction. Further, I'm not fully sure, if our
  212. * network routines actually re-use network buffers.
  213. */
  214. nbd->sz = size;
  215. }
  216. } else {
  217. /* Buffer was not allocated from heap. */
  218. nbd->sz = 0;
  219. }
  220. /* If the size is zero at this point,
  221. we need to allocate a new buffer. */
  222. if (nbd->sz == 0) {
  223. if (NutNetBufAllocData(nbd, size, offs)) {
  224. /* Out of memory, release the complete net buffer. */
  225. NutNetBufFree(nb);
  226. nb = NULL;
  227. } else {
  228. /* Success, mark the buffer allocated. */
  229. nb->nb_flags |= type;
  230. }
  231. }
  232. }
  233. return nb;
  234. }
  235. /*!
  236. * \brief Create a referencing copy of an existing network buffer structure.
  237. *
  238. * \param nb Points to an existing network buffer structure, previously
  239. * allocated by NutNetBufAlloc().
  240. * \param type Part of the buffer to be additionally allocated. This can
  241. * be any combination of the following flags:
  242. * - NBAF_DATALINK
  243. * - NBAF_NETWORK
  244. * - NBAF_TRANSPORT
  245. * - NBAF_APPLICATION
  246. *
  247. * \return Pointer to a newly allocated copy.
  248. */
  249. NETBUF *NutNetBufClonePart(NETBUF * nb, uint8_t inserts)
  250. {
  251. NETBUF *cb;
  252. NUTASSERT(nb != NULL);
  253. if ((nb->nb_flags & NBAF_REFCNT) == NBAF_REFCNT) {
  254. inserts = NBAF_ALL;
  255. }
  256. cb = NutHeapAllocClear(sizeof(NETBUF));
  257. if (cb) {
  258. uint_fast8_t referenced = 0;
  259. register int e = 0;
  260. register int s;
  261. s = nb->nb_dl.sz;
  262. if (s) {
  263. if (inserts & NBAF_DATALINK) {
  264. e = NutNetBufAllocData(&cb->nb_dl, s, (intptr_t)nb->nb_dl.vp & (NUTMEM_ALIGNMENT - 1));
  265. if (e == 0) {
  266. memcpy(cb->nb_dl.vp, nb->nb_dl.vp, s);
  267. cb->nb_flags |= NBAF_DATALINK;
  268. }
  269. } else {
  270. cb->nb_dl.vp = nb->nb_dl.vp;
  271. cb->nb_dl.sz = s;
  272. referenced = 1;
  273. }
  274. }
  275. s = nb->nb_nw.sz;
  276. if (s && e == 0) {
  277. if (inserts & NBAF_NETWORK) {
  278. e = NutNetBufAllocData(&cb->nb_nw, s, 0);
  279. if (e == 0) {
  280. memcpy(cb->nb_nw.vp, nb->nb_nw.vp, s);
  281. cb->nb_flags |= NBAF_NETWORK;
  282. }
  283. } else {
  284. cb->nb_nw.vp = nb->nb_nw.vp;
  285. cb->nb_nw.sz = s;
  286. referenced = 1;
  287. }
  288. }
  289. s = nb->nb_tp.sz;
  290. if (s && e == 0) {
  291. if (inserts & NBAF_TRANSPORT) {
  292. e = NutNetBufAllocData(&cb->nb_tp, s, 0);
  293. if (e == 0) {
  294. memcpy(cb->nb_tp.vp, nb->nb_tp.vp, s);
  295. cb->nb_flags |= NBAF_TRANSPORT;
  296. }
  297. } else {
  298. cb->nb_tp.vp = nb->nb_tp.vp;
  299. cb->nb_tp.sz = s;
  300. referenced = 1;
  301. }
  302. }
  303. s = nb->nb_ap.sz;
  304. if (s && e == 0) {
  305. if (inserts & NBAF_APPLICATION) {
  306. e = NutNetBufAllocData(&cb->nb_ap, s, 0);
  307. if (e == 0) {
  308. memcpy(cb->nb_ap.vp, nb->nb_ap.vp, s);
  309. cb->nb_flags |= NBAF_APPLICATION;
  310. }
  311. } else {
  312. cb->nb_ap.vp = nb->nb_ap.vp;
  313. cb->nb_ap.sz = s;
  314. referenced = 1;
  315. }
  316. }
  317. if (e) {
  318. NutNetBufFree(cb);
  319. cb = NULL;
  320. }
  321. else if (referenced) {
  322. cb->nb_ref = nb;
  323. nb->nb_flags++;
  324. }
  325. }
  326. return cb;
  327. }
  328. /*!
  329. * \brief Create a copy of an existing network buffer
  330. * structure.
  331. *
  332. * \param nb Points to an existing network buffer
  333. * structure, previously allocated by
  334. * NutNetBufAlloc().
  335. *
  336. * \return Pointer to a newly allocated copy.
  337. */
  338. NETBUF *NutNetBufClone(NETBUF * nb)
  339. {
  340. return NutNetBufClonePart(nb, NBAF_ALL);
  341. }
  342. /*!
  343. * \brief Release a network buffer structure.
  344. *
  345. * Returns all memory previously allocated by a
  346. * network buffer to the available heap space.
  347. *
  348. * \param nb Points to an existing network buffer
  349. * structure, previously allocated by
  350. * NutNetBufAlloc().
  351. */
  352. void NutNetBufFree(NETBUF * nb)
  353. {
  354. NUTASSERT(nb);
  355. if (nb->nb_flags & NBAF_REFCNT) {
  356. nb->nb_flags--;
  357. } else {
  358. if (nb->nb_flags & NBAF_DATALINK) {
  359. NutHeapFree((void *) NUTMEM_BOTTOM_ALIGN((uintptr_t) nb->nb_dl.vp));
  360. }
  361. if (nb->nb_flags & NBAF_NETWORK) {
  362. NutHeapFree(nb->nb_nw.vp);
  363. }
  364. if (nb->nb_flags & NBAF_TRANSPORT) {
  365. NutHeapFree(nb->nb_tp.vp);
  366. }
  367. if (nb->nb_flags & NBAF_APPLICATION) {
  368. NutHeapFree(nb->nb_ap.vp);
  369. }
  370. if (nb->nb_ref) {
  371. NutNetBufFree(nb->nb_ref);
  372. }
  373. NutHeapFree(nb);
  374. }
  375. }
  376. /*!
  377. * \brief Collect linked list of network buffers.
  378. *
  379. * \param nbq Points to an existing network buffer queue.
  380. *
  381. * \return Number of released network buffers.
  382. */
  383. int NutNetBufCollect(NETBUF * nbq, int total)
  384. {
  385. int rc = 0;
  386. NETBUF *nb;
  387. NETBUF *nbx;
  388. NBDATA nbd;
  389. uint8_t *ap;
  390. if (NutNetBufAllocData(&nbd, total, 0) == 0) {
  391. ap = (uint8_t *) nbd.vp;
  392. memcpy(nbd.vp, nbq->nb_ap.vp, nbq->nb_ap.sz);
  393. nbd.sz = nbq->nb_ap.sz;
  394. nb = nbq->nb_next;
  395. while (nb && total >= nbd.sz + nb->nb_ap.sz) {
  396. nbx = nb->nb_next;
  397. memcpy(ap + nbd.sz, nb->nb_ap.vp, nb->nb_ap.sz);
  398. nbd.sz += nb->nb_ap.sz;
  399. NutNetBufFree(nb);
  400. nb = nbx;
  401. rc++;
  402. }
  403. nbq->nb_next = nb;
  404. if (nbq->nb_flags & NBAF_APPLICATION) {
  405. NutHeapFree(nbq->nb_ap.vp);
  406. } else {
  407. nbq->nb_flags |= NBAF_APPLICATION;
  408. }
  409. nbq->nb_ap.vp = nbd.vp;
  410. nbq->nb_ap.sz = nbd.sz;
  411. }
  412. return rc;
  413. }
  414. /*@}*/