datapath.c 50 KB

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