PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/packet.cc

https://github.com/bhesmans/click
C++ | 965 lines | 662 code | 73 blank | 230 comment | 191 complexity | 8bc1ef296c9a9ed235efc56b0bcb4440 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. // -*- related-file-name: "../include/click/packet.hh" -*-
  2. /*
  3. * packet.{cc,hh} -- a packet structure. In the Linux kernel, a synonym for
  4. * `struct sk_buff'
  5. * Eddie Kohler, Robert Morris, Nickolai Zeldovich
  6. *
  7. * Copyright (c) 1999-2001 Massachusetts Institute of Technology
  8. * Copyright (c) 2008-2011 Regents of the University of California
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a
  11. * copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, subject to the conditions
  13. * listed in the Click LICENSE file. These conditions include: you must
  14. * preserve this copyright notice, and you cannot mention the copyright
  15. * holders in advertising related to the Software without their permission.
  16. * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
  17. * notice is a summary of the Click LICENSE file; the license in that file is
  18. * legally binding.
  19. */
  20. #include <click/config.h>
  21. #define CLICK_PACKET_DEPRECATED_ENUM
  22. #include <click/packet.hh>
  23. #include <click/packet_anno.hh>
  24. #include <click/glue.hh>
  25. #include <click/sync.hh>
  26. #if CLICK_USERLEVEL
  27. # include <unistd.h>
  28. #endif
  29. CLICK_DECLS
  30. /** @file packet.hh
  31. * @brief The Packet class models packets in Click.
  32. */
  33. /** @class Packet
  34. * @brief A network packet.
  35. * @nosubgrouping
  36. *
  37. * Click's Packet class represents network packets within a router. Packet
  38. * objects are passed from Element to Element via the Element::push() and
  39. * Element::pull() functions. The vast majority of elements handle packets.
  40. *
  41. * A packet consists of a <em>data buffer</em>, which stores the actual packet
  42. * wire data, and a set of <em>annotations</em>, which store extra information
  43. * calculated about the packet, such as the destination address to be used for
  44. * routing. Every Packet object has different annotations, but a data buffer
  45. * may be shared among multiple Packet objects, saving memory and speeding up
  46. * packet copies. (See Packet::clone.) As a result a Packet's data buffer is
  47. * not writable. To write into a packet, turn it into a nonshared
  48. * WritablePacket first, using uniqueify(), push(), or put().
  49. *
  50. * <h3>Data Buffer</h3>
  51. *
  52. * A packet's data buffer is a single flat array of bytes. The buffer may be
  53. * larger than the actual packet data, leaving unused spaces called
  54. * <em>headroom</em> and <em>tailroom</em> before and after the data proper.
  55. * Prepending headers or appending data to a packet can be quite efficient if
  56. * there is enough headroom or tailroom.
  57. *
  58. * The relationships among a Packet object's data buffer variables is shown
  59. * here:
  60. *
  61. * <pre>
  62. * data() end_data()
  63. * | |
  64. * |<- headroom() ->|<----- length() ----->|<- tailroom() ->|
  65. * | v v |
  66. * +================+======================+================+
  67. * |XXXXXXXXXXXXXXXX| PACKET CONTENTS |XXXXXXXXXXXXXXXX|
  68. * +================+======================+================+
  69. * ^ ^
  70. * |<------------------ buffer_length() ------------------->|
  71. * | |
  72. * buffer() end_buffer()
  73. * </pre>
  74. *
  75. * Most code that manipulates packets is interested only in data() and
  76. * length().
  77. *
  78. * To create a Packet, call one of the make() functions. To destroy a Packet,
  79. * call kill(). To clone a Packet, which creates a new Packet object that
  80. * shares this packet's data, call clone(). To uniqueify a Packet, which
  81. * unshares the packet data if necessary, call uniqueify(). To allocate extra
  82. * space for headers or trailers, call push() and put(). To remove headers or
  83. * trailers, call pull() and take().
  84. *
  85. * <pre>
  86. * data() end_data()
  87. * | |
  88. * push() | pull() take() | put()
  89. * <======= | =======> <======= | =======>
  90. * v v
  91. * +===========+================================+===========+
  92. * |XXXXXXXXXXX| PACKET CONTENTS |XXXXXXXXXXX|
  93. * +===========+================================+===========+
  94. * </pre>
  95. *
  96. * Packet objects are implemented in different ways in different drivers. The
  97. * userlevel driver has its own C++ implementation. In the linuxmodule
  98. * driver, however, Packet is an overlay on Linux's native sk_buff
  99. * object: the Packet methods access underlying sk_buff data directly, with no
  100. * overhead. (For example, Packet::data() returns the sk_buff's data field.)
  101. *
  102. * <h3>Annotations</h3>
  103. *
  104. * Annotations are extra information about a packet above and beyond the
  105. * packet data. Packet supports several specific annotations, plus a <em>user
  106. * annotation area</em> available for arbitrary use by elements.
  107. *
  108. * <ul>
  109. * <li><b>Header pointers:</b> Each packet has three header pointers, designed
  110. * to point to the packet's MAC header, network header, and transport header,
  111. * respectively. Convenience functions like ip_header() access these pointers
  112. * cast to common header types. The header pointers are kept up to date when
  113. * operations like push() or uniqueify() change the packet's data buffer.
  114. * Header pointers can be null, and they can even point to memory outside the
  115. * current packet data bounds. For example, a MAC header pointer will remain
  116. * set even after pull() is used to shift the packet data past the MAC header.
  117. * As a result, functions like mac_header_offset() can return negative
  118. * numbers.</li>
  119. * <li><b>Timestamp:</b> A timestamp associated with the packet. Most packet
  120. * sources timestamp packets when they enter the router; other elements
  121. * examine or modify the timestamp.</li>
  122. * <li><b>Device:</b> A pointer to the device on which the packet arrived.
  123. * Only meaningful in the linuxmodule driver, but provided in every
  124. * driver.</li>
  125. * <li><b>Packet type:</b> A small integer indicating whether the packet is
  126. * meant for this host, broadcast, multicast, or some other purpose. Several
  127. * elements manipulate this annotation; in linuxmodule, setting the annotation
  128. * is required for the host network stack to process incoming packets
  129. * correctly.</li>
  130. * <li><b>Performance counter</b> (linuxmodule only): A 64-bit integer
  131. * intended to hold a performance counter value. Used by SetCycleCount and
  132. * others.</li>
  133. * <li><b>Next and previous packet:</b> Pointers provided to allow elements to
  134. * chain packets into a doubly linked list.</li>
  135. * <li><b>Annotations:</b> Each packet has @link Packet::anno_size anno_size
  136. * @endlink bytes available for annotations. Elements agree to use portions
  137. * of the annotation area to communicate per-packet information. Macros in
  138. * the <click/packet_anno.hh> header file define the annotations used by
  139. * Click's current elements. One common annotation is the network address
  140. * annotation -- see Packet::dst_ip_anno(). Routing elements, such as
  141. * RadixIPLookup, set the address annotation to indicate the desired next hop;
  142. * ARPQuerier uses this annotation to query the next hop's MAC.</li>
  143. * </ul>
  144. *
  145. * New packets start wth all annotations set to zero or null. Cloning a
  146. * packet copies its annotations.
  147. */
  148. /** @class WritablePacket
  149. * @brief A network packet believed not to be shared.
  150. *
  151. * The WritablePacket type represents Packet objects whose data buffers are
  152. * not shared. As a result, WritablePacket's versions of functions that
  153. * access the packet data buffer, such as data(), end_buffer(), and
  154. * ip_header(), return mutable pointers (<tt>char *</tt> rather than <tt>const
  155. * char *</tt>).
  156. *
  157. * WritablePacket objects are created by Packet::make(), Packet::uniqueify(),
  158. * Packet::push(), and Packet::put(), which ensure that the returned packet
  159. * does not share its data buffer.
  160. *
  161. * WritablePacket's interface is the same as Packet's except for these type
  162. * differences. For documentation, see Packet.
  163. *
  164. * @warning The WritablePacket convention reduces the likelihood of error
  165. * when modifying packet data, but does not eliminate it. For instance, by
  166. * calling WritablePacket::clone(), it is possible to create a WritablePacket
  167. * whose data is shared:
  168. * @code
  169. * Packet *p = ...;
  170. * if (WritablePacket *q = p->uniqueify()) {
  171. * Packet *p2 = q->clone();
  172. * assert(p2);
  173. * q->ip_header()->ip_v = 6; // modifies p2's data as well
  174. * }
  175. * @endcode
  176. * Avoid writing buggy code like this! Use WritablePacket selectively, and
  177. * try to avoid calling WritablePacket::clone() when possible. */
  178. Packet::~Packet()
  179. {
  180. // This is a convenient place to put static assertions.
  181. static_assert(addr_anno_offset % 8 == 0 && user_anno_offset % 8 == 0,
  182. "Annotations must begin at multiples of 8 bytes.");
  183. static_assert(addr_anno_offset + addr_anno_size <= anno_size,
  184. "Annotation area too small for address annotations.");
  185. static_assert(user_anno_offset + user_anno_size <= anno_size,
  186. "Annotation area too small for user annotations.");
  187. static_assert(dst_ip_anno_offset == DST_IP_ANNO_OFFSET
  188. && dst_ip6_anno_offset == DST_IP6_ANNO_OFFSET
  189. && dst_ip_anno_size == DST_IP_ANNO_SIZE
  190. && dst_ip6_anno_size == DST_IP6_ANNO_SIZE
  191. && dst_ip_anno_size == 4
  192. && dst_ip6_anno_size == 16
  193. && dst_ip_anno_offset + 4 <= anno_size
  194. && dst_ip6_anno_offset + 16 <= anno_size,
  195. "Address annotations at unexpected locations.");
  196. static_assert((default_headroom & 3) == 0,
  197. "Default headroom should be a multiple of 4 bytes.");
  198. #if CLICK_LINUXMODULE
  199. static_assert(sizeof(Anno) <= sizeof(((struct sk_buff *)0)->cb),
  200. "Anno structure too big for Linux packet annotation area.");
  201. #endif
  202. #if CLICK_LINUXMODULE
  203. panic("Packet destructor");
  204. #else
  205. if (_data_packet)
  206. _data_packet->kill();
  207. # if CLICK_USERLEVEL
  208. else if (_head && _destructor)
  209. _destructor(_head, _end - _head);
  210. else
  211. delete[] _head;
  212. # elif CLICK_BSDMODULE
  213. if (_m)
  214. m_freem(_m);
  215. # endif
  216. _head = _data = 0;
  217. #endif
  218. }
  219. #if !CLICK_LINUXMODULE
  220. # if HAVE_CLICK_PACKET_POOL
  221. # define CLICK_PACKET_POOL_BUFSIZ 2048
  222. # define CLICK_PACKET_POOL_SIZE 1000 // see LIMIT in packetpool-01.testie
  223. # define CLICK_GLOBAL_PACKET_POOL_COUNT 16
  224. namespace {
  225. struct PacketData {
  226. PacketData *next;
  227. # if HAVE_MULTITHREAD
  228. PacketData *pool_next;
  229. # endif
  230. };
  231. struct PacketPool {
  232. WritablePacket *p;
  233. unsigned pcount;
  234. PacketData *pd;
  235. unsigned pdcount;
  236. # if HAVE_MULTITHREAD
  237. PacketPool *chain;
  238. # endif
  239. };
  240. }
  241. # if HAVE_MULTITHREAD
  242. static __thread PacketPool *thread_packet_pool;
  243. static PacketPool *all_thread_packet_pools;
  244. static PacketPool global_packet_pool;
  245. static volatile uint32_t global_packet_pool_lock;
  246. static inline PacketPool *
  247. get_packet_pool()
  248. {
  249. PacketPool *pp = thread_packet_pool;
  250. if (!pp && (pp = new PacketPool)) {
  251. memset(pp, 0, sizeof(PacketPool));
  252. while (atomic_uint32_t::swap(global_packet_pool_lock, 1) == 1)
  253. /* do nothing */;
  254. pp->chain = all_thread_packet_pools;
  255. all_thread_packet_pools = pp;
  256. thread_packet_pool = pp;
  257. click_compiler_fence();
  258. global_packet_pool_lock = 0;
  259. }
  260. return pp;
  261. }
  262. # else
  263. static PacketPool packet_pool;
  264. # endif
  265. WritablePacket *
  266. WritablePacket::pool_allocate(bool with_data)
  267. {
  268. # if HAVE_MULTITHREAD
  269. PacketPool &packet_pool = *get_packet_pool();
  270. if ((!packet_pool.p && global_packet_pool.p)
  271. || (with_data && !packet_pool.pd && global_packet_pool.pd)) {
  272. while (atomic_uint32_t::swap(global_packet_pool_lock, 1) == 1)
  273. /* do nothing */;
  274. WritablePacket *pp;
  275. if (!packet_pool.p && (pp = global_packet_pool.p)) {
  276. global_packet_pool.p = static_cast<WritablePacket *>(pp->prev());
  277. --global_packet_pool.pcount;
  278. packet_pool.p = pp;
  279. packet_pool.pcount = CLICK_PACKET_POOL_SIZE;
  280. }
  281. PacketData *pd;
  282. if (with_data && !packet_pool.pd && (pd = global_packet_pool.pd)) {
  283. global_packet_pool.pd = pd->pool_next;
  284. --global_packet_pool.pdcount;
  285. packet_pool.pd = pd;
  286. packet_pool.pdcount = CLICK_PACKET_POOL_SIZE;
  287. }
  288. click_compiler_fence();
  289. global_packet_pool_lock = 0;
  290. }
  291. # else
  292. (void) with_data;
  293. # endif
  294. WritablePacket *p = packet_pool.p;
  295. if (p) {
  296. packet_pool.p = static_cast<WritablePacket *>(p->next());
  297. --packet_pool.pcount;
  298. } else
  299. p = new WritablePacket;
  300. return p;
  301. }
  302. WritablePacket *
  303. WritablePacket::pool_allocate(uint32_t headroom, uint32_t length,
  304. uint32_t tailroom)
  305. {
  306. uint32_t n = headroom + length + tailroom;
  307. if (n < CLICK_PACKET_POOL_BUFSIZ)
  308. n = CLICK_PACKET_POOL_BUFSIZ;
  309. WritablePacket *p = pool_allocate(n == CLICK_PACKET_POOL_BUFSIZ);
  310. if (p) {
  311. p->initialize();
  312. PacketData *pd;
  313. # if HAVE_MULTITHREAD
  314. PacketPool &packet_pool = *thread_packet_pool;
  315. # endif
  316. if (n == CLICK_PACKET_POOL_BUFSIZ && (pd = packet_pool.pd)) {
  317. packet_pool.pd = pd->next;
  318. --packet_pool.pdcount;
  319. p->_head = reinterpret_cast<unsigned char *>(pd);
  320. } else if ((p->_head = new unsigned char[n]))
  321. /* OK */;
  322. else {
  323. delete p;
  324. return 0;
  325. }
  326. p->_data = p->_head + headroom;
  327. p->_tail = p->_data + length;
  328. p->_end = p->_head + n;
  329. }
  330. return p;
  331. }
  332. void
  333. WritablePacket::recycle(WritablePacket *p)
  334. {
  335. unsigned char *data = 0;
  336. if (!p->_data_packet && p->_head && !p->_destructor
  337. && p->_end - p->_head == CLICK_PACKET_POOL_BUFSIZ) {
  338. data = p->_head;
  339. p->_head = 0;
  340. }
  341. p->~WritablePacket();
  342. # if HAVE_MULTITHREAD
  343. PacketPool &packet_pool = *get_packet_pool();
  344. if ((packet_pool.p && packet_pool.pcount == CLICK_PACKET_POOL_SIZE)
  345. || (data && packet_pool.pd && packet_pool.pdcount == CLICK_PACKET_POOL_SIZE)) {
  346. while (atomic_uint32_t::swap(global_packet_pool_lock, 1) == 1)
  347. /* do nothing */;
  348. if (packet_pool.p && packet_pool.pcount == CLICK_PACKET_POOL_SIZE) {
  349. if (global_packet_pool.pcount == CLICK_GLOBAL_PACKET_POOL_COUNT) {
  350. while (WritablePacket *p = packet_pool.p) {
  351. packet_pool.p = static_cast<WritablePacket *>(p->next());
  352. ::operator delete((void *) p);
  353. }
  354. } else {
  355. packet_pool.p->set_prev(global_packet_pool.p);
  356. global_packet_pool.p = packet_pool.p;
  357. ++global_packet_pool.pcount;
  358. packet_pool.p = 0;
  359. }
  360. packet_pool.pcount = 0;
  361. }
  362. if (data && packet_pool.pd && packet_pool.pdcount == CLICK_PACKET_POOL_SIZE) {
  363. if (global_packet_pool.pdcount == CLICK_GLOBAL_PACKET_POOL_COUNT) {
  364. while (PacketData *pd = packet_pool.pd) {
  365. packet_pool.pd = pd->next;
  366. delete[] reinterpret_cast<unsigned char *>(pd);
  367. }
  368. } else {
  369. packet_pool.pd->pool_next = global_packet_pool.pd;
  370. global_packet_pool.pd = packet_pool.pd;
  371. ++global_packet_pool.pdcount;
  372. packet_pool.pd = 0;
  373. }
  374. packet_pool.pdcount = 0;
  375. }
  376. click_compiler_fence();
  377. global_packet_pool_lock = 0;
  378. }
  379. # else
  380. if (packet_pool.pcount == CLICK_PACKET_POOL_SIZE) {
  381. ::operator delete((void *) p);
  382. p = 0;
  383. }
  384. if (data && packet_pool.pdcount == CLICK_PACKET_POOL_SIZE) {
  385. delete[] data;
  386. data = 0;
  387. }
  388. # endif
  389. if (p) {
  390. ++packet_pool.pcount;
  391. p->set_next(packet_pool.p);
  392. packet_pool.p = p;
  393. assert(packet_pool.pcount <= CLICK_PACKET_POOL_SIZE);
  394. }
  395. if (data) {
  396. ++packet_pool.pdcount;
  397. PacketData *pd = reinterpret_cast<PacketData *>(data);
  398. pd->next = packet_pool.pd;
  399. packet_pool.pd = pd;
  400. assert(packet_pool.pdcount <= CLICK_PACKET_POOL_SIZE);
  401. }
  402. }
  403. #endif
  404. bool
  405. Packet::alloc_data(uint32_t headroom, uint32_t length, uint32_t tailroom)
  406. {
  407. uint32_t n = length + headroom + tailroom;
  408. if (n < min_buffer_length) {
  409. tailroom = min_buffer_length - length - headroom;
  410. n = min_buffer_length;
  411. }
  412. #if CLICK_USERLEVEL
  413. unsigned char *d = new unsigned char[n];
  414. if (!d)
  415. return false;
  416. _head = d;
  417. _data = d + headroom;
  418. _tail = _data + length;
  419. _end = _head + n;
  420. #elif CLICK_BSDMODULE
  421. //click_chatter("allocate new mbuf, length=%d", n);
  422. if (n > MJUM16BYTES) {
  423. click_chatter("trying to allocate %d bytes: too many\n", n);
  424. return false;
  425. }
  426. struct mbuf *m;
  427. MGETHDR(m, M_DONTWAIT, MT_DATA);
  428. if (!m)
  429. return false;
  430. if (n > MHLEN) {
  431. if (n > MCLBYTES)
  432. m_cljget(m, M_DONTWAIT, (n <= MJUMPAGESIZE ? MJUMPAGESIZE :
  433. n <= MJUM9BYTES ? MJUM9BYTES :
  434. MJUM16BYTES));
  435. else
  436. MCLGET(m, M_DONTWAIT);
  437. if (!(m->m_flags & M_EXT)) {
  438. m_freem(m);
  439. return false;
  440. }
  441. }
  442. _m = m;
  443. _m->m_data += headroom;
  444. _m->m_len = length;
  445. _m->m_pkthdr.len = length;
  446. assimilate_mbuf();
  447. #endif
  448. return true;
  449. }
  450. #endif
  451. /** @brief Create and return a new packet.
  452. * @param headroom headroom in new packet
  453. * @param data data to be copied into the new packet
  454. * @param length length of packet
  455. * @param tailroom tailroom in new packet
  456. * @return new packet, or null if no packet could be created
  457. *
  458. * The @a data is copied into the new packet. If @a data is null, the
  459. * packet's data is left uninitialized. The resulting packet's
  460. * buffer_length() will be at least @link Packet::min_buffer_length
  461. * min_buffer_length @endlink; if @a headroom + @a length + @a tailroom would
  462. * be less, then @a tailroom is increased to make the total @link
  463. * Packet::min_buffer_length min_buffer_length @endlink.
  464. *
  465. * The new packet's annotations are cleared and its header pointers are
  466. * null. */
  467. WritablePacket *
  468. Packet::make(uint32_t headroom, const void *data,
  469. uint32_t length, uint32_t tailroom)
  470. {
  471. #if CLICK_LINUXMODULE
  472. int want = 1;
  473. if (struct sk_buff *skb = skbmgr_allocate_skbs(headroom, length + tailroom, &want)) {
  474. assert(want == 1);
  475. // packet comes back from skbmgr with headroom reserved
  476. __skb_put(skb, length); // leave space for data
  477. if (data)
  478. memcpy(skb->data, data, length);
  479. # if PACKET_CLEAN
  480. skb->pkt_type = HOST | PACKET_CLEAN;
  481. # else
  482. skb->pkt_type = HOST;
  483. # endif
  484. WritablePacket *q = reinterpret_cast<WritablePacket *>(skb);
  485. q->clear_annotations();
  486. return q;
  487. } else
  488. return 0;
  489. #else
  490. # if HAVE_CLICK_PACKET_POOL
  491. WritablePacket *p = WritablePacket::pool_allocate(headroom, length, tailroom);
  492. if (!p)
  493. return 0;
  494. # else
  495. WritablePacket *p = new WritablePacket;
  496. if (!p)
  497. return 0;
  498. p->initialize();
  499. if (!p->alloc_data(headroom, length, tailroom)) {
  500. p->_head = 0;
  501. delete p;
  502. return 0;
  503. }
  504. # endif
  505. if (data)
  506. memcpy(p->data(), data, length);
  507. return p;
  508. #endif
  509. }
  510. #if CLICK_USERLEVEL
  511. /** @brief Create and return a new packet (userlevel).
  512. * @param data data used in the new packet
  513. * @param length length of packet
  514. * @param destructor destructor function
  515. * @return new packet, or null if no packet could be created
  516. *
  517. * The packet's data pointer becomes the @a data: the data is not copied into
  518. * the new packet, rather the packet owns the @a data pointer. When the
  519. * packet's data is eventually destroyed, either because the packet is deleted
  520. * or because of something like a push() or full(), the @a destructor will be
  521. * called with arguments @a destructor(@a data, @a length). (If @a destructor
  522. * is null, the packet data will be freed by <tt>delete[] @a data</tt>.) The
  523. * packet has zero headroom and tailroom.
  524. *
  525. * The returned packet's annotations are cleared and its header pointers are
  526. * null. */
  527. WritablePacket *
  528. Packet::make(unsigned char *data, uint32_t length,
  529. buffer_destructor_type destructor)
  530. {
  531. # if HAVE_CLICK_PACKET_POOL
  532. WritablePacket *p = WritablePacket::pool_allocate(false);
  533. # else
  534. WritablePacket *p = new WritablePacket;
  535. # endif
  536. if (p) {
  537. p->initialize();
  538. p->_head = p->_data = data;
  539. p->_tail = p->_end = data + length;
  540. p->_destructor = destructor;
  541. }
  542. return p;
  543. }
  544. #endif
  545. //
  546. // UNIQUEIFICATION
  547. //
  548. /** @brief Create a clone of this packet.
  549. * @return the cloned packet
  550. *
  551. * The returned clone has independent annotations, initially copied from this
  552. * packet, but shares this packet's data. shared() returns true for both the
  553. * packet and its clone. Returns null if there's no memory for the clone. */
  554. Packet *
  555. Packet::clone()
  556. {
  557. #if CLICK_LINUXMODULE
  558. struct sk_buff *nskb = skb_clone(skb(), GFP_ATOMIC);
  559. return reinterpret_cast<Packet *>(nskb);
  560. #elif CLICK_USERLEVEL || CLICK_BSDMODULE
  561. # if CLICK_BSDMODULE
  562. struct mbuf *m;
  563. if (this->_m == NULL)
  564. return 0;
  565. if (this->_m->m_flags & M_EXT
  566. && ( this->_m->m_ext.ext_type == EXT_JUMBOP
  567. || this->_m->m_ext.ext_type == EXT_JUMBO9
  568. || this->_m->m_ext.ext_type == EXT_JUMBO16)) {
  569. if ((m = dup_jumbo_m(this->_m)) == NULL)
  570. return 0;
  571. }
  572. else if ((m = m_dup(this->_m, M_DONTWAIT)) == NULL)
  573. return 0;
  574. # endif
  575. // timing: .31-.39 normal, .43-.55 two allocs, .55-.58 two memcpys
  576. # if HAVE_CLICK_PACKET_POOL
  577. Packet *p = WritablePacket::pool_allocate(false);
  578. # else
  579. Packet *p = new WritablePacket; // no initialization
  580. # endif
  581. if (!p)
  582. return 0;
  583. memcpy(p, this, sizeof(Packet));
  584. p->_use_count = 1;
  585. p->_data_packet = this;
  586. # if CLICK_USERLEVEL
  587. p->_destructor = 0;
  588. # else
  589. p->_m = m;
  590. # endif
  591. // increment our reference count because of _data_packet reference
  592. _use_count++;
  593. return p;
  594. #endif /* CLICK_LINUXMODULE */
  595. }
  596. WritablePacket *
  597. Packet::expensive_uniqueify(int32_t extra_headroom, int32_t extra_tailroom,
  598. bool free_on_failure)
  599. {
  600. assert(extra_headroom >= (int32_t)(-headroom()) && extra_tailroom >= (int32_t)(-tailroom()));
  601. #if CLICK_LINUXMODULE
  602. struct sk_buff *nskb = skb();
  603. unsigned char *old_head = nskb->head;
  604. uint32_t old_headroom = headroom(), old_length = length();
  605. uint32_t size = buffer_length() + extra_headroom + extra_tailroom;
  606. size = SKB_DATA_ALIGN(size);
  607. unsigned char *new_head = reinterpret_cast<unsigned char *>(kmalloc(size + sizeof(struct skb_shared_info), GFP_ATOMIC));
  608. if (!new_head) {
  609. if (free_on_failure)
  610. kill();
  611. return 0;
  612. }
  613. unsigned char *start_copy = old_head + (extra_headroom >= 0 ? 0 : -extra_headroom);
  614. unsigned char *end_copy = old_head + buffer_length() + (extra_tailroom >= 0 ? 0 : extra_tailroom);
  615. memcpy(new_head + (extra_headroom >= 0 ? extra_headroom : 0), start_copy, end_copy - start_copy);
  616. if (!nskb->cloned || atomic_dec_and_test(&(skb_shinfo(nskb)->dataref))) {
  617. assert(!skb_shinfo(nskb)->nr_frags && !skb_shinfo(nskb)->frag_list);
  618. kfree(old_head);
  619. }
  620. nskb->head = new_head;
  621. nskb->data = new_head + old_headroom + extra_headroom;
  622. # if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
  623. skb_set_tail_pointer(nskb, old_length);
  624. # else
  625. nskb->tail = nskb->data + old_length;
  626. # endif
  627. # ifdef NET_SKBUFF_DATA_USES_OFFSET
  628. nskb->end = size;
  629. # else
  630. nskb->end = new_head + size;
  631. # endif
  632. nskb->len = old_length;
  633. nskb->cloned = 0;
  634. nskb->truesize = size + sizeof(struct sk_buff);
  635. struct skb_shared_info *nskb_shinfo = skb_shinfo(nskb);
  636. atomic_set(&nskb_shinfo->dataref, 1);
  637. nskb_shinfo->nr_frags = 0;
  638. nskb_shinfo->frag_list = 0;
  639. # if HAVE_LINUX_SKB_SHINFO_GSO_SIZE
  640. nskb_shinfo->gso_size = 0;
  641. nskb_shinfo->gso_segs = 0;
  642. nskb_shinfo->gso_type = 0;
  643. # elif HAVE_LINUX_SKB_SHINFO_TSO_SIZE
  644. nskb_shinfo->tso_size = 0;
  645. nskb_shinfo->tso_segs = 0;
  646. # endif
  647. # if HAVE_LINUX_SKB_SHINFO_UFO_SIZE
  648. nskb_shinfo->ufo_size = 0;
  649. # endif
  650. # if HAVE_LINUX_SKB_SHINFO_IP6_FRAG_ID
  651. nskb_shinfo->ip6_frag_id = 0;
  652. # endif
  653. # if HAVE_LINUX_SKB_SHINFO_TX_FLAGS_UNION
  654. nskb_shinfo->tx_flags.flags = 0;
  655. # endif
  656. # if HAVE_LINUX_SKB_SHINFO_TX_FLAGS_SKBTX_DEV_ZEROCOPY
  657. nskb_shinfo->tx_flags = 0;
  658. # endif
  659. shift_header_annotations(old_head, extra_headroom);
  660. return static_cast<WritablePacket *>(this);
  661. #else /* !CLICK_LINUXMODULE */
  662. // If someone else has cloned this packet, then we need to leave its data
  663. // pointers around. Make a clone and uniqueify that.
  664. if (_use_count > 1) {
  665. Packet *p = clone();
  666. WritablePacket *q = (p ? p->expensive_uniqueify(extra_headroom, extra_tailroom, true) : 0);
  667. if (q || free_on_failure)
  668. kill();
  669. return q;
  670. }
  671. uint8_t *old_head = _head, *old_end = _end;
  672. # if CLICK_BSDMODULE
  673. struct mbuf *old_m = _m;
  674. # endif
  675. if (!alloc_data(headroom() + extra_headroom, length(), tailroom() + extra_tailroom)) {
  676. if (free_on_failure)
  677. kill();
  678. return 0;
  679. }
  680. unsigned char *start_copy = old_head + (extra_headroom >= 0 ? 0 : -extra_headroom);
  681. unsigned char *end_copy = old_end + (extra_tailroom >= 0 ? 0 : extra_tailroom);
  682. memcpy(_head + (extra_headroom >= 0 ? extra_headroom : 0), start_copy, end_copy - start_copy);
  683. // free old data
  684. if (_data_packet)
  685. _data_packet->kill();
  686. # if CLICK_USERLEVEL
  687. else if (_destructor)
  688. _destructor(old_head, old_end - old_head);
  689. else
  690. delete[] old_head;
  691. _destructor = 0;
  692. # elif CLICK_BSDMODULE
  693. m_freem(old_m); // alloc_data() created a new mbuf, so free the old one
  694. # endif
  695. _use_count = 1;
  696. _data_packet = 0;
  697. shift_header_annotations(old_head, extra_headroom);
  698. return static_cast<WritablePacket *>(this);
  699. #endif /* CLICK_LINUXMODULE */
  700. }
  701. #ifdef CLICK_BSDMODULE /* BSD kernel module */
  702. struct mbuf *
  703. Packet::steal_m()
  704. {
  705. struct Packet *p;
  706. struct mbuf *m2;
  707. p = uniqueify();
  708. m2 = p->m();
  709. /* Clear the mbuf from the packet: otherwise kill will MFREE it */
  710. p->_m = 0;
  711. p->kill();
  712. return m2;
  713. }
  714. /*
  715. * Duplicate a packet by copying data from an mbuf chain to a new mbuf with a
  716. * jumbo cluster (i.e., contiguous storage).
  717. */
  718. struct mbuf *
  719. Packet::dup_jumbo_m(struct mbuf *m)
  720. {
  721. int len = m->m_pkthdr.len;
  722. struct mbuf *new_m;
  723. if (len > MJUM16BYTES) {
  724. click_chatter("warning: cannot allocate jumbo cluster for %d bytes", len);
  725. return NULL;
  726. }
  727. new_m = m_getjcl(M_DONTWAIT, m->m_type, m->m_flags & M_COPYFLAGS,
  728. (len <= MJUMPAGESIZE ? MJUMPAGESIZE :
  729. len <= MJUM9BYTES ? MJUM9BYTES :
  730. MJUM16BYTES));
  731. if (!new_m) {
  732. click_chatter("warning: jumbo cluster mbuf allocation failed");
  733. return NULL;
  734. }
  735. m_copydata(m, 0, len, mtod(new_m, caddr_t));
  736. new_m->m_len = len;
  737. new_m->m_pkthdr.len = len;
  738. /* XXX: Only a subset of what m_dup_pkthdr() would copy: */
  739. new_m->m_pkthdr.rcvif = m->m_pkthdr.rcvif;
  740. # if __FreeBSD_version >= 800000
  741. new_m->m_pkthdr.flowid = m->m_pkthdr.flowid;
  742. # endif
  743. new_m->m_pkthdr.ether_vtag = m->m_pkthdr.ether_vtag;
  744. return new_m;
  745. }
  746. #endif /* CLICK_BSDMODULE */
  747. //
  748. // EXPENSIVE_PUSH, EXPENSIVE_PUT
  749. //
  750. /*
  751. * Prepend some empty space before a packet.
  752. * May kill this packet and return a new one.
  753. */
  754. WritablePacket *
  755. Packet::expensive_push(uint32_t nbytes)
  756. {
  757. static int chatter = 0;
  758. if (headroom() < nbytes && chatter < 5) {
  759. click_chatter("expensive Packet::push; have %d wanted %d",
  760. headroom(), nbytes);
  761. chatter++;
  762. }
  763. if (WritablePacket *q = expensive_uniqueify((nbytes + 128) & ~3, 0, true)) {
  764. #ifdef CLICK_LINUXMODULE /* Linux kernel module */
  765. __skb_push(q->skb(), nbytes);
  766. #else /* User-space and BSD kernel module */
  767. q->_data -= nbytes;
  768. # ifdef CLICK_BSDMODULE
  769. q->m()->m_data -= nbytes;
  770. q->m()->m_len += nbytes;
  771. q->m()->m_pkthdr.len += nbytes;
  772. # endif
  773. #endif
  774. return q;
  775. } else
  776. return 0;
  777. }
  778. WritablePacket *
  779. Packet::expensive_put(uint32_t nbytes)
  780. {
  781. static int chatter = 0;
  782. if (tailroom() < nbytes && chatter < 5) {
  783. click_chatter("expensive Packet::put; have %d wanted %d",
  784. tailroom(), nbytes);
  785. chatter++;
  786. }
  787. if (WritablePacket *q = expensive_uniqueify(0, nbytes + 128, true)) {
  788. #ifdef CLICK_LINUXMODULE /* Linux kernel module */
  789. __skb_put(q->skb(), nbytes);
  790. #else /* User-space and BSD kernel module */
  791. q->_tail += nbytes;
  792. # ifdef CLICK_BSDMODULE
  793. q->m()->m_len += nbytes;
  794. q->m()->m_pkthdr.len += nbytes;
  795. # endif
  796. #endif
  797. return q;
  798. } else
  799. return 0;
  800. }
  801. Packet *
  802. Packet::shift_data(int offset, bool free_on_failure)
  803. {
  804. if (offset == 0)
  805. return this;
  806. // Preserve mac_header, network_header, and transport_header.
  807. const unsigned char *dp = data();
  808. if (has_mac_header() && mac_header() >= buffer()
  809. && mac_header() <= end_buffer() && mac_header() < dp)
  810. dp = mac_header();
  811. if (has_network_header() && network_header() >= buffer()
  812. && network_header() <= end_buffer() && network_header() < dp)
  813. dp = network_header();
  814. if (has_transport_header() && transport_header() >= buffer()
  815. && transport_header() <= end_buffer() && transport_header() < dp)
  816. dp = network_header();
  817. if (!shared()
  818. && (offset < 0 ? (dp - buffer()) >= (ptrdiff_t)(-offset)
  819. : tailroom() >= (uint32_t)offset)) {
  820. WritablePacket *q = static_cast<WritablePacket *>(this);
  821. memmove((unsigned char *) dp + offset, dp, q->end_data() - dp);
  822. #if CLICK_LINUXMODULE
  823. struct sk_buff *mskb = q->skb();
  824. mskb->data += offset;
  825. mskb->tail += offset;
  826. #else /* User-space and BSD kernel module */
  827. q->_data += offset;
  828. q->_tail += offset;
  829. # if CLICK_BSDMODULE
  830. q->m()->m_data += offset;
  831. # endif
  832. #endif
  833. shift_header_annotations(q->buffer(), offset);
  834. return this;
  835. } else {
  836. int tailroom_offset = (offset < 0 ? -offset : 0);
  837. if (offset < 0 && headroom() < (uint32_t)(-offset))
  838. offset = -headroom() + ((uintptr_t)(data() + offset) & 7);
  839. else
  840. offset += ((uintptr_t)buffer() & 7);
  841. return expensive_uniqueify(offset, tailroom_offset, free_on_failure);
  842. }
  843. }
  844. #if HAVE_CLICK_PACKET_POOL
  845. static void
  846. cleanup_pool(PacketPool *pp, int global)
  847. {
  848. unsigned pcount = 0, pdcount = 0;
  849. while (WritablePacket *p = pp->p) {
  850. ++pcount;
  851. pp->p = static_cast<WritablePacket *>(p->next());
  852. ::operator delete((void *) p);
  853. }
  854. while (PacketData *pd = pp->pd) {
  855. ++pdcount;
  856. pp->pd = pd->next;
  857. delete[] reinterpret_cast<unsigned char *>(pd);
  858. }
  859. assert(pcount <= CLICK_PACKET_POOL_SIZE);
  860. assert(pdcount <= CLICK_PACKET_POOL_SIZE);
  861. assert(global || (pcount == pp->pcount && pdcount == pp->pdcount));
  862. }
  863. #endif
  864. void
  865. Packet::static_cleanup()
  866. {
  867. #if HAVE_CLICK_PACKET_POOL
  868. # if HAVE_MULTITHREAD
  869. while (PacketPool *pp = all_thread_packet_pools) {
  870. all_thread_packet_pools = pp->chain;
  871. cleanup_pool(pp, 0);
  872. delete pp;
  873. }
  874. unsigned rounds = (global_packet_pool.pcount > global_packet_pool.pdcount ? global_packet_pool.pcount : global_packet_pool.pdcount);
  875. assert(rounds <= CLICK_GLOBAL_PACKET_POOL_COUNT);
  876. while (global_packet_pool.p || global_packet_pool.pd) {
  877. WritablePacket *next_p = global_packet_pool.p;
  878. next_p = (next_p ? static_cast<WritablePacket *>(next_p->prev()) : 0);
  879. PacketData *next_pd = global_packet_pool.pd;
  880. next_pd = (next_pd ? next_pd->pool_next : 0);
  881. cleanup_pool(&global_packet_pool, 1);
  882. global_packet_pool.p = next_p;
  883. global_packet_pool.pd = next_pd;
  884. --rounds;
  885. }
  886. assert(rounds == 0);
  887. # else
  888. cleanup_pool(&packet_pool, 0);
  889. # endif
  890. #endif
  891. }
  892. CLICK_ENDDECLS