vcl: fix api detach if attach failed
[vpp.git] / src / vcl / vcl_private.c
1 /*
2  * Copyright (c) 2018-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vcl/vcl_private.h>
17
18 static pthread_key_t vcl_worker_stop_key;
19
20 vcl_mq_evt_conn_t *
21 vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
22 {
23   vcl_mq_evt_conn_t *mqc;
24   pool_get (wrk->mq_evt_conns, mqc);
25   memset (mqc, 0, sizeof (*mqc));
26   return mqc;
27 }
28
29 u32
30 vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
31 {
32   return (mqc - wrk->mq_evt_conns);
33 }
34
35 vcl_mq_evt_conn_t *
36 vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
37 {
38   return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
39 }
40
41 /* Add unix socket to epoll.
42  * Used only to get a notification on socket close
43  * We can't use eventfd because we don't get notifications on that fds
44  */
45 static int
46 vcl_mq_epoll_add_api_sock (vcl_worker_t *wrk)
47 {
48   clib_socket_t *cs = &wrk->app_api_sock;
49   struct epoll_event e = { 0 };
50   int rv;
51
52   e.data.u32 = ~0;
53   rv = epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, cs->fd, &e);
54   if (rv != EEXIST && rv < 0)
55     return -1;
56
57   return 0;
58 }
59
60 int
61 vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
62 {
63   struct epoll_event e = { 0 };
64   vcl_mq_evt_conn_t *mqc;
65   u32 mqc_index;
66   int mq_fd;
67
68   mq_fd = svm_msg_q_get_eventfd (mq);
69
70   if (wrk->mqs_epfd < 0 || mq_fd == -1)
71     return -1;
72
73   mqc = vcl_mq_evt_conn_alloc (wrk);
74   mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
75   mqc->mq_fd = mq_fd;
76   mqc->mq = mq;
77
78   e.events = EPOLLIN;
79   e.data.u32 = mqc_index;
80   if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
81     {
82       VDBG (0, "failed to add mq eventfd to mq epoll fd");
83       return -1;
84     }
85
86   if (vcl_mq_epoll_add_api_sock (wrk))
87     {
88       VDBG (0, "failed to add mq socket to mq epoll fd");
89       return -1;
90     }
91
92   return mqc_index;
93 }
94
95 int
96 vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
97 {
98   vcl_mq_evt_conn_t *mqc;
99
100   if (wrk->mqs_epfd || mqc_index == ~0)
101     return -1;
102
103   mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
104   if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
105     {
106       VDBG (0, "failed to del mq eventfd to mq epoll fd");
107       return -1;
108     }
109   return 0;
110 }
111
112 static vcl_worker_t *
113 vcl_worker_alloc (void)
114 {
115   vcl_worker_t *wrk;
116   pool_get (vcm->workers, wrk);
117   memset (wrk, 0, sizeof (*wrk));
118   wrk->wrk_index = wrk - vcm->workers;
119   wrk->forked_child = ~0;
120   return wrk;
121 }
122
123 static void
124 vcl_worker_free (vcl_worker_t * wrk)
125 {
126   pool_put (vcm->workers, wrk);
127 }
128
129 int
130 vcl_api_app_worker_add (void)
131 {
132   if (vcm->cfg.vpp_app_socket_api)
133     return vcl_sapi_app_worker_add ();
134
135   return vcl_bapi_app_worker_add ();
136 }
137
138 void
139 vcl_api_app_worker_del (vcl_worker_t * wrk)
140 {
141   if (wrk->api_client_handle == ~0)
142     return;
143
144   if (vcm->cfg.vpp_app_socket_api)
145     return vcl_sapi_app_worker_del (wrk);
146
147   vcl_bapi_app_worker_del (wrk);
148 }
149
150 void
151 vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
152 {
153   clib_spinlock_lock (&vcm->workers_lock);
154   if (notify_vpp)
155     vcl_api_app_worker_del (wrk);
156
157   if (wrk->mqs_epfd > 0)
158     close (wrk->mqs_epfd);
159   pool_free (wrk->sessions);
160   pool_free (wrk->mq_evt_conns);
161   hash_free (wrk->session_index_by_vpp_handles);
162   vec_free (wrk->mq_events);
163   vec_free (wrk->mq_msg_vector);
164   vec_free (wrk->unhandled_evts_vector);
165   vec_free (wrk->pending_session_wrk_updates);
166   clib_bitmap_free (wrk->rd_bitmap);
167   clib_bitmap_free (wrk->wr_bitmap);
168   clib_bitmap_free (wrk->ex_bitmap);
169   vcl_worker_free (wrk);
170   clib_spinlock_unlock (&vcm->workers_lock);
171 }
172
173 static void
174 vcl_worker_cleanup_cb (void *arg)
175 {
176   vcl_worker_t *wrk;
177   u32 wrk_index;
178
179   wrk_index = vcl_get_worker_index ();
180   wrk = vcl_worker_get_if_valid (wrk_index);
181   if (!wrk)
182     return;
183
184   vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
185   vcl_set_worker_index (~0);
186   VDBG (0, "cleaned up worker %u", wrk_index);
187 }
188
189 void
190 vcl_worker_detach_sessions (vcl_worker_t *wrk)
191 {
192   session_event_t *e;
193   vcl_session_t *s;
194   uword *seg_indices_map = 0;
195   u32 seg_index, val, *seg_indices = 0;
196
197   close (wrk->app_api_sock.fd);
198   pool_foreach (s, wrk->sessions)
199     {
200       if (s->session_state == VCL_STATE_LISTEN)
201         {
202           s->session_state = VCL_STATE_LISTEN_NO_MQ;
203           continue;
204         }
205       if ((s->flags & VCL_SESSION_F_IS_VEP) ||
206           s->session_state == VCL_STATE_LISTEN_NO_MQ ||
207           s->session_state == VCL_STATE_CLOSED)
208         continue;
209
210       hash_set (seg_indices_map, s->tx_fifo->segment_index, 1);
211
212       s->session_state = VCL_STATE_DETACHED;
213       vec_add2 (wrk->unhandled_evts_vector, e, 1);
214       e->event_type = SESSION_CTRL_EVT_DISCONNECTED;
215       e->session_index = s->session_index;
216       e->postponed = 1;
217     }
218
219   hash_foreach (seg_index, val, seg_indices_map,
220                 ({ vec_add1 (seg_indices, seg_index); }));
221
222   vcl_segment_detach_segments (seg_indices);
223
224   /* Detach worker's mqs segment */
225   vcl_segment_detach (vcl_vpp_worker_segment_handle (wrk->wrk_index));
226
227   vec_free (seg_indices);
228   hash_free (seg_indices_map);
229 }
230
231 vcl_worker_t *
232 vcl_worker_alloc_and_init ()
233 {
234   vcl_worker_t *wrk;
235
236   /* This was initialized already */
237   if (vcl_get_worker_index () != ~0)
238     return 0;
239
240   /* Grab lock before selecting mem thread index */
241   clib_spinlock_lock (&vcm->workers_lock);
242
243   /* Use separate heap map entry for worker */
244   clib_mem_set_thread_index ();
245
246   if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
247     {
248       VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
249       wrk = 0;
250       goto done;
251     }
252
253   wrk = vcl_worker_alloc ();
254   vcl_set_worker_index (wrk->wrk_index);
255   wrk->api_client_handle = ~0;
256   wrk->thread_id = pthread_self ();
257   wrk->current_pid = getpid ();
258
259   wrk->mqs_epfd = -1;
260   if (vcm->cfg.use_mq_eventfd)
261     {
262       wrk->vcl_needs_real_epoll = 1;
263       wrk->mqs_epfd = epoll_create (1);
264       wrk->vcl_needs_real_epoll = 0;
265       if (wrk->mqs_epfd < 0)
266         {
267           clib_unix_warning ("epoll_create() returned");
268           goto done;
269         }
270     }
271
272   wrk->ep_lt_current = VCL_INVALID_SESSION_INDEX;
273   wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
274   clib_time_init (&wrk->clib_time);
275   vec_validate (wrk->mq_events, 64);
276   vec_validate (wrk->mq_msg_vector, 128);
277   vec_reset_length (wrk->mq_msg_vector);
278   vec_validate (wrk->unhandled_evts_vector, 128);
279   vec_reset_length (wrk->unhandled_evts_vector);
280
281 done:
282   clib_spinlock_unlock (&vcm->workers_lock);
283   return wrk;
284 }
285
286 int
287 vcl_worker_register_with_vpp (void)
288 {
289   vcl_worker_t *wrk = vcl_worker_get_current ();
290
291   clib_spinlock_lock (&vcm->workers_lock);
292
293   if (vcl_api_app_worker_add ())
294     {
295       VDBG (0, "failed to add worker to vpp");
296       clib_spinlock_unlock (&vcm->workers_lock);
297       return -1;
298     }
299   if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
300     VDBG (0, "failed to add pthread cleanup function");
301   if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
302     VDBG (0, "failed to setup key value");
303
304   clib_spinlock_unlock (&vcm->workers_lock);
305
306   VDBG (0, "added worker %u", wrk->wrk_index);
307   return 0;
308 }
309
310 svm_msg_q_t *
311 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
312 {
313   return wrk->ctrl_mq;
314 }
315
316 int
317 vcl_session_read_ready (vcl_session_t * s)
318 {
319   if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
320     {
321       VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
322             s->session_index);
323       return VPPCOM_EBADFD;
324     }
325
326   if (vcl_session_is_open (s))
327     {
328       if (vcl_session_is_ct (s))
329         return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
330
331       if (s->is_dgram)
332         {
333           session_dgram_pre_hdr_t ph;
334           u32 max_deq;
335
336           max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
337           if (max_deq <= SESSION_CONN_HDR_LEN)
338             return 0;
339           if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
340             return 0;
341           if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
342             return 0;
343
344           return ph.data_length;
345         }
346
347       return svm_fifo_max_dequeue_cons (s->rx_fifo);
348     }
349   else if (s->session_state == VCL_STATE_LISTEN)
350     {
351       return clib_fifo_elts (s->accept_evts_fifo);
352     }
353   else
354     {
355       return (s->session_state == VCL_STATE_DISCONNECT) ?
356         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
357     }
358 }
359
360 int
361 vcl_session_write_ready (vcl_session_t * s)
362 {
363   if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
364     {
365       VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
366             s->session_index, s->vpp_handle);
367       return VPPCOM_EBADFD;
368     }
369
370   if (vcl_session_is_open (s))
371     {
372       if (vcl_session_is_ct (s))
373         return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
374
375       if (s->is_dgram)
376         {
377           u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
378
379           if (max_enq <= sizeof (session_dgram_hdr_t))
380             return 0;
381           return max_enq - sizeof (session_dgram_hdr_t);
382         }
383
384       return svm_fifo_max_enqueue_prod (s->tx_fifo);
385     }
386   else if (s->session_state == VCL_STATE_LISTEN)
387     {
388       if (s->tx_fifo)
389         return svm_fifo_max_enqueue_prod (s->tx_fifo);
390       else
391         return VPPCOM_EBADFD;
392     }
393   else if (s->session_state == VCL_STATE_UPDATED)
394     {
395       return 0;
396     }
397   else
398     {
399       return (s->session_state == VCL_STATE_DISCONNECT) ?
400         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
401     }
402 }
403
404 int
405 vcl_session_alloc_ext_cfg (vcl_session_t *s,
406                            transport_endpt_ext_cfg_type_t type, u32 len)
407 {
408   if (s->ext_config)
409     return -1;
410
411   s->ext_config = clib_mem_alloc (len);
412   clib_memset (s->ext_config, 0, len);
413   s->ext_config->len = len;
414   s->ext_config->type = type;
415
416   return 0;
417 }
418
419 int
420 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
421                     int fd)
422 {
423   fifo_segment_create_args_t _a, *a = &_a;
424   int rv;
425
426   memset (a, 0, sizeof (*a));
427   a->segment_name = name;
428   a->segment_type = type;
429
430   if (type == SSVM_SEGMENT_MEMFD)
431     a->memfd_fd = fd;
432
433   clib_rwlock_writer_lock (&vcm->segment_table_lock);
434
435   if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
436     {
437       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
438       return rv;
439     }
440   hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
441
442   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
443
444   vec_free (a->new_segment_indices);
445   return 0;
446 }
447
448 u32
449 vcl_segment_table_lookup (u64 segment_handle)
450 {
451   uword *seg_indexp;
452
453   clib_rwlock_reader_lock (&vcm->segment_table_lock);
454   seg_indexp = hash_get (vcm->segment_table, segment_handle);
455   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
456
457   if (!seg_indexp)
458     return VCL_INVALID_SEGMENT_INDEX;
459   return ((u32) * seg_indexp);
460 }
461
462 void
463 vcl_segment_detach (u64 segment_handle)
464 {
465   fifo_segment_main_t *sm = &vcm->segment_main;
466   fifo_segment_t *segment;
467   u32 segment_index;
468
469   segment_index = vcl_segment_table_lookup (segment_handle);
470   if (segment_index == (u32) ~ 0)
471     return;
472
473   clib_rwlock_writer_lock (&vcm->segment_table_lock);
474
475   segment = fifo_segment_get_segment (sm, segment_index);
476   fifo_segment_delete (sm, segment);
477   hash_unset (vcm->segment_table, segment_handle);
478
479   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
480
481   VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
482 }
483
484 void
485 vcl_segment_detach_segments (u32 *seg_indices)
486 {
487   u64 *seg_handles = 0, *seg_handle, key;
488   u32 *seg_index;
489   u32 val;
490
491   clib_rwlock_reader_lock (&vcm->segment_table_lock);
492
493   vec_foreach (seg_index, seg_indices)
494     {
495       /* clang-format off */
496       hash_foreach (key, val, vcm->segment_table, ({
497         if (val == *seg_index)
498           {
499             vec_add1 (seg_handles, key);
500             break;
501           }
502       }));
503       /* clang-format on */
504     }
505
506   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
507
508   vec_foreach (seg_handle, seg_handles)
509     vcl_segment_detach (seg_handle[0]);
510
511   vec_free (seg_handles);
512 }
513
514 int
515 vcl_segment_attach_session (uword segment_handle, uword rxf_offset,
516                             uword txf_offset, uword mq_offset, u32 mq_index,
517                             u8 is_ct, vcl_session_t *s)
518 {
519   u32 fs_index, eqs_index;
520   svm_fifo_t *rxf, *txf;
521   fifo_segment_t *fs;
522   u64 eqs_handle;
523
524   fs_index = vcl_segment_table_lookup (segment_handle);
525   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
526     {
527       VDBG (0, "ERROR: segment for session %u is not mounted!",
528             s->session_index);
529       return -1;
530     }
531
532   if (!is_ct && mq_offset != (uword) ~0)
533     {
534       eqs_handle = vcl_vpp_worker_segment_handle (0);
535       eqs_index = vcl_segment_table_lookup (eqs_handle);
536       ASSERT (eqs_index != VCL_INVALID_SEGMENT_INDEX);
537     }
538
539   clib_rwlock_reader_lock (&vcm->segment_table_lock);
540
541   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
542   rxf = fifo_segment_alloc_fifo_w_offset (fs, rxf_offset);
543   txf = fifo_segment_alloc_fifo_w_offset (fs, txf_offset);
544   rxf->segment_index = fs_index;
545   txf->segment_index = fs_index;
546
547   if (!is_ct && mq_offset != (uword) ~0)
548     {
549       fs = fifo_segment_get_segment (&vcm->segment_main, eqs_index);
550       s->vpp_evt_q = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
551     }
552
553   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
554
555   if (!is_ct)
556     {
557       rxf->shr->client_session_index = s->session_index;
558       txf->shr->client_session_index = s->session_index;
559       rxf->client_thread_index = vcl_get_worker_index ();
560       txf->client_thread_index = vcl_get_worker_index ();
561       s->rx_fifo = rxf;
562       s->tx_fifo = txf;
563     }
564   else
565     {
566       s->ct_rx_fifo = rxf;
567       s->ct_tx_fifo = txf;
568     }
569
570   return 0;
571 }
572
573 void
574 vcl_session_detach_fifos (vcl_session_t *s)
575 {
576   fifo_segment_t *fs;
577
578   if (!s->rx_fifo)
579     return;
580
581   clib_rwlock_reader_lock (&vcm->segment_table_lock);
582
583   fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
584                                           s->rx_fifo->segment_index);
585   if (!fs)
586     goto done;
587
588   fifo_segment_free_client_fifo (fs, s->rx_fifo);
589   fifo_segment_free_client_fifo (fs, s->tx_fifo);
590   if (s->ct_rx_fifo)
591     {
592       fs = fifo_segment_get_segment_if_valid (&vcm->segment_main,
593                                               s->ct_rx_fifo->segment_index);
594       if (!fs)
595         goto done;
596
597       fifo_segment_free_client_fifo (fs, s->ct_rx_fifo);
598       fifo_segment_free_client_fifo (fs, s->ct_tx_fifo);
599     }
600
601 done:
602   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
603 }
604
605 int
606 vcl_segment_attach_mq (uword segment_handle, uword mq_offset, u32 mq_index,
607                        svm_msg_q_t **mq)
608 {
609   fifo_segment_t *fs;
610   u32 fs_index;
611
612   fs_index = vcl_segment_table_lookup (segment_handle);
613   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
614     {
615       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
616       return -1;
617     }
618
619   clib_rwlock_reader_lock (&vcm->segment_table_lock);
620
621   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
622   *mq = fifo_segment_msg_q_attach (fs, mq_offset, mq_index);
623
624   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
625
626   return 0;
627 }
628
629 int
630 vcl_segment_discover_mqs (uword segment_handle, int *fds, u32 n_fds)
631 {
632   fifo_segment_t *fs;
633   u32 fs_index;
634
635   fs_index = vcl_segment_table_lookup (segment_handle);
636   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
637     {
638       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
639       return -1;
640     }
641
642   clib_rwlock_reader_lock (&vcm->segment_table_lock);
643
644   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
645   fifo_segment_msg_qs_discover (fs, fds, n_fds);
646
647   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
648
649   return 0;
650 }
651
652 svm_fifo_chunk_t *
653 vcl_segment_alloc_chunk (uword segment_handle, u32 slice_index, u32 size,
654                          uword *offset)
655 {
656   svm_fifo_chunk_t *c;
657   fifo_segment_t *fs;
658   u32 fs_index;
659
660   fs_index = vcl_segment_table_lookup (segment_handle);
661   if (fs_index == VCL_INVALID_SEGMENT_INDEX)
662     {
663       VDBG (0, "ERROR: mq segment %lx for is not attached!", segment_handle);
664       return 0;
665     }
666
667   clib_rwlock_reader_lock (&vcm->segment_table_lock);
668
669   fs = fifo_segment_get_segment (&vcm->segment_main, fs_index);
670   c = fifo_segment_alloc_chunk_w_slice (fs, slice_index, size);
671   *offset = fifo_segment_chunk_offset (fs, c);
672
673   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
674
675   return c;
676 }
677
678 int
679 vcl_session_share_fifos (vcl_session_t *s, svm_fifo_t *rxf, svm_fifo_t *txf)
680 {
681   vcl_worker_t *wrk = vcl_worker_get_current ();
682   fifo_segment_t *fs;
683
684   clib_rwlock_reader_lock (&vcm->segment_table_lock);
685
686   fs = fifo_segment_get_segment (&vcm->segment_main, rxf->segment_index);
687   s->rx_fifo = fifo_segment_duplicate_fifo (fs, rxf);
688   s->tx_fifo = fifo_segment_duplicate_fifo (fs, txf);
689
690   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
691
692   svm_fifo_add_subscriber (s->rx_fifo, wrk->vpp_wrk_index);
693   svm_fifo_add_subscriber (s->tx_fifo, wrk->vpp_wrk_index);
694
695   return 0;
696 }
697
698 const char *
699 vcl_session_state_str (vcl_session_state_t state)
700 {
701   char *st;
702
703   switch (state)
704     {
705     case VCL_STATE_CLOSED:
706       st = "STATE_CLOSED";
707       break;
708     case VCL_STATE_LISTEN:
709       st = "STATE_LISTEN";
710       break;
711     case VCL_STATE_READY:
712       st = "STATE_READY";
713       break;
714     case VCL_STATE_VPP_CLOSING:
715       st = "STATE_VPP_CLOSING";
716       break;
717     case VCL_STATE_DISCONNECT:
718       st = "STATE_DISCONNECT";
719       break;
720     case VCL_STATE_DETACHED:
721       st = "STATE_DETACHED";
722       break;
723     case VCL_STATE_UPDATED:
724       st = "STATE_UPDATED";
725       break;
726     case VCL_STATE_LISTEN_NO_MQ:
727       st = "STATE_LISTEN_NO_MQ";
728       break;
729     default:
730       st = "UNKNOWN_STATE";
731       break;
732     }
733
734   return st;
735 }
736
737 u8 *
738 vcl_format_ip4_address (u8 *s, va_list *args)
739 {
740   u8 *a = va_arg (*args, u8 *);
741   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
742 }
743
744 u8 *
745 vcl_format_ip6_address (u8 *s, va_list *args)
746 {
747   ip6_address_t *a = va_arg (*args, ip6_address_t *);
748   u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
749
750   i_max_n_zero = ARRAY_LEN (a->as_u16);
751   max_n_zeros = 0;
752   i_first_zero = i_max_n_zero;
753   n_zeros = 0;
754   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
755     {
756       u32 is_zero = a->as_u16[i] == 0;
757       if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
758         {
759           i_first_zero = i;
760           n_zeros = 0;
761         }
762       n_zeros += is_zero;
763       if ((!is_zero && n_zeros > max_n_zeros) ||
764           (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
765         {
766           i_max_n_zero = i_first_zero;
767           max_n_zeros = n_zeros;
768           i_first_zero = ARRAY_LEN (a->as_u16);
769           n_zeros = 0;
770         }
771     }
772
773   last_double_colon = 0;
774   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
775     {
776       if (i == i_max_n_zero && max_n_zeros > 1)
777         {
778           s = format (s, "::");
779           i += max_n_zeros - 1;
780           last_double_colon = 1;
781         }
782       else
783         {
784           s = format (s, "%s%x", (last_double_colon || i == 0) ? "" : ":",
785                       clib_net_to_host_u16 (a->as_u16[i]));
786           last_double_colon = 0;
787         }
788     }
789
790   return s;
791 }
792
793 /* Format an IP46 address. */
794 u8 *
795 vcl_format_ip46_address (u8 *s, va_list *args)
796 {
797   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
798   ip46_type_t type = va_arg (*args, ip46_type_t);
799   int is_ip4 = 1;
800
801   switch (type)
802     {
803     case IP46_TYPE_ANY:
804       is_ip4 = ip46_address_is_ip4 (ip46);
805       break;
806     case IP46_TYPE_IP4:
807       is_ip4 = 1;
808       break;
809     case IP46_TYPE_IP6:
810       is_ip4 = 0;
811       break;
812     }
813
814   return is_ip4 ? format (s, "%U", vcl_format_ip4_address, &ip46->ip4) :
815                   format (s, "%U", vcl_format_ip6_address, &ip46->ip6);
816 }
817
818 /*
819  * fd.io coding-style-patch-verification: ON
820  *
821  * Local Variables:
822  * eval: (c-set-style "gnu")
823  * End:
824  */