datapath.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. /*
  2. * Copyright (c) 2007-2012 Nicira Networks.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <linux/workqueue.h>
  49. #include <net/genetlink.h>
  50. #include "datapath.h"
  51. #include "flow.h"
  52. #include "vport-internal_dev.h"
  53. /**
  54. * DOC: Locking:
  55. *
  56. * Writes to device state (add/remove datapath, port, set operations on vports,
  57. * etc.) are protected by RTNL.
  58. *
  59. * Writes to other state (flow table modifications, set miscellaneous datapath
  60. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  61. * genl_mutex.
  62. *
  63. * Reads are protected by RCU.
  64. *
  65. * There are a few special cases (mostly stats) that have their own
  66. * synchronization but they nest under all of above and don't interact with
  67. * each other.
  68. */
  69. /* Global list of datapaths to enable dumping them all out.
  70. * Protected by genl_mutex.
  71. */
  72. static LIST_HEAD(dps);
  73. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  74. static void rehash_flow_table(struct work_struct *work);
  75. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  76. static struct vport *new_vport(const struct vport_parms *);
  77. static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
  78. const struct dp_upcall_info *);
  79. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
  80. const struct dp_upcall_info *);
  81. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  82. static struct datapath *get_dp(int dp_ifindex)
  83. {
  84. struct datapath *dp = NULL;
  85. struct net_device *dev;
  86. rcu_read_lock();
  87. dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
  88. if (dev) {
  89. struct vport *vport = ovs_internal_dev_get_vport(dev);
  90. if (vport)
  91. dp = vport->dp;
  92. }
  93. rcu_read_unlock();
  94. return dp;
  95. }
  96. /* Must be called with rcu_read_lock or RTNL lock. */
  97. const char *ovs_dp_name(const struct datapath *dp)
  98. {
  99. struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
  100. return vport->ops->get_name(vport);
  101. }
  102. static int get_dpifindex(struct datapath *dp)
  103. {
  104. struct vport *local;
  105. int ifindex;
  106. rcu_read_lock();
  107. local = rcu_dereference(dp->ports[OVSP_LOCAL]);
  108. if (local)
  109. ifindex = local->ops->get_ifindex(local);
  110. else
  111. ifindex = 0;
  112. rcu_read_unlock();
  113. return ifindex;
  114. }
  115. static void destroy_dp_rcu(struct rcu_head *rcu)
  116. {
  117. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  118. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  119. free_percpu(dp->stats_percpu);
  120. kfree(dp);
  121. }
  122. /* Called with RTNL lock and genl_lock. */
  123. static struct vport *new_vport(const struct vport_parms *parms)
  124. {
  125. struct vport *vport;
  126. vport = ovs_vport_add(parms);
  127. if (!IS_ERR(vport)) {
  128. struct datapath *dp = parms->dp;
  129. rcu_assign_pointer(dp->ports[parms->port_no], vport);
  130. list_add(&vport->node, &dp->port_list);
  131. }
  132. return vport;
  133. }
  134. /* Called with RTNL lock. */
  135. void ovs_dp_detach_port(struct vport *p)
  136. {
  137. ASSERT_RTNL();
  138. /* First drop references to device. */
  139. list_del(&p->node);
  140. rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
  141. /* Then destroy it. */
  142. ovs_vport_del(p);
  143. }
  144. /* Must be called with rcu_read_lock. */
  145. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  146. {
  147. struct datapath *dp = p->dp;
  148. struct sw_flow *flow;
  149. struct dp_stats_percpu *stats;
  150. struct sw_flow_key key;
  151. u64 *stats_counter;
  152. int error;
  153. int key_len;
  154. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  155. /* Extract flow from 'skb' into 'key'. */
  156. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  157. if (unlikely(error)) {
  158. kfree_skb(skb);
  159. return;
  160. }
  161. /* Look up flow. */
  162. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  163. if (unlikely(!flow)) {
  164. struct dp_upcall_info upcall;
  165. upcall.cmd = OVS_PACKET_CMD_MISS;
  166. upcall.key = &key;
  167. upcall.userdata = NULL;
  168. upcall.pid = p->upcall_pid;
  169. ovs_dp_upcall(dp, skb, &upcall);
  170. consume_skb(skb);
  171. stats_counter = &stats->n_missed;
  172. goto out;
  173. }
  174. OVS_CB(skb)->flow = flow;
  175. stats_counter = &stats->n_hit;
  176. ovs_flow_used(OVS_CB(skb)->flow, skb);
  177. ovs_execute_actions(dp, skb);
  178. out:
  179. /* Update datapath statistics. */
  180. u64_stats_update_begin(&stats->sync);
  181. (*stats_counter)++;
  182. u64_stats_update_end(&stats->sync);
  183. }
  184. static struct genl_family dp_packet_genl_family = {
  185. .id = GENL_ID_GENERATE,
  186. .hdrsize = sizeof(struct ovs_header),
  187. .name = OVS_PACKET_FAMILY,
  188. .version = OVS_PACKET_VERSION,
  189. .maxattr = OVS_PACKET_ATTR_MAX
  190. };
  191. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  192. const struct dp_upcall_info *upcall_info)
  193. {
  194. struct dp_stats_percpu *stats;
  195. int dp_ifindex;
  196. int err;
  197. if (upcall_info->pid == 0) {
  198. err = -ENOTCONN;
  199. goto err;
  200. }
  201. dp_ifindex = get_dpifindex(dp);
  202. if (!dp_ifindex) {
  203. err = -ENODEV;
  204. goto err;
  205. }
  206. if (!skb_is_gso(skb))
  207. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  208. else
  209. err = queue_gso_packets(dp_ifindex, skb, upcall_info);
  210. if (err)
  211. goto err;
  212. return 0;
  213. err:
  214. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  215. u64_stats_update_begin(&stats->sync);
  216. stats->n_lost++;
  217. u64_stats_update_end(&stats->sync);
  218. return err;
  219. }
  220. static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
  221. const struct dp_upcall_info *upcall_info)
  222. {
  223. struct dp_upcall_info later_info;
  224. struct sw_flow_key later_key;
  225. struct sk_buff *segs, *nskb;
  226. int err;
  227. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  228. if (IS_ERR(skb))
  229. return PTR_ERR(skb);
  230. /* Queue all of the segments. */
  231. skb = segs;
  232. do {
  233. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  234. if (err)
  235. break;
  236. if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
  237. /* The initial flow key extracted by ovs_flow_extract()
  238. * in this case is for a first fragment, so we need to
  239. * properly mark later fragments.
  240. */
  241. later_key = *upcall_info->key;
  242. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  243. later_info = *upcall_info;
  244. later_info.key = &later_key;
  245. upcall_info = &later_info;
  246. }
  247. } while ((skb = skb->next));
  248. /* Free all of the segments. */
  249. skb = segs;
  250. do {
  251. nskb = skb->next;
  252. if (err)
  253. kfree_skb(skb);
  254. else
  255. consume_skb(skb);
  256. } while ((skb = nskb));
  257. return err;
  258. }
  259. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
  260. const struct dp_upcall_info *upcall_info)
  261. {
  262. struct ovs_header *upcall;
  263. struct sk_buff *nskb = NULL;
  264. struct sk_buff *user_skb; /* to be queued to userspace */
  265. struct nlattr *nla;
  266. unsigned int len;
  267. int err;
  268. if (vlan_tx_tag_present(skb)) {
  269. nskb = skb_clone(skb, GFP_ATOMIC);
  270. if (!nskb)
  271. return -ENOMEM;
  272. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  273. if (!nskb)
  274. return -ENOMEM;
  275. nskb->vlan_tci = 0;
  276. skb = nskb;
  277. }
  278. if (nla_attr_size(skb->len) > USHRT_MAX) {
  279. err = -EFBIG;
  280. goto out;
  281. }
  282. len = sizeof(struct ovs_header);
  283. len += nla_total_size(skb->len);
  284. len += nla_total_size(FLOW_BUFSIZE);
  285. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  286. len += nla_total_size(8);
  287. user_skb = genlmsg_new(len, GFP_ATOMIC);
  288. if (!user_skb) {
  289. err = -ENOMEM;
  290. goto out;
  291. }
  292. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  293. 0, upcall_info->cmd);
  294. upcall->dp_ifindex = dp_ifindex;
  295. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  296. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  297. nla_nest_end(user_skb, nla);
  298. if (upcall_info->userdata)
  299. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  300. nla_get_u64(upcall_info->userdata));
  301. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  302. skb_copy_and_csum_dev(skb, nla_data(nla));
  303. err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
  304. out:
  305. kfree_skb(nskb);
  306. return err;
  307. }
  308. /* Called with genl_mutex. */
  309. static int flush_flows(int dp_ifindex)
  310. {
  311. struct flow_table *old_table;
  312. struct flow_table *new_table;
  313. struct datapath *dp;
  314. dp = get_dp(dp_ifindex);
  315. if (!dp)
  316. return -ENODEV;
  317. old_table = genl_dereference(dp->table);
  318. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  319. if (!new_table)
  320. return -ENOMEM;
  321. rcu_assign_pointer(dp->table, new_table);
  322. ovs_flow_tbl_deferred_destroy(old_table);
  323. return 0;
  324. }
  325. static int validate_actions(const struct nlattr *attr,
  326. const struct sw_flow_key *key, int depth);
  327. static int validate_sample(const struct nlattr *attr,
  328. const struct sw_flow_key *key, int depth)
  329. {
  330. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  331. const struct nlattr *probability, *actions;
  332. const struct nlattr *a;
  333. int rem;
  334. memset(attrs, 0, sizeof(attrs));
  335. nla_for_each_nested(a, attr, rem) {
  336. int type = nla_type(a);
  337. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  338. return -EINVAL;
  339. attrs[type] = a;
  340. }
  341. if (rem)
  342. return -EINVAL;
  343. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  344. if (!probability || nla_len(probability) != sizeof(u32))
  345. return -EINVAL;
  346. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  347. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  348. return -EINVAL;
  349. return validate_actions(actions, key, depth + 1);
  350. }
  351. static int validate_tp_port(const struct sw_flow_key *flow_key)
  352. {
  353. if (flow_key->eth.type == htons(ETH_P_IP)) {
  354. if (flow_key->ipv4.tp.src && flow_key->ipv4.tp.dst)
  355. return 0;
  356. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  357. if (flow_key->ipv6.tp.src && flow_key->ipv6.tp.dst)
  358. return 0;
  359. }
  360. return -EINVAL;
  361. }
  362. static int validate_set(const struct nlattr *a,
  363. const struct sw_flow_key *flow_key)
  364. {
  365. const struct nlattr *ovs_key = nla_data(a);
  366. int key_type = nla_type(ovs_key);
  367. /* There can be only one key in a action */
  368. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  369. return -EINVAL;
  370. if (key_type > OVS_KEY_ATTR_MAX ||
  371. nla_len(ovs_key) != ovs_key_lens[key_type])
  372. return -EINVAL;
  373. switch (key_type) {
  374. const struct ovs_key_ipv4 *ipv4_key;
  375. case OVS_KEY_ATTR_PRIORITY:
  376. case OVS_KEY_ATTR_ETHERNET:
  377. break;
  378. case OVS_KEY_ATTR_IPV4:
  379. if (flow_key->eth.type != htons(ETH_P_IP))
  380. return -EINVAL;
  381. if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
  382. return -EINVAL;
  383. ipv4_key = nla_data(ovs_key);
  384. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  385. return -EINVAL;
  386. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  387. return -EINVAL;
  388. break;
  389. case OVS_KEY_ATTR_TCP:
  390. if (flow_key->ip.proto != IPPROTO_TCP)
  391. return -EINVAL;
  392. return validate_tp_port(flow_key);
  393. case OVS_KEY_ATTR_UDP:
  394. if (flow_key->ip.proto != IPPROTO_UDP)
  395. return -EINVAL;
  396. return validate_tp_port(flow_key);
  397. default:
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. static int validate_userspace(const struct nlattr *attr)
  403. {
  404. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  405. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  406. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  407. };
  408. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  409. int error;
  410. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  411. attr, userspace_policy);
  412. if (error)
  413. return error;
  414. if (!a[OVS_USERSPACE_ATTR_PID] ||
  415. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  416. return -EINVAL;
  417. return 0;
  418. }
  419. static int validate_actions(const struct nlattr *attr,
  420. const struct sw_flow_key *key, int depth)
  421. {
  422. const struct nlattr *a;
  423. int rem, err;
  424. if (depth >= SAMPLE_ACTION_DEPTH)
  425. return -EOVERFLOW;
  426. nla_for_each_nested(a, attr, rem) {
  427. /* Expected argument lengths, (u32)-1 for variable length. */
  428. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  429. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  430. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  431. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  432. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  433. [OVS_ACTION_ATTR_SET] = (u32)-1,
  434. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  435. };
  436. const struct ovs_action_push_vlan *vlan;
  437. int type = nla_type(a);
  438. if (type > OVS_ACTION_ATTR_MAX ||
  439. (action_lens[type] != nla_len(a) &&
  440. action_lens[type] != (u32)-1))
  441. return -EINVAL;
  442. switch (type) {
  443. case OVS_ACTION_ATTR_UNSPEC:
  444. return -EINVAL;
  445. case OVS_ACTION_ATTR_USERSPACE:
  446. err = validate_userspace(a);
  447. if (err)
  448. return err;
  449. break;
  450. case OVS_ACTION_ATTR_OUTPUT:
  451. if (nla_get_u32(a) >= DP_MAX_PORTS)
  452. return -EINVAL;
  453. break;
  454. case OVS_ACTION_ATTR_POP_VLAN:
  455. break;
  456. case OVS_ACTION_ATTR_PUSH_VLAN:
  457. vlan = nla_data(a);
  458. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  459. return -EINVAL;
  460. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  461. return -EINVAL;
  462. break;
  463. case OVS_ACTION_ATTR_SET:
  464. err = validate_set(a, key);
  465. if (err)
  466. return err;
  467. break;
  468. case OVS_ACTION_ATTR_SAMPLE:
  469. err = validate_sample(a, key, depth);
  470. if (err)
  471. return err;
  472. break;
  473. default:
  474. return -EINVAL;
  475. }
  476. }
  477. if (rem > 0)
  478. return -EINVAL;
  479. return 0;
  480. }
  481. static void clear_stats(struct sw_flow *flow)
  482. {
  483. flow->used = 0;
  484. flow->tcp_flags = 0;
  485. flow->packet_count = 0;
  486. flow->byte_count = 0;
  487. }
  488. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  489. {
  490. struct ovs_header *ovs_header = info->userhdr;
  491. struct nlattr **a = info->attrs;
  492. struct sw_flow_actions *acts;
  493. struct sk_buff *packet;
  494. struct sw_flow *flow;
  495. struct datapath *dp;
  496. struct ethhdr *eth;
  497. int len;
  498. int err;
  499. int key_len;
  500. err = -EINVAL;
  501. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  502. !a[OVS_PACKET_ATTR_ACTIONS] ||
  503. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  504. goto err;
  505. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  506. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  507. err = -ENOMEM;
  508. if (!packet)
  509. goto err;
  510. skb_reserve(packet, NET_IP_ALIGN);
  511. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  512. skb_reset_mac_header(packet);
  513. eth = eth_hdr(packet);
  514. /* Normally, setting the skb 'protocol' field would be handled by a
  515. * call to eth_type_trans(), but it assumes there's a sending
  516. * device, which we may not have. */
  517. if (ntohs(eth->h_proto) >= 1536)
  518. packet->protocol = eth->h_proto;
  519. else
  520. packet->protocol = htons(ETH_P_802_2);
  521. /* Build an sw_flow for sending this packet. */
  522. flow = ovs_flow_alloc();
  523. err = PTR_ERR(flow);
  524. if (IS_ERR(flow))
  525. goto err_kfree_skb;
  526. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  527. if (err)
  528. goto err_flow_free;
  529. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  530. &flow->key.phy.in_port,
  531. a[OVS_PACKET_ATTR_KEY]);
  532. if (err)
  533. goto err_flow_free;
  534. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  535. if (err)
  536. goto err_flow_free;
  537. flow->hash = ovs_flow_hash(&flow->key, key_len);
  538. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  539. err = PTR_ERR(acts);
  540. if (IS_ERR(acts))
  541. goto err_flow_free;
  542. rcu_assign_pointer(flow->sf_acts, acts);
  543. OVS_CB(packet)->flow = flow;
  544. packet->priority = flow->key.phy.priority;
  545. rcu_read_lock();
  546. dp = get_dp(ovs_header->dp_ifindex);
  547. err = -ENODEV;
  548. if (!dp)
  549. goto err_unlock;
  550. local_bh_disable();
  551. err = ovs_execute_actions(dp, packet);
  552. local_bh_enable();
  553. rcu_read_unlock();
  554. ovs_flow_free(flow);
  555. return err;
  556. err_unlock:
  557. rcu_read_unlock();
  558. err_flow_free:
  559. ovs_flow_free(flow);
  560. err_kfree_skb:
  561. kfree_skb(packet);
  562. err:
  563. return err;
  564. }
  565. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  566. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  567. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  568. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  569. };
  570. static struct genl_ops dp_packet_genl_ops[] = {
  571. { .cmd = OVS_PACKET_CMD_EXECUTE,
  572. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  573. .policy = packet_policy,
  574. .doit = ovs_packet_cmd_execute
  575. }
  576. };
  577. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  578. {
  579. int i;
  580. struct flow_table *table = genl_dereference(dp->table);
  581. stats->n_flows = ovs_flow_tbl_count(table);
  582. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  583. for_each_possible_cpu(i) {
  584. const struct dp_stats_percpu *percpu_stats;
  585. struct dp_stats_percpu local_stats;
  586. unsigned int start;
  587. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  588. do {
  589. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  590. local_stats = *percpu_stats;
  591. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  592. stats->n_hit += local_stats.n_hit;
  593. stats->n_missed += local_stats.n_missed;
  594. stats->n_lost += local_stats.n_lost;
  595. }
  596. }
  597. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  598. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  599. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  600. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  601. };
  602. static struct genl_family dp_flow_genl_family = {
  603. .id = GENL_ID_GENERATE,
  604. .hdrsize = sizeof(struct ovs_header),
  605. .name = OVS_FLOW_FAMILY,
  606. .version = OVS_FLOW_VERSION,
  607. .maxattr = OVS_FLOW_ATTR_MAX
  608. };
  609. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  610. .name = OVS_FLOW_MCGROUP
  611. };
  612. /* Called with genl_lock. */
  613. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  614. struct sk_buff *skb, u32 pid,
  615. u32 seq, u32 flags, u8 cmd)
  616. {
  617. const int skb_orig_len = skb->len;
  618. const struct sw_flow_actions *sf_acts;
  619. struct ovs_flow_stats stats;
  620. struct ovs_header *ovs_header;
  621. struct nlattr *nla;
  622. unsigned long used;
  623. u8 tcp_flags;
  624. int err;
  625. sf_acts = rcu_dereference_protected(flow->sf_acts,
  626. lockdep_genl_is_held());
  627. ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
  628. if (!ovs_header)
  629. return -EMSGSIZE;
  630. ovs_header->dp_ifindex = get_dpifindex(dp);
  631. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  632. if (!nla)
  633. goto nla_put_failure;
  634. err = ovs_flow_to_nlattrs(&flow->key, skb);
  635. if (err)
  636. goto error;
  637. nla_nest_end(skb, nla);
  638. spin_lock_bh(&flow->lock);
  639. used = flow->used;
  640. stats.n_packets = flow->packet_count;
  641. stats.n_bytes = flow->byte_count;
  642. tcp_flags = flow->tcp_flags;
  643. spin_unlock_bh(&flow->lock);
  644. if (used)
  645. NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used));
  646. if (stats.n_packets)
  647. NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
  648. sizeof(struct ovs_flow_stats), &stats);
  649. if (tcp_flags)
  650. NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
  651. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  652. * this is the first flow to be dumped into 'skb'. This is unusual for
  653. * Netlink but individual action lists can be longer than
  654. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  655. * The userspace caller can always fetch the actions separately if it
  656. * really wants them. (Most userspace callers in fact don't care.)
  657. *
  658. * This can only fail for dump operations because the skb is always
  659. * properly sized for single flows.
  660. */
  661. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  662. sf_acts->actions);
  663. if (err < 0 && skb_orig_len)
  664. goto error;
  665. return genlmsg_end(skb, ovs_header);
  666. nla_put_failure:
  667. err = -EMSGSIZE;
  668. error:
  669. genlmsg_cancel(skb, ovs_header);
  670. return err;
  671. }
  672. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  673. {
  674. const struct sw_flow_actions *sf_acts;
  675. int len;
  676. sf_acts = rcu_dereference_protected(flow->sf_acts,
  677. lockdep_genl_is_held());
  678. /* OVS_FLOW_ATTR_KEY */
  679. len = nla_total_size(FLOW_BUFSIZE);
  680. /* OVS_FLOW_ATTR_ACTIONS */
  681. len += nla_total_size(sf_acts->actions_len);
  682. /* OVS_FLOW_ATTR_STATS */
  683. len += nla_total_size(sizeof(struct ovs_flow_stats));
  684. /* OVS_FLOW_ATTR_TCP_FLAGS */
  685. len += nla_total_size(1);
  686. /* OVS_FLOW_ATTR_USED */
  687. len += nla_total_size(8);
  688. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  689. return genlmsg_new(len, GFP_KERNEL);
  690. }
  691. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  692. struct datapath *dp,
  693. u32 pid, u32 seq, u8 cmd)
  694. {
  695. struct sk_buff *skb;
  696. int retval;
  697. skb = ovs_flow_cmd_alloc_info(flow);
  698. if (!skb)
  699. return ERR_PTR(-ENOMEM);
  700. retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
  701. BUG_ON(retval < 0);
  702. return skb;
  703. }
  704. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  705. {
  706. struct nlattr **a = info->attrs;
  707. struct ovs_header *ovs_header = info->userhdr;
  708. struct sw_flow_key key;
  709. struct sw_flow *flow;
  710. struct sk_buff *reply;
  711. struct datapath *dp;
  712. struct flow_table *table;
  713. int error;
  714. int key_len;
  715. /* Extract key. */
  716. error = -EINVAL;
  717. if (!a[OVS_FLOW_ATTR_KEY])
  718. goto error;
  719. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  720. if (error)
  721. goto error;
  722. /* Validate actions. */
  723. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  724. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  725. if (error)
  726. goto error;
  727. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  728. error = -EINVAL;
  729. goto error;
  730. }
  731. dp = get_dp(ovs_header->dp_ifindex);
  732. error = -ENODEV;
  733. if (!dp)
  734. goto error;
  735. table = genl_dereference(dp->table);
  736. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  737. if (!flow) {
  738. struct sw_flow_actions *acts;
  739. /* Bail out if we're not allowed to create a new flow. */
  740. error = -ENOENT;
  741. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  742. goto error;
  743. /* Expand table, if necessary, to make room. */
  744. if (ovs_flow_tbl_need_to_expand(table)) {
  745. struct flow_table *new_table;
  746. new_table = ovs_flow_tbl_expand(table);
  747. if (!IS_ERR(new_table)) {
  748. rcu_assign_pointer(dp->table, new_table);
  749. ovs_flow_tbl_deferred_destroy(table);
  750. table = genl_dereference(dp->table);
  751. }
  752. }
  753. /* Allocate flow. */
  754. flow = ovs_flow_alloc();
  755. if (IS_ERR(flow)) {
  756. error = PTR_ERR(flow);
  757. goto error;
  758. }
  759. flow->key = key;
  760. clear_stats(flow);
  761. /* Obtain actions. */
  762. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  763. error = PTR_ERR(acts);
  764. if (IS_ERR(acts))
  765. goto error_free_flow;
  766. rcu_assign_pointer(flow->sf_acts, acts);
  767. /* Put flow in bucket. */
  768. flow->hash = ovs_flow_hash(&key, key_len);
  769. ovs_flow_tbl_insert(table, flow);
  770. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  771. info->snd_seq,
  772. OVS_FLOW_CMD_NEW);
  773. } else {
  774. /* We found a matching flow. */
  775. struct sw_flow_actions *old_acts;
  776. struct nlattr *acts_attrs;
  777. /* Bail out if we're not allowed to modify an existing flow.
  778. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  779. * because Generic Netlink treats the latter as a dump
  780. * request. We also accept NLM_F_EXCL in case that bug ever
  781. * gets fixed.
  782. */
  783. error = -EEXIST;
  784. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  785. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  786. goto error;
  787. /* Update actions. */
  788. old_acts = rcu_dereference_protected(flow->sf_acts,
  789. lockdep_genl_is_held());
  790. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  791. if (acts_attrs &&
  792. (old_acts->actions_len != nla_len(acts_attrs) ||
  793. memcmp(old_acts->actions, nla_data(acts_attrs),
  794. old_acts->actions_len))) {
  795. struct sw_flow_actions *new_acts;
  796. new_acts = ovs_flow_actions_alloc(acts_attrs);
  797. error = PTR_ERR(new_acts);
  798. if (IS_ERR(new_acts))
  799. goto error;
  800. rcu_assign_pointer(flow->sf_acts, new_acts);
  801. ovs_flow_deferred_free_acts(old_acts);
  802. }
  803. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  804. info->snd_seq, OVS_FLOW_CMD_NEW);
  805. /* Clear stats. */
  806. if (a[OVS_FLOW_ATTR_CLEAR]) {
  807. spin_lock_bh(&flow->lock);
  808. clear_stats(flow);
  809. spin_unlock_bh(&flow->lock);
  810. }
  811. }
  812. if (!IS_ERR(reply))
  813. genl_notify(reply, genl_info_net(info), info->snd_pid,
  814. ovs_dp_flow_multicast_group.id, info->nlhdr,
  815. GFP_KERNEL);
  816. else
  817. netlink_set_err(init_net.genl_sock, 0,
  818. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  819. return 0;
  820. error_free_flow:
  821. ovs_flow_free(flow);
  822. error:
  823. return error;
  824. }
  825. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  826. {
  827. struct nlattr **a = info->attrs;
  828. struct ovs_header *ovs_header = info->userhdr;
  829. struct sw_flow_key key;
  830. struct sk_buff *reply;
  831. struct sw_flow *flow;
  832. struct datapath *dp;
  833. struct flow_table *table;
  834. int err;
  835. int key_len;
  836. if (!a[OVS_FLOW_ATTR_KEY])
  837. return -EINVAL;
  838. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  839. if (err)
  840. return err;
  841. dp = get_dp(ovs_header->dp_ifindex);
  842. if (!dp)
  843. return -ENODEV;
  844. table = genl_dereference(dp->table);
  845. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  846. if (!flow)
  847. return -ENOENT;
  848. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  849. info->snd_seq, OVS_FLOW_CMD_NEW);
  850. if (IS_ERR(reply))
  851. return PTR_ERR(reply);
  852. return genlmsg_reply(reply, info);
  853. }
  854. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  855. {
  856. struct nlattr **a = info->attrs;
  857. struct ovs_header *ovs_header = info->userhdr;
  858. struct sw_flow_key key;
  859. struct sk_buff *reply;
  860. struct sw_flow *flow;
  861. struct datapath *dp;
  862. struct flow_table *table;
  863. int err;
  864. int key_len;
  865. if (!a[OVS_FLOW_ATTR_KEY])
  866. return flush_flows(ovs_header->dp_ifindex);
  867. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  868. if (err)
  869. return err;
  870. dp = get_dp(ovs_header->dp_ifindex);
  871. if (!dp)
  872. return -ENODEV;
  873. table = genl_dereference(dp->table);
  874. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  875. if (!flow)
  876. return -ENOENT;
  877. reply = ovs_flow_cmd_alloc_info(flow);
  878. if (!reply)
  879. return -ENOMEM;
  880. ovs_flow_tbl_remove(table, flow);
  881. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
  882. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  883. BUG_ON(err < 0);
  884. ovs_flow_deferred_free(flow);
  885. genl_notify(reply, genl_info_net(info), info->snd_pid,
  886. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  887. return 0;
  888. }
  889. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  890. {
  891. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  892. struct datapath *dp;
  893. struct flow_table *table;
  894. dp = get_dp(ovs_header->dp_ifindex);
  895. if (!dp)
  896. return -ENODEV;
  897. table = genl_dereference(dp->table);
  898. for (;;) {
  899. struct sw_flow *flow;
  900. u32 bucket, obj;
  901. bucket = cb->args[0];
  902. obj = cb->args[1];
  903. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  904. if (!flow)
  905. break;
  906. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  907. NETLINK_CB(cb->skb).pid,
  908. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  909. OVS_FLOW_CMD_NEW) < 0)
  910. break;
  911. cb->args[0] = bucket;
  912. cb->args[1] = obj;
  913. }
  914. return skb->len;
  915. }
  916. static struct genl_ops dp_flow_genl_ops[] = {
  917. { .cmd = OVS_FLOW_CMD_NEW,
  918. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  919. .policy = flow_policy,
  920. .doit = ovs_flow_cmd_new_or_set
  921. },
  922. { .cmd = OVS_FLOW_CMD_DEL,
  923. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  924. .policy = flow_policy,
  925. .doit = ovs_flow_cmd_del
  926. },
  927. { .cmd = OVS_FLOW_CMD_GET,
  928. .flags = 0, /* OK for unprivileged users. */
  929. .policy = flow_policy,
  930. .doit = ovs_flow_cmd_get,
  931. .dumpit = ovs_flow_cmd_dump
  932. },
  933. { .cmd = OVS_FLOW_CMD_SET,
  934. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  935. .policy = flow_policy,
  936. .doit = ovs_flow_cmd_new_or_set,
  937. },
  938. };
  939. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  940. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  941. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  942. };
  943. static struct genl_family dp_datapath_genl_family = {
  944. .id = GENL_ID_GENERATE,
  945. .hdrsize = sizeof(struct ovs_header),
  946. .name = OVS_DATAPATH_FAMILY,
  947. .version = OVS_DATAPATH_VERSION,
  948. .maxattr = OVS_DP_ATTR_MAX
  949. };
  950. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  951. .name = OVS_DATAPATH_MCGROUP
  952. };
  953. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  954. u32 pid, u32 seq, u32 flags, u8 cmd)
  955. {
  956. struct ovs_header *ovs_header;
  957. struct ovs_dp_stats dp_stats;
  958. int err;
  959. ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
  960. flags, cmd);
  961. if (!ovs_header)
  962. goto error;
  963. ovs_header->dp_ifindex = get_dpifindex(dp);
  964. rcu_read_lock();
  965. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  966. rcu_read_unlock();
  967. if (err)
  968. goto nla_put_failure;
  969. get_dp_stats(dp, &dp_stats);
  970. NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
  971. return genlmsg_end(skb, ovs_header);
  972. nla_put_failure:
  973. genlmsg_cancel(skb, ovs_header);
  974. error:
  975. return -EMSGSIZE;
  976. }
  977. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
  978. u32 seq, u8 cmd)
  979. {
  980. struct sk_buff *skb;
  981. int retval;
  982. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  983. if (!skb)
  984. return ERR_PTR(-ENOMEM);
  985. retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
  986. if (retval < 0) {
  987. kfree_skb(skb);
  988. return ERR_PTR(retval);
  989. }
  990. return skb;
  991. }
  992. /* Called with genl_mutex and optionally with RTNL lock also. */
  993. static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
  994. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  995. {
  996. struct datapath *dp;
  997. if (!a[OVS_DP_ATTR_NAME])
  998. dp = get_dp(ovs_header->dp_ifindex);
  999. else {
  1000. struct vport *vport;
  1001. rcu_read_lock();
  1002. vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
  1003. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1004. rcu_read_unlock();
  1005. }
  1006. return dp ? dp : ERR_PTR(-ENODEV);
  1007. }
  1008. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1009. {
  1010. struct nlattr **a = info->attrs;
  1011. struct vport_parms parms;
  1012. struct sk_buff *reply;
  1013. struct datapath *dp;
  1014. struct vport *vport;
  1015. int err;
  1016. err = -EINVAL;
  1017. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1018. goto err;
  1019. rtnl_lock();
  1020. err = -ENODEV;
  1021. if (!try_module_get(THIS_MODULE))
  1022. goto err_unlock_rtnl;
  1023. err = -ENOMEM;
  1024. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1025. if (dp == NULL)
  1026. goto err_put_module;
  1027. INIT_LIST_HEAD(&dp->port_list);
  1028. /* Allocate table. */
  1029. err = -ENOMEM;
  1030. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1031. if (!dp->table)
  1032. goto err_free_dp;
  1033. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1034. if (!dp->stats_percpu) {
  1035. err = -ENOMEM;
  1036. goto err_destroy_table;
  1037. }
  1038. /* Set up our datapath device. */
  1039. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1040. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1041. parms.options = NULL;
  1042. parms.dp = dp;
  1043. parms.port_no = OVSP_LOCAL;
  1044. parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1045. vport = new_vport(&parms);
  1046. if (IS_ERR(vport)) {
  1047. err = PTR_ERR(vport);
  1048. if (err == -EBUSY)
  1049. err = -EEXIST;
  1050. goto err_destroy_percpu;
  1051. }
  1052. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1053. info->snd_seq, OVS_DP_CMD_NEW);
  1054. err = PTR_ERR(reply);
  1055. if (IS_ERR(reply))
  1056. goto err_destroy_local_port;
  1057. list_add_tail(&dp->list_node, &dps);
  1058. rtnl_unlock();
  1059. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1060. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1061. GFP_KERNEL);
  1062. return 0;
  1063. err_destroy_local_port:
  1064. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1065. err_destroy_percpu:
  1066. free_percpu(dp->stats_percpu);
  1067. err_destroy_table:
  1068. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1069. err_free_dp:
  1070. kfree(dp);
  1071. err_put_module:
  1072. module_put(THIS_MODULE);
  1073. err_unlock_rtnl:
  1074. rtnl_unlock();
  1075. err:
  1076. return err;
  1077. }
  1078. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1079. {
  1080. struct vport *vport, *next_vport;
  1081. struct sk_buff *reply;
  1082. struct datapath *dp;
  1083. int err;
  1084. rtnl_lock();
  1085. dp = lookup_datapath(info->userhdr, info->attrs);
  1086. err = PTR_ERR(dp);
  1087. if (IS_ERR(dp))
  1088. goto exit_unlock;
  1089. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1090. info->snd_seq, OVS_DP_CMD_DEL);
  1091. err = PTR_ERR(reply);
  1092. if (IS_ERR(reply))
  1093. goto exit_unlock;
  1094. list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
  1095. if (vport->port_no != OVSP_LOCAL)
  1096. ovs_dp_detach_port(vport);
  1097. list_del(&dp->list_node);
  1098. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1099. /* rtnl_unlock() will wait until all the references to devices that
  1100. * are pending unregistration have been dropped. We do it here to
  1101. * ensure that any internal devices (which contain DP pointers) are
  1102. * fully destroyed before freeing the datapath.
  1103. */
  1104. rtnl_unlock();
  1105. call_rcu(&dp->rcu, destroy_dp_rcu);
  1106. module_put(THIS_MODULE);
  1107. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1108. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1109. GFP_KERNEL);
  1110. return 0;
  1111. exit_unlock:
  1112. rtnl_unlock();
  1113. return err;
  1114. }
  1115. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1116. {
  1117. struct sk_buff *reply;
  1118. struct datapath *dp;
  1119. int err;
  1120. dp = lookup_datapath(info->userhdr, info->attrs);
  1121. if (IS_ERR(dp))
  1122. return PTR_ERR(dp);
  1123. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1124. info->snd_seq, OVS_DP_CMD_NEW);
  1125. if (IS_ERR(reply)) {
  1126. err = PTR_ERR(reply);
  1127. netlink_set_err(init_net.genl_sock, 0,
  1128. ovs_dp_datapath_multicast_group.id, err);
  1129. return 0;
  1130. }
  1131. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1132. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1133. GFP_KERNEL);
  1134. return 0;
  1135. }
  1136. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1137. {
  1138. struct sk_buff *reply;
  1139. struct datapath *dp;
  1140. dp = lookup_datapath(info->userhdr, info->attrs);
  1141. if (IS_ERR(dp))
  1142. return PTR_ERR(dp);
  1143. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1144. info->snd_seq, OVS_DP_CMD_NEW);
  1145. if (IS_ERR(reply))
  1146. return PTR_ERR(reply);
  1147. return genlmsg_reply(reply, info);
  1148. }
  1149. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1150. {
  1151. struct datapath *dp;
  1152. int skip = cb->args[0];
  1153. int i = 0;
  1154. list_for_each_entry(dp, &dps, list_node) {
  1155. if (i >= skip &&
  1156. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
  1157. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1158. OVS_DP_CMD_NEW) < 0)
  1159. break;
  1160. i++;
  1161. }
  1162. cb->args[0] = i;
  1163. return skb->len;
  1164. }
  1165. static struct genl_ops dp_datapath_genl_ops[] = {
  1166. { .cmd = OVS_DP_CMD_NEW,
  1167. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1168. .policy = datapath_policy,
  1169. .doit = ovs_dp_cmd_new
  1170. },
  1171. { .cmd = OVS_DP_CMD_DEL,
  1172. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1173. .policy = datapath_policy,
  1174. .doit = ovs_dp_cmd_del
  1175. },
  1176. { .cmd = OVS_DP_CMD_GET,
  1177. .flags = 0, /* OK for unprivileged users. */
  1178. .policy = datapath_policy,
  1179. .doit = ovs_dp_cmd_get,
  1180. .dumpit = ovs_dp_cmd_dump
  1181. },
  1182. { .cmd = OVS_DP_CMD_SET,
  1183. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1184. .policy = datapath_policy,
  1185. .doit = ovs_dp_cmd_set,
  1186. },
  1187. };
  1188. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1189. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1190. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1191. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1192. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1193. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1194. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1195. };
  1196. static struct genl_family dp_vport_genl_family = {
  1197. .id = GENL_ID_GENERATE,
  1198. .hdrsize = sizeof(struct ovs_header),
  1199. .name = OVS_VPORT_FAMILY,
  1200. .version = OVS_VPORT_VERSION,
  1201. .maxattr = OVS_VPORT_ATTR_MAX
  1202. };
  1203. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1204. .name = OVS_VPORT_MCGROUP
  1205. };
  1206. /* Called with RTNL lock or RCU read lock. */
  1207. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1208. u32 pid, u32 seq, u32 flags, u8 cmd)
  1209. {
  1210. struct ovs_header *ovs_header;
  1211. struct ovs_vport_stats vport_stats;
  1212. int err;
  1213. ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
  1214. flags, cmd);
  1215. if (!ovs_header)
  1216. return -EMSGSIZE;
  1217. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1218. NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
  1219. NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
  1220. NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
  1221. NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
  1222. ovs_vport_get_stats(vport, &vport_stats);
  1223. NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1224. &vport_stats);
  1225. err = ovs_vport_get_options(vport, skb);
  1226. if (err == -EMSGSIZE)
  1227. goto error;
  1228. return genlmsg_end(skb, ovs_header);
  1229. nla_put_failure:
  1230. err = -EMSGSIZE;
  1231. error:
  1232. genlmsg_cancel(skb, ovs_header);
  1233. return err;
  1234. }
  1235. /* Called with RTNL lock or RCU read lock. */
  1236. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
  1237. u32 seq, u8 cmd)
  1238. {
  1239. struct sk_buff *skb;
  1240. int retval;
  1241. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1242. if (!skb)
  1243. return ERR_PTR(-ENOMEM);
  1244. retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
  1245. if (retval < 0) {
  1246. kfree_skb(skb);
  1247. return ERR_PTR(retval);
  1248. }
  1249. return skb;
  1250. }
  1251. /* Called with RTNL lock or RCU read lock. */
  1252. static struct vport *lookup_vport(struct ovs_header *ovs_header,
  1253. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1254. {
  1255. struct datapath *dp;
  1256. struct vport *vport;
  1257. if (a[OVS_VPORT_ATTR_NAME]) {
  1258. vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
  1259. if (!vport)
  1260. return ERR_PTR(-ENODEV);
  1261. if (ovs_header->dp_ifindex &&
  1262. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1263. return ERR_PTR(-ENODEV);
  1264. return vport;
  1265. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1266. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1267. if (port_no >= DP_MAX_PORTS)
  1268. return ERR_PTR(-EFBIG);
  1269. dp = get_dp(ovs_header->dp_ifindex);
  1270. if (!dp)
  1271. return ERR_PTR(-ENODEV);
  1272. vport = rcu_dereference_rtnl(dp->ports[port_no]);
  1273. if (!vport)
  1274. return ERR_PTR(-ENOENT);
  1275. return vport;
  1276. } else
  1277. return ERR_PTR(-EINVAL);
  1278. }
  1279. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1280. {
  1281. struct nlattr **a = info->attrs;
  1282. struct ovs_header *ovs_header = info->userhdr;
  1283. struct vport_parms parms;
  1284. struct sk_buff *reply;
  1285. struct vport *vport;
  1286. struct datapath *dp;
  1287. u32 port_no;
  1288. int err;
  1289. err = -EINVAL;
  1290. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1291. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1292. goto exit;
  1293. rtnl_lock();
  1294. dp = get_dp(ovs_header->dp_ifindex);
  1295. err = -ENODEV;
  1296. if (!dp)
  1297. goto exit_unlock;
  1298. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1299. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1300. err = -EFBIG;
  1301. if (port_no >= DP_MAX_PORTS)
  1302. goto exit_unlock;
  1303. vport = rtnl_dereference(dp->ports[port_no]);
  1304. err = -EBUSY;
  1305. if (vport)
  1306. goto exit_unlock;
  1307. } else {
  1308. for (port_no = 1; ; port_no++) {
  1309. if (port_no >= DP_MAX_PORTS) {
  1310. err = -EFBIG;
  1311. goto exit_unlock;
  1312. }
  1313. vport = rtnl_dereference(dp->ports[port_no]);
  1314. if (!vport)
  1315. break;
  1316. }
  1317. }
  1318. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1319. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1320. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1321. parms.dp = dp;
  1322. parms.port_no = port_no;
  1323. parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1324. vport = new_vport(&parms);
  1325. err = PTR_ERR(vport);
  1326. if (IS_ERR(vport))
  1327. goto exit_unlock;
  1328. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1329. OVS_VPORT_CMD_NEW);
  1330. if (IS_ERR(reply)) {
  1331. err = PTR_ERR(reply);
  1332. ovs_dp_detach_port(vport);
  1333. goto exit_unlock;
  1334. }
  1335. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1336. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1337. exit_unlock:
  1338. rtnl_unlock();
  1339. exit:
  1340. return err;
  1341. }
  1342. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1343. {
  1344. struct nlattr **a = info->attrs;
  1345. struct sk_buff *reply;
  1346. struct vport *vport;
  1347. int err;
  1348. rtnl_lock();
  1349. vport = lookup_vport(info->userhdr, a);
  1350. err = PTR_ERR(vport);
  1351. if (IS_ERR(vport))
  1352. goto exit_unlock;
  1353. err = 0;
  1354. if (a[OVS_VPORT_ATTR_TYPE] &&
  1355. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1356. err = -EINVAL;
  1357. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1358. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1359. if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
  1360. vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1361. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1362. OVS_VPORT_CMD_NEW);
  1363. if (IS_ERR(reply)) {
  1364. netlink_set_err(init_net.genl_sock, 0,
  1365. ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
  1366. goto exit_unlock;
  1367. }
  1368. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1369. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1370. exit_unlock:
  1371. rtnl_unlock();
  1372. return err;
  1373. }
  1374. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1375. {
  1376. struct nlattr **a = info->attrs;
  1377. struct sk_buff *reply;
  1378. struct vport *vport;
  1379. int err;
  1380. rtnl_lock();
  1381. vport = lookup_vport(info->userhdr, a);
  1382. err = PTR_ERR(vport);
  1383. if (IS_ERR(vport))
  1384. goto exit_unlock;
  1385. if (vport->port_no == OVSP_LOCAL) {
  1386. err = -EINVAL;
  1387. goto exit_unlock;
  1388. }
  1389. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1390. OVS_VPORT_CMD_DEL);
  1391. err = PTR_ERR(reply);
  1392. if (IS_ERR(reply))
  1393. goto exit_unlock;
  1394. ovs_dp_detach_port(vport);
  1395. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1396. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1397. exit_unlock:
  1398. rtnl_unlock();
  1399. return err;
  1400. }
  1401. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1402. {
  1403. struct nlattr **a = info->attrs;
  1404. struct ovs_header *ovs_header = info->userhdr;
  1405. struct sk_buff *reply;
  1406. struct vport *vport;
  1407. int err;
  1408. rcu_read_lock();
  1409. vport = lookup_vport(ovs_header, a);
  1410. err = PTR_ERR(vport);
  1411. if (IS_ERR(vport))
  1412. goto exit_unlock;
  1413. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1414. OVS_VPORT_CMD_NEW);
  1415. err = PTR_ERR(reply);
  1416. if (IS_ERR(reply))
  1417. goto exit_unlock;
  1418. rcu_read_unlock();
  1419. return genlmsg_reply(reply, info);
  1420. exit_unlock:
  1421. rcu_read_unlock();
  1422. return err;
  1423. }
  1424. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1425. {
  1426. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1427. struct datapath *dp;
  1428. u32 port_no;
  1429. int retval;
  1430. dp = get_dp(ovs_header->dp_ifindex);
  1431. if (!dp)
  1432. return -ENODEV;
  1433. rcu_read_lock();
  1434. for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
  1435. struct vport *vport;
  1436. vport = rcu_dereference(dp->ports[port_no]);
  1437. if (!vport)
  1438. continue;
  1439. if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
  1440. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1441. OVS_VPORT_CMD_NEW) < 0)
  1442. break;
  1443. }
  1444. rcu_read_unlock();
  1445. cb->args[0] = port_no;
  1446. retval = skb->len;
  1447. return retval;
  1448. }
  1449. static void rehash_flow_table(struct work_struct *work)
  1450. {
  1451. struct datapath *dp;
  1452. genl_lock();
  1453. list_for_each_entry(dp, &dps, list_node) {
  1454. struct flow_table *old_table = genl_dereference(dp->table);
  1455. struct flow_table *new_table;
  1456. new_table = ovs_flow_tbl_rehash(old_table);
  1457. if (!IS_ERR(new_table)) {
  1458. rcu_assign_pointer(dp->table, new_table);
  1459. ovs_flow_tbl_deferred_destroy(old_table);
  1460. }
  1461. }
  1462. genl_unlock();
  1463. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1464. }
  1465. static struct genl_ops dp_vport_genl_ops[] = {
  1466. { .cmd = OVS_VPORT_CMD_NEW,
  1467. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1468. .policy = vport_policy,
  1469. .doit = ovs_vport_cmd_new
  1470. },
  1471. { .cmd = OVS_VPORT_CMD_DEL,
  1472. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1473. .policy = vport_policy,
  1474. .doit = ovs_vport_cmd_del
  1475. },
  1476. { .cmd = OVS_VPORT_CMD_GET,
  1477. .flags = 0, /* OK for unprivileged users. */
  1478. .policy = vport_policy,
  1479. .doit = ovs_vport_cmd_get,
  1480. .dumpit = ovs_vport_cmd_dump
  1481. },
  1482. { .cmd = OVS_VPORT_CMD_SET,
  1483. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1484. .policy = vport_policy,
  1485. .doit = ovs_vport_cmd_set,
  1486. },
  1487. };
  1488. struct genl_family_and_ops {
  1489. struct genl_family *family;
  1490. struct genl_ops *ops;
  1491. int n_ops;
  1492. struct genl_multicast_group *group;
  1493. };
  1494. static const struct genl_family_and_ops dp_genl_families[] = {
  1495. { &dp_datapath_genl_family,
  1496. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1497. &ovs_dp_datapath_multicast_group },
  1498. { &dp_vport_genl_family,
  1499. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1500. &ovs_dp_vport_multicast_group },
  1501. { &dp_flow_genl_family,
  1502. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1503. &ovs_dp_flow_multicast_group },
  1504. { &dp_packet_genl_family,
  1505. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1506. NULL },
  1507. };
  1508. static void dp_unregister_genl(int n_families)
  1509. {
  1510. int i;
  1511. for (i = 0; i < n_families; i++)
  1512. genl_unregister_family(dp_genl_families[i].family);
  1513. }
  1514. static int dp_register_genl(void)
  1515. {
  1516. int n_registered;
  1517. int err;
  1518. int i;
  1519. n_registered = 0;
  1520. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1521. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1522. err = genl_register_family_with_ops(f->family, f->ops,
  1523. f->n_ops);
  1524. if (err)
  1525. goto error;
  1526. n_registered++;
  1527. if (f->group) {
  1528. err = genl_register_mc_group(f->family, f->group);
  1529. if (err)
  1530. goto error;
  1531. }
  1532. }
  1533. return 0;
  1534. error:
  1535. dp_unregister_genl(n_registered);
  1536. return err;
  1537. }
  1538. static int __init dp_init(void)
  1539. {
  1540. struct sk_buff *dummy_skb;
  1541. int err;
  1542. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1543. pr_info("Open vSwitch switching datapath\n");
  1544. err = ovs_flow_init();
  1545. if (err)
  1546. goto error;
  1547. err = ovs_vport_init();
  1548. if (err)
  1549. goto error_flow_exit;
  1550. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1551. if (err)
  1552. goto error_vport_exit;
  1553. err = dp_register_genl();
  1554. if (err < 0)
  1555. goto error_unreg_notifier;
  1556. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1557. return 0;
  1558. error_unreg_notifier:
  1559. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1560. error_vport_exit:
  1561. ovs_vport_exit();
  1562. error_flow_exit:
  1563. ovs_flow_exit();
  1564. error:
  1565. return err;
  1566. }
  1567. static void dp_cleanup(void)
  1568. {
  1569. cancel_delayed_work_sync(&rehash_flow_wq);
  1570. rcu_barrier();
  1571. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1572. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1573. ovs_vport_exit();
  1574. ovs_flow_exit();
  1575. }
  1576. module_init(dp_init);
  1577. module_exit(dp_cleanup);
  1578. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1579. MODULE_LICENSE("GPL");