]> git.proxmox.com Git - mirror_ovs.git/blame - lib/lldp/lldpd-structs.c
lib/list: Add LIST_FOR_EACH_POP.
[mirror_ovs.git] / lib / lldp / lldpd-structs.c
CommitLineData
be53a5c4
DF
1/* -*- mode: c; c-file-style: "openbsd" -*- */
2/*
d9f3b69e 3 * Copyright (c) 2015 Nicira, Inc.
be53a5c4
DF
4 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <config.h>
20#include "lldpd-structs.h"
21#include <stdlib.h>
be53a5c4
DF
22#include <unistd.h>
23#include "lldpd.h"
d9f3b69e 24#include "timeval.h"
be53a5c4
DF
25
26VLOG_DEFINE_THIS_MODULE(lldpd_structs);
27
28void
29lldpd_chassis_mgmt_cleanup(struct lldpd_chassis *chassis)
30{
5f03c983 31 struct lldpd_mgmt *mgmt;
be53a5c4
DF
32
33 VLOG_DBG("cleanup management addresses for chassis %s",
34 chassis->c_name ? chassis->c_name : "(unknown)");
35
5f03c983 36 LIST_FOR_EACH_POP (mgmt, m_entries, &chassis->c_mgmt) {
be53a5c4
DF
37 free(mgmt);
38 }
39
32590179 40 list_init(&chassis->c_mgmt);
be53a5c4
DF
41}
42
43void
02d593b1 44lldpd_chassis_cleanup(struct lldpd_chassis *chassis, bool all)
be53a5c4
DF
45{
46 lldpd_chassis_mgmt_cleanup(chassis);
47 VLOG_DBG("cleanup chassis %s",
e8fa10d7 48 chassis->c_name ? chassis->c_name : "(unknown)");
be53a5c4
DF
49 free(chassis->c_id);
50 free(chassis->c_name);
51 free(chassis->c_descr);
52 if (all) {
53 free(chassis);
54 }
55}
56
57/* Cleanup a remote port. The before last argument, `expire` is a function that
02d593b1
BP
58 * should be called when a remote port is removed. If the last argument is
59 * true, all remote ports are removed.
be53a5c4
DF
60 */
61void
62lldpd_remote_cleanup(struct lldpd_hardware *hw,
63 void(*expire)(struct lldpd_hardware *,
64 struct lldpd_port *),
02d593b1 65 bool all)
be53a5c4
DF
66{
67 struct lldpd_port *port, *port_next;
d9f3b69e 68 time_t now = time_now();
be53a5c4
DF
69
70 VLOG_DBG("cleanup remote port on %s", hw->h_ifname);
135706b3 71 LIST_FOR_EACH_SAFE (port, port_next, p_entries, &hw->h_rports) {
02d593b1 72 bool del = all;
be53a5c4
DF
73 if (!all && expire &&
74 (now >= port->p_lastupdate + port->p_chassis->c_ttl)) {
75 hw->h_ageout_cnt++;
76 hw->h_delete_cnt++;
02d593b1 77 del = true;
be53a5c4
DF
78 }
79 if (del) {
80 if (expire) {
81 expire(hw, port);
82 }
83
84 if (!all) {
85 list_remove(&port->p_entries);
86 }
02d593b1 87 lldpd_port_cleanup(port, true);
be53a5c4
DF
88 free(port);
89 }
90 }
91 if (all) {
135706b3 92 list_init(&hw->h_rports);
be53a5c4
DF
93 }
94}
95
96/* If `all' is true, clear all information, including information that
97 are not refreshed periodically. Port should be freed manually. */
98void
02d593b1 99lldpd_port_cleanup(struct lldpd_port *port, bool all)
be53a5c4
DF
100{
101 /* We set these to NULL so we don't free wrong memory */
102
103 free(port->p_id);
104 port->p_id = NULL;
105 free(port->p_descr);
106 port->p_descr = NULL;
107 if (all) {
108 free(port->p_lastframe);
109 /* Chassis may not have been attributed, yet.*/
110 if (port->p_chassis) {
111 port->p_chassis->c_refcount--;
112 port->p_chassis = NULL;
113 }
114 }
115}
116
117void
118lldpd_config_cleanup(struct lldpd_config *config)
119{
120 VLOG_DBG("general configuration cleanup");
121 free(config->c_mgmt_pattern);
122 free(config->c_cid_pattern);
123 free(config->c_iface_pattern);
124 free(config->c_platform);
125 free(config->c_description);
126}