lldp: Miscellaneous coding style fixes.
[cascardo/ovs.git] / lib / ovs-lldp.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  * Copyright (c) 2014 WindRiver, Inc.
4  * Copyright (c) 2015 Avaya, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 /* Implementation of Auto Attach.
20  * Based on sample implementation in 802.1ab.  Above copyright and license
21  * applies to all modifications.
22  * Limitations:
23  * - No support for multiple bridge.
24  * - Auto Attach state machine not implemented.
25  * - Auto Attach and LLDP code are bundled together.  The plan is to decoupled
26  *   them.
27  */
28
29 #include <config.h>
30 #include "ovs-lldp.h"
31 #include <arpa/inet.h>
32 #include <inttypes.h>
33 #include <netinet/in.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include "dynamic-string.h"
38 #include "flow.h"
39 #include "list.h"
40 #include "lldp/lldpd.h"
41 #include "lldp/lldpd-structs.h"
42 #include "netdev.h"
43 #include "openvswitch/types.h"
44 #include "packets.h"
45 #include "poll-loop.h"
46 #include "smap.h"
47 #include "unixctl.h"
48 #include "util.h"
49 #include "openvswitch/vlog.h"
50
51 VLOG_DEFINE_THIS_MODULE(ovs_lldp);
52
53 #define LLDP_PROTOCOL_ID        0x0000
54 #define LLDP_PROTOCOL_VERSION   0x00
55 #define LLDP_TYPE_CONFIG        0x00
56 #define LLDP_CHASSIS_TTL        120
57 #define ETH_TYPE_LLDP           0x88cc
58 #define MINIMUM_ETH_PACKET_SIZE 68
59
60 #define AA_STATUS_MULTIPLE \
61     AA_STATUS(ACTIVE,2,Active) \
62     AA_STATUS(REJECT_GENERIC,3,Reject (Generic)) \
63     AA_STATUS(REJECT_AA_RES_NOTAVAIL,4,Reject (AA resources unavailable)) \
64     AA_STATUS(REJECT_INVALID,6,Reject (Invalid)) \
65     AA_STATUS(REJECT_VLAN_RES_UNAVAIL,8,Reject (VLAN resources unavailable)) \
66     AA_STATUS(REJECT_VLAN_APP_ISSUE,9,Reject (Application interaction issue)) \
67     AA_STATUS(PENDING,255,Pending)
68
69 enum aa_status {
70 #define AA_STATUS(NAME, VALUE, STR) AA_STATUS_##NAME = VALUE,
71     AA_STATUS_MULTIPLE
72 #undef AA_STATUS
73     AA_STATUS_N_MULTIPLE
74 };
75
76 /* Internal structure for an Auto Attach mapping.
77  */
78 struct aa_mapping_internal {
79     struct hmap_node hmap_node_isid;
80     struct hmap_node hmap_node_aux;
81     uint32_t         isid;
82     uint16_t         vlan;
83     void             *aux;
84     enum aa_status   status;
85 };
86
87 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
88
89 /* Hash map of all LLDP instances keyed by name (port at the moment).
90  */
91 static struct hmap all_lldps__ = HMAP_INITIALIZER(&all_lldps__);
92 static struct hmap *const all_lldps OVS_GUARDED_BY(mutex) = &all_lldps__;
93
94 /* Hash map of all the Auto Attach mappings.  Global at the moment (but will
95  * be per bridge).  Used when adding a new port to a bridge so that we can
96  * properly install all the configured mapping on the port and export them
97  * To the Auto Attach server via LLDP.
98  */
99 static struct hmap all_mappings__ = HMAP_INITIALIZER(&all_mappings__);
100 static struct hmap *const all_mappings OVS_GUARDED_BY(mutex) = &all_mappings__;
101
102 static struct lldp_aa_element_system_id system_id_null;
103
104 /* Convert an LLDP chassis ID to a string.
105  */
106 static void
107 chassisid_to_string(uint8_t *array, size_t len, char **str)
108 {
109     unsigned int i;
110
111     *str = xmalloc(len * 3);
112
113     for (i = 0; i < len; i++) {
114         snprintf(&(*str)[i * 3], 4, "%02x:", array[i]);
115     }
116     (*str)[(i * 3) - 1] = '\0';
117 }
118
119 /* Find an Auto Attach mapping keyed by I-SID.
120  */
121 static struct aa_mapping_internal *
122 mapping_find_by_isid(struct lldp *lldp, uint32_t isid)
123     OVS_REQUIRES(mutex)
124 {
125     struct aa_mapping_internal *m;
126
127     HMAP_FOR_EACH_IN_BUCKET (m, hmap_node_isid, hash_int(isid, 0),
128                              &lldp->mappings_by_isid) {
129         if (isid == m->isid) {
130             return m;
131         }
132     }
133
134     return NULL;
135 }
136
137 /* Find an Auto Attach mapping keyed by aux.  aux is an opaque pointer created
138  * by the bridge that refers to an OVSDB mapping record.
139  */
140 static struct aa_mapping_internal *
141 mapping_find_by_aux(struct lldp *lldp, const void *aux) OVS_REQUIRES(mutex)
142 {
143     struct aa_mapping_internal *m;
144
145     HMAP_FOR_EACH_IN_BUCKET (m, hmap_node_aux, hash_pointer(aux, 0),
146                              &lldp->mappings_by_aux) {
147         if (aux == m->aux) {
148             return m;
149         }
150     }
151
152     return NULL;
153 }
154
155 /* Convert an Auto Attach request status to a string.
156  */
157 static char *
158 aa_status_to_str(uint8_t status)
159 {
160     switch (status) {
161 #define AA_STATUS(NAME, VALUE, STR) case AA_STATUS_##NAME: return #STR;
162         AA_STATUS_MULTIPLE
163 #undef AA_STATUS
164         default: return "Undefined";
165     }
166 }
167
168 /* Display LLDP and Auto Attach statistics.
169  */
170 static void
171 aa_print_lldp_and_aa_stats(struct ds *ds, struct lldp *lldp)
172     OVS_REQUIRES(mutex)
173 {
174     struct lldpd_hardware *hw;
175
176     ds_put_format(ds, "Statistics: %s\n", lldp->name);
177
178     if (!lldp->lldpd) {
179         return;
180     }
181
182     LIST_FOR_EACH (hw, h_entries, &lldp->lldpd->g_hardware.h_entries) {
183         ds_put_format(ds, "\ttx cnt: %"PRIu64"\n", hw->h_tx_cnt);
184         ds_put_format(ds, "\trx cnt: %"PRIu64"\n", hw->h_rx_cnt);
185         ds_put_format(ds, "\trx discarded cnt: %"PRIu64"\n",
186                       hw->h_rx_discarded_cnt);
187         ds_put_format(ds, "\trx unrecognized cnt: %"PRIu64"\n",
188                       hw->h_rx_unrecognized_cnt);
189         ds_put_format(ds, "\tageout cnt: %"PRIu64"\n", hw->h_ageout_cnt);
190         ds_put_format(ds, "\tinsert cnt: %"PRIu64"\n", hw->h_insert_cnt);
191         ds_put_format(ds, "\tdelete cnt: %"PRIu64"\n", hw->h_delete_cnt);
192         ds_put_format(ds, "\tdrop cnt: %"PRIu64"\n", hw->h_drop_cnt);
193     }
194 }
195
196 static void
197 aa_print_element_status_port(struct ds *ds, struct lldpd_hardware *hw)
198 {
199     struct lldpd_port *port;
200
201     LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
202         if (memcmp(&port->p_element.system_id,
203                    &system_id_null,
204                    sizeof port->p_element.system_id)) {
205             static char *none_str = "<None>";
206             char *id = none_str, *descr = none_str, *system = none_str;
207
208             if (port->p_chassis) {
209                 if (port->p_chassis->c_id_len > 0) {
210                     chassisid_to_string(port->p_chassis->c_id,
211                                         port->p_chassis->c_id_len, &id);
212                 }
213
214                 descr = port->p_chassis->c_descr
215                     ? port->p_chassis->c_descr : none_str;
216             }
217
218             chassisid_to_string((uint8_t *) &port->p_element.system_id,
219                 sizeof port->p_element.system_id, &system);
220
221             ds_put_format(ds, "\tAuto Attach Primary Server Id: %s\n", id);
222             ds_put_format(ds, "\tAuto Attach Primary Server Descr: %s\n",
223                           descr);
224             ds_put_format(ds, "\tAuto Attach Primary Server System Id: %s\n",
225                           system);
226
227             free(id);
228             free(system);
229         }
230     }
231 }
232
233 /* Auto Attach server broadcast an LLDP message periodically.  Display
234  * the discovered server.
235  */
236 static void
237 aa_print_element_status(struct ds *ds, struct lldp *lldp) OVS_REQUIRES(mutex)
238 {
239     struct lldpd_hardware *hw;
240
241     ds_put_format(ds, "LLDP: %s\n", lldp->name);
242
243     if (!lldp->lldpd) {
244         return;
245     }
246
247     LIST_FOR_EACH (hw, h_entries, &lldp->lldpd->g_hardware.h_entries) {
248         aa_print_element_status_port(ds, hw);
249     }
250 }
251
252 static void
253 aa_print_isid_status_port_isid(struct lldp *lldp, struct lldpd_port *port)
254     OVS_REQUIRES(mutex)
255 {
256     struct lldpd_aa_isid_vlan_maps_tlv *mapping;
257
258     if (list_is_empty(&port->p_isid_vlan_maps)) {
259         return;
260     }
261
262     LIST_FOR_EACH (mapping, m_entries, &port->p_isid_vlan_maps) {
263         uint32_t isid = mapping->isid_vlan_data.isid;
264         struct aa_mapping_internal *m = mapping_find_by_isid(lldp, isid);
265
266         VLOG_INFO("h_rport: isid=%u, vlan=%u, status=%d",
267                   isid,
268                   mapping->isid_vlan_data.vlan,
269                   mapping->isid_vlan_data.status);
270
271         /* Update the status of our internal state for the mapping. */
272         if (m) {
273             VLOG_INFO("Setting status for ISID=%"PRIu32" to %"PRIu16,
274                       isid, mapping->isid_vlan_data.status);
275             m->status = mapping->isid_vlan_data.status;
276         } else {
277             VLOG_WARN("Couldn't find mapping for I-SID=%"PRIu32, isid);
278         }
279     }
280 }
281
282 static void
283 aa_print_isid_status_port(struct lldp *lldp, struct lldpd_hardware *hw)
284     OVS_REQUIRES(mutex)
285 {
286     struct lldpd_port *port;
287
288     LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
289         aa_print_isid_status_port_isid(lldp, port);
290     }
291 }
292
293 /* The Auto Attach server will broadcast the status of the configured mappings
294  * via LLDP.  Display the status.
295  */
296 static void
297 aa_print_isid_status(struct ds *ds, struct lldp *lldp) OVS_REQUIRES(mutex)
298 {
299     struct lldpd_hardware *hw;
300     struct aa_mapping_internal *m;
301
302     if (!lldp->lldpd) {
303         return;
304     }
305
306     ds_put_format(ds, "LLDP: %s\n", lldp->name);
307
308     LIST_FOR_EACH (hw, h_entries, &lldp->lldpd->g_hardware.h_entries) {
309         aa_print_isid_status_port(lldp, hw);
310     }
311
312     ds_put_format(ds, "%-8s %-4s %-11s %-8s\n",
313                       "I-SID",
314                       "VLAN",
315                       "Source",
316                       "Status");
317     ds_put_format(ds, "-------- ---- ----------- --------\n");
318
319     HMAP_FOR_EACH (m, hmap_node_isid, &lldp->mappings_by_isid) {
320         ds_put_format(ds, "%-8"PRIu32" %-4"PRIu16" %-11s %-11s\n",
321                       m->isid, m->vlan, "Switch", aa_status_to_str(m->status));
322     }
323 }
324
325 static void
326 aa_unixctl_status(struct unixctl_conn *conn, int argc OVS_UNUSED,
327                   const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
328     OVS_EXCLUDED(mutex)
329 {
330     struct lldp *lldp;
331     struct ds ds = DS_EMPTY_INITIALIZER;
332
333     ovs_mutex_lock(&mutex);
334
335     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
336         aa_print_element_status(&ds, lldp);
337     }
338     unixctl_command_reply(conn, ds_cstr(&ds));
339     ds_destroy(&ds);
340
341     ovs_mutex_unlock(&mutex);
342 }
343
344 static void
345 aa_unixctl_show_isid(struct unixctl_conn *conn, int argc OVS_UNUSED,
346                      const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
347     OVS_EXCLUDED(mutex)
348 {
349     struct lldp *lldp;
350     struct ds ds = DS_EMPTY_INITIALIZER;
351
352     ovs_mutex_lock(&mutex);
353
354     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
355         aa_print_isid_status(&ds, lldp);
356     }
357     unixctl_command_reply(conn, ds_cstr(&ds));
358     ds_destroy(&ds);
359
360     ovs_mutex_unlock(&mutex);
361 }
362
363 static void
364 aa_unixctl_statistics(struct unixctl_conn *conn, int argc OVS_UNUSED,
365                       const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
366     OVS_EXCLUDED(mutex)
367 {
368     struct ds ds = DS_EMPTY_INITIALIZER;
369     struct lldp *lldp;
370
371     ovs_mutex_lock(&mutex);
372
373     /* Cycle through all ports and dump the stats for each one */
374     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
375         aa_print_lldp_and_aa_stats(&ds, lldp);
376     }
377
378     ovs_mutex_unlock(&mutex);
379
380     unixctl_command_reply(conn, ds_cstr(&ds));
381 }
382
383 /* An Auto Attach mapping was configured.  Populate the corresponding
384  * structures in the LLDP hardware.
385  */
386 static void
387 update_mapping_on_lldp(struct lldp *lldp, struct lldpd_hardware *hardware,
388                        struct aa_mapping_internal *m)
389 {
390     struct lldpd_aa_isid_vlan_maps_tlv *lm = xzalloc(sizeof *lm);
391
392     if (hardware->h_ifname) {
393         VLOG_INFO("\t\t hardware->h_ifname=%s", hardware->h_ifname);
394     }
395
396     lm->isid_vlan_data.isid = m->isid;
397     lm->isid_vlan_data.vlan = m->vlan;
398
399     list_push_back(&hardware->h_lport.p_isid_vlan_maps, &lm->m_entries);
400
401     /* TODO Should be done in the Auto Attach state machine when a mapping goes
402      * from "pending" to "active".
403      */
404     struct bridge_aa_vlan *node = xmalloc(sizeof *node);
405
406     node->port_name = xstrdup(hardware->h_ifname);
407     node->vlan = m->vlan;
408     node->oper = BRIDGE_AA_VLAN_OPER_ADD;
409
410     list_push_back(&lldp->active_mapping_queue, &node->list_node);
411 }
412
413 /* Bridge will poll the list of VLAN that needs to be auto configure based on
414  * the Auto Attach mappings that have been exchanged with the server.
415  */
416 int
417 aa_get_vlan_queued(struct ovs_list *list)
418 {
419     struct lldp *lldp;
420
421     ovs_mutex_lock(&mutex);
422
423     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
424         struct bridge_aa_vlan *node, *node_next;
425
426         LIST_FOR_EACH_SAFE (node,
427                             node_next,
428                             list_node,
429                             &lldp->active_mapping_queue) {
430             struct bridge_aa_vlan *copy;
431
432             copy = xmalloc(sizeof *copy);
433             copy->port_name = xstrdup(node->port_name);
434             copy->vlan = node->vlan;
435             copy->oper = node->oper;
436
437             list_push_back(list, &copy->list_node);
438
439             /* Cleanup */
440             list_remove(&node->list_node);
441             free(node->port_name);
442             free(node);
443         }
444     }
445
446     ovs_mutex_unlock(&mutex);
447
448     return 0;
449 }
450
451 /* Bridge will poll whether or not VLAN have been auto-configured.
452  */
453 unsigned int
454 aa_get_vlan_queue_size(void)
455 {
456     struct lldp *lldp;
457     unsigned int size = 0;
458
459     ovs_mutex_lock(&mutex);
460
461     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
462         size += list_size(&lldp->active_mapping_queue);
463     }
464
465     ovs_mutex_unlock(&mutex);
466
467     return size;
468 }
469
470 /* Configure Auto Attach.
471  */
472 int
473 aa_configure(const struct aa_settings *s)
474 {
475     struct lldp *lldp;
476
477     ovs_mutex_lock(&mutex);
478
479     /* TODO Change all instances for now */
480     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
481         struct lldpd_chassis *chassis;
482
483         LIST_FOR_EACH (chassis, list, &lldp->lldpd->g_chassis.list) {
484             /* System Description */
485             if (chassis->c_descr) {
486                 free(chassis->c_descr);
487             }
488             chassis->c_descr = s->system_description[0] ?
489                 xstrdup(s->system_description) : xstrdup(PACKAGE_STRING);
490
491             /* System Name */
492             if (chassis->c_name) {
493                 free(chassis->c_name);
494             }
495             chassis->c_name = xstrdup(s->system_name);
496         }
497     }
498
499     ovs_mutex_unlock(&mutex);
500
501     return 0;
502 }
503
504 /* Add a new Auto Attach mapping.
505  */
506 int
507 aa_mapping_register(void *aux, const struct aa_mapping_settings *s)
508 {
509     struct aa_mapping_internal *bridge_m;
510     struct lldp *lldp;
511
512     VLOG_INFO("Adding mapping ISID=%"PRIu32", VLAN=%"PRIu16", aux=%p",
513               s->isid, s->vlan, aux);
514
515     ovs_mutex_lock(&mutex);
516
517     /* TODO These mappings should be stores per bridge.  This is used
518      * When a port is added.  Auto Attach mappings need to be added on this
519      * port.
520      */
521     bridge_m = xzalloc(sizeof *bridge_m);
522     bridge_m->isid = s->isid;
523     bridge_m->vlan = s->vlan;
524     bridge_m->aux = aux;
525     bridge_m->status = AA_STATUS_PENDING;
526     hmap_insert(all_mappings, &bridge_m->hmap_node_isid,
527                 hash_int(bridge_m->isid, 0));
528
529
530     /* Update mapping on the all the LLDP instances. */
531     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
532         struct lldpd_hardware *hw;
533         struct aa_mapping_internal *m;
534
535         VLOG_INFO("\t lldp->name=%s", lldp->name);
536
537         if (mapping_find_by_isid(lldp, s->isid)) {
538             continue;
539         }
540
541         m = xzalloc(sizeof *m);
542         m->isid = s->isid;
543         m->vlan = s->vlan;
544         m->status = AA_STATUS_PENDING;
545         m->aux = aux;
546         hmap_insert(&lldp->mappings_by_isid, &m->hmap_node_isid,
547                     hash_int(m->isid, 0));
548         hmap_insert(&lldp->mappings_by_aux,
549                     &m->hmap_node_aux,
550                     hash_pointer(m->aux, 0));
551
552         /* Configure the mapping on each port of the LLDP stack. */
553         LIST_FOR_EACH (hw, h_entries, &lldp->lldpd->g_hardware.h_entries) {
554             update_mapping_on_lldp(lldp, hw, m);
555         }
556     }
557
558     ovs_mutex_unlock(&mutex);
559
560     return 0;
561 }
562
563 static void
564 aa_mapping_unregister_mapping(struct lldp *lldp,
565                               struct lldpd_hardware *hw,
566                               struct aa_mapping_internal *m)
567 {
568     struct lldpd_aa_isid_vlan_maps_tlv *lm, *lm_next;
569
570     LIST_FOR_EACH_SAFE (lm, lm_next, m_entries,
571                         &hw->h_lport.p_isid_vlan_maps) {
572         uint32_t isid = lm->isid_vlan_data.isid;
573
574         if (isid == m->isid) {
575             VLOG_INFO("\t\t Removing lport, isid=%u, vlan=%u",
576                       isid,
577                       lm->isid_vlan_data.vlan);
578
579             list_remove(&lm->m_entries);
580
581             /* TODO Should be done in the AA SM when a mapping goes
582              * from "pending" to "active".
583              */
584             struct bridge_aa_vlan *node = xmalloc(sizeof *node);
585
586             node->port_name = xstrdup(hw->h_ifname);
587             node->vlan = m->vlan;
588             node->oper = BRIDGE_AA_VLAN_OPER_REMOVE;
589
590             list_push_back(&lldp->active_mapping_queue, &node->list_node);
591
592             break;
593         }
594     }
595 }
596
597 /* Remove an existing Auto Attach mapping.
598  */
599 int
600 aa_mapping_unregister(void *aux)
601 {
602     struct lldp *lldp;
603
604     VLOG_INFO("Removing mapping aux=%p", aux);
605
606     ovs_mutex_lock(&mutex);
607
608     HMAP_FOR_EACH (lldp, hmap_node, all_lldps) {
609         struct lldpd_hardware *hw;
610         struct aa_mapping_internal *m = mapping_find_by_aux(lldp, aux);
611
612         /* Remove from internal hash tables. */
613         if (m) {
614             uint32_t isid = m->isid;
615             uint16_t vlan = m->vlan;
616             struct aa_mapping_internal *p = mapping_find_by_isid(lldp, isid);
617
618             VLOG_INFO("\t Removing mapping ISID=%"PRIu32", VLAN=%"PRIu16
619                       " (lldp->name=%s)", isid, vlan, lldp->name);
620
621             if (p) {
622                 hmap_remove(&lldp->mappings_by_isid, &p->hmap_node_isid);
623             }
624
625             hmap_remove(&lldp->mappings_by_aux, &m->hmap_node_aux);
626             free(m);
627
628             /* Remove from all the lldp instances */
629             LIST_FOR_EACH (hw, h_entries, &lldp->lldpd->g_hardware.h_entries) {
630                 if (hw->h_ifname) {
631                     VLOG_INFO("\t\t hardware->h_ifname=%s", hw->h_ifname);
632                 }
633
634                 aa_mapping_unregister_mapping(lldp, hw, m);
635             }
636
637             /* Remove from the all_mappings */
638             HMAP_FOR_EACH (m, hmap_node_isid, all_mappings) {
639                 if (m && isid == m->isid && vlan == m->vlan) {
640                     hmap_remove(all_mappings, &m->hmap_node_isid);
641                     break;
642                 }
643             }
644         }
645     }
646
647     ovs_mutex_unlock(&mutex);
648
649     return 0;
650 }
651
652 void
653 lldp_init(void)
654 {
655     unixctl_command_register("autoattach/status", "[bridge]", 0, 1,
656                              aa_unixctl_status, NULL);
657     unixctl_command_register("autoattach/show-isid", "[bridge]", 0, 1,
658                              aa_unixctl_show_isid, NULL);
659     unixctl_command_register("autoattach/statistics", "[bridge]", 0, 1,
660                              aa_unixctl_statistics, NULL);
661 }
662
663 /* Returns true if 'lldp' should process packets from 'flow'.  Sets
664  * fields in 'wc' that were used to make the determination.
665  */
666 bool
667 lldp_should_process_flow(const struct flow *flow)
668 {
669     return (flow->dl_type == htons(ETH_TYPE_LLDP));
670 }
671
672
673 /* Process an LLDP packet that was received on a bridge port.
674  */
675 void
676 lldp_process_packet(struct lldp *lldp, const struct dp_packet *p)
677 {
678     if (lldp) {
679         lldpd_recv(lldp->lldpd,
680                    (struct lldpd_hardware *)
681                        lldp->lldpd->g_hardware.h_entries.next,
682                    (char *) p->data_,
683                    p->size_);
684     }
685 }
686
687 /* This code is called periodically to check if the LLDP module has an LLDP
688  * message it wishes to send.  It is called several times every second.
689  */
690 bool
691 lldp_should_send_packet(struct lldp *cfg) OVS_EXCLUDED(mutex)
692 {
693     bool ret;
694
695     ovs_mutex_lock(&mutex);
696     ret = timer_expired(&cfg->tx_timer);
697     ovs_mutex_unlock(&mutex);
698
699     return ret;
700 }
701
702 /* Returns the next wake up time.
703  */
704 long long int
705 lldp_wake_time(const struct lldp *lldp) OVS_EXCLUDED(mutex)
706 {
707     long long int retval;
708
709     if (!lldp) {
710         return LLONG_MAX;
711     }
712
713     ovs_mutex_lock(&mutex);
714     retval = lldp->tx_timer.t;
715     ovs_mutex_unlock(&mutex);
716
717     return retval;
718 }
719
720 /* Put the monitor thread to sleep until it's next wake time.
721  */
722 long long int
723 lldp_wait(struct lldp *lldp) OVS_EXCLUDED(mutex)
724 {
725     long long int wake_time = lldp_wake_time(lldp);
726     poll_timer_wait_until(wake_time);
727     return wake_time;
728 }
729
730 /* Prepare the LLDP packet to be sent on a bridge port.
731  */
732 void
733 lldp_put_packet(struct lldp *lldp, struct dp_packet *packet,
734                 uint8_t eth_src[ETH_ADDR_LEN]) OVS_EXCLUDED(mutex)
735 {
736     struct lldpd *mylldpd = lldp->lldpd;
737     struct lldpd_hardware *hw = (struct lldpd_hardware *)
738         mylldpd->g_hardware.h_entries.next;
739     uint32_t lldp_size = 0;
740     static const uint8_t eth_addr_lldp[6] =
741         {0x01, 0x80, 0xC2, 0x00, 0x00, 0x0e};
742
743     ovs_mutex_lock(&mutex);
744
745     eth_compose(packet, eth_addr_lldp, eth_src, ETH_TYPE_LLDP, 0);
746
747     lldp_size = lldpd_send(hw, packet);
748     if (lldp_size + ETH_HEADER_LEN < MINIMUM_ETH_PACKET_SIZE) {
749         lldp_size = MINIMUM_ETH_PACKET_SIZE;
750     }
751
752     timer_set_duration(&lldp->tx_timer, lldp->lldpd->g_config.c_tx_interval);
753     ovs_mutex_unlock(&mutex);
754 }
755
756 /* Configures the LLDP stack.
757  */
758 bool
759 lldp_configure(struct lldp *lldp) OVS_EXCLUDED(mutex)
760 {
761     if (lldp) {
762         ovs_mutex_lock(&mutex);
763         timer_set_expired(&lldp->tx_timer);
764         timer_set_duration(&lldp->tx_timer, LLDP_DEFAULT_TRANSMIT_INTERVAL_MS);
765         lldp->lldpd->g_config.c_tx_interval =
766             LLDP_DEFAULT_TRANSMIT_INTERVAL_MS;
767         ovs_mutex_unlock(&mutex);
768     }
769
770     return true;
771 }
772
773 /* Create an LLDP stack instance.  At the moment there is one per bridge port.
774  */
775 struct lldp *
776 lldp_create(const struct netdev *netdev,
777             const uint32_t mtu,
778             const struct smap *cfg) OVS_EXCLUDED(mutex)
779 {
780     struct lldp *lldp;
781     struct lldpd_chassis *lchassis;
782     struct lldpd_hardware *hw;
783     struct aa_mapping_internal *m;
784
785     if (!cfg || !smap_get_bool(cfg, "enable", false)) {
786         return NULL;
787     }
788
789     lldp = xzalloc(sizeof *lldp);
790     lldp->name = xstrdup(netdev_get_name(netdev));
791     lldp->lldpd = xzalloc(sizeof *lldp->lldpd);
792
793     hmap_init(&lldp->mappings_by_isid);
794     hmap_init(&lldp->mappings_by_aux);
795     list_init(&lldp->active_mapping_queue);
796
797     lchassis = xzalloc(sizeof *lchassis);
798     lchassis->c_cap_available = LLDP_CAP_BRIDGE;
799     lchassis->c_cap_enabled = LLDP_CAP_BRIDGE;
800     lchassis->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
801     lchassis->c_id_len = ETH_ADDR_LEN;
802     lchassis->c_id = xmalloc(ETH_ADDR_LEN);
803     netdev_get_etheraddr(netdev, lchassis->c_id);
804
805     list_init(&lchassis->c_mgmt);
806     lchassis->c_ttl = lldp->lldpd->g_config.c_tx_interval *
807                       lldp->lldpd->g_config.c_tx_hold;
808     lchassis->c_ttl = LLDP_CHASSIS_TTL;
809     lldpd_assign_cfg_to_protocols(lldp->lldpd);
810     list_init(&lldp->lldpd->g_chassis.list);
811     list_push_back(&lldp->lldpd->g_chassis.list, &lchassis->list);
812
813     if ((hw = lldpd_alloc_hardware(lldp->lldpd,
814                                    (char *) netdev_get_name(netdev),
815                                    0)) == NULL) {
816         VLOG_WARN("Unable to allocate space for %s",
817                   (char *) netdev_get_name(netdev));
818         out_of_memory();
819     }
820
821     ovs_refcount_init(&lldp->ref_cnt);
822 #ifndef _WIN32
823     hw->h_flags |= IFF_RUNNING;
824 #endif
825     hw->h_mtu = mtu;
826     hw->h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
827     hw->h_lport.p_id = xstrdup(netdev_get_name(netdev));
828
829     /* p_id is not necessarily a null terminated string. */
830     hw->h_lport.p_id_len = strlen(netdev_get_name(netdev));
831
832     /* Auto Attach element tlv */
833     hw->h_lport.p_element.type = LLDP_TLV_AA_ELEM_TYPE_TAG_CLIENT;
834     hw->h_lport.p_element.mgmt_vlan = 0;
835     memcpy(&hw->h_lport.p_element.system_id.system_mac,
836            lchassis->c_id, lchassis->c_id_len);
837     hw->h_lport.p_element.system_id.conn_type =
838         LLDP_TLV_AA_ELEM_CONN_TYPE_SINGLE;
839
840     hw->h_lport.p_element.system_id.smlt_id = 0;
841     hw->h_lport.p_element.system_id.mlt_id[0] = 0;
842     hw->h_lport.p_element.system_id.mlt_id[1] = 0;
843
844     list_init(&hw->h_lport.p_isid_vlan_maps);
845     list_init(&lldp->lldpd->g_hardware.h_entries);
846     list_push_back(&lldp->lldpd->g_hardware.h_entries, &hw->h_entries);
847
848     ovs_mutex_lock(&mutex);
849
850     /* Update port with Auto Attach mappings configured. */
851     HMAP_FOR_EACH (m, hmap_node_isid, all_mappings) {
852         struct aa_mapping_internal *p;
853
854         if (mapping_find_by_isid(lldp, m->isid)) {
855             continue;
856         }
857
858         p = xmemdup(m, sizeof *p);
859         hmap_insert(&lldp->mappings_by_isid, &p->hmap_node_isid,
860                     hash_int(p->isid, 0));
861         hmap_insert(&lldp->mappings_by_aux,
862                     &p->hmap_node_aux,
863                     hash_pointer(p->aux, 0));
864
865         update_mapping_on_lldp(lldp, hw, p);
866     }
867
868     hmap_insert(all_lldps, &lldp->hmap_node,
869                 hash_string(netdev_get_name(netdev), 0));
870
871     ovs_mutex_unlock(&mutex);
872
873     return lldp;
874 }
875
876
877 struct lldp *
878 lldp_create_dummy(void)
879 {
880     struct lldp *lldp;
881     struct lldpd_chassis *lchassis;
882     struct lldpd_hardware *hw;
883
884     lldp = xzalloc(sizeof *lldp);
885     lldp->name = "dummy-lldp";
886     lldp->lldpd = xzalloc(sizeof *lldp->lldpd);
887
888     hmap_init(&lldp->mappings_by_isid);
889     hmap_init(&lldp->mappings_by_aux);
890     list_init(&lldp->active_mapping_queue);
891
892     lchassis = xzalloc(sizeof *lchassis);
893     lchassis->c_cap_available = LLDP_CAP_BRIDGE;
894     lchassis->c_cap_enabled = LLDP_CAP_BRIDGE;
895     lchassis->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR;
896     lchassis->c_id_len = ETH_ADDR_LEN;
897     lchassis->c_id = xmalloc(ETH_ADDR_LEN);
898
899     list_init(&lchassis->c_mgmt);
900     lchassis->c_ttl = LLDP_CHASSIS_TTL;
901     lldpd_assign_cfg_to_protocols(lldp->lldpd);
902     list_init(&lldp->lldpd->g_chassis.list);
903     list_push_back(&lldp->lldpd->g_chassis.list, &lchassis->list);
904
905     hw = lldpd_alloc_hardware(lldp->lldpd, "dummy-hw", 0);
906
907     ovs_refcount_init(&lldp->ref_cnt);
908 #ifndef _WIN32
909     hw->h_flags |= IFF_RUNNING;
910 #endif
911     hw->h_mtu = 1500;
912     hw->h_lport.p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME;
913     hw->h_lport.p_id = "dummy-port";
914
915     /* p_id is not necessarily a null terminated string. */
916     hw->h_lport.p_id_len = strlen(hw->h_lport.p_id);
917
918     /* Auto Attach element tlv */
919     hw->h_lport.p_element.type = LLDP_TLV_AA_ELEM_TYPE_TAG_CLIENT;
920     hw->h_lport.p_element.mgmt_vlan = 0;
921     memcpy(&hw->h_lport.p_element.system_id.system_mac,
922            lchassis->c_id, lchassis->c_id_len);
923     hw->h_lport.p_element.system_id.conn_type =
924         LLDP_TLV_AA_ELEM_CONN_TYPE_SINGLE;
925     hw->h_lport.p_element.system_id.smlt_id = 0;
926     hw->h_lport.p_element.system_id.mlt_id[0] = 0;
927     hw->h_lport.p_element.system_id.mlt_id[1] = 0;
928
929     list_init(&hw->h_lport.p_isid_vlan_maps);
930     list_init(&lldp->lldpd->g_hardware.h_entries);
931     list_push_back(&lldp->lldpd->g_hardware.h_entries, &hw->h_entries);
932
933     return lldp;
934 }
935
936 /* Unreference a specific LLDP instance.
937  */
938 void
939 lldp_unref(struct lldp *lldp)
940 {
941     if (!lldp) {
942         return;
943     }
944
945     ovs_mutex_lock(&mutex);
946     if (ovs_refcount_unref_relaxed(&lldp->ref_cnt) != 1) {
947         ovs_mutex_unlock(&mutex);
948         return;
949     }
950
951     hmap_remove(all_lldps, &lldp->hmap_node);
952     ovs_mutex_unlock(&mutex);
953
954     lldpd_cleanup(lldp->lldpd);
955     free(lldp->lldpd);
956     free(lldp->name);
957     free(lldp);
958 }
959
960 /* Unreference a specific LLDP instance.
961  */
962 struct lldp *
963 lldp_ref(const struct lldp *lldp_)
964 {
965     struct lldp *lldp = CONST_CAST(struct lldp *, lldp_);
966     if (lldp) {
967         ovs_refcount_ref(&lldp->ref_cnt);
968     }
969     return lldp;
970 }