📄 subscr.c
字号:
/* * net/tipc/subscr.c: TIPC subscription service * * Copyright (c) 2000-2006, Ericsson AB * Copyright (c) 2005, Wind River Systems * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the names of the copyright holders nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * Alternatively, this software may be distributed under the terms of the * GNU General Public License ("GPL") version 2 as published by the Free * Software Foundation. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include "core.h"#include "dbg.h"#include "subscr.h"#include "name_table.h"#include "ref.h"/** * struct subscriber - TIPC network topology subscriber * @ref: object reference to subscriber object itself * @lock: pointer to spinlock controlling access to subscriber object * @subscriber_list: adjacent subscribers in top. server's list of subscribers * @subscription_list: list of subscription objects for this subscriber * @port_ref: object reference to port used to communicate with subscriber * @swap: indicates if subscriber uses opposite endianness in its messages */struct subscriber { u32 ref; spinlock_t *lock; struct list_head subscriber_list; struct list_head subscription_list; u32 port_ref; int swap;};/** * struct top_srv - TIPC network topology subscription service * @user_ref: TIPC userid of subscription service * @setup_port: reference to TIPC port that handles subscription requests * @subscription_count: number of active subscriptions (not subscribers!) * @subscriber_list: list of ports subscribing to service * @lock: spinlock govering access to subscriber list */struct top_srv { u32 user_ref; u32 setup_port; atomic_t subscription_count; struct list_head subscriber_list; spinlock_t lock;};static struct top_srv topsrv = { 0 };/** * htohl - convert value to endianness used by destination * @in: value to convert * @swap: non-zero if endianness must be reversed * * Returns converted value */static u32 htohl(u32 in, int swap){ char *c = (char *)∈ return swap ? ((c[3] << 3) + (c[2] << 2) + (c[1] << 1) + c[0]) : in;}/** * subscr_send_event - send a message containing a tipc_event to the subscriber */static void subscr_send_event(struct subscription *sub, u32 found_lower, u32 found_upper, u32 event, u32 port_ref, u32 node){ struct iovec msg_sect; msg_sect.iov_base = (void *)&sub->evt; msg_sect.iov_len = sizeof(struct tipc_event); sub->evt.event = htohl(event, sub->owner->swap); sub->evt.found_lower = htohl(found_lower, sub->owner->swap); sub->evt.found_upper = htohl(found_upper, sub->owner->swap); sub->evt.port.ref = htohl(port_ref, sub->owner->swap); sub->evt.port.node = htohl(node, sub->owner->swap); tipc_send(sub->owner->port_ref, 1, &msg_sect);}/** * tipc_subscr_overlap - test for subscription overlap with the given values * * Returns 1 if there is overlap, otherwise 0. */int tipc_subscr_overlap(struct subscription *sub, u32 found_lower, u32 found_upper){ if (found_lower < sub->seq.lower) found_lower = sub->seq.lower; if (found_upper > sub->seq.upper) found_upper = sub->seq.upper; if (found_lower > found_upper) return 0; return 1;}/** * tipc_subscr_report_overlap - issue event if there is subscription overlap * * Protected by nameseq.lock in name_table.c */void tipc_subscr_report_overlap(struct subscription *sub, u32 found_lower, u32 found_upper, u32 event, u32 port_ref, u32 node, int must){ dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower, sub->seq.upper, found_lower, found_upper); if (!tipc_subscr_overlap(sub, found_lower, found_upper)) return; if (!must && !(sub->filter & TIPC_SUB_PORTS)) return; subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);}/** * subscr_timeout - subscription timeout has occurred */static void subscr_timeout(struct subscription *sub){ struct subscriber *subscriber; u32 subscriber_ref; /* Validate subscriber reference (in case subscriber is terminating) */ subscriber_ref = sub->owner->ref; subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref); if (subscriber == NULL) return; /* Validate timeout (in case subscription is being cancelled) */ if (sub->timeout == TIPC_WAIT_FOREVER) { tipc_ref_unlock(subscriber_ref); return; } /* Unlink subscription from name table */ tipc_nametbl_unsubscribe(sub); /* Notify subscriber of timeout, then unlink subscription */ subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper, TIPC_SUBSCR_TIMEOUT, 0, 0); list_del(&sub->subscription_list); /* Now destroy subscription */ tipc_ref_unlock(subscriber_ref); k_term_timer(&sub->timer); kfree(sub); atomic_dec(&topsrv.subscription_count);}/** * subscr_del - delete a subscription within a subscription list * * Called with subscriber locked. */static void subscr_del(struct subscription *sub){ tipc_nametbl_unsubscribe(sub); list_del(&sub->subscription_list); kfree(sub); atomic_dec(&topsrv.subscription_count);}/** * subscr_terminate - terminate communication with a subscriber * * Called with subscriber locked. Routine must temporarily release this lock * to enable subscription timeout routine(s) to finish without deadlocking; * the lock is then reclaimed to allow caller to release it upon return. * (This should work even in the unlikely event some other thread creates * a new object reference in the interim that uses this lock; this routine will * simply wait for it to be released, then claim it.) */static void subscr_terminate(struct subscriber *subscriber){ struct subscription *sub; struct subscription *sub_temp; /* Invalidate subscriber reference */ tipc_ref_discard(subscriber->ref); spin_unlock_bh(subscriber->lock); /* Destroy any existing subscriptions for subscriber */ list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list, subscription_list) { if (sub->timeout != TIPC_WAIT_FOREVER) { k_cancel_timer(&sub->timer); k_term_timer(&sub->timer); } dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n", sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber); subscr_del(sub); } /* Sever connection to subscriber */ tipc_shutdown(subscriber->port_ref); tipc_deleteport(subscriber->port_ref); /* Remove subscriber from topology server's subscriber list */ spin_lock_bh(&topsrv.lock); list_del(&subscriber->subscriber_list); spin_unlock_bh(&topsrv.lock); /* Now destroy subscriber */ spin_lock_bh(subscriber->lock); kfree(subscriber);}/** * subscr_cancel - handle subscription cancellation request * * Called with subscriber locked. Routine must temporarily release this lock * to enable the subscription timeout routine to finish without deadlocking; * the lock is then reclaimed to allow caller to release it upon return. * * Note that fields of 's' use subscriber's endianness! */static void subscr_cancel(struct tipc_subscr *s, struct subscriber *subscriber){ struct subscription *sub; struct subscription *sub_temp; int found = 0; /* Find first matching subscription, exit if not found */ list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list, subscription_list) { if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) { found = 1; break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -