1cb9cadadSEd Warnicke/*
2cb9cadadSEd Warnicke * Copyright (c) 2015 Cisco and/or its affiliates.
3cb9cadadSEd Warnicke * Licensed under the Apache License, Version 2.0 (the "License");
4cb9cadadSEd Warnicke * you may not use this file except in compliance with the License.
5cb9cadadSEd Warnicke * You may obtain a copy of the License at:
6cb9cadadSEd Warnicke *
7cb9cadadSEd Warnicke *     http://www.apache.org/licenses/LICENSE-2.0
8cb9cadadSEd Warnicke *
9cb9cadadSEd Warnicke * Unless required by applicable law or agreed to in writing, software
10cb9cadadSEd Warnicke * distributed under the License is distributed on an "AS IS" BASIS,
11cb9cadadSEd Warnicke * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb9cadadSEd Warnicke * See the License for the specific language governing permissions and
13cb9cadadSEd Warnicke * limitations under the License.
14cb9cadadSEd Warnicke */
15cb9cadadSEd Warnicke/*
16cb9cadadSEd Warnicke * pg_output.c: packet generator output
17cb9cadadSEd Warnicke *
18cb9cadadSEd Warnicke * Copyright (c) 2008 Eliot Dresselhaus
19cb9cadadSEd Warnicke *
20cb9cadadSEd Warnicke * Permission is hereby granted, free of charge, to any person obtaining
21cb9cadadSEd Warnicke * a copy of this software and associated documentation files (the
22cb9cadadSEd Warnicke * "Software"), to deal in the Software without restriction, including
23cb9cadadSEd Warnicke * without limitation the rights to use, copy, modify, merge, publish,
24cb9cadadSEd Warnicke * distribute, sublicense, and/or sell copies of the Software, and to
25cb9cadadSEd Warnicke * permit persons to whom the Software is furnished to do so, subject to
26cb9cadadSEd Warnicke * the following conditions:
27cb9cadadSEd Warnicke *
28cb9cadadSEd Warnicke * The above copyright notice and this permission notice shall be
29cb9cadadSEd Warnicke * included in all copies or substantial portions of the Software.
30cb9cadadSEd Warnicke *
31cb9cadadSEd Warnicke *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32cb9cadadSEd Warnicke *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33cb9cadadSEd Warnicke *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34cb9cadadSEd Warnicke *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35cb9cadadSEd Warnicke *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36cb9cadadSEd Warnicke *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37cb9cadadSEd Warnicke *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38cb9cadadSEd Warnicke */
39cb9cadadSEd Warnicke
40bfe4dfa7SDamjan Marion#include <vppinfra/string.h>
41cb9cadadSEd Warnicke#include <vlib/vlib.h>
423d9c86e9SDamjan Marion#include <vnet/vnet.h>
43cb9cadadSEd Warnicke#include <vnet/pg/pg.h>
443d9c86e9SDamjan Marion#include <vnet/ethernet/ethernet.h>
45cb9cadadSEd Warnicke
46cb9cadadSEd Warnickeuword
4771e97c63SCalvinpg_output (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
48cb9cadadSEd Warnicke{
4971e97c63SCalvin  pg_main_t *pg = &pg_main;
50a3d5986aSDamjan Marion  u32 *buffers = vlib_frame_vector_args (frame);
51cb9cadadSEd Warnicke  uword n_buffers = frame->n_vectors;
523d9c86e9SDamjan Marion  uword n_left = n_buffers;
5371e97c63SCalvin  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
5471e97c63SCalvin  pg_interface_t *pif = pool_elt_at_index (pg->interfaces, rd->dev_instance);
553d9c86e9SDamjan Marion
5664034367SDamjan Marion  if (PREDICT_FALSE (pif->lockp != 0))
572f6d7bb9SSirshak Das    while (clib_atomic_test_and_set (pif->lockp))
5864034367SDamjan Marion      ;
5964034367SDamjan Marion
60bfe4dfa7SDamjan Marion  while (n_left > 0)
613d9c86e9SDamjan Marion    {
62bfe4dfa7SDamjan Marion      n_left--;
63bfe4dfa7SDamjan Marion      u32 bi0 = buffers[0];
64bfe4dfa7SDamjan Marion      vlib_buffer_t *b = vlib_get_buffer (vm, bi0);
65bfe4dfa7SDamjan Marion      buffers++;
663d9c86e9SDamjan Marion
67bfe4dfa7SDamjan Marion      if (b->flags & VLIB_BUFFER_IS_TRACED)
68bfe4dfa7SDamjan Marion	{
69bfe4dfa7SDamjan Marion	  pg_output_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
70bfe4dfa7SDamjan Marion	  t->buffer_index = bi0;
71178cf493SDave Barach	  clib_memcpy_fast (&t->buffer, b,
72178cf493SDave Barach			    sizeof (b[0]) - sizeof (b->pre_data));
73178cf493SDave Barach	  clib_memcpy_fast (t->buffer.pre_data, b->data + b->current_data,
74178cf493SDave Barach			    sizeof (t->buffer.pre_data));
753d9c86e9SDamjan Marion	}
76bfe4dfa7SDamjan Marion
77bfe4dfa7SDamjan Marion      if (pif->pcap_file_name != 0)
78bfe4dfa7SDamjan Marion	pcap_add_buffer (&pif->pcap_main, vm, bi0, ETHERNET_MAX_PACKET_BYTES);
793d9c86e9SDamjan Marion    }
80bfe4dfa7SDamjan Marion  if (pif->pcap_file_name != 0)
81bfe4dfa7SDamjan Marion    pcap_write (&pif->pcap_main);
824da1506dSAndrew Yourtchenko  if ((pif->pcap_main.flags & PCAP_MAIN_INIT_DONE)
839af7e2e8SJack Xu      && pif->pcap_main.n_packets_captured >=
849af7e2e8SJack Xu      pif->pcap_main.n_packets_to_capture)
859af7e2e8SJack Xu    pcap_close (&pif->pcap_main);
863d9c86e9SDamjan Marion
87a3d5986aSDamjan Marion  vlib_buffer_free (vm, vlib_frame_vector_args (frame), n_buffers);
8864034367SDamjan Marion  if (PREDICT_FALSE (pif->lockp != 0))
892f6d7bb9SSirshak Das    clib_atomic_release (pif->lockp);
902f6d7bb9SSirshak Das
91cb9cadadSEd Warnicke  return n_buffers;
92cb9cadadSEd Warnicke}
9371e97c63SCalvin
9471e97c63SCalvin/*
9571e97c63SCalvin * fd.io coding-style-patch-verification: ON
9671e97c63SCalvin *
9771e97c63SCalvin * Local Variables:
9871e97c63SCalvin * eval: (c-set-style "gnu")
9971e97c63SCalvin * End:
10071e97c63SCalvin */
101